blob: bfcf03ab5245d6b68b585ca4b25571e8761d59d0 [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
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -030028#include <linux/cpufreq.h>
Sam Ravnborgd0e93592019-01-26 13:25:24 +010029#include <linux/module.h>
Chris Wilson08ea70a2018-08-12 23:36:31 +010030#include <linux/pm_runtime.h>
Sam Ravnborgd0e93592019-01-26 13:25:24 +010031
32#include <drm/drm_atomic_helper.h>
33#include <drm/drm_fourcc.h>
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -070034#include <drm/drm_plane_helper.h>
Sam Ravnborgd0e93592019-01-26 13:25:24 +010035
Jani Nikuladf0566a2019-06-13 11:44:16 +030036#include "display/intel_atomic.h"
Jani Nikula1d455f82019-08-06 14:39:33 +030037#include "display/intel_display_types.h"
Jani Nikuladf0566a2019-06-13 11:44:16 +030038#include "display/intel_fbc.h"
39#include "display/intel_sprite.h"
40
Eugeni Dodonov85208be2012-04-16 22:20:34 -030041#include "i915_drv.h"
Jani Nikula440e2b32019-04-29 15:29:27 +030042#include "i915_irq.h"
Jani Nikulaa09d9a82019-08-06 13:07:28 +030043#include "i915_trace.h"
Jani Nikula696173b2019-04-05 14:00:15 +030044#include "intel_pm.h"
Chris Wilson56c50982019-04-26 09:17:22 +010045#include "intel_sideband.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020046#include "../../../platform/x86/intel_ips.h"
Eugeni Dodonov85208be2012-04-16 22:20:34 -030047
Ville Syrjälä46f16e62016-10-31 22:37:22 +020048static void gen9_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppalab033bb62016-06-07 17:19:04 +030049{
Ville Syrjälä93564042017-08-24 22:10:51 +030050 if (HAS_LLC(dev_priv)) {
51 /*
52 * WaCompressedResourceDisplayNewHashMode:skl,kbl
Lucas De Marchie0403cb2017-12-05 11:01:17 -080053 * Display WA #0390: skl,kbl
Ville Syrjälä93564042017-08-24 22:10:51 +030054 *
55 * Must match Sampler, Pixel Back End, and Media. See
56 * WaCompressedResourceSamplerPbeMediaNewHashMode.
57 */
58 I915_WRITE(CHICKEN_PAR1_1,
59 I915_READ(CHICKEN_PAR1_1) |
60 SKL_DE_COMPRESSED_HASH_MODE);
61 }
62
Rodrigo Vivi82525c12017-06-08 08:50:00 -070063 /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl,cfl */
Mika Kuoppalab033bb62016-06-07 17:19:04 +030064 I915_WRITE(CHICKEN_PAR1_1,
65 I915_READ(CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
66
Rodrigo Vivi82525c12017-06-08 08:50:00 -070067 /* WaEnableChickenDCPR:skl,bxt,kbl,glk,cfl */
Mika Kuoppala590e8ff2016-06-07 17:19:13 +030068 I915_WRITE(GEN8_CHICKEN_DCPR_1,
69 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
Mika Kuoppala0f78dee2016-06-07 17:19:16 +030070
Rodrigo Vivi82525c12017-06-08 08:50:00 -070071 /* WaFbcTurnOffFbcWatermark:skl,bxt,kbl,cfl */
72 /* WaFbcWakeMemOn:skl,bxt,kbl,glk,cfl */
Mika Kuoppala303d4ea2016-06-07 17:19:17 +030073 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
74 DISP_FBC_WM_DIS |
75 DISP_FBC_MEMORY_WAKE);
Mika Kuoppalad1b4eef2016-06-07 17:19:19 +030076
Rodrigo Vivi82525c12017-06-08 08:50:00 -070077 /* WaFbcHighMemBwCorruptionAvoidance:skl,bxt,kbl,cfl */
Mika Kuoppalad1b4eef2016-06-07 17:19:19 +030078 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
79 ILK_DPFC_DISABLE_DUMMY0);
Praveen Paneri32087d12017-08-03 23:02:10 +053080
81 if (IS_SKYLAKE(dev_priv)) {
82 /* WaDisableDopClockGating */
83 I915_WRITE(GEN7_MISCCPCTL, I915_READ(GEN7_MISCCPCTL)
84 & ~GEN7_DOP_CLOCK_GATE_ENABLE);
85 }
Mika Kuoppalab033bb62016-06-07 17:19:04 +030086}
87
Ville Syrjälä46f16e62016-10-31 22:37:22 +020088static void bxt_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deaka82abe42015-03-27 14:00:04 +020089{
Ville Syrjälä46f16e62016-10-31 22:37:22 +020090 gen9_init_clock_gating(dev_priv);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +020091
Nick Hoatha7546152015-06-29 14:07:32 +010092 /* WaDisableSDEUnitClockGating:bxt */
93 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
94 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
95
Imre Deak32608ca2015-03-11 11:10:27 +020096 /*
97 * FIXME:
Ben Widawsky868434c2015-03-11 10:49:32 +020098 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
Imre Deak32608ca2015-03-11 11:10:27 +020099 */
Imre Deak32608ca2015-03-11 11:10:27 +0200100 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
Ben Widawsky868434c2015-03-11 10:49:32 +0200101 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
Imre Deakd965e7ac2015-12-01 10:23:52 +0200102
103 /*
104 * Wa: Backlight PWM may stop in the asserted state, causing backlight
105 * to stay fully on.
106 */
Jani Nikula8aeaf642017-02-15 17:21:37 +0200107 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
108 PWM1_GATING_DIS | PWM2_GATING_DIS);
Imre Deaka82abe42015-03-27 14:00:04 +0200109}
110
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200111static void glk_init_clock_gating(struct drm_i915_private *dev_priv)
112{
113 gen9_init_clock_gating(dev_priv);
114
115 /*
116 * WaDisablePWMClockGating:glk
117 * Backlight PWM may stop in the asserted state, causing backlight
118 * to stay fully on.
119 */
120 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
121 PWM1_GATING_DIS | PWM2_GATING_DIS);
Ander Conselvan de Oliveiraf4f4b592017-02-22 08:34:29 +0200122
123 /* WaDDIIOTimeout:glk */
124 if (IS_GLK_REVID(dev_priv, 0, GLK_REVID_A1)) {
125 u32 val = I915_READ(CHICKEN_MISC_2);
126 val &= ~(GLK_CL0_PWR_DOWN |
127 GLK_CL1_PWR_DOWN |
128 GLK_CL2_PWR_DOWN);
129 I915_WRITE(CHICKEN_MISC_2, val);
130 }
131
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200132}
133
Ville Syrjälä148ac1f2016-10-31 22:37:16 +0200134static void i915_pineview_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200135{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200136 u32 tmp;
137
138 tmp = I915_READ(CLKCFG);
139
140 switch (tmp & CLKCFG_FSB_MASK) {
141 case CLKCFG_FSB_533:
142 dev_priv->fsb_freq = 533; /* 133*4 */
143 break;
144 case CLKCFG_FSB_800:
145 dev_priv->fsb_freq = 800; /* 200*4 */
146 break;
147 case CLKCFG_FSB_667:
148 dev_priv->fsb_freq = 667; /* 167*4 */
149 break;
150 case CLKCFG_FSB_400:
151 dev_priv->fsb_freq = 400; /* 100*4 */
152 break;
153 }
154
155 switch (tmp & CLKCFG_MEM_MASK) {
156 case CLKCFG_MEM_533:
157 dev_priv->mem_freq = 533;
158 break;
159 case CLKCFG_MEM_667:
160 dev_priv->mem_freq = 667;
161 break;
162 case CLKCFG_MEM_800:
163 dev_priv->mem_freq = 800;
164 break;
165 }
166
167 /* detect pineview DDR3 setting */
168 tmp = I915_READ(CSHRDDR3CTL);
169 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
170}
171
Ville Syrjälä148ac1f2016-10-31 22:37:16 +0200172static void i915_ironlake_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200173{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200174 u16 ddrpll, csipll;
175
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +0100176 ddrpll = intel_uncore_read16(&dev_priv->uncore, DDRMPLL1);
177 csipll = intel_uncore_read16(&dev_priv->uncore, CSIPLL0);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200178
179 switch (ddrpll & 0xff) {
180 case 0xc:
181 dev_priv->mem_freq = 800;
182 break;
183 case 0x10:
184 dev_priv->mem_freq = 1066;
185 break;
186 case 0x14:
187 dev_priv->mem_freq = 1333;
188 break;
189 case 0x18:
190 dev_priv->mem_freq = 1600;
191 break;
192 default:
193 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
194 ddrpll & 0xff);
195 dev_priv->mem_freq = 0;
196 break;
197 }
198
Daniel Vetter20e4d402012-08-08 23:35:39 +0200199 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200200
201 switch (csipll & 0x3ff) {
202 case 0x00c:
203 dev_priv->fsb_freq = 3200;
204 break;
205 case 0x00e:
206 dev_priv->fsb_freq = 3733;
207 break;
208 case 0x010:
209 dev_priv->fsb_freq = 4266;
210 break;
211 case 0x012:
212 dev_priv->fsb_freq = 4800;
213 break;
214 case 0x014:
215 dev_priv->fsb_freq = 5333;
216 break;
217 case 0x016:
218 dev_priv->fsb_freq = 5866;
219 break;
220 case 0x018:
221 dev_priv->fsb_freq = 6400;
222 break;
223 default:
224 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
225 csipll & 0x3ff);
226 dev_priv->fsb_freq = 0;
227 break;
228 }
229
230 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200231 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200232 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200233 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200234 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200235 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200236 }
237}
238
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300239static const struct cxsr_latency cxsr_latency_table[] = {
240 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
241 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
242 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
243 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
244 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
245
246 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
247 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
248 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
249 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
250 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
251
252 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
253 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
254 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
255 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
256 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
257
258 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
259 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
260 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
261 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
262 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
263
264 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
265 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
266 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
267 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
268 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
269
270 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
271 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
272 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
273 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
274 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
275};
276
Tvrtko Ursulin44a655c2016-10-13 11:09:23 +0100277static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
278 bool is_ddr3,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300279 int fsb,
280 int mem)
281{
282 const struct cxsr_latency *latency;
283 int i;
284
285 if (fsb == 0 || mem == 0)
286 return NULL;
287
288 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
289 latency = &cxsr_latency_table[i];
290 if (is_desktop == latency->is_desktop &&
291 is_ddr3 == latency->is_ddr3 &&
292 fsb == latency->fsb_freq && mem == latency->mem_freq)
293 return latency;
294 }
295
296 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
297
298 return NULL;
299}
300
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200301static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
302{
303 u32 val;
304
Chris Wilson337fa6e2019-04-26 09:17:20 +0100305 vlv_punit_get(dev_priv);
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200306
307 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
308 if (enable)
309 val &= ~FORCE_DDR_HIGH_FREQ;
310 else
311 val |= FORCE_DDR_HIGH_FREQ;
312 val &= ~FORCE_DDR_LOW_FREQ;
313 val |= FORCE_DDR_FREQ_REQ_ACK;
314 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
315
316 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
317 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
318 DRM_ERROR("timed out waiting for Punit DDR DVFS request\n");
319
Chris Wilson337fa6e2019-04-26 09:17:20 +0100320 vlv_punit_put(dev_priv);
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200321}
322
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200323static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
324{
325 u32 val;
326
Chris Wilson337fa6e2019-04-26 09:17:20 +0100327 vlv_punit_get(dev_priv);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200328
Ville Syrjäläc11b8132018-11-29 19:55:03 +0200329 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200330 if (enable)
331 val |= DSP_MAXFIFO_PM5_ENABLE;
332 else
333 val &= ~DSP_MAXFIFO_PM5_ENABLE;
Ville Syrjäläc11b8132018-11-29 19:55:03 +0200334 vlv_punit_write(dev_priv, PUNIT_REG_DSPSSPM, val);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200335
Chris Wilson337fa6e2019-04-26 09:17:20 +0100336 vlv_punit_put(dev_priv);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200337}
338
Ville Syrjäläf4998962015-03-10 17:02:21 +0200339#define FW_WM(value, plane) \
340 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
341
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200342static bool _intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300343{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200344 bool was_enabled;
Imre Deak5209b1f2014-07-01 12:36:17 +0300345 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300346
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100347 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200348 was_enabled = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300349 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300350 POSTING_READ(FW_BLC_SELF_VLV);
Jani Nikulac0f86832016-12-07 12:13:04 +0200351 } else if (IS_G4X(dev_priv) || IS_I965GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200352 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300353 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300354 POSTING_READ(FW_BLC_SELF);
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +0200355 } else if (IS_PINEVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200356 val = I915_READ(DSPFW3);
357 was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
358 if (enable)
359 val |= PINEVIEW_SELF_REFRESH_EN;
360 else
361 val &= ~PINEVIEW_SELF_REFRESH_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300362 I915_WRITE(DSPFW3, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300363 POSTING_READ(DSPFW3);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100364 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200365 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300366 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
367 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
368 I915_WRITE(FW_BLC_SELF, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300369 POSTING_READ(FW_BLC_SELF);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100370 } else if (IS_I915GM(dev_priv)) {
Ville Syrjäläacb91352016-07-29 17:57:02 +0300371 /*
372 * FIXME can't find a bit like this for 915G, and
373 * and yet it does have the related watermark in
374 * FW_BLC_SELF. What's going on?
375 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200376 was_enabled = I915_READ(INSTPM) & INSTPM_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300377 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
378 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
379 I915_WRITE(INSTPM, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300380 POSTING_READ(INSTPM);
Imre Deak5209b1f2014-07-01 12:36:17 +0300381 } else {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200382 return false;
Imre Deak5209b1f2014-07-01 12:36:17 +0300383 }
384
Ville Syrjälä1489bba2017-03-02 19:15:07 +0200385 trace_intel_memory_cxsr(dev_priv, was_enabled, enable);
386
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200387 DRM_DEBUG_KMS("memory self-refresh is %s (was %s)\n",
388 enableddisabled(enable),
389 enableddisabled(was_enabled));
390
391 return was_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300392}
393
Ville Syrjälä62571fc2017-04-21 21:14:23 +0300394/**
395 * intel_set_memory_cxsr - Configure CxSR state
396 * @dev_priv: i915 device
397 * @enable: Allow vs. disallow CxSR
398 *
399 * Allow or disallow the system to enter a special CxSR
400 * (C-state self refresh) state. What typically happens in CxSR mode
401 * is that several display FIFOs may get combined into a single larger
402 * FIFO for a particular plane (so called max FIFO mode) to allow the
403 * system to defer memory fetches longer, and the memory will enter
404 * self refresh.
405 *
406 * Note that enabling CxSR does not guarantee that the system enter
407 * this special mode, nor does it guarantee that the system stays
408 * in that mode once entered. So this just allows/disallows the system
409 * to autonomously utilize the CxSR mode. Other factors such as core
410 * C-states will affect when/if the system actually enters/exits the
411 * CxSR mode.
412 *
413 * Note that on VLV/CHV this actually only controls the max FIFO mode,
414 * and the system is free to enter/exit memory self refresh at any time
415 * even when the use of CxSR has been disallowed.
416 *
417 * While the system is actually in the CxSR/max FIFO mode, some plane
418 * control registers will not get latched on vblank. Thus in order to
419 * guarantee the system will respond to changes in the plane registers
420 * we must always disallow CxSR prior to making changes to those registers.
421 * Unfortunately the system will re-evaluate the CxSR conditions at
422 * frame start which happens after vblank start (which is when the plane
423 * registers would get latched), so we can't proceed with the plane update
424 * during the same frame where we disallowed CxSR.
425 *
426 * Certain platforms also have a deeper HPLL SR mode. Fortunately the
427 * HPLL SR mode depends on CxSR itself, so we don't have to hand hold
428 * the hardware w.r.t. HPLL SR when writing to plane registers.
429 * Disallowing just CxSR is sufficient.
430 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200431bool intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200432{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200433 bool ret;
434
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200435 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200436 ret = _intel_set_memory_cxsr(dev_priv, enable);
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300437 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
438 dev_priv->wm.vlv.cxsr = enable;
439 else if (IS_G4X(dev_priv))
440 dev_priv->wm.g4x.cxsr = enable;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200441 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200442
443 return ret;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200444}
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200445
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300446/*
447 * Latency for FIFO fetches is dependent on several factors:
448 * - memory configuration (speed, channels)
449 * - chipset
450 * - current MCH state
451 * It can be fairly high in some situations, so here we assume a fairly
452 * pessimal value. It's a tradeoff between extra memory fetches (if we
453 * set this value too high, the FIFO will fetch frequently to stay full)
454 * and power consumption (set it too low to save power and we might see
455 * FIFO underruns and display "flicker").
456 *
457 * A value of 5us seems to be a good balance; safe for very low end
458 * platforms but not overly aggressive on lower latency configs.
459 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100460static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300461
Ville Syrjäläb5004722015-03-05 21:19:47 +0200462#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
463 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
464
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200465static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
Ville Syrjäläb5004722015-03-05 21:19:47 +0200466{
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200467 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200468 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200469 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200470 enum pipe pipe = crtc->pipe;
471 int sprite0_start, sprite1_start;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200472
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200473 switch (pipe) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200474 u32 dsparb, dsparb2, dsparb3;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200475 case PIPE_A:
476 dsparb = I915_READ(DSPARB);
477 dsparb2 = I915_READ(DSPARB2);
478 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
479 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
480 break;
481 case PIPE_B:
482 dsparb = I915_READ(DSPARB);
483 dsparb2 = I915_READ(DSPARB2);
484 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
485 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
486 break;
487 case PIPE_C:
488 dsparb2 = I915_READ(DSPARB2);
489 dsparb3 = I915_READ(DSPARB3);
490 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
491 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
492 break;
493 default:
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200494 MISSING_CASE(pipe);
495 return;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200496 }
497
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200498 fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
499 fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
500 fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
501 fifo_state->plane[PLANE_CURSOR] = 63;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200502}
503
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200504static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv,
505 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300506{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200507 u32 dsparb = I915_READ(DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300508 int size;
509
510 size = dsparb & 0x7f;
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200511 if (i9xx_plane == PLANE_B)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300512 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
513
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200514 DRM_DEBUG_KMS("FIFO size - (0x%08x) %c: %d\n",
515 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300516
517 return size;
518}
519
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200520static int i830_get_fifo_size(struct drm_i915_private *dev_priv,
521 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300522{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200523 u32 dsparb = I915_READ(DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300524 int size;
525
526 size = dsparb & 0x1ff;
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200527 if (i9xx_plane == PLANE_B)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300528 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
529 size >>= 1; /* Convert to cachelines */
530
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200531 DRM_DEBUG_KMS("FIFO size - (0x%08x) %c: %d\n",
532 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300533
534 return size;
535}
536
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200537static int i845_get_fifo_size(struct drm_i915_private *dev_priv,
538 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300539{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200540 u32 dsparb = I915_READ(DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300541 int size;
542
543 size = dsparb & 0x7f;
544 size >>= 2; /* Convert to cachelines */
545
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200546 DRM_DEBUG_KMS("FIFO size - (0x%08x) %c: %d\n",
547 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300548
549 return size;
550}
551
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300552/* Pineview has different values for various configs */
553static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300554 .fifo_size = PINEVIEW_DISPLAY_FIFO,
555 .max_wm = PINEVIEW_MAX_WM,
556 .default_wm = PINEVIEW_DFT_WM,
557 .guard_size = PINEVIEW_GUARD_WM,
558 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300559};
560static const struct intel_watermark_params pineview_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};
567static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300568 .fifo_size = PINEVIEW_CURSOR_FIFO,
569 .max_wm = PINEVIEW_CURSOR_MAX_WM,
570 .default_wm = PINEVIEW_CURSOR_DFT_WM,
571 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
572 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300573};
574static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300575 .fifo_size = PINEVIEW_CURSOR_FIFO,
576 .max_wm = PINEVIEW_CURSOR_MAX_WM,
577 .default_wm = PINEVIEW_CURSOR_DFT_WM,
578 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
579 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300580};
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300581static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300582 .fifo_size = I965_CURSOR_FIFO,
583 .max_wm = I965_CURSOR_MAX_WM,
584 .default_wm = I965_CURSOR_DFT_WM,
585 .guard_size = 2,
586 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300587};
588static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300589 .fifo_size = I945_FIFO_SIZE,
590 .max_wm = I915_MAX_WM,
591 .default_wm = 1,
592 .guard_size = 2,
593 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300594};
595static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300596 .fifo_size = I915_FIFO_SIZE,
597 .max_wm = I915_MAX_WM,
598 .default_wm = 1,
599 .guard_size = 2,
600 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300601};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300602static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300603 .fifo_size = I855GM_FIFO_SIZE,
604 .max_wm = I915_MAX_WM,
605 .default_wm = 1,
606 .guard_size = 2,
607 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300608};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300609static const struct intel_watermark_params i830_bc_wm_info = {
610 .fifo_size = I855GM_FIFO_SIZE,
611 .max_wm = I915_MAX_WM/2,
612 .default_wm = 1,
613 .guard_size = 2,
614 .cacheline_size = I830_FIFO_LINE_SIZE,
615};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200616static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300617 .fifo_size = I830_FIFO_SIZE,
618 .max_wm = I915_MAX_WM,
619 .default_wm = 1,
620 .guard_size = 2,
621 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300622};
623
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300624/**
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300625 * intel_wm_method1 - Method 1 / "small buffer" watermark formula
626 * @pixel_rate: Pipe pixel rate in kHz
627 * @cpp: Plane bytes per pixel
628 * @latency: Memory wakeup latency in 0.1us units
629 *
630 * Compute the watermark using the method 1 or "small buffer"
631 * formula. The caller may additonally add extra cachelines
632 * to account for TLB misses and clock crossings.
633 *
634 * This method is concerned with the short term drain rate
635 * of the FIFO, ie. it does not account for blanking periods
636 * which would effectively reduce the average drain rate across
637 * a longer period. The name "small" refers to the fact the
638 * FIFO is relatively small compared to the amount of data
639 * fetched.
640 *
641 * The FIFO level vs. time graph might look something like:
642 *
643 * |\ |\
644 * | \ | \
645 * __---__---__ (- plane active, _ blanking)
646 * -> time
647 *
648 * or perhaps like this:
649 *
650 * |\|\ |\|\
651 * __----__----__ (- plane active, _ blanking)
652 * -> time
653 *
654 * Returns:
655 * The watermark in bytes
656 */
657static unsigned int intel_wm_method1(unsigned int pixel_rate,
658 unsigned int cpp,
659 unsigned int latency)
660{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200661 u64 ret;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300662
Ville Syrjäläd492a292019-04-08 18:27:01 +0300663 ret = mul_u32_u32(pixel_rate, cpp * latency);
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300664 ret = DIV_ROUND_UP_ULL(ret, 10000);
665
666 return ret;
667}
668
669/**
670 * intel_wm_method2 - Method 2 / "large buffer" watermark formula
671 * @pixel_rate: Pipe pixel rate in kHz
672 * @htotal: Pipe horizontal total
673 * @width: Plane width in pixels
674 * @cpp: Plane bytes per pixel
675 * @latency: Memory wakeup latency in 0.1us units
676 *
677 * Compute the watermark using the method 2 or "large buffer"
678 * formula. The caller may additonally add extra cachelines
679 * to account for TLB misses and clock crossings.
680 *
681 * This method is concerned with the long term drain rate
682 * of the FIFO, ie. it does account for blanking periods
683 * which effectively reduce the average drain rate across
684 * a longer period. The name "large" refers to the fact the
685 * FIFO is relatively large compared to the amount of data
686 * fetched.
687 *
688 * The FIFO level vs. time graph might look something like:
689 *
690 * |\___ |\___
691 * | \___ | \___
692 * | \ | \
693 * __ --__--__--__--__--__--__ (- plane active, _ blanking)
694 * -> time
695 *
696 * Returns:
697 * The watermark in bytes
698 */
699static unsigned int intel_wm_method2(unsigned int pixel_rate,
700 unsigned int htotal,
701 unsigned int width,
702 unsigned int cpp,
703 unsigned int latency)
704{
705 unsigned int ret;
706
707 /*
708 * FIXME remove once all users are computing
709 * watermarks in the correct place.
710 */
711 if (WARN_ON_ONCE(htotal == 0))
712 htotal = 1;
713
714 ret = (latency * pixel_rate) / (htotal * 10000);
715 ret = (ret + 1) * width * cpp;
716
717 return ret;
718}
719
720/**
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300721 * intel_calculate_wm - calculate watermark level
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300722 * @pixel_rate: pixel clock
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300723 * @wm: chip FIFO params
Chris Wilson31383412018-02-14 14:03:03 +0000724 * @fifo_size: size of the FIFO buffer
Ville Syrjäläac484962016-01-20 21:05:26 +0200725 * @cpp: bytes per pixel
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300726 * @latency_ns: memory latency for the platform
727 *
728 * Calculate the watermark level (the level at which the display plane will
729 * start fetching from memory again). Each chip has a different display
730 * FIFO size and allocation, so the caller needs to figure that out and pass
731 * in the correct intel_watermark_params structure.
732 *
733 * As the pixel clock runs, the FIFO will be drained at a rate that depends
734 * on the pixel size. When it reaches the watermark level, it'll start
735 * fetching FIFO line sized based chunks from memory until the FIFO fills
736 * past the watermark point. If the FIFO drains completely, a FIFO underrun
737 * will occur, and a display engine hang could result.
738 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300739static unsigned int intel_calculate_wm(int pixel_rate,
740 const struct intel_watermark_params *wm,
741 int fifo_size, int cpp,
742 unsigned int latency_ns)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300743{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300744 int entries, wm_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300745
746 /*
747 * Note: we need to make sure we don't overflow for various clock &
748 * latency values.
749 * clocks go from a few thousand to several hundred thousand.
750 * latency is usually a few thousand
751 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300752 entries = intel_wm_method1(pixel_rate, cpp,
753 latency_ns / 100);
754 entries = DIV_ROUND_UP(entries, wm->cacheline_size) +
755 wm->guard_size;
756 DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300757
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300758 wm_size = fifo_size - entries;
759 DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300760
761 /* Don't promote wm_size to unsigned... */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300762 if (wm_size > wm->max_wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300763 wm_size = wm->max_wm;
764 if (wm_size <= 0)
765 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300766
767 /*
768 * Bspec seems to indicate that the value shouldn't be lower than
769 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
770 * Lets go for 8 which is the burst size since certain platforms
771 * already use a hardcoded 8 (which is what the spec says should be
772 * done).
773 */
774 if (wm_size <= 8)
775 wm_size = 8;
776
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300777 return wm_size;
778}
779
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300780static bool is_disabling(int old, int new, int threshold)
781{
782 return old >= threshold && new < threshold;
783}
784
785static bool is_enabling(int old, int new, int threshold)
786{
787 return old < threshold && new >= threshold;
788}
789
Ville Syrjälä6d5019b2017-04-21 21:14:20 +0300790static int intel_wm_num_levels(struct drm_i915_private *dev_priv)
791{
792 return dev_priv->wm.max_level + 1;
793}
794
Ville Syrjälä24304d812017-03-14 17:10:49 +0200795static bool intel_wm_plane_visible(const struct intel_crtc_state *crtc_state,
796 const struct intel_plane_state *plane_state)
797{
798 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
799
800 /* FIXME check the 'enable' instead */
801 if (!crtc_state->base.active)
802 return false;
803
804 /*
805 * Treat cursor with fb as always visible since cursor updates
806 * can happen faster than the vrefresh rate, and the current
807 * watermark code doesn't handle that correctly. Cursor updates
808 * which set/clear the fb or change the cursor size are going
809 * to get throttled by intel_legacy_cursor_update() to work
810 * around this problem with the watermark code.
811 */
812 if (plane->id == PLANE_CURSOR)
813 return plane_state->base.fb != NULL;
814 else
815 return plane_state->base.visible;
816}
817
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200818static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300819{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200820 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300821
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200822 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200823 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300824 if (enabled)
825 return NULL;
826 enabled = crtc;
827 }
828 }
829
830 return enabled;
831}
832
Ville Syrjälä432081b2016-10-31 22:37:03 +0200833static void pineview_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300834{
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200835 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200836 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300837 const struct cxsr_latency *latency;
838 u32 reg;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300839 unsigned int wm;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300840
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +0000841 latency = intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100842 dev_priv->is_ddr3,
843 dev_priv->fsb_freq,
844 dev_priv->mem_freq);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300845 if (!latency) {
846 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300847 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300848 return;
849 }
850
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200851 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300852 if (crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200853 const struct drm_display_mode *adjusted_mode =
854 &crtc->config->base.adjusted_mode;
855 const struct drm_framebuffer *fb =
856 crtc->base.primary->state->fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200857 int cpp = fb->format->cpp[0];
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300858 int clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300859
860 /* Display SR */
861 wm = intel_calculate_wm(clock, &pineview_display_wm,
862 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200863 cpp, latency->display_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300864 reg = I915_READ(DSPFW1);
865 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200866 reg |= FW_WM(wm, SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300867 I915_WRITE(DSPFW1, reg);
868 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
869
870 /* cursor SR */
871 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
872 pineview_display_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300873 4, latency->cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300874 reg = I915_READ(DSPFW3);
875 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200876 reg |= FW_WM(wm, CURSOR_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300877 I915_WRITE(DSPFW3, reg);
878
879 /* Display HPLL off SR */
880 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
881 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200882 cpp, latency->display_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300883 reg = I915_READ(DSPFW3);
884 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200885 reg |= FW_WM(wm, HPLL_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300886 I915_WRITE(DSPFW3, reg);
887
888 /* cursor HPLL off SR */
889 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
890 pineview_display_hplloff_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300891 4, latency->cursor_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300892 reg = I915_READ(DSPFW3);
893 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200894 reg |= FW_WM(wm, HPLL_CURSOR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300895 I915_WRITE(DSPFW3, reg);
896 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
897
Imre Deak5209b1f2014-07-01 12:36:17 +0300898 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300899 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300900 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300901 }
902}
903
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300904/*
905 * Documentation says:
906 * "If the line size is small, the TLB fetches can get in the way of the
907 * data fetches, causing some lag in the pixel data return which is not
908 * accounted for in the above formulas. The following adjustment only
909 * needs to be applied if eight whole lines fit in the buffer at once.
910 * The WM is adjusted upwards by the difference between the FIFO size
911 * and the size of 8 whole lines. This adjustment is always performed
912 * in the actual pixel depth regardless of whether FBC is enabled or not."
913 */
Chris Wilson1a1f1282017-11-07 14:03:38 +0000914static unsigned int g4x_tlb_miss_wa(int fifo_size, int width, int cpp)
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300915{
916 int tlb_miss = fifo_size * 64 - width * cpp * 8;
917
918 return max(0, tlb_miss);
919}
920
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300921static void g4x_write_wm_values(struct drm_i915_private *dev_priv,
922 const struct g4x_wm_values *wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300923{
Ville Syrjäläe93329a2017-04-21 21:14:31 +0300924 enum pipe pipe;
925
926 for_each_pipe(dev_priv, pipe)
927 trace_g4x_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
928
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300929 I915_WRITE(DSPFW1,
930 FW_WM(wm->sr.plane, SR) |
931 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
932 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
933 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
934 I915_WRITE(DSPFW2,
935 (wm->fbc_en ? DSPFW_FBC_SR_EN : 0) |
936 FW_WM(wm->sr.fbc, FBC_SR) |
937 FW_WM(wm->hpll.fbc, FBC_HPLL_SR) |
938 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEB) |
939 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
940 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
941 I915_WRITE(DSPFW3,
942 (wm->hpll_en ? DSPFW_HPLL_SR_EN : 0) |
943 FW_WM(wm->sr.cursor, CURSOR_SR) |
944 FW_WM(wm->hpll.cursor, HPLL_CURSOR) |
945 FW_WM(wm->hpll.plane, HPLL_SR));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300946
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300947 POSTING_READ(DSPFW1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300948}
949
Ville Syrjälä15665972015-03-10 16:16:28 +0200950#define FW_WM_VLV(value, plane) \
951 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
952
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200953static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200954 const struct vlv_wm_values *wm)
955{
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200956 enum pipe pipe;
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200957
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200958 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläc137d662017-03-02 19:15:06 +0200959 trace_vlv_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
960
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200961 I915_WRITE(VLV_DDL(pipe),
962 (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
963 (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
964 (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
965 (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
966 }
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200967
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +0200968 /*
969 * Zero the (unused) WM1 watermarks, and also clear all the
970 * high order bits so that there are no out of bounds values
971 * present in the registers during the reprogramming.
972 */
973 I915_WRITE(DSPHOWM, 0);
974 I915_WRITE(DSPHOWM1, 0);
975 I915_WRITE(DSPFW4, 0);
976 I915_WRITE(DSPFW5, 0);
977 I915_WRITE(DSPFW6, 0);
978
Ville Syrjäläae801522015-03-05 21:19:49 +0200979 I915_WRITE(DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +0200980 FW_WM(wm->sr.plane, SR) |
Ville Syrjälä1b313892016-11-28 19:37:08 +0200981 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
982 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
983 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200984 I915_WRITE(DSPFW2,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200985 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
986 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
987 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200988 I915_WRITE(DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +0200989 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +0200990
991 if (IS_CHERRYVIEW(dev_priv)) {
992 I915_WRITE(DSPFW7_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200993 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
994 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200995 I915_WRITE(DSPFW8_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200996 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
997 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
Ville Syrjäläae801522015-03-05 21:19:49 +0200998 I915_WRITE(DSPFW9_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200999 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
1000 FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001001 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001002 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001003 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
1004 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
1005 FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
1006 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1007 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1008 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1009 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1010 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1011 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001012 } else {
1013 I915_WRITE(DSPFW7,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001014 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1015 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001016 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001017 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001018 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1019 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1020 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1021 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1022 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1023 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001024 }
1025
1026 POSTING_READ(DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001027}
1028
Ville Syrjälä15665972015-03-10 16:16:28 +02001029#undef FW_WM_VLV
1030
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001031static void g4x_setup_wm_latency(struct drm_i915_private *dev_priv)
1032{
1033 /* all latencies in usec */
1034 dev_priv->wm.pri_latency[G4X_WM_LEVEL_NORMAL] = 5;
1035 dev_priv->wm.pri_latency[G4X_WM_LEVEL_SR] = 12;
Ville Syrjälä79d94302017-04-21 21:14:30 +03001036 dev_priv->wm.pri_latency[G4X_WM_LEVEL_HPLL] = 35;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001037
Ville Syrjälä79d94302017-04-21 21:14:30 +03001038 dev_priv->wm.max_level = G4X_WM_LEVEL_HPLL;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001039}
1040
1041static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
1042{
1043 /*
1044 * DSPCNTR[13] supposedly controls whether the
1045 * primary plane can use the FIFO space otherwise
1046 * reserved for the sprite plane. It's not 100% clear
1047 * what the actual FIFO size is, but it looks like we
1048 * can happily set both primary and sprite watermarks
1049 * up to 127 cachelines. So that would seem to mean
1050 * that either DSPCNTR[13] doesn't do anything, or that
1051 * the total FIFO is >= 256 cachelines in size. Either
1052 * way, we don't seem to have to worry about this
1053 * repartitioning as the maximum watermark value the
1054 * register can hold for each plane is lower than the
1055 * minimum FIFO size.
1056 */
1057 switch (plane_id) {
1058 case PLANE_CURSOR:
1059 return 63;
1060 case PLANE_PRIMARY:
1061 return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
1062 case PLANE_SPRITE0:
1063 return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
1064 default:
1065 MISSING_CASE(plane_id);
1066 return 0;
1067 }
1068}
1069
1070static int g4x_fbc_fifo_size(int level)
1071{
1072 switch (level) {
1073 case G4X_WM_LEVEL_SR:
1074 return 7;
1075 case G4X_WM_LEVEL_HPLL:
1076 return 15;
1077 default:
1078 MISSING_CASE(level);
1079 return 0;
1080 }
1081}
1082
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001083static u16 g4x_compute_wm(const struct intel_crtc_state *crtc_state,
1084 const struct intel_plane_state *plane_state,
1085 int level)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001086{
1087 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1088 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1089 const struct drm_display_mode *adjusted_mode =
1090 &crtc_state->base.adjusted_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001091 unsigned int latency = dev_priv->wm.pri_latency[level] * 10;
1092 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001093
1094 if (latency == 0)
1095 return USHRT_MAX;
1096
1097 if (!intel_wm_plane_visible(crtc_state, plane_state))
1098 return 0;
1099
Ville Syrjäläd56e8232019-07-03 23:08:22 +03001100 cpp = plane_state->base.fb->format->cpp[0];
1101
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001102 /*
1103 * Not 100% sure which way ELK should go here as the
1104 * spec only says CL/CTG should assume 32bpp and BW
1105 * doesn't need to. But as these things followed the
1106 * mobile vs. desktop lines on gen3 as well, let's
1107 * assume ELK doesn't need this.
1108 *
1109 * The spec also fails to list such a restriction for
1110 * the HPLL watermark, which seems a little strange.
1111 * Let's use 32bpp for the HPLL watermark as well.
1112 */
1113 if (IS_GM45(dev_priv) && plane->id == PLANE_PRIMARY &&
1114 level != G4X_WM_LEVEL_NORMAL)
Ville Syrjäläd56e8232019-07-03 23:08:22 +03001115 cpp = max(cpp, 4u);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001116
1117 clock = adjusted_mode->crtc_clock;
1118 htotal = adjusted_mode->crtc_htotal;
1119
1120 if (plane->id == PLANE_CURSOR)
1121 width = plane_state->base.crtc_w;
1122 else
1123 width = drm_rect_width(&plane_state->base.dst);
1124
1125 if (plane->id == PLANE_CURSOR) {
1126 wm = intel_wm_method2(clock, htotal, width, cpp, latency);
1127 } else if (plane->id == PLANE_PRIMARY &&
1128 level == G4X_WM_LEVEL_NORMAL) {
1129 wm = intel_wm_method1(clock, cpp, latency);
1130 } else {
Chris Wilson1a1f1282017-11-07 14:03:38 +00001131 unsigned int small, large;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001132
1133 small = intel_wm_method1(clock, cpp, latency);
1134 large = intel_wm_method2(clock, htotal, width, cpp, latency);
1135
1136 wm = min(small, large);
1137 }
1138
1139 wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
1140 width, cpp);
1141
1142 wm = DIV_ROUND_UP(wm, 64) + 2;
1143
Chris Wilson1a1f1282017-11-07 14:03:38 +00001144 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001145}
1146
1147static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1148 int level, enum plane_id plane_id, u16 value)
1149{
1150 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1151 bool dirty = false;
1152
1153 for (; level < intel_wm_num_levels(dev_priv); level++) {
1154 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1155
1156 dirty |= raw->plane[plane_id] != value;
1157 raw->plane[plane_id] = value;
1158 }
1159
1160 return dirty;
1161}
1162
1163static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
1164 int level, u16 value)
1165{
1166 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1167 bool dirty = false;
1168
1169 /* NORMAL level doesn't have an FBC watermark */
1170 level = max(level, G4X_WM_LEVEL_SR);
1171
1172 for (; level < intel_wm_num_levels(dev_priv); level++) {
1173 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1174
1175 dirty |= raw->fbc != value;
1176 raw->fbc = value;
1177 }
1178
1179 return dirty;
1180}
1181
Maarten Lankhorstec193642019-06-28 10:55:17 +02001182static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
1183 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001184 u32 pri_val);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001185
1186static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1187 const struct intel_plane_state *plane_state)
1188{
1189 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1190 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
1191 enum plane_id plane_id = plane->id;
1192 bool dirty = false;
1193 int level;
1194
1195 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1196 dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1197 if (plane_id == PLANE_PRIMARY)
1198 dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
1199 goto out;
1200 }
1201
1202 for (level = 0; level < num_levels; level++) {
1203 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1204 int wm, max_wm;
1205
1206 wm = g4x_compute_wm(crtc_state, plane_state, level);
1207 max_wm = g4x_plane_fifo_size(plane_id, level);
1208
1209 if (wm > max_wm)
1210 break;
1211
1212 dirty |= raw->plane[plane_id] != wm;
1213 raw->plane[plane_id] = wm;
1214
1215 if (plane_id != PLANE_PRIMARY ||
1216 level == G4X_WM_LEVEL_NORMAL)
1217 continue;
1218
1219 wm = ilk_compute_fbc_wm(crtc_state, plane_state,
1220 raw->plane[plane_id]);
1221 max_wm = g4x_fbc_fifo_size(level);
1222
1223 /*
1224 * FBC wm is not mandatory as we
1225 * can always just disable its use.
1226 */
1227 if (wm > max_wm)
1228 wm = USHRT_MAX;
1229
1230 dirty |= raw->fbc != wm;
1231 raw->fbc = wm;
1232 }
1233
1234 /* mark watermarks as invalid */
1235 dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1236
1237 if (plane_id == PLANE_PRIMARY)
1238 dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
1239
1240 out:
1241 if (dirty) {
1242 DRM_DEBUG_KMS("%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
1243 plane->base.name,
1244 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
1245 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
1246 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
1247
1248 if (plane_id == PLANE_PRIMARY)
1249 DRM_DEBUG_KMS("FBC watermarks: SR=%d, HPLL=%d\n",
1250 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
1251 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
1252 }
1253
1254 return dirty;
1255}
1256
1257static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1258 enum plane_id plane_id, int level)
1259{
1260 const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1261
1262 return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
1263}
1264
1265static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
1266 int level)
1267{
1268 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1269
1270 if (level > dev_priv->wm.max_level)
1271 return false;
1272
1273 return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1274 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1275 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1276}
1277
1278/* mark all levels starting from 'level' as invalid */
1279static void g4x_invalidate_wms(struct intel_crtc *crtc,
1280 struct g4x_wm_state *wm_state, int level)
1281{
1282 if (level <= G4X_WM_LEVEL_NORMAL) {
1283 enum plane_id plane_id;
1284
1285 for_each_plane_id_on_crtc(crtc, plane_id)
1286 wm_state->wm.plane[plane_id] = USHRT_MAX;
1287 }
1288
1289 if (level <= G4X_WM_LEVEL_SR) {
1290 wm_state->cxsr = false;
1291 wm_state->sr.cursor = USHRT_MAX;
1292 wm_state->sr.plane = USHRT_MAX;
1293 wm_state->sr.fbc = USHRT_MAX;
1294 }
1295
1296 if (level <= G4X_WM_LEVEL_HPLL) {
1297 wm_state->hpll_en = false;
1298 wm_state->hpll.cursor = USHRT_MAX;
1299 wm_state->hpll.plane = USHRT_MAX;
1300 wm_state->hpll.fbc = USHRT_MAX;
1301 }
1302}
1303
1304static int g4x_compute_pipe_wm(struct intel_crtc_state *crtc_state)
1305{
1306 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1307 struct intel_atomic_state *state =
1308 to_intel_atomic_state(crtc_state->base.state);
1309 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001310 int num_active_planes = hweight8(crtc_state->active_planes &
1311 ~BIT(PLANE_CURSOR));
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001312 const struct g4x_pipe_wm *raw;
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001313 const struct intel_plane_state *old_plane_state;
1314 const struct intel_plane_state *new_plane_state;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001315 struct intel_plane *plane;
1316 enum plane_id plane_id;
1317 int i, level;
1318 unsigned int dirty = 0;
1319
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001320 for_each_oldnew_intel_plane_in_state(state, plane,
1321 old_plane_state,
1322 new_plane_state, i) {
1323 if (new_plane_state->base.crtc != &crtc->base &&
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001324 old_plane_state->base.crtc != &crtc->base)
1325 continue;
1326
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001327 if (g4x_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001328 dirty |= BIT(plane->id);
1329 }
1330
1331 if (!dirty)
1332 return 0;
1333
1334 level = G4X_WM_LEVEL_NORMAL;
1335 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1336 goto out;
1337
1338 raw = &crtc_state->wm.g4x.raw[level];
1339 for_each_plane_id_on_crtc(crtc, plane_id)
1340 wm_state->wm.plane[plane_id] = raw->plane[plane_id];
1341
1342 level = G4X_WM_LEVEL_SR;
1343
1344 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1345 goto out;
1346
1347 raw = &crtc_state->wm.g4x.raw[level];
1348 wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
1349 wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
1350 wm_state->sr.fbc = raw->fbc;
1351
1352 wm_state->cxsr = num_active_planes == BIT(PLANE_PRIMARY);
1353
1354 level = G4X_WM_LEVEL_HPLL;
1355
1356 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1357 goto out;
1358
1359 raw = &crtc_state->wm.g4x.raw[level];
1360 wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
1361 wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
1362 wm_state->hpll.fbc = raw->fbc;
1363
1364 wm_state->hpll_en = wm_state->cxsr;
1365
1366 level++;
1367
1368 out:
1369 if (level == G4X_WM_LEVEL_NORMAL)
1370 return -EINVAL;
1371
1372 /* invalidate the higher levels */
1373 g4x_invalidate_wms(crtc, wm_state, level);
1374
1375 /*
1376 * Determine if the FBC watermark(s) can be used. IF
1377 * this isn't the case we prefer to disable the FBC
1378 ( watermark(s) rather than disable the SR/HPLL
1379 * level(s) entirely.
1380 */
1381 wm_state->fbc_en = level > G4X_WM_LEVEL_NORMAL;
1382
1383 if (level >= G4X_WM_LEVEL_SR &&
1384 wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
1385 wm_state->fbc_en = false;
1386 else if (level >= G4X_WM_LEVEL_HPLL &&
1387 wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
1388 wm_state->fbc_en = false;
1389
1390 return 0;
1391}
1392
Matt Ropercd1d3ee2018-12-10 13:54:14 -08001393static int g4x_compute_intermediate_wm(struct intel_crtc_state *new_crtc_state)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001394{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08001395 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001396 struct g4x_wm_state *intermediate = &new_crtc_state->wm.g4x.intermediate;
1397 const struct g4x_wm_state *optimal = &new_crtc_state->wm.g4x.optimal;
1398 struct intel_atomic_state *intel_state =
1399 to_intel_atomic_state(new_crtc_state->base.state);
1400 const struct intel_crtc_state *old_crtc_state =
1401 intel_atomic_get_old_crtc_state(intel_state, crtc);
1402 const struct g4x_wm_state *active = &old_crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001403 enum plane_id plane_id;
1404
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001405 if (!new_crtc_state->base.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->base)) {
1406 *intermediate = *optimal;
1407
1408 intermediate->cxsr = false;
1409 intermediate->hpll_en = false;
1410 goto out;
1411 }
1412
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001413 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001414 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001415 intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001416 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001417 intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
1418
1419 for_each_plane_id_on_crtc(crtc, plane_id) {
1420 intermediate->wm.plane[plane_id] =
1421 max(optimal->wm.plane[plane_id],
1422 active->wm.plane[plane_id]);
1423
1424 WARN_ON(intermediate->wm.plane[plane_id] >
1425 g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
1426 }
1427
1428 intermediate->sr.plane = max(optimal->sr.plane,
1429 active->sr.plane);
1430 intermediate->sr.cursor = max(optimal->sr.cursor,
1431 active->sr.cursor);
1432 intermediate->sr.fbc = max(optimal->sr.fbc,
1433 active->sr.fbc);
1434
1435 intermediate->hpll.plane = max(optimal->hpll.plane,
1436 active->hpll.plane);
1437 intermediate->hpll.cursor = max(optimal->hpll.cursor,
1438 active->hpll.cursor);
1439 intermediate->hpll.fbc = max(optimal->hpll.fbc,
1440 active->hpll.fbc);
1441
1442 WARN_ON((intermediate->sr.plane >
1443 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
1444 intermediate->sr.cursor >
1445 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
1446 intermediate->cxsr);
1447 WARN_ON((intermediate->sr.plane >
1448 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
1449 intermediate->sr.cursor >
1450 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
1451 intermediate->hpll_en);
1452
1453 WARN_ON(intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
1454 intermediate->fbc_en && intermediate->cxsr);
1455 WARN_ON(intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
1456 intermediate->fbc_en && intermediate->hpll_en);
1457
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001458out:
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001459 /*
1460 * If our intermediate WM are identical to the final WM, then we can
1461 * omit the post-vblank programming; only update if it's different.
1462 */
1463 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001464 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001465
1466 return 0;
1467}
1468
1469static void g4x_merge_wm(struct drm_i915_private *dev_priv,
1470 struct g4x_wm_values *wm)
1471{
1472 struct intel_crtc *crtc;
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001473 int num_active_pipes = 0;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001474
1475 wm->cxsr = true;
1476 wm->hpll_en = true;
1477 wm->fbc_en = true;
1478
1479 for_each_intel_crtc(&dev_priv->drm, crtc) {
1480 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1481
1482 if (!crtc->active)
1483 continue;
1484
1485 if (!wm_state->cxsr)
1486 wm->cxsr = false;
1487 if (!wm_state->hpll_en)
1488 wm->hpll_en = false;
1489 if (!wm_state->fbc_en)
1490 wm->fbc_en = false;
1491
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001492 num_active_pipes++;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001493 }
1494
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001495 if (num_active_pipes != 1) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001496 wm->cxsr = false;
1497 wm->hpll_en = false;
1498 wm->fbc_en = false;
1499 }
1500
1501 for_each_intel_crtc(&dev_priv->drm, crtc) {
1502 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1503 enum pipe pipe = crtc->pipe;
1504
1505 wm->pipe[pipe] = wm_state->wm;
1506 if (crtc->active && wm->cxsr)
1507 wm->sr = wm_state->sr;
1508 if (crtc->active && wm->hpll_en)
1509 wm->hpll = wm_state->hpll;
1510 }
1511}
1512
1513static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
1514{
1515 struct g4x_wm_values *old_wm = &dev_priv->wm.g4x;
1516 struct g4x_wm_values new_wm = {};
1517
1518 g4x_merge_wm(dev_priv, &new_wm);
1519
1520 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
1521 return;
1522
1523 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
1524 _intel_set_memory_cxsr(dev_priv, false);
1525
1526 g4x_write_wm_values(dev_priv, &new_wm);
1527
1528 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
1529 _intel_set_memory_cxsr(dev_priv, true);
1530
1531 *old_wm = new_wm;
1532}
1533
1534static void g4x_initial_watermarks(struct intel_atomic_state *state,
1535 struct intel_crtc_state *crtc_state)
1536{
1537 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1538 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1539
1540 mutex_lock(&dev_priv->wm.wm_mutex);
1541 crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
1542 g4x_program_watermarks(dev_priv);
1543 mutex_unlock(&dev_priv->wm.wm_mutex);
1544}
1545
1546static void g4x_optimize_watermarks(struct intel_atomic_state *state,
1547 struct intel_crtc_state *crtc_state)
1548{
1549 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä88016a92019-07-01 19:05:45 +03001550 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001551
1552 if (!crtc_state->wm.need_postvbl_update)
1553 return;
1554
1555 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03001556 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001557 g4x_program_watermarks(dev_priv);
1558 mutex_unlock(&dev_priv->wm.wm_mutex);
1559}
1560
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001561/* latency must be in 0.1us units. */
1562static unsigned int vlv_wm_method2(unsigned int pixel_rate,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001563 unsigned int htotal,
1564 unsigned int width,
Ville Syrjäläac484962016-01-20 21:05:26 +02001565 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001566 unsigned int latency)
1567{
1568 unsigned int ret;
1569
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001570 ret = intel_wm_method2(pixel_rate, htotal,
1571 width, cpp, latency);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001572 ret = DIV_ROUND_UP(ret, 64);
1573
1574 return ret;
1575}
1576
Ville Syrjäläbb726512016-10-31 22:37:24 +02001577static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001578{
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001579 /* all latencies in usec */
1580 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1581
Ville Syrjälä58590c12015-09-08 21:05:12 +03001582 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
1583
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001584 if (IS_CHERRYVIEW(dev_priv)) {
1585 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1586 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001587
1588 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001589 }
1590}
1591
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001592static u16 vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1593 const struct intel_plane_state *plane_state,
1594 int level)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001595{
Ville Syrjäläe339d672016-11-28 19:37:17 +02001596 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001597 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläe339d672016-11-28 19:37:17 +02001598 const struct drm_display_mode *adjusted_mode =
1599 &crtc_state->base.adjusted_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001600 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001601
1602 if (dev_priv->wm.pri_latency[level] == 0)
1603 return USHRT_MAX;
1604
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001605 if (!intel_wm_plane_visible(crtc_state, plane_state))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001606 return 0;
1607
Daniel Vetteref426c12017-01-04 11:41:10 +01001608 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjäläe339d672016-11-28 19:37:17 +02001609 clock = adjusted_mode->crtc_clock;
1610 htotal = adjusted_mode->crtc_htotal;
1611 width = crtc_state->pipe_src_w;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001612
Ville Syrjälä709f3fc2017-03-03 17:19:26 +02001613 if (plane->id == PLANE_CURSOR) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001614 /*
1615 * FIXME the formula gives values that are
1616 * too big for the cursor FIFO, and hence we
1617 * would never be able to use cursors. For
1618 * now just hardcode the watermark.
1619 */
1620 wm = 63;
1621 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +02001622 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001623 dev_priv->wm.pri_latency[level] * 10);
1624 }
1625
Chris Wilson1a1f1282017-11-07 14:03:38 +00001626 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001627}
1628
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001629static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1630{
1631 return (active_planes & (BIT(PLANE_SPRITE0) |
1632 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1633}
1634
Ville Syrjälä5012e602017-03-02 19:14:56 +02001635static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001636{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001637 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001638 const struct g4x_pipe_wm *raw =
Ville Syrjälä5012e602017-03-02 19:14:56 +02001639 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001640 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001641 unsigned int active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001642 int num_active_planes = hweight8(active_planes);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001643 const int fifo_size = 511;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001644 int fifo_extra, fifo_left = fifo_size;
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001645 int sprite0_fifo_extra = 0;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001646 unsigned int total_rate;
1647 enum plane_id plane_id;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001648
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001649 /*
1650 * When enabling sprite0 after sprite1 has already been enabled
1651 * we tend to get an underrun unless sprite0 already has some
1652 * FIFO space allcoated. Hence we always allocate at least one
1653 * cacheline for sprite0 whenever sprite1 is enabled.
1654 *
1655 * All other plane enable sequences appear immune to this problem.
1656 */
1657 if (vlv_need_sprite0_fifo_workaround(active_planes))
1658 sprite0_fifo_extra = 1;
1659
Ville Syrjälä5012e602017-03-02 19:14:56 +02001660 total_rate = raw->plane[PLANE_PRIMARY] +
1661 raw->plane[PLANE_SPRITE0] +
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001662 raw->plane[PLANE_SPRITE1] +
1663 sprite0_fifo_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001664
Ville Syrjälä5012e602017-03-02 19:14:56 +02001665 if (total_rate > fifo_size)
1666 return -EINVAL;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001667
Ville Syrjälä5012e602017-03-02 19:14:56 +02001668 if (total_rate == 0)
1669 total_rate = 1;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001670
Ville Syrjälä5012e602017-03-02 19:14:56 +02001671 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001672 unsigned int rate;
1673
Ville Syrjälä5012e602017-03-02 19:14:56 +02001674 if ((active_planes & BIT(plane_id)) == 0) {
1675 fifo_state->plane[plane_id] = 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001676 continue;
1677 }
1678
Ville Syrjälä5012e602017-03-02 19:14:56 +02001679 rate = raw->plane[plane_id];
1680 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1681 fifo_left -= fifo_state->plane[plane_id];
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001682 }
1683
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001684 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1685 fifo_left -= sprite0_fifo_extra;
1686
Ville Syrjälä5012e602017-03-02 19:14:56 +02001687 fifo_state->plane[PLANE_CURSOR] = 63;
1688
1689 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001690
1691 /* spread the remainder evenly */
Ville Syrjälä5012e602017-03-02 19:14:56 +02001692 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001693 int plane_extra;
1694
1695 if (fifo_left == 0)
1696 break;
1697
Ville Syrjälä5012e602017-03-02 19:14:56 +02001698 if ((active_planes & BIT(plane_id)) == 0)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001699 continue;
1700
1701 plane_extra = min(fifo_extra, fifo_left);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001702 fifo_state->plane[plane_id] += plane_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001703 fifo_left -= plane_extra;
1704 }
1705
Ville Syrjälä5012e602017-03-02 19:14:56 +02001706 WARN_ON(active_planes != 0 && fifo_left != 0);
1707
1708 /* give it all to the first plane if none are active */
1709 if (active_planes == 0) {
1710 WARN_ON(fifo_left != fifo_size);
1711 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1712 }
1713
1714 return 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001715}
1716
Ville Syrjäläff32c542017-03-02 19:14:57 +02001717/* mark all levels starting from 'level' as invalid */
1718static void vlv_invalidate_wms(struct intel_crtc *crtc,
1719 struct vlv_wm_state *wm_state, int level)
1720{
1721 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1722
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001723 for (; level < intel_wm_num_levels(dev_priv); level++) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02001724 enum plane_id plane_id;
1725
1726 for_each_plane_id_on_crtc(crtc, plane_id)
1727 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1728
1729 wm_state->sr[level].cursor = USHRT_MAX;
1730 wm_state->sr[level].plane = USHRT_MAX;
1731 }
1732}
1733
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001734static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1735{
1736 if (wm > fifo_size)
1737 return USHRT_MAX;
1738 else
1739 return fifo_size - wm;
1740}
1741
Ville Syrjäläff32c542017-03-02 19:14:57 +02001742/*
1743 * Starting from 'level' set all higher
1744 * levels to 'value' in the "raw" watermarks.
1745 */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001746static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001747 int level, enum plane_id plane_id, u16 value)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001748{
Ville Syrjäläff32c542017-03-02 19:14:57 +02001749 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001750 int num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001751 bool dirty = false;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001752
Ville Syrjäläff32c542017-03-02 19:14:57 +02001753 for (; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001754 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001755
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001756 dirty |= raw->plane[plane_id] != value;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001757 raw->plane[plane_id] = value;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001758 }
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001759
1760 return dirty;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001761}
1762
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001763static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1764 const struct intel_plane_state *plane_state)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001765{
1766 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1767 enum plane_id plane_id = plane->id;
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001768 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
Ville Syrjäläff32c542017-03-02 19:14:57 +02001769 int level;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001770 bool dirty = false;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001771
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001772 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001773 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1774 goto out;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001775 }
1776
1777 for (level = 0; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001778 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001779 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1780 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1781
Ville Syrjäläff32c542017-03-02 19:14:57 +02001782 if (wm > max_wm)
1783 break;
1784
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001785 dirty |= raw->plane[plane_id] != wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001786 raw->plane[plane_id] = wm;
1787 }
1788
1789 /* mark all higher levels as invalid */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001790 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001791
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001792out:
1793 if (dirty)
Ville Syrjälä57a65282017-04-21 21:14:22 +03001794 DRM_DEBUG_KMS("%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001795 plane->base.name,
1796 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1797 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1798 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
1799
1800 return dirty;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001801}
1802
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001803static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1804 enum plane_id plane_id, int level)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001805{
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001806 const struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02001807 &crtc_state->wm.vlv.raw[level];
1808 const struct vlv_fifo_state *fifo_state =
1809 &crtc_state->wm.vlv.fifo_state;
1810
1811 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1812}
1813
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001814static 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 +02001815{
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001816 return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1817 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1818 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1819 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001820}
1821
1822static int vlv_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001823{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001824 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001825 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001826 struct intel_atomic_state *state =
1827 to_intel_atomic_state(crtc_state->base.state);
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001828 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001829 const struct vlv_fifo_state *fifo_state =
1830 &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001831 int num_active_planes = hweight8(crtc_state->active_planes &
1832 ~BIT(PLANE_CURSOR));
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001833 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->base);
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001834 const struct intel_plane_state *old_plane_state;
1835 const struct intel_plane_state *new_plane_state;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001836 struct intel_plane *plane;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001837 enum plane_id plane_id;
1838 int level, ret, i;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001839 unsigned int dirty = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001840
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001841 for_each_oldnew_intel_plane_in_state(state, plane,
1842 old_plane_state,
1843 new_plane_state, i) {
1844 if (new_plane_state->base.crtc != &crtc->base &&
Ville Syrjäläff32c542017-03-02 19:14:57 +02001845 old_plane_state->base.crtc != &crtc->base)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001846 continue;
1847
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001848 if (vlv_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001849 dirty |= BIT(plane->id);
1850 }
1851
1852 /*
1853 * DSPARB registers may have been reset due to the
1854 * power well being turned off. Make sure we restore
1855 * them to a consistent state even if no primary/sprite
1856 * planes are initially active.
1857 */
1858 if (needs_modeset)
1859 crtc_state->fifo_changed = true;
1860
1861 if (!dirty)
1862 return 0;
1863
1864 /* cursor changes don't warrant a FIFO recompute */
1865 if (dirty & ~BIT(PLANE_CURSOR)) {
1866 const struct intel_crtc_state *old_crtc_state =
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001867 intel_atomic_get_old_crtc_state(state, crtc);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001868 const struct vlv_fifo_state *old_fifo_state =
1869 &old_crtc_state->wm.vlv.fifo_state;
1870
1871 ret = vlv_compute_fifo(crtc_state);
1872 if (ret)
1873 return ret;
1874
1875 if (needs_modeset ||
1876 memcmp(old_fifo_state, fifo_state,
1877 sizeof(*fifo_state)) != 0)
1878 crtc_state->fifo_changed = true;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001879 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001880
Ville Syrjäläff32c542017-03-02 19:14:57 +02001881 /* initially allow all levels */
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001882 wm_state->num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001883 /*
1884 * Note that enabling cxsr with no primary/sprite planes
1885 * enabled can wedge the pipe. Hence we only allow cxsr
1886 * with exactly one enabled primary/sprite plane.
1887 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001888 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001889
Ville Syrjälä5012e602017-03-02 19:14:56 +02001890 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001891 const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Jani Nikula24977872019-09-11 12:26:08 +03001892 const int sr_fifo_size = INTEL_NUM_PIPES(dev_priv) * 512 - 1;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001893
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001894 if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
Ville Syrjäläff32c542017-03-02 19:14:57 +02001895 break;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001896
Ville Syrjäläff32c542017-03-02 19:14:57 +02001897 for_each_plane_id_on_crtc(crtc, plane_id) {
1898 wm_state->wm[level].plane[plane_id] =
1899 vlv_invert_wm_value(raw->plane[plane_id],
1900 fifo_state->plane[plane_id]);
1901 }
1902
1903 wm_state->sr[level].plane =
1904 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
Ville Syrjälä5012e602017-03-02 19:14:56 +02001905 raw->plane[PLANE_SPRITE0],
Ville Syrjäläff32c542017-03-02 19:14:57 +02001906 raw->plane[PLANE_SPRITE1]),
1907 sr_fifo_size);
1908
1909 wm_state->sr[level].cursor =
1910 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1911 63);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001912 }
1913
Ville Syrjäläff32c542017-03-02 19:14:57 +02001914 if (level == 0)
1915 return -EINVAL;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001916
Ville Syrjäläff32c542017-03-02 19:14:57 +02001917 /* limit to only levels we can actually handle */
1918 wm_state->num_levels = level;
1919
1920 /* invalidate the higher levels */
1921 vlv_invalidate_wms(crtc, wm_state, level);
1922
1923 return 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001924}
1925
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001926#define VLV_FIFO(plane, value) \
1927 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1928
Ville Syrjäläff32c542017-03-02 19:14:57 +02001929static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
1930 struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001931{
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001932 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001933 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001934 struct intel_uncore *uncore = &dev_priv->uncore;
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001935 const struct vlv_fifo_state *fifo_state =
1936 &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001937 int sprite0_start, sprite1_start, fifo_size;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001938
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001939 if (!crtc_state->fifo_changed)
1940 return;
1941
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001942 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
1943 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
1944 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001945
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001946 WARN_ON(fifo_state->plane[PLANE_CURSOR] != 63);
1947 WARN_ON(fifo_size != 511);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001948
Ville Syrjäläc137d662017-03-02 19:15:06 +02001949 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
1950
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001951 /*
1952 * uncore.lock serves a double purpose here. It allows us to
1953 * use the less expensive I915_{READ,WRITE}_FW() functions, and
1954 * it protects the DSPARB registers from getting clobbered by
1955 * parallel updates from multiple pipes.
1956 *
1957 * intel_pipe_update_start() has already disabled interrupts
1958 * for us, so a plain spin_lock() is sufficient here.
1959 */
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001960 spin_lock(&uncore->lock);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001961
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001962 switch (crtc->pipe) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001963 u32 dsparb, dsparb2, dsparb3;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001964 case PIPE_A:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001965 dsparb = intel_uncore_read_fw(uncore, DSPARB);
1966 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001967
1968 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1969 VLV_FIFO(SPRITEB, 0xff));
1970 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1971 VLV_FIFO(SPRITEB, sprite1_start));
1972
1973 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1974 VLV_FIFO(SPRITEB_HI, 0x1));
1975 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1976 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1977
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001978 intel_uncore_write_fw(uncore, DSPARB, dsparb);
1979 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001980 break;
1981 case PIPE_B:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001982 dsparb = intel_uncore_read_fw(uncore, DSPARB);
1983 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001984
1985 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1986 VLV_FIFO(SPRITED, 0xff));
1987 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1988 VLV_FIFO(SPRITED, sprite1_start));
1989
1990 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
1991 VLV_FIFO(SPRITED_HI, 0xff));
1992 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
1993 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
1994
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001995 intel_uncore_write_fw(uncore, DSPARB, dsparb);
1996 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001997 break;
1998 case PIPE_C:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001999 dsparb3 = intel_uncore_read_fw(uncore, DSPARB3);
2000 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002001
2002 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
2003 VLV_FIFO(SPRITEF, 0xff));
2004 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
2005 VLV_FIFO(SPRITEF, sprite1_start));
2006
2007 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
2008 VLV_FIFO(SPRITEF_HI, 0xff));
2009 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
2010 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
2011
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002012 intel_uncore_write_fw(uncore, DSPARB3, dsparb3);
2013 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002014 break;
2015 default:
2016 break;
2017 }
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002018
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002019 intel_uncore_posting_read_fw(uncore, DSPARB);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002020
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002021 spin_unlock(&uncore->lock);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002022}
2023
2024#undef VLV_FIFO
2025
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002026static int vlv_compute_intermediate_wm(struct intel_crtc_state *new_crtc_state)
Ville Syrjälä4841da52017-03-02 19:14:59 +02002027{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002028 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002029 struct vlv_wm_state *intermediate = &new_crtc_state->wm.vlv.intermediate;
2030 const struct vlv_wm_state *optimal = &new_crtc_state->wm.vlv.optimal;
2031 struct intel_atomic_state *intel_state =
2032 to_intel_atomic_state(new_crtc_state->base.state);
2033 const struct intel_crtc_state *old_crtc_state =
2034 intel_atomic_get_old_crtc_state(intel_state, crtc);
2035 const struct vlv_wm_state *active = &old_crtc_state->wm.vlv.optimal;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002036 int level;
2037
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002038 if (!new_crtc_state->base.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->base)) {
2039 *intermediate = *optimal;
2040
2041 intermediate->cxsr = false;
2042 goto out;
2043 }
2044
Ville Syrjälä4841da52017-03-02 19:14:59 +02002045 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002046 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002047 !new_crtc_state->disable_cxsr;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002048
2049 for (level = 0; level < intermediate->num_levels; level++) {
2050 enum plane_id plane_id;
2051
2052 for_each_plane_id_on_crtc(crtc, plane_id) {
2053 intermediate->wm[level].plane[plane_id] =
2054 min(optimal->wm[level].plane[plane_id],
2055 active->wm[level].plane[plane_id]);
2056 }
2057
2058 intermediate->sr[level].plane = min(optimal->sr[level].plane,
2059 active->sr[level].plane);
2060 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
2061 active->sr[level].cursor);
2062 }
2063
2064 vlv_invalidate_wms(crtc, intermediate, level);
2065
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002066out:
Ville Syrjälä4841da52017-03-02 19:14:59 +02002067 /*
2068 * If our intermediate WM are identical to the final WM, then we can
2069 * omit the post-vblank programming; only update if it's different.
2070 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002071 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002072 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002073
2074 return 0;
2075}
2076
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002077static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002078 struct vlv_wm_values *wm)
2079{
2080 struct intel_crtc *crtc;
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002081 int num_active_pipes = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002082
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002083 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002084 wm->cxsr = true;
2085
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002086 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002087 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002088
2089 if (!crtc->active)
2090 continue;
2091
2092 if (!wm_state->cxsr)
2093 wm->cxsr = false;
2094
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002095 num_active_pipes++;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002096 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
2097 }
2098
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002099 if (num_active_pipes != 1)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002100 wm->cxsr = false;
2101
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002102 if (num_active_pipes > 1)
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03002103 wm->level = VLV_WM_LEVEL_PM2;
2104
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002105 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002106 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002107 enum pipe pipe = crtc->pipe;
2108
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002109 wm->pipe[pipe] = wm_state->wm[wm->level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02002110 if (crtc->active && wm->cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002111 wm->sr = wm_state->sr[wm->level];
2112
Ville Syrjälä1b313892016-11-28 19:37:08 +02002113 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
2114 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
2115 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
2116 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002117 }
2118}
2119
Ville Syrjäläff32c542017-03-02 19:14:57 +02002120static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002121{
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002122 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
2123 struct vlv_wm_values new_wm = {};
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002124
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002125 vlv_merge_wm(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002126
Ville Syrjäläff32c542017-03-02 19:14:57 +02002127 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002128 return;
2129
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002130 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002131 chv_set_memory_dvfs(dev_priv, false);
2132
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002133 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002134 chv_set_memory_pm5(dev_priv, false);
2135
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002136 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002137 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002138
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002139 vlv_write_wm_values(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002140
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002141 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002142 _intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002143
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002144 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002145 chv_set_memory_pm5(dev_priv, true);
2146
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002147 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002148 chv_set_memory_dvfs(dev_priv, true);
2149
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002150 *old_wm = new_wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03002151}
2152
Ville Syrjäläff32c542017-03-02 19:14:57 +02002153static void vlv_initial_watermarks(struct intel_atomic_state *state,
2154 struct intel_crtc_state *crtc_state)
2155{
2156 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2157 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
2158
2159 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002160 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
2161 vlv_program_watermarks(dev_priv);
2162 mutex_unlock(&dev_priv->wm.wm_mutex);
2163}
2164
2165static void vlv_optimize_watermarks(struct intel_atomic_state *state,
2166 struct intel_crtc_state *crtc_state)
2167{
2168 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä88016a92019-07-01 19:05:45 +03002169 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002170
2171 if (!crtc_state->wm.need_postvbl_update)
2172 return;
2173
2174 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03002175 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02002176 vlv_program_watermarks(dev_priv);
2177 mutex_unlock(&dev_priv->wm.wm_mutex);
2178}
2179
Ville Syrjälä432081b2016-10-31 22:37:03 +02002180static void i965_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002181{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002182 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002183 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002184 int srwm = 1;
2185 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03002186 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002187
2188 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002189 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002190 if (crtc) {
2191 /* self-refresh has much higher latency */
2192 static const int sr_latency_ns = 12000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002193 const struct drm_display_mode *adjusted_mode =
2194 &crtc->config->base.adjusted_mode;
2195 const struct drm_framebuffer *fb =
2196 crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002197 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002198 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002199 int hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002200 int cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002201 int entries;
2202
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002203 entries = intel_wm_method2(clock, htotal,
2204 hdisplay, cpp, sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002205 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
2206 srwm = I965_FIFO_SIZE - entries;
2207 if (srwm < 0)
2208 srwm = 1;
2209 srwm &= 0x1ff;
2210 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
2211 entries, srwm);
2212
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002213 entries = intel_wm_method2(clock, htotal,
2214 crtc->base.cursor->state->crtc_w, 4,
2215 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002216 entries = DIV_ROUND_UP(entries,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002217 i965_cursor_wm_info.cacheline_size) +
2218 i965_cursor_wm_info.guard_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002219
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002220 cursor_sr = i965_cursor_wm_info.fifo_size - entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002221 if (cursor_sr > i965_cursor_wm_info.max_wm)
2222 cursor_sr = i965_cursor_wm_info.max_wm;
2223
2224 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
2225 "cursor %d\n", srwm, cursor_sr);
2226
Imre Deak98584252014-06-13 14:54:20 +03002227 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002228 } else {
Imre Deak98584252014-06-13 14:54:20 +03002229 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002230 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03002231 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002232 }
2233
2234 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2235 srwm);
2236
2237 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002238 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
2239 FW_WM(8, CURSORB) |
2240 FW_WM(8, PLANEB) |
2241 FW_WM(8, PLANEA));
2242 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
2243 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002244 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002245 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03002246
2247 if (cxsr_enabled)
2248 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002249}
2250
Ville Syrjäläf4998962015-03-10 17:02:21 +02002251#undef FW_WM
2252
Ville Syrjälä432081b2016-10-31 22:37:03 +02002253static void i9xx_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002254{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002255 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002256 const struct intel_watermark_params *wm_info;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002257 u32 fwater_lo;
2258 u32 fwater_hi;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002259 int cwm, srwm = 1;
2260 int fifo_size;
2261 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002262 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002263
Ville Syrjäläa9097be2016-10-31 22:37:20 +02002264 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002265 wm_info = &i945_wm_info;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002266 else if (!IS_GEN(dev_priv, 2))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002267 wm_info = &i915_wm_info;
2268 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03002269 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002270
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002271 fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_A);
2272 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_A);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002273 if (intel_crtc_active(crtc)) {
2274 const struct drm_display_mode *adjusted_mode =
2275 &crtc->config->base.adjusted_mode;
2276 const struct drm_framebuffer *fb =
2277 crtc->base.primary->state->fb;
2278 int cpp;
2279
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002280 if (IS_GEN(dev_priv, 2))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002281 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002282 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002283 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002284
Damien Lespiau241bfc32013-09-25 16:45:37 +01002285 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002286 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002287 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002288 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002289 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002290 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002291 if (planea_wm > (long)wm_info->max_wm)
2292 planea_wm = wm_info->max_wm;
2293 }
2294
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002295 if (IS_GEN(dev_priv, 2))
Ville Syrjälä9d539102014-08-15 01:21:53 +03002296 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002297
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002298 fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_B);
2299 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_B);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002300 if (intel_crtc_active(crtc)) {
2301 const struct drm_display_mode *adjusted_mode =
2302 &crtc->config->base.adjusted_mode;
2303 const struct drm_framebuffer *fb =
2304 crtc->base.primary->state->fb;
2305 int cpp;
2306
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002307 if (IS_GEN(dev_priv, 2))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002308 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002309 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002310 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002311
Damien Lespiau241bfc32013-09-25 16:45:37 +01002312 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002313 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002314 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002315 if (enabled == NULL)
2316 enabled = crtc;
2317 else
2318 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002319 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002320 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002321 if (planeb_wm > (long)wm_info->max_wm)
2322 planeb_wm = wm_info->max_wm;
2323 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002324
2325 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
2326
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002327 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07002328 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002329
Ville Syrjäläefc26112016-10-31 22:37:04 +02002330 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002331
2332 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01002333 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002334 enabled = NULL;
2335 }
2336
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002337 /*
2338 * Overlay gets an aggressive default since video jitter is bad.
2339 */
2340 cwm = 2;
2341
2342 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03002343 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002344
2345 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02002346 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002347 /* self-refresh has much higher latency */
2348 static const int sr_latency_ns = 6000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002349 const struct drm_display_mode *adjusted_mode =
2350 &enabled->config->base.adjusted_mode;
2351 const struct drm_framebuffer *fb =
2352 enabled->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002353 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002354 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002355 int hdisplay = enabled->config->pipe_src_w;
2356 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002357 int entries;
2358
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002359 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002360 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002361 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002362 cpp = fb->format->cpp[0];
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002363
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002364 entries = intel_wm_method2(clock, htotal, hdisplay, cpp,
2365 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002366 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
2367 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
2368 srwm = wm_info->fifo_size - entries;
2369 if (srwm < 0)
2370 srwm = 1;
2371
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002372 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002373 I915_WRITE(FW_BLC_SELF,
2374 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03002375 else
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002376 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
2377 }
2378
2379 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
2380 planea_wm, planeb_wm, cwm, srwm);
2381
2382 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2383 fwater_hi = (cwm & 0x1f);
2384
2385 /* Set request length to 8 cachelines per fetch */
2386 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2387 fwater_hi = fwater_hi | (1 << 8);
2388
2389 I915_WRITE(FW_BLC, fwater_lo);
2390 I915_WRITE(FW_BLC2, fwater_hi);
2391
Imre Deak5209b1f2014-07-01 12:36:17 +03002392 if (enabled)
2393 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002394}
2395
Ville Syrjälä432081b2016-10-31 22:37:03 +02002396static void i845_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002397{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002398 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002399 struct intel_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002400 const struct drm_display_mode *adjusted_mode;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002401 u32 fwater_lo;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002402 int planea_wm;
2403
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002404 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002405 if (crtc == NULL)
2406 return;
2407
Ville Syrjäläefc26112016-10-31 22:37:04 +02002408 adjusted_mode = &crtc->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002409 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02002410 &i845_wm_info,
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002411 dev_priv->display.get_fifo_size(dev_priv, PLANE_A),
Chris Wilson5aef6002014-09-03 11:56:07 +01002412 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002413 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
2414 fwater_lo |= (3<<8) | planea_wm;
2415
2416 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
2417
2418 I915_WRITE(FW_BLC, fwater_lo);
2419}
2420
Ville Syrjälä37126462013-08-01 16:18:55 +03002421/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002422static unsigned int ilk_wm_method1(unsigned int pixel_rate,
2423 unsigned int cpp,
2424 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002425{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002426 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002427
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002428 ret = intel_wm_method1(pixel_rate, cpp, latency);
2429 ret = DIV_ROUND_UP(ret, 64) + 2;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002430
2431 return ret;
2432}
2433
Ville Syrjälä37126462013-08-01 16:18:55 +03002434/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002435static unsigned int ilk_wm_method2(unsigned int pixel_rate,
2436 unsigned int htotal,
2437 unsigned int width,
2438 unsigned int cpp,
2439 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002440{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002441 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002442
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002443 ret = intel_wm_method2(pixel_rate, htotal,
2444 width, cpp, latency);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002445 ret = DIV_ROUND_UP(ret, 64) + 2;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002446
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002447 return ret;
2448}
2449
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002450static u32 ilk_wm_fbc(u32 pri_val, u32 horiz_pixels, u8 cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002451{
Matt Roper15126882015-12-03 11:37:40 -08002452 /*
2453 * Neither of these should be possible since this function shouldn't be
2454 * called if the CRTC is off or the plane is invisible. But let's be
2455 * extra paranoid to avoid a potential divide-by-zero if we screw up
2456 * elsewhere in the driver.
2457 */
Ville Syrjäläac484962016-01-20 21:05:26 +02002458 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08002459 return 0;
2460 if (WARN_ON(!horiz_pixels))
2461 return 0;
2462
Ville Syrjäläac484962016-01-20 21:05:26 +02002463 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002464}
2465
Imre Deak820c1982013-12-17 14:46:36 +02002466struct ilk_wm_maximums {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002467 u16 pri;
2468 u16 spr;
2469 u16 cur;
2470 u16 fbc;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002471};
2472
Ville Syrjälä37126462013-08-01 16:18:55 +03002473/*
2474 * For both WM_PIPE and WM_LP.
2475 * mem_value must be in 0.1us units.
2476 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002477static u32 ilk_compute_pri_wm(const struct intel_crtc_state *crtc_state,
2478 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002479 u32 mem_value, bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002480{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002481 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002482 int cpp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002483
Ville Syrjälä03981c62018-11-14 19:34:40 +02002484 if (mem_value == 0)
2485 return U32_MAX;
2486
Maarten Lankhorstec193642019-06-28 10:55:17 +02002487 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002488 return 0;
2489
Maarten Lankhorstec193642019-06-28 10:55:17 +02002490 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002491
Maarten Lankhorstec193642019-06-28 10:55:17 +02002492 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002493
2494 if (!is_lp)
2495 return method1;
2496
Maarten Lankhorstec193642019-06-28 10:55:17 +02002497 method2 = ilk_wm_method2(crtc_state->pixel_rate,
2498 crtc_state->base.adjusted_mode.crtc_htotal,
2499 drm_rect_width(&plane_state->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002500 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002501
2502 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002503}
2504
Ville Syrjälä37126462013-08-01 16:18:55 +03002505/*
2506 * For both WM_PIPE and WM_LP.
2507 * mem_value must be in 0.1us units.
2508 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002509static u32 ilk_compute_spr_wm(const struct intel_crtc_state *crtc_state,
2510 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002511 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002512{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002513 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002514 int cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002515
Ville Syrjälä03981c62018-11-14 19:34:40 +02002516 if (mem_value == 0)
2517 return U32_MAX;
2518
Maarten Lankhorstec193642019-06-28 10:55:17 +02002519 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002520 return 0;
2521
Maarten Lankhorstec193642019-06-28 10:55:17 +02002522 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002523
Maarten Lankhorstec193642019-06-28 10:55:17 +02002524 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
2525 method2 = ilk_wm_method2(crtc_state->pixel_rate,
2526 crtc_state->base.adjusted_mode.crtc_htotal,
2527 drm_rect_width(&plane_state->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002528 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002529 return min(method1, method2);
2530}
2531
Ville Syrjälä37126462013-08-01 16:18:55 +03002532/*
2533 * For both WM_PIPE and WM_LP.
2534 * mem_value must be in 0.1us units.
2535 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002536static u32 ilk_compute_cur_wm(const struct intel_crtc_state *crtc_state,
2537 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002538 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002539{
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002540 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002541
Ville Syrjälä03981c62018-11-14 19:34:40 +02002542 if (mem_value == 0)
2543 return U32_MAX;
2544
Maarten Lankhorstec193642019-06-28 10:55:17 +02002545 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002546 return 0;
2547
Maarten Lankhorstec193642019-06-28 10:55:17 +02002548 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002549
Maarten Lankhorstec193642019-06-28 10:55:17 +02002550 return ilk_wm_method2(crtc_state->pixel_rate,
2551 crtc_state->base.adjusted_mode.crtc_htotal,
2552 plane_state->base.crtc_w, cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002553}
2554
Paulo Zanonicca32e92013-05-31 11:45:06 -03002555/* Only for WM_LP. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002556static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
2557 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002558 u32 pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002559{
Ville Syrjälä83054942016-11-18 21:53:00 +02002560 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002561
Maarten Lankhorstec193642019-06-28 10:55:17 +02002562 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanonicca32e92013-05-31 11:45:06 -03002563 return 0;
2564
Maarten Lankhorstec193642019-06-28 10:55:17 +02002565 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002566
Maarten Lankhorstec193642019-06-28 10:55:17 +02002567 return ilk_wm_fbc(pri_val, drm_rect_width(&plane_state->base.dst), cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002568}
2569
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002570static unsigned int
2571ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002572{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002573 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002574 return 3072;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002575 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002576 return 768;
2577 else
2578 return 512;
2579}
2580
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002581static unsigned int
2582ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2583 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002584{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002585 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002586 /* BDW primary/sprite plane watermarks */
2587 return level == 0 ? 255 : 2047;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002588 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002589 /* IVB/HSW primary/sprite plane watermarks */
2590 return level == 0 ? 127 : 1023;
2591 else if (!is_sprite)
2592 /* ILK/SNB primary plane watermarks */
2593 return level == 0 ? 127 : 511;
2594 else
2595 /* ILK/SNB sprite plane watermarks */
2596 return level == 0 ? 63 : 255;
2597}
2598
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002599static unsigned int
2600ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002601{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002602 if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002603 return level == 0 ? 63 : 255;
2604 else
2605 return level == 0 ? 31 : 63;
2606}
2607
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002608static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002609{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002610 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002611 return 31;
2612 else
2613 return 15;
2614}
2615
Ville Syrjälä158ae642013-08-07 13:28:19 +03002616/* Calculate the maximum primary/sprite plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002617static unsigned int ilk_plane_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002618 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002619 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002620 enum intel_ddb_partitioning ddb_partitioning,
2621 bool is_sprite)
2622{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002623 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002624
2625 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002626 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002627 return 0;
2628
2629 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002630 if (level == 0 || config->num_pipes_active > 1) {
Jani Nikula24977872019-09-11 12:26:08 +03002631 fifo_size /= INTEL_NUM_PIPES(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002632
2633 /*
2634 * For some reason the non self refresh
2635 * FIFO size is only half of the self
2636 * refresh FIFO size on ILK/SNB.
2637 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002638 if (INTEL_GEN(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002639 fifo_size /= 2;
2640 }
2641
Ville Syrjälä240264f2013-08-07 13:29:12 +03002642 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002643 /* level 0 is always calculated with 1:1 split */
2644 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2645 if (is_sprite)
2646 fifo_size *= 5;
2647 fifo_size /= 6;
2648 } else {
2649 fifo_size /= 2;
2650 }
2651 }
2652
2653 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002654 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002655}
2656
2657/* Calculate the maximum cursor plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002658static unsigned int ilk_cursor_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002659 int level,
2660 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002661{
2662 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002663 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002664 return 64;
2665
2666 /* otherwise just report max that registers can hold */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002667 return ilk_cursor_wm_reg_max(dev_priv, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002668}
2669
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002670static void ilk_compute_wm_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002671 int level,
2672 const struct intel_wm_config *config,
2673 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002674 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002675{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002676 max->pri = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, false);
2677 max->spr = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, true);
2678 max->cur = ilk_cursor_wm_max(dev_priv, level, config);
2679 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002680}
2681
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002682static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002683 int level,
2684 struct ilk_wm_maximums *max)
2685{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002686 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2687 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2688 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2689 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002690}
2691
Ville Syrjäläd9395652013-10-09 19:18:10 +03002692static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002693 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002694 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002695{
2696 bool ret;
2697
2698 /* already determined to be invalid? */
2699 if (!result->enable)
2700 return false;
2701
2702 result->enable = result->pri_val <= max->pri &&
2703 result->spr_val <= max->spr &&
2704 result->cur_val <= max->cur;
2705
2706 ret = result->enable;
2707
2708 /*
2709 * HACK until we can pre-compute everything,
2710 * and thus fail gracefully if LP0 watermarks
2711 * are exceeded...
2712 */
2713 if (level == 0 && !result->enable) {
2714 if (result->pri_val > max->pri)
2715 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2716 level, result->pri_val, max->pri);
2717 if (result->spr_val > max->spr)
2718 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2719 level, result->spr_val, max->spr);
2720 if (result->cur_val > max->cur)
2721 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2722 level, result->cur_val, max->cur);
2723
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002724 result->pri_val = min_t(u32, result->pri_val, max->pri);
2725 result->spr_val = min_t(u32, result->spr_val, max->spr);
2726 result->cur_val = min_t(u32, result->cur_val, max->cur);
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002727 result->enable = true;
2728 }
2729
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002730 return ret;
2731}
2732
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002733static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Matt Roper43d59ed2015-09-24 15:53:07 -07002734 const struct intel_crtc *intel_crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002735 int level,
Maarten Lankhorstec193642019-06-28 10:55:17 +02002736 struct intel_crtc_state *crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02002737 const struct intel_plane_state *pristate,
2738 const struct intel_plane_state *sprstate,
2739 const struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002740 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002741{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002742 u16 pri_latency = dev_priv->wm.pri_latency[level];
2743 u16 spr_latency = dev_priv->wm.spr_latency[level];
2744 u16 cur_latency = dev_priv->wm.cur_latency[level];
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002745
2746 /* WM1+ latency values stored in 0.5us units */
2747 if (level > 0) {
2748 pri_latency *= 5;
2749 spr_latency *= 5;
2750 cur_latency *= 5;
2751 }
2752
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002753 if (pristate) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02002754 result->pri_val = ilk_compute_pri_wm(crtc_state, pristate,
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002755 pri_latency, level);
Maarten Lankhorstec193642019-06-28 10:55:17 +02002756 result->fbc_val = ilk_compute_fbc_wm(crtc_state, pristate, result->pri_val);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002757 }
2758
2759 if (sprstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002760 result->spr_val = ilk_compute_spr_wm(crtc_state, sprstate, spr_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002761
2762 if (curstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002763 result->cur_val = ilk_compute_cur_wm(crtc_state, curstate, cur_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002764
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002765 result->enable = true;
2766}
2767
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002768static u32
Maarten Lankhorstec193642019-06-28 10:55:17 +02002769hsw_compute_linetime_wm(const struct intel_crtc_state *crtc_state)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002770{
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002771 const struct intel_atomic_state *intel_state =
Maarten Lankhorstec193642019-06-28 10:55:17 +02002772 to_intel_atomic_state(crtc_state->base.state);
Matt Roperee91a152015-12-03 11:37:39 -08002773 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorstec193642019-06-28 10:55:17 +02002774 &crtc_state->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002775 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002776
Maarten Lankhorstec193642019-06-28 10:55:17 +02002777 if (!crtc_state->base.active)
Matt Roperee91a152015-12-03 11:37:39 -08002778 return 0;
2779 if (WARN_ON(adjusted_mode->crtc_clock == 0))
2780 return 0;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002781 if (WARN_ON(intel_state->cdclk.logical.cdclk == 0))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002782 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002783
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002784 /* The WM are computed with base on how long it takes to fill a single
2785 * row at the given clock rate, multiplied by 8.
2786 * */
Ville Syrjälä124abe02015-09-08 13:40:45 +03002787 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2788 adjusted_mode->crtc_clock);
2789 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002790 intel_state->cdclk.logical.cdclk);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002791
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002792 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2793 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002794}
2795
Ville Syrjäläbb726512016-10-31 22:37:24 +02002796static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002797 u16 wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002798{
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002799 struct intel_uncore *uncore = &dev_priv->uncore;
2800
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002801 if (INTEL_GEN(dev_priv) >= 9) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002802 u32 val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002803 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002804 int level, max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002805
2806 /* read the first set of memory latencies[0:3] */
2807 val = 0; /* data0 to be programmed to 0 for first set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002808 ret = sandybridge_pcode_read(dev_priv,
2809 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002810 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002811
2812 if (ret) {
2813 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2814 return;
2815 }
2816
2817 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2818 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2819 GEN9_MEM_LATENCY_LEVEL_MASK;
2820 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2821 GEN9_MEM_LATENCY_LEVEL_MASK;
2822 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2823 GEN9_MEM_LATENCY_LEVEL_MASK;
2824
2825 /* read the second set of memory latencies[4:7] */
2826 val = 1; /* data0 to be programmed to 1 for second set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002827 ret = sandybridge_pcode_read(dev_priv,
2828 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002829 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002830 if (ret) {
2831 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2832 return;
2833 }
2834
2835 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2836 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2837 GEN9_MEM_LATENCY_LEVEL_MASK;
2838 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2839 GEN9_MEM_LATENCY_LEVEL_MASK;
2840 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2841 GEN9_MEM_LATENCY_LEVEL_MASK;
2842
Vandana Kannan367294b2014-11-04 17:06:46 +00002843 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002844 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2845 * need to be disabled. We make sure to sanitize the values out
2846 * of the punit to satisfy this requirement.
2847 */
2848 for (level = 1; level <= max_level; level++) {
2849 if (wm[level] == 0) {
2850 for (i = level + 1; i <= max_level; i++)
2851 wm[i] = 0;
2852 break;
2853 }
2854 }
2855
2856 /*
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002857 * WaWmMemoryReadLatency:skl+,glk
Damien Lespiau6f972352015-02-09 19:33:07 +00002858 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002859 * punit doesn't take into account the read latency so we need
Paulo Zanoni0727e402016-09-22 18:00:30 -03002860 * to add 2us to the various latency levels we retrieve from the
2861 * punit when level 0 response data us 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002862 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002863 if (wm[0] == 0) {
2864 wm[0] += 2;
2865 for (level = 1; level <= max_level; level++) {
2866 if (wm[level] == 0)
2867 break;
Vandana Kannan367294b2014-11-04 17:06:46 +00002868 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002869 }
Paulo Zanoni0727e402016-09-22 18:00:30 -03002870 }
2871
Mahesh Kumar86b59282018-08-31 16:39:42 +05302872 /*
2873 * WA Level-0 adjustment for 16GB DIMMs: SKL+
2874 * If we could not get dimm info enable this WA to prevent from
2875 * any underrun. If not able to get Dimm info assume 16GB dimm
2876 * to avoid any underrun.
2877 */
Ville Syrjälä5d6f36b2018-10-23 21:21:02 +03002878 if (dev_priv->dram_info.is_16gb_dimm)
Mahesh Kumar86b59282018-08-31 16:39:42 +05302879 wm[0] += 1;
2880
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002881 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002882 u64 sskpd = intel_uncore_read64(uncore, MCH_SSKPD);
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002883
2884 wm[0] = (sskpd >> 56) & 0xFF;
2885 if (wm[0] == 0)
2886 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002887 wm[1] = (sskpd >> 4) & 0xFF;
2888 wm[2] = (sskpd >> 12) & 0xFF;
2889 wm[3] = (sskpd >> 20) & 0x1FF;
2890 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002891 } else if (INTEL_GEN(dev_priv) >= 6) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002892 u32 sskpd = intel_uncore_read(uncore, MCH_SSKPD);
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002893
2894 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2895 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2896 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2897 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002898 } else if (INTEL_GEN(dev_priv) >= 5) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002899 u32 mltr = intel_uncore_read(uncore, MLTR_ILK);
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002900
2901 /* ILK primary LP0 latency is 700 ns */
2902 wm[0] = 7;
2903 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2904 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002905 } else {
2906 MISSING_CASE(INTEL_DEVID(dev_priv));
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002907 }
2908}
2909
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002910static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002911 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002912{
2913 /* ILK sprite LP0 latency is 1300 ns */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002914 if (IS_GEN(dev_priv, 5))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002915 wm[0] = 13;
2916}
2917
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002918static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002919 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002920{
2921 /* ILK cursor LP0 latency is 1300 ns */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002922 if (IS_GEN(dev_priv, 5))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002923 wm[0] = 13;
Ville Syrjälä53615a52013-08-01 16:18:50 +03002924}
2925
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002926int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002927{
2928 /* how many WM levels are we expecting */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002929 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002930 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002931 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002932 return 4;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002933 else if (INTEL_GEN(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002934 return 3;
2935 else
2936 return 2;
2937}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002938
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002939static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002940 const char *name,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002941 const u16 wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002942{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002943 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002944
2945 for (level = 0; level <= max_level; level++) {
2946 unsigned int latency = wm[level];
2947
2948 if (latency == 0) {
Chris Wilson86c1c872018-07-26 17:15:27 +01002949 DRM_DEBUG_KMS("%s WM%d latency not provided\n",
2950 name, level);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002951 continue;
2952 }
2953
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002954 /*
2955 * - latencies are in us on gen9.
2956 * - before then, WM1+ latency values are in 0.5us units
2957 */
Paulo Zanonidfc267a2017-08-09 13:52:46 -07002958 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002959 latency *= 10;
2960 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002961 latency *= 5;
2962
2963 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2964 name, level, wm[level],
2965 latency / 10, latency % 10);
2966 }
2967}
2968
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002969static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002970 u16 wm[5], u16 min)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002971{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002972 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002973
2974 if (wm[0] >= min)
2975 return false;
2976
2977 wm[0] = max(wm[0], min);
2978 for (level = 1; level <= max_level; level++)
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002979 wm[level] = max_t(u16, wm[level], DIV_ROUND_UP(min, 5));
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002980
2981 return true;
2982}
2983
Ville Syrjäläbb726512016-10-31 22:37:24 +02002984static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002985{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002986 bool changed;
2987
2988 /*
2989 * The BIOS provided WM memory latency values are often
2990 * inadequate for high resolution displays. Adjust them.
2991 */
2992 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2993 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2994 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2995
2996 if (!changed)
2997 return;
2998
2999 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003000 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3001 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3002 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003003}
3004
Ville Syrjälä03981c62018-11-14 19:34:40 +02003005static void snb_wm_lp3_irq_quirk(struct drm_i915_private *dev_priv)
3006{
3007 /*
3008 * On some SNB machines (Thinkpad X220 Tablet at least)
3009 * LP3 usage can cause vblank interrupts to be lost.
3010 * The DEIIR bit will go high but it looks like the CPU
3011 * never gets interrupted.
3012 *
3013 * It's not clear whether other interrupt source could
3014 * be affected or if this is somehow limited to vblank
3015 * interrupts only. To play it safe we disable LP3
3016 * watermarks entirely.
3017 */
3018 if (dev_priv->wm.pri_latency[3] == 0 &&
3019 dev_priv->wm.spr_latency[3] == 0 &&
3020 dev_priv->wm.cur_latency[3] == 0)
3021 return;
3022
3023 dev_priv->wm.pri_latency[3] = 0;
3024 dev_priv->wm.spr_latency[3] = 0;
3025 dev_priv->wm.cur_latency[3] = 0;
3026
3027 DRM_DEBUG_KMS("LP3 watermarks disabled due to potential for lost interrupts\n");
3028 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3029 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3030 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
3031}
3032
Ville Syrjäläbb726512016-10-31 22:37:24 +02003033static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03003034{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003035 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03003036
3037 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
3038 sizeof(dev_priv->wm.pri_latency));
3039 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
3040 sizeof(dev_priv->wm.pri_latency));
3041
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003042 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003043 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003044
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003045 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3046 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3047 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003048
Lucas De Marchicf819ef2018-12-12 10:10:43 -08003049 if (IS_GEN(dev_priv, 6)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02003050 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä03981c62018-11-14 19:34:40 +02003051 snb_wm_lp3_irq_quirk(dev_priv);
3052 }
Ville Syrjälä53615a52013-08-01 16:18:50 +03003053}
3054
Ville Syrjäläbb726512016-10-31 22:37:24 +02003055static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003056{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003057 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003058 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003059}
3060
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003061static bool ilk_validate_pipe_wm(const struct drm_i915_private *dev_priv,
Matt Ropered4a6a72016-02-23 17:20:13 -08003062 struct intel_pipe_wm *pipe_wm)
3063{
3064 /* LP0 watermark maximums depend on this pipe alone */
3065 const struct intel_wm_config config = {
3066 .num_pipes_active = 1,
3067 .sprites_enabled = pipe_wm->sprites_enabled,
3068 .sprites_scaled = pipe_wm->sprites_scaled,
3069 };
3070 struct ilk_wm_maximums max;
3071
3072 /* LP0 watermarks always use 1/2 DDB partitioning */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003073 ilk_compute_wm_maximums(dev_priv, 0, &config, INTEL_DDB_PART_1_2, &max);
Matt Ropered4a6a72016-02-23 17:20:13 -08003074
3075 /* At least LP0 must be valid */
3076 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
3077 DRM_DEBUG_KMS("LP0 watermark invalid\n");
3078 return false;
3079 }
3080
3081 return true;
3082}
3083
Matt Roper261a27d2015-10-08 15:28:25 -07003084/* Compute new watermarks for the pipe */
Maarten Lankhorstec193642019-06-28 10:55:17 +02003085static int ilk_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Matt Roper261a27d2015-10-08 15:28:25 -07003086{
Maarten Lankhorstec193642019-06-28 10:55:17 +02003087 struct drm_atomic_state *state = crtc_state->base.state;
3088 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07003089 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003090 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003091 const struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003092 struct drm_plane *plane;
3093 const struct drm_plane_state *plane_state;
3094 const struct intel_plane_state *pristate = NULL;
3095 const struct intel_plane_state *sprstate = NULL;
3096 const struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003097 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02003098 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003099
Maarten Lankhorstec193642019-06-28 10:55:17 +02003100 pipe_wm = &crtc_state->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003101
Maarten Lankhorstec193642019-06-28 10:55:17 +02003102 drm_atomic_crtc_state_for_each_plane_state(plane, plane_state, &crtc_state->base) {
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003103 const struct intel_plane_state *ps = to_intel_plane_state(plane_state);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003104
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003105 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003106 pristate = ps;
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003107 else if (plane->type == DRM_PLANE_TYPE_OVERLAY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003108 sprstate = ps;
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003109 else if (plane->type == DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003110 curstate = ps;
Matt Roper43d59ed2015-09-24 15:53:07 -07003111 }
3112
Maarten Lankhorstec193642019-06-28 10:55:17 +02003113 pipe_wm->pipe_enabled = crtc_state->base.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003114 if (sprstate) {
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003115 pipe_wm->sprites_enabled = sprstate->base.visible;
3116 pipe_wm->sprites_scaled = sprstate->base.visible &&
3117 (drm_rect_width(&sprstate->base.dst) != drm_rect_width(&sprstate->base.src) >> 16 ||
3118 drm_rect_height(&sprstate->base.dst) != drm_rect_height(&sprstate->base.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003119 }
3120
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003121 usable_level = max_level;
3122
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003123 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003124 if (INTEL_GEN(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003125 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003126
3127 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08003128 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003129 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003130
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003131 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
Maarten Lankhorstec193642019-06-28 10:55:17 +02003132 ilk_compute_wm_level(dev_priv, intel_crtc, 0, crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003133 pristate, sprstate, curstate, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003134
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003135 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Maarten Lankhorstec193642019-06-28 10:55:17 +02003136 pipe_wm->linetime = hsw_compute_linetime_wm(crtc_state);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003137
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003138 if (!ilk_validate_pipe_wm(dev_priv, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01003139 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003140
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003141 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003142
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003143 for (level = 1; level <= usable_level; level++) {
3144 struct intel_wm_level *wm = &pipe_wm->wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003145
Maarten Lankhorstec193642019-06-28 10:55:17 +02003146 ilk_compute_wm_level(dev_priv, intel_crtc, level, crtc_state,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003147 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003148
3149 /*
3150 * Disable any watermark level that exceeds the
3151 * register maximums since such watermarks are
3152 * always invalid.
3153 */
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003154 if (!ilk_validate_wm_level(level, &max, wm)) {
3155 memset(wm, 0, sizeof(*wm));
3156 break;
3157 }
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003158 }
3159
Matt Roper86c8bbb2015-09-24 15:53:16 -07003160 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003161}
3162
3163/*
Matt Ropered4a6a72016-02-23 17:20:13 -08003164 * Build a set of 'intermediate' watermark values that satisfy both the old
3165 * state and the new state. These can be programmed to the hardware
3166 * immediately.
3167 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003168static int ilk_compute_intermediate_wm(struct intel_crtc_state *newstate)
Matt Ropered4a6a72016-02-23 17:20:13 -08003169{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003170 struct intel_crtc *intel_crtc = to_intel_crtc(newstate->base.crtc);
3171 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Matt Ropere8f1f022016-05-12 07:05:55 -07003172 struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003173 struct intel_atomic_state *intel_state =
3174 to_intel_atomic_state(newstate->base.state);
3175 const struct intel_crtc_state *oldstate =
3176 intel_atomic_get_old_crtc_state(intel_state, intel_crtc);
3177 const struct intel_pipe_wm *b = &oldstate->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003178 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08003179
3180 /*
3181 * Start with the final, target watermarks, then combine with the
3182 * currently active watermarks to get values that are safe both before
3183 * and after the vblank.
3184 */
Matt Ropere8f1f022016-05-12 07:05:55 -07003185 *a = newstate->wm.ilk.optimal;
Ville Syrjäläf255c622018-11-08 17:10:13 +02003186 if (!newstate->base.active || drm_atomic_crtc_needs_modeset(&newstate->base) ||
3187 intel_state->skip_intermediate_wm)
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003188 return 0;
3189
Matt Ropered4a6a72016-02-23 17:20:13 -08003190 a->pipe_enabled |= b->pipe_enabled;
3191 a->sprites_enabled |= b->sprites_enabled;
3192 a->sprites_scaled |= b->sprites_scaled;
3193
3194 for (level = 0; level <= max_level; level++) {
3195 struct intel_wm_level *a_wm = &a->wm[level];
3196 const struct intel_wm_level *b_wm = &b->wm[level];
3197
3198 a_wm->enable &= b_wm->enable;
3199 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
3200 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
3201 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
3202 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
3203 }
3204
3205 /*
3206 * We need to make sure that these merged watermark values are
3207 * actually a valid configuration themselves. If they're not,
3208 * there's no safe way to transition from the old state to
3209 * the new state, so we need to fail the atomic transaction.
3210 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003211 if (!ilk_validate_pipe_wm(dev_priv, a))
Matt Ropered4a6a72016-02-23 17:20:13 -08003212 return -EINVAL;
3213
3214 /*
3215 * If our intermediate WM are identical to the final WM, then we can
3216 * omit the post-vblank programming; only update if it's different.
3217 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02003218 if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) != 0)
3219 newstate->wm.need_postvbl_update = true;
Matt Ropered4a6a72016-02-23 17:20:13 -08003220
3221 return 0;
3222}
3223
3224/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003225 * Merge the watermarks from all active pipes for a specific level.
3226 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003227static void ilk_merge_wm_level(struct drm_i915_private *dev_priv,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003228 int level,
3229 struct intel_wm_level *ret_wm)
3230{
3231 const struct intel_crtc *intel_crtc;
3232
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003233 ret_wm->enable = true;
3234
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003235 for_each_intel_crtc(&dev_priv->drm, intel_crtc) {
Matt Ropered4a6a72016-02-23 17:20:13 -08003236 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02003237 const struct intel_wm_level *wm = &active->wm[level];
3238
3239 if (!active->pipe_enabled)
3240 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003241
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003242 /*
3243 * The watermark values may have been used in the past,
3244 * so we must maintain them in the registers for some
3245 * time even if the level is now disabled.
3246 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003247 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003248 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003249
3250 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
3251 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
3252 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
3253 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
3254 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003255}
3256
3257/*
3258 * Merge all low power watermarks for all active pipes.
3259 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003260static void ilk_wm_merge(struct drm_i915_private *dev_priv,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003261 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02003262 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003263 struct intel_pipe_wm *merged)
3264{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003265 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003266 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003267
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003268 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003269 if ((INTEL_GEN(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003270 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03003271 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003272
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003273 /* ILK: FBC WM must be disabled always */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003274 merged->fbc_wm_enabled = INTEL_GEN(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003275
3276 /* merge each WM1+ level */
3277 for (level = 1; level <= max_level; level++) {
3278 struct intel_wm_level *wm = &merged->wm[level];
3279
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003280 ilk_merge_wm_level(dev_priv, level, wm);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003281
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003282 if (level > last_enabled_level)
3283 wm->enable = false;
3284 else if (!ilk_validate_wm_level(level, max, wm))
3285 /* make sure all following levels get disabled */
3286 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003287
3288 /*
3289 * The spec says it is preferred to disable
3290 * FBC WMs instead of disabling a WM level.
3291 */
3292 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003293 if (wm->enable)
3294 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003295 wm->fbc_val = 0;
3296 }
3297 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003298
3299 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
3300 /*
3301 * FIXME this is racy. FBC might get enabled later.
3302 * What we should check here is whether FBC can be
3303 * enabled sometime later.
3304 */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08003305 if (IS_GEN(dev_priv, 5) && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03003306 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003307 for (level = 2; level <= max_level; level++) {
3308 struct intel_wm_level *wm = &merged->wm[level];
3309
3310 wm->enable = false;
3311 }
3312 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003313}
3314
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003315static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
3316{
3317 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
3318 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
3319}
3320
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003321/* The value we need to program into the WM_LPx latency field */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003322static unsigned int ilk_wm_lp_latency(struct drm_i915_private *dev_priv,
3323 int level)
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003324{
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003325 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003326 return 2 * level;
3327 else
3328 return dev_priv->wm.pri_latency[level];
3329}
3330
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003331static void ilk_compute_wm_results(struct drm_i915_private *dev_priv,
Ville Syrjälä0362c782013-10-09 19:17:57 +03003332 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03003333 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02003334 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003335{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003336 struct intel_crtc *intel_crtc;
3337 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003338
Ville Syrjälä0362c782013-10-09 19:17:57 +03003339 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03003340 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003341
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003342 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03003343 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03003344 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003345
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003346 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003347
Ville Syrjälä0362c782013-10-09 19:17:57 +03003348 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03003349
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003350 /*
3351 * Maintain the watermark values even if the level is
3352 * disabled. Doing otherwise could cause underruns.
3353 */
3354 results->wm_lp[wm_lp - 1] =
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003355 (ilk_wm_lp_latency(dev_priv, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07003356 (r->pri_val << WM1_LP_SR_SHIFT) |
3357 r->cur_val;
3358
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003359 if (r->enable)
3360 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
3361
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003362 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07003363 results->wm_lp[wm_lp - 1] |=
3364 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
3365 else
3366 results->wm_lp[wm_lp - 1] |=
3367 r->fbc_val << WM1_LP_FBC_SHIFT;
3368
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003369 /*
3370 * Always set WM1S_LP_EN when spr_val != 0, even if the
3371 * level is disabled. Doing otherwise could cause underruns.
3372 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003373 if (INTEL_GEN(dev_priv) <= 6 && r->spr_val) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003374 WARN_ON(wm_lp != 1);
3375 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
3376 } else
3377 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003378 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003379
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003380 /* LP0 register values */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003381 for_each_intel_crtc(&dev_priv->drm, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003382 enum pipe pipe = intel_crtc->pipe;
Matt Ropered4a6a72016-02-23 17:20:13 -08003383 const struct intel_wm_level *r =
3384 &intel_crtc->wm.active.ilk.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003385
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003386 if (WARN_ON(!r->enable))
3387 continue;
3388
Matt Ropered4a6a72016-02-23 17:20:13 -08003389 results->wm_linetime[pipe] = intel_crtc->wm.active.ilk.linetime;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003390
3391 results->wm_pipe[pipe] =
3392 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
3393 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
3394 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003395 }
3396}
3397
Paulo Zanoni861f3382013-05-31 10:19:21 -03003398/* Find the result with the highest level enabled. Check for enable_fbc_wm in
3399 * case both are at the same level. Prefer r1 in case they're the same. */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003400static struct intel_pipe_wm *
3401ilk_find_best_result(struct drm_i915_private *dev_priv,
3402 struct intel_pipe_wm *r1,
3403 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003404{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003405 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003406 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003407
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003408 for (level = 1; level <= max_level; level++) {
3409 if (r1->wm[level].enable)
3410 level1 = level;
3411 if (r2->wm[level].enable)
3412 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003413 }
3414
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003415 if (level1 == level2) {
3416 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003417 return r2;
3418 else
3419 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003420 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03003421 return r1;
3422 } else {
3423 return r2;
3424 }
3425}
3426
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003427/* dirty bits used to track which watermarks need changes */
3428#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
3429#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
3430#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
3431#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
3432#define WM_DIRTY_FBC (1 << 24)
3433#define WM_DIRTY_DDB (1 << 25)
3434
Damien Lespiau055e3932014-08-18 13:49:10 +01003435static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02003436 const struct ilk_wm_values *old,
3437 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003438{
3439 unsigned int dirty = 0;
3440 enum pipe pipe;
3441 int wm_lp;
3442
Damien Lespiau055e3932014-08-18 13:49:10 +01003443 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003444 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
3445 dirty |= WM_DIRTY_LINETIME(pipe);
3446 /* Must disable LP1+ watermarks too */
3447 dirty |= WM_DIRTY_LP_ALL;
3448 }
3449
3450 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
3451 dirty |= WM_DIRTY_PIPE(pipe);
3452 /* Must disable LP1+ watermarks too */
3453 dirty |= WM_DIRTY_LP_ALL;
3454 }
3455 }
3456
3457 if (old->enable_fbc_wm != new->enable_fbc_wm) {
3458 dirty |= WM_DIRTY_FBC;
3459 /* Must disable LP1+ watermarks too */
3460 dirty |= WM_DIRTY_LP_ALL;
3461 }
3462
3463 if (old->partitioning != new->partitioning) {
3464 dirty |= WM_DIRTY_DDB;
3465 /* Must disable LP1+ watermarks too */
3466 dirty |= WM_DIRTY_LP_ALL;
3467 }
3468
3469 /* LP1+ watermarks already deemed dirty, no need to continue */
3470 if (dirty & WM_DIRTY_LP_ALL)
3471 return dirty;
3472
3473 /* Find the lowest numbered LP1+ watermark in need of an update... */
3474 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3475 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
3476 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
3477 break;
3478 }
3479
3480 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
3481 for (; wm_lp <= 3; wm_lp++)
3482 dirty |= WM_DIRTY_LP(wm_lp);
3483
3484 return dirty;
3485}
3486
Ville Syrjälä8553c182013-12-05 15:51:39 +02003487static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
3488 unsigned int dirty)
3489{
Imre Deak820c1982013-12-17 14:46:36 +02003490 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02003491 bool changed = false;
3492
3493 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
3494 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
3495 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
3496 changed = true;
3497 }
3498 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
3499 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
3500 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
3501 changed = true;
3502 }
3503 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
3504 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
3505 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
3506 changed = true;
3507 }
3508
3509 /*
3510 * Don't touch WM1S_LP_EN here.
3511 * Doing so could cause underruns.
3512 */
3513
3514 return changed;
3515}
3516
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003517/*
3518 * The spec says we shouldn't write when we don't need, because every write
3519 * causes WMs to be re-evaluated, expending some power.
3520 */
Imre Deak820c1982013-12-17 14:46:36 +02003521static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
3522 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003523{
Imre Deak820c1982013-12-17 14:46:36 +02003524 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003525 unsigned int dirty;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003526 u32 val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003527
Damien Lespiau055e3932014-08-18 13:49:10 +01003528 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003529 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003530 return;
3531
Ville Syrjälä8553c182013-12-05 15:51:39 +02003532 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003533
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003534 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003535 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003536 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003537 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003538 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003539 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
3540
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003541 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003542 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003543 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003544 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003545 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003546 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
3547
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003548 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003549 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003550 val = I915_READ(WM_MISC);
3551 if (results->partitioning == INTEL_DDB_PART_1_2)
3552 val &= ~WM_MISC_DATA_PARTITION_5_6;
3553 else
3554 val |= WM_MISC_DATA_PARTITION_5_6;
3555 I915_WRITE(WM_MISC, val);
3556 } else {
3557 val = I915_READ(DISP_ARB_CTL2);
3558 if (results->partitioning == INTEL_DDB_PART_1_2)
3559 val &= ~DISP_DATA_PARTITION_5_6;
3560 else
3561 val |= DISP_DATA_PARTITION_5_6;
3562 I915_WRITE(DISP_ARB_CTL2, val);
3563 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003564 }
3565
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003566 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03003567 val = I915_READ(DISP_ARB_CTL);
3568 if (results->enable_fbc_wm)
3569 val &= ~DISP_FBC_WM_DIS;
3570 else
3571 val |= DISP_FBC_WM_DIS;
3572 I915_WRITE(DISP_ARB_CTL, val);
3573 }
3574
Imre Deak954911e2013-12-17 14:46:34 +02003575 if (dirty & WM_DIRTY_LP(1) &&
3576 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
3577 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
3578
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003579 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003580 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
3581 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
3582 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
3583 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
3584 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003585
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003586 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003587 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003588 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003589 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003590 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003591 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003592
3593 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003594}
3595
Matt Ropered4a6a72016-02-23 17:20:13 -08003596bool ilk_disable_lp_wm(struct drm_device *dev)
Ville Syrjälä8553c182013-12-05 15:51:39 +02003597{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003598 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003599
3600 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3601}
3602
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303603static u8 intel_enabled_dbuf_slices_num(struct drm_i915_private *dev_priv)
3604{
3605 u8 enabled_slices;
3606
3607 /* Slice 1 will always be enabled */
3608 enabled_slices = 1;
3609
3610 /* Gen prior to GEN11 have only one DBuf slice */
3611 if (INTEL_GEN(dev_priv) < 11)
3612 return enabled_slices;
3613
Imre Deak209d7352019-03-07 12:32:35 +02003614 /*
3615 * FIXME: for now we'll only ever use 1 slice; pretend that we have
3616 * only that 1 slice enabled until we have a proper way for on-demand
3617 * toggling of the second slice.
3618 */
3619 if (0 && I915_READ(DBUF_CTL_S2) & DBUF_POWER_STATE)
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303620 enabled_slices++;
3621
3622 return enabled_slices;
3623}
3624
Matt Roper024c9042015-09-24 15:53:11 -07003625/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003626 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3627 * so assume we'll always need it in order to avoid underruns.
3628 */
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003629static bool skl_needs_memory_bw_wa(struct drm_i915_private *dev_priv)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003630{
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003631 return IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003632}
3633
Paulo Zanoni56feca92016-09-22 18:00:28 -03003634static bool
3635intel_has_sagv(struct drm_i915_private *dev_priv)
3636{
Lucas De Marchi8ffa4392019-09-04 14:34:18 -07003637 /* HACK! */
3638 if (IS_GEN(dev_priv, 12))
3639 return false;
3640
Rodrigo Vivi1ca2b062018-10-26 13:03:17 -07003641 return (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) &&
3642 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED;
Paulo Zanoni56feca92016-09-22 18:00:28 -03003643}
3644
Lyude656d1b82016-08-17 15:55:54 -04003645/*
3646 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3647 * depending on power and performance requirements. The display engine access
3648 * to system memory is blocked during the adjustment time. Because of the
3649 * blocking time, having this enabled can cause full system hangs and/or pipe
3650 * underruns if we don't meet all of the following requirements:
3651 *
3652 * - <= 1 pipe enabled
3653 * - All planes can enable watermarks for latencies >= SAGV engine block time
3654 * - We're not using an interlaced display configuration
3655 */
3656int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003657intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003658{
3659 int ret;
3660
Paulo Zanoni56feca92016-09-22 18:00:28 -03003661 if (!intel_has_sagv(dev_priv))
3662 return 0;
3663
3664 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003665 return 0;
3666
Ville Syrjäläff61a972018-12-21 19:14:34 +02003667 DRM_DEBUG_KMS("Enabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003668 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3669 GEN9_SAGV_ENABLE);
3670
Ville Syrjäläff61a972018-12-21 19:14:34 +02003671 /* We don't need to wait for SAGV when enabling */
Lyude656d1b82016-08-17 15:55:54 -04003672
3673 /*
3674 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003675 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003676 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003677 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003678 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003679 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003680 return 0;
3681 } else if (ret < 0) {
Ville Syrjäläff61a972018-12-21 19:14:34 +02003682 DRM_ERROR("Failed to enable SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003683 return ret;
3684 }
3685
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003686 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003687 return 0;
3688}
3689
Lyude656d1b82016-08-17 15:55:54 -04003690int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003691intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003692{
Imre Deakb3b8e992016-12-05 18:27:38 +02003693 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003694
Paulo Zanoni56feca92016-09-22 18:00:28 -03003695 if (!intel_has_sagv(dev_priv))
3696 return 0;
3697
3698 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003699 return 0;
3700
Ville Syrjäläff61a972018-12-21 19:14:34 +02003701 DRM_DEBUG_KMS("Disabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003702 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003703 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3704 GEN9_SAGV_DISABLE,
3705 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3706 1);
Lyude656d1b82016-08-17 15:55:54 -04003707 /*
3708 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003709 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003710 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003711 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003712 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003713 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003714 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003715 } else if (ret < 0) {
Ville Syrjäläff61a972018-12-21 19:14:34 +02003716 DRM_ERROR("Failed to disable SAGV (%d)\n", ret);
Imre Deakb3b8e992016-12-05 18:27:38 +02003717 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003718 }
3719
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003720 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003721 return 0;
3722}
3723
Maarten Lankhorst855e0d62019-06-28 10:55:13 +02003724bool intel_can_enable_sagv(struct intel_atomic_state *state)
Lyude656d1b82016-08-17 15:55:54 -04003725{
Maarten Lankhorst855e0d62019-06-28 10:55:13 +02003726 struct drm_device *dev = state->base.dev;
Lyude656d1b82016-08-17 15:55:54 -04003727 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003728 struct intel_crtc *crtc;
3729 struct intel_plane *plane;
Maarten Lankhorstec193642019-06-28 10:55:17 +02003730 struct intel_crtc_state *crtc_state;
Lyude656d1b82016-08-17 15:55:54 -04003731 enum pipe pipe;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003732 int level, latency;
Paulo Zanoni4357ce02018-01-30 11:49:15 -02003733 int sagv_block_time_us;
Lyude656d1b82016-08-17 15:55:54 -04003734
Paulo Zanoni56feca92016-09-22 18:00:28 -03003735 if (!intel_has_sagv(dev_priv))
3736 return false;
3737
Lucas De Marchicf819ef2018-12-12 10:10:43 -08003738 if (IS_GEN(dev_priv, 9))
Paulo Zanoni4357ce02018-01-30 11:49:15 -02003739 sagv_block_time_us = 30;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08003740 else if (IS_GEN(dev_priv, 10))
Paulo Zanoni4357ce02018-01-30 11:49:15 -02003741 sagv_block_time_us = 20;
3742 else
3743 sagv_block_time_us = 10;
3744
Lyude656d1b82016-08-17 15:55:54 -04003745 /*
Lyude656d1b82016-08-17 15:55:54 -04003746 * If there are no active CRTCs, no additional checks need be performed
3747 */
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003748 if (hweight8(state->active_pipes) == 0)
Lyude656d1b82016-08-17 15:55:54 -04003749 return true;
Lucas De Marchida172232019-04-04 16:04:26 -07003750
3751 /*
3752 * SKL+ workaround: bspec recommends we disable SAGV when we have
3753 * more then one pipe enabled
3754 */
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003755 if (hweight8(state->active_pipes) > 1)
Lyude656d1b82016-08-17 15:55:54 -04003756 return false;
3757
3758 /* Since we're now guaranteed to only have one active CRTC... */
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03003759 pipe = ffs(state->active_pipes) - 1;
Ville Syrjälä98187832016-10-31 22:37:10 +02003760 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Maarten Lankhorstec193642019-06-28 10:55:17 +02003761 crtc_state = to_intel_crtc_state(crtc->base.state);
Lyude656d1b82016-08-17 15:55:54 -04003762
Paulo Zanonic89cadd2016-10-10 17:30:59 -03003763 if (crtc->base.state->adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003764 return false;
3765
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003766 for_each_intel_plane_on_crtc(dev, crtc, plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003767 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02003768 &crtc_state->wm.skl.optimal.planes[plane->id];
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003769
Lyude656d1b82016-08-17 15:55:54 -04003770 /* Skip this plane if it's not enabled */
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003771 if (!wm->wm[0].plane_en)
Lyude656d1b82016-08-17 15:55:54 -04003772 continue;
3773
3774 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003775 for (level = ilk_wm_max_level(dev_priv);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003776 !wm->wm[level].plane_en; --level)
Lyude656d1b82016-08-17 15:55:54 -04003777 { }
3778
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003779 latency = dev_priv->wm.skl_latency[level];
3780
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003781 if (skl_needs_memory_bw_wa(dev_priv) &&
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003782 plane->base.state->fb->modifier ==
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003783 I915_FORMAT_MOD_X_TILED)
3784 latency += 15;
3785
Lyude656d1b82016-08-17 15:55:54 -04003786 /*
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003787 * If any of the planes on this pipe don't enable wm levels that
3788 * incur memory latencies higher than sagv_block_time_us we
Ville Syrjäläff61a972018-12-21 19:14:34 +02003789 * can't enable SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003790 */
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003791 if (latency < sagv_block_time_us)
Lyude656d1b82016-08-17 15:55:54 -04003792 return false;
3793 }
3794
3795 return true;
3796}
3797
Mahesh Kumaraaa02372018-07-31 19:54:44 +05303798static u16 intel_get_ddb_size(struct drm_i915_private *dev_priv,
Maarten Lankhorstec193642019-06-28 10:55:17 +02003799 const struct intel_crtc_state *crtc_state,
Maarten Lankhorst24719e92018-10-22 12:20:00 +02003800 const u64 total_data_rate,
Mahesh Kumaraaa02372018-07-31 19:54:44 +05303801 const int num_active,
3802 struct skl_ddb_allocation *ddb)
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303803{
3804 const struct drm_display_mode *adjusted_mode;
3805 u64 total_data_bw;
3806 u16 ddb_size = INTEL_INFO(dev_priv)->ddb_size;
3807
3808 WARN_ON(ddb_size == 0);
3809
3810 if (INTEL_GEN(dev_priv) < 11)
3811 return ddb_size - 4; /* 4 blocks for bypass path allocation */
3812
Maarten Lankhorstec193642019-06-28 10:55:17 +02003813 adjusted_mode = &crtc_state->base.adjusted_mode;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02003814 total_data_bw = total_data_rate * drm_mode_vrefresh(adjusted_mode);
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303815
3816 /*
3817 * 12GB/s is maximum BW supported by single DBuf slice.
Ville Syrjäläad3e7b82019-01-30 17:51:10 +02003818 *
3819 * FIXME dbuf slice code is broken:
3820 * - must wait for planes to stop using the slice before powering it off
3821 * - plane straddling both slices is illegal in multi-pipe scenarios
3822 * - should validate we stay within the hw bandwidth limits
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303823 */
Ville Syrjäläad3e7b82019-01-30 17:51:10 +02003824 if (0 && (num_active > 1 || total_data_bw >= GBps(12))) {
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303825 ddb->enabled_slices = 2;
3826 } else {
3827 ddb->enabled_slices = 1;
3828 ddb_size /= 2;
3829 }
3830
3831 return ddb_size;
3832}
3833
Damien Lespiaub9cec072014-11-04 17:06:43 +00003834static void
Maarten Lankhorstb048a002018-10-18 13:51:30 +02003835skl_ddb_get_pipe_allocation_limits(struct drm_i915_private *dev_priv,
Maarten Lankhorstec193642019-06-28 10:55:17 +02003836 const struct intel_crtc_state *crtc_state,
Maarten Lankhorst24719e92018-10-22 12:20:00 +02003837 const u64 total_data_rate,
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303838 struct skl_ddb_allocation *ddb,
Matt Roperc107acf2016-05-12 07:06:01 -07003839 struct skl_ddb_entry *alloc, /* out */
3840 int *num_active /* out */)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003841{
Maarten Lankhorstec193642019-06-28 10:55:17 +02003842 struct drm_atomic_state *state = crtc_state->base.state;
Matt Roperc107acf2016-05-12 07:06:01 -07003843 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorstec193642019-06-28 10:55:17 +02003844 struct drm_crtc *for_crtc = crtc_state->base.crtc;
3845 const struct intel_crtc *crtc;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303846 u32 pipe_width = 0, total_width = 0, width_before_pipe = 0;
3847 enum pipe for_pipe = to_intel_crtc(for_crtc)->pipe;
3848 u16 ddb_size;
3849 u32 i;
Matt Roperc107acf2016-05-12 07:06:01 -07003850
Maarten Lankhorstec193642019-06-28 10:55:17 +02003851 if (WARN_ON(!state) || !crtc_state->base.active) {
Damien Lespiaub9cec072014-11-04 17:06:43 +00003852 alloc->start = 0;
3853 alloc->end = 0;
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003854 *num_active = hweight8(dev_priv->active_pipes);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003855 return;
3856 }
3857
Matt Ropera6d3460e2016-05-12 07:06:04 -07003858 if (intel_state->active_pipe_changes)
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003859 *num_active = hweight8(intel_state->active_pipes);
Matt Ropera6d3460e2016-05-12 07:06:04 -07003860 else
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003861 *num_active = hweight8(dev_priv->active_pipes);
Matt Ropera6d3460e2016-05-12 07:06:04 -07003862
Maarten Lankhorstec193642019-06-28 10:55:17 +02003863 ddb_size = intel_get_ddb_size(dev_priv, crtc_state, total_data_rate,
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303864 *num_active, ddb);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003865
Matt Roperc107acf2016-05-12 07:06:01 -07003866 /*
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303867 * If the state doesn't change the active CRTC's or there is no
3868 * modeset request, then there's no need to recalculate;
3869 * the existing pipe allocation limits should remain unchanged.
3870 * Note that we're safe from racing commits since any racing commit
3871 * that changes the active CRTC list or do modeset would need to
3872 * grab _all_ crtc locks, including the one we currently hold.
Matt Roperc107acf2016-05-12 07:06:01 -07003873 */
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303874 if (!intel_state->active_pipe_changes && !intel_state->modeset) {
Maarten Lankhorst512b5522016-11-08 13:55:34 +01003875 /*
3876 * alloc may be cleared by clear_intel_crtc_state,
3877 * copy from old state to be sure
3878 */
3879 *alloc = to_intel_crtc_state(for_crtc->state)->wm.skl.ddb;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003880 return;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003881 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07003882
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303883 /*
3884 * Watermark/ddb requirement highly depends upon width of the
3885 * framebuffer, So instead of allocating DDB equally among pipes
3886 * distribute DDB based on resolution/width of the display.
3887 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02003888 for_each_new_intel_crtc_in_state(intel_state, crtc, crtc_state, i) {
3889 const struct drm_display_mode *adjusted_mode =
3890 &crtc_state->base.adjusted_mode;
3891 enum pipe pipe = crtc->pipe;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303892 int hdisplay, vdisplay;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303893
Maarten Lankhorstec193642019-06-28 10:55:17 +02003894 if (!crtc_state->base.enable)
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303895 continue;
3896
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303897 drm_mode_get_hv_timing(adjusted_mode, &hdisplay, &vdisplay);
3898 total_width += hdisplay;
3899
3900 if (pipe < for_pipe)
3901 width_before_pipe += hdisplay;
3902 else if (pipe == for_pipe)
3903 pipe_width = hdisplay;
3904 }
3905
3906 alloc->start = ddb_size * width_before_pipe / total_width;
3907 alloc->end = ddb_size * (width_before_pipe + pipe_width) / total_width;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003908}
3909
Ville Syrjälädf331de2019-03-19 18:03:11 +02003910static int skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
3911 int width, const struct drm_format_info *format,
3912 u64 modifier, unsigned int rotation,
3913 u32 plane_pixel_rate, struct skl_wm_params *wp,
3914 int color_plane);
Maarten Lankhorstec193642019-06-28 10:55:17 +02003915static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Ville Syrjälädf331de2019-03-19 18:03:11 +02003916 int level,
3917 const struct skl_wm_params *wp,
3918 const struct skl_wm_level *result_prev,
3919 struct skl_wm_level *result /* out */);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003920
Ville Syrjälädf331de2019-03-19 18:03:11 +02003921static unsigned int
3922skl_cursor_allocation(const struct intel_crtc_state *crtc_state,
3923 int num_active)
3924{
3925 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
3926 int level, max_level = ilk_wm_max_level(dev_priv);
3927 struct skl_wm_level wm = {};
3928 int ret, min_ddb_alloc = 0;
3929 struct skl_wm_params wp;
3930
3931 ret = skl_compute_wm_params(crtc_state, 256,
3932 drm_format_info(DRM_FORMAT_ARGB8888),
3933 DRM_FORMAT_MOD_LINEAR,
3934 DRM_MODE_ROTATE_0,
3935 crtc_state->pixel_rate, &wp, 0);
3936 WARN_ON(ret);
3937
3938 for (level = 0; level <= max_level; level++) {
Ville Syrjälä6086e472019-03-21 19:51:28 +02003939 skl_compute_plane_wm(crtc_state, level, &wp, &wm, &wm);
Ville Syrjälädf331de2019-03-19 18:03:11 +02003940 if (wm.min_ddb_alloc == U16_MAX)
3941 break;
3942
3943 min_ddb_alloc = wm.min_ddb_alloc;
3944 }
3945
3946 return max(num_active == 1 ? 32 : 8, min_ddb_alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003947}
3948
Mahesh Kumar37cde112018-04-26 19:55:17 +05303949static void skl_ddb_entry_init_from_hw(struct drm_i915_private *dev_priv,
3950 struct skl_ddb_entry *entry, u32 reg)
Damien Lespiaua269c582014-11-04 17:06:49 +00003951{
Mahesh Kumar37cde112018-04-26 19:55:17 +05303952
Ville Syrjäläd7e449a2019-02-05 22:50:56 +02003953 entry->start = reg & DDB_ENTRY_MASK;
3954 entry->end = (reg >> DDB_ENTRY_END_SHIFT) & DDB_ENTRY_MASK;
Mahesh Kumar37cde112018-04-26 19:55:17 +05303955
Damien Lespiau16160e32014-11-04 17:06:53 +00003956 if (entry->end)
3957 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00003958}
3959
Mahesh Kumarddf34312018-04-09 09:11:03 +05303960static void
3961skl_ddb_get_hw_plane_state(struct drm_i915_private *dev_priv,
3962 const enum pipe pipe,
3963 const enum plane_id plane_id,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02003964 struct skl_ddb_entry *ddb_y,
3965 struct skl_ddb_entry *ddb_uv)
Mahesh Kumarddf34312018-04-09 09:11:03 +05303966{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02003967 u32 val, val2;
3968 u32 fourcc = 0;
Mahesh Kumarddf34312018-04-09 09:11:03 +05303969
3970 /* Cursor doesn't support NV12/planar, so no extra calculation needed */
3971 if (plane_id == PLANE_CURSOR) {
3972 val = I915_READ(CUR_BUF_CFG(pipe));
Ville Syrjäläff43bc32018-11-27 18:59:00 +02003973 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
Mahesh Kumarddf34312018-04-09 09:11:03 +05303974 return;
3975 }
3976
3977 val = I915_READ(PLANE_CTL(pipe, plane_id));
3978
3979 /* No DDB allocated for disabled planes */
Ville Syrjäläff43bc32018-11-27 18:59:00 +02003980 if (val & PLANE_CTL_ENABLE)
3981 fourcc = skl_format_to_fourcc(val & PLANE_CTL_FORMAT_MASK,
3982 val & PLANE_CTL_ORDER_RGBX,
3983 val & PLANE_CTL_ALPHA_MASK);
Mahesh Kumarddf34312018-04-09 09:11:03 +05303984
Ville Syrjäläff43bc32018-11-27 18:59:00 +02003985 if (INTEL_GEN(dev_priv) >= 11) {
3986 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
3987 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
3988 } else {
3989 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
Paulo Zanoni12a6c932018-07-31 17:46:14 -07003990 val2 = I915_READ(PLANE_NV12_BUF_CFG(pipe, plane_id));
Mahesh Kumarddf34312018-04-09 09:11:03 +05303991
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03003992 if (fourcc &&
3993 drm_format_info_is_yuv_semiplanar(drm_format_info(fourcc)))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02003994 swap(val, val2);
3995
3996 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
3997 skl_ddb_entry_init_from_hw(dev_priv, ddb_uv, val2);
Mahesh Kumarddf34312018-04-09 09:11:03 +05303998 }
3999}
4000
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004001void skl_pipe_ddb_get_hw_state(struct intel_crtc *crtc,
4002 struct skl_ddb_entry *ddb_y,
4003 struct skl_ddb_entry *ddb_uv)
4004{
4005 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4006 enum intel_display_power_domain power_domain;
4007 enum pipe pipe = crtc->pipe;
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004008 intel_wakeref_t wakeref;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004009 enum plane_id plane_id;
4010
4011 power_domain = POWER_DOMAIN_PIPE(pipe);
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004012 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
4013 if (!wakeref)
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004014 return;
4015
4016 for_each_plane_id_on_crtc(crtc, plane_id)
4017 skl_ddb_get_hw_plane_state(dev_priv, pipe,
4018 plane_id,
4019 &ddb_y[plane_id],
4020 &ddb_uv[plane_id]);
4021
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004022 intel_display_power_put(dev_priv, power_domain, wakeref);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004023}
4024
Damien Lespiau08db6652014-11-04 17:06:52 +00004025void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
4026 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00004027{
Mahesh Kumar74bd8002018-04-26 19:55:15 +05304028 ddb->enabled_slices = intel_enabled_dbuf_slices_num(dev_priv);
Damien Lespiaua269c582014-11-04 17:06:49 +00004029}
4030
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004031/*
4032 * Determines the downscale amount of a plane for the purposes of watermark calculations.
4033 * The bspec defines downscale amount as:
4034 *
4035 * """
4036 * Horizontal down scale amount = maximum[1, Horizontal source size /
4037 * Horizontal destination size]
4038 * Vertical down scale amount = maximum[1, Vertical source size /
4039 * Vertical destination size]
4040 * Total down scale amount = Horizontal down scale amount *
4041 * Vertical down scale amount
4042 * """
4043 *
4044 * Return value is provided in 16.16 fixed point form to retain fractional part.
4045 * Caller should take care of dividing & rounding off the value.
4046 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304047static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02004048skl_plane_downscale_amount(const struct intel_crtc_state *crtc_state,
4049 const struct intel_plane_state *plane_state)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004050{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004051 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004052 u32 src_w, src_h, dst_w, dst_h;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304053 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
4054 uint_fixed_16_16_t downscale_h, downscale_w;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004055
Maarten Lankhorstec193642019-06-28 10:55:17 +02004056 if (WARN_ON(!intel_wm_plane_visible(crtc_state, plane_state)))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304057 return u32_to_fixed16(0);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004058
4059 /* n.b., src is 16.16 fixed point, dst is whole integer */
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004060 if (plane->id == PLANE_CURSOR) {
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004061 /*
4062 * Cursors only support 0/180 degree rotation,
4063 * hence no need to account for rotation here.
4064 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004065 src_w = plane_state->base.src_w >> 16;
4066 src_h = plane_state->base.src_h >> 16;
4067 dst_w = plane_state->base.crtc_w;
4068 dst_h = plane_state->base.crtc_h;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004069 } else {
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004070 /*
4071 * Src coordinates are already rotated by 270 degrees for
4072 * the 90/270 degree plane rotation cases (to match the
4073 * GTT mapping), hence no need to account for rotation here.
4074 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004075 src_w = drm_rect_width(&plane_state->base.src) >> 16;
4076 src_h = drm_rect_height(&plane_state->base.src) >> 16;
4077 dst_w = drm_rect_width(&plane_state->base.dst);
4078 dst_h = drm_rect_height(&plane_state->base.dst);
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004079 }
4080
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304081 fp_w_ratio = div_fixed16(src_w, dst_w);
4082 fp_h_ratio = div_fixed16(src_h, dst_h);
4083 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
4084 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004085
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304086 return mul_fixed16(downscale_w, downscale_h);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004087}
4088
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304089static uint_fixed_16_16_t
4090skl_pipe_downscale_amount(const struct intel_crtc_state *crtc_state)
4091{
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304092 uint_fixed_16_16_t pipe_downscale = u32_to_fixed16(1);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304093
4094 if (!crtc_state->base.enable)
4095 return pipe_downscale;
4096
4097 if (crtc_state->pch_pfit.enabled) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004098 u32 src_w, src_h, dst_w, dst_h;
4099 u32 pfit_size = crtc_state->pch_pfit.size;
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304100 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
4101 uint_fixed_16_16_t downscale_h, downscale_w;
4102
4103 src_w = crtc_state->pipe_src_w;
4104 src_h = crtc_state->pipe_src_h;
4105 dst_w = pfit_size >> 16;
4106 dst_h = pfit_size & 0xffff;
4107
4108 if (!dst_w || !dst_h)
4109 return pipe_downscale;
4110
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304111 fp_w_ratio = div_fixed16(src_w, dst_w);
4112 fp_h_ratio = div_fixed16(src_h, dst_h);
4113 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
4114 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304115
4116 pipe_downscale = mul_fixed16(downscale_w, downscale_h);
4117 }
4118
4119 return pipe_downscale;
4120}
4121
4122int skl_check_pipe_max_pixel_rate(struct intel_crtc *intel_crtc,
Maarten Lankhorstec193642019-06-28 10:55:17 +02004123 struct intel_crtc_state *crtc_state)
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304124{
Rodrigo Vivi43037c82017-10-03 15:31:42 -07004125 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Maarten Lankhorstec193642019-06-28 10:55:17 +02004126 struct drm_atomic_state *state = crtc_state->base.state;
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304127 struct drm_plane *plane;
Maarten Lankhorstec193642019-06-28 10:55:17 +02004128 const struct drm_plane_state *drm_plane_state;
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02004129 int crtc_clock, dotclk;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004130 u32 pipe_max_pixel_rate;
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304131 uint_fixed_16_16_t pipe_downscale;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304132 uint_fixed_16_16_t max_downscale = u32_to_fixed16(1);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304133
Maarten Lankhorstec193642019-06-28 10:55:17 +02004134 if (!crtc_state->base.enable)
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304135 return 0;
4136
Maarten Lankhorstec193642019-06-28 10:55:17 +02004137 drm_atomic_crtc_state_for_each_plane_state(plane, drm_plane_state, &crtc_state->base) {
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304138 uint_fixed_16_16_t plane_downscale;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304139 uint_fixed_16_16_t fp_9_div_8 = div_fixed16(9, 8);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304140 int bpp;
Maarten Lankhorstec193642019-06-28 10:55:17 +02004141 const struct intel_plane_state *plane_state =
4142 to_intel_plane_state(drm_plane_state);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304143
Maarten Lankhorstec193642019-06-28 10:55:17 +02004144 if (!intel_wm_plane_visible(crtc_state, plane_state))
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304145 continue;
4146
Maarten Lankhorstec193642019-06-28 10:55:17 +02004147 if (WARN_ON(!plane_state->base.fb))
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304148 return -EINVAL;
4149
Maarten Lankhorstec193642019-06-28 10:55:17 +02004150 plane_downscale = skl_plane_downscale_amount(crtc_state, plane_state);
4151 bpp = plane_state->base.fb->format->cpp[0] * 8;
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304152 if (bpp == 64)
4153 plane_downscale = mul_fixed16(plane_downscale,
4154 fp_9_div_8);
4155
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304156 max_downscale = max_fixed16(plane_downscale, max_downscale);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304157 }
Maarten Lankhorstec193642019-06-28 10:55:17 +02004158 pipe_downscale = skl_pipe_downscale_amount(crtc_state);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304159
4160 pipe_downscale = mul_fixed16(pipe_downscale, max_downscale);
4161
Maarten Lankhorstec193642019-06-28 10:55:17 +02004162 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02004163 dotclk = to_intel_atomic_state(state)->cdclk.logical.cdclk;
4164
Rodrigo Vivi43037c82017-10-03 15:31:42 -07004165 if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10)
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02004166 dotclk *= 2;
4167
4168 pipe_max_pixel_rate = div_round_up_u32_fixed16(dotclk, pipe_downscale);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304169
4170 if (pipe_max_pixel_rate < crtc_clock) {
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02004171 DRM_DEBUG_KMS("Max supported pixel clock with scaling exceeded\n");
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304172 return -EINVAL;
4173 }
4174
4175 return 0;
4176}
4177
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004178static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004179skl_plane_relative_data_rate(const struct intel_crtc_state *crtc_state,
4180 const struct intel_plane_state *plane_state,
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004181 int color_plane)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004182{
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004183 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
4184 const struct drm_framebuffer *fb = plane_state->base.fb;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004185 u32 data_rate;
4186 u32 width = 0, height = 0;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304187 uint_fixed_16_16_t down_scale_amount;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004188 u64 rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07004189
Maarten Lankhorstec193642019-06-28 10:55:17 +02004190 if (!plane_state->base.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07004191 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02004192
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004193 if (plane->id == PLANE_CURSOR)
Matt Ropera1de91e2016-05-12 07:05:57 -07004194 return 0;
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004195
4196 if (color_plane == 1 &&
4197 !drm_format_info_is_yuv_semiplanar(fb->format))
Matt Ropera1de91e2016-05-12 07:05:57 -07004198 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004199
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004200 /*
4201 * Src coordinates are already rotated by 270 degrees for
4202 * the 90/270 degree plane rotation cases (to match the
4203 * GTT mapping), hence no need to account for rotation here.
4204 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004205 width = drm_rect_width(&plane_state->base.src) >> 16;
4206 height = drm_rect_height(&plane_state->base.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004207
Mahesh Kumarb879d582018-04-09 09:11:01 +05304208 /* UV plane does 1/2 pixel sub-sampling */
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004209 if (color_plane == 1) {
Mahesh Kumarb879d582018-04-09 09:11:01 +05304210 width /= 2;
4211 height /= 2;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004212 }
4213
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004214 data_rate = width * height;
Mahesh Kumarb879d582018-04-09 09:11:01 +05304215
Maarten Lankhorstec193642019-06-28 10:55:17 +02004216 down_scale_amount = skl_plane_downscale_amount(crtc_state, plane_state);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004217
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004218 rate = mul_round_up_u32_fixed16(data_rate, down_scale_amount);
4219
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004220 rate *= fb->format->cpp[color_plane];
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004221 return rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004222}
4223
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004224static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004225skl_get_total_relative_data_rate(struct intel_crtc_state *crtc_state,
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004226 u64 *plane_data_rate,
4227 u64 *uv_plane_data_rate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004228{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004229 struct drm_atomic_state *state = crtc_state->base.state;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02004230 struct drm_plane *plane;
Maarten Lankhorstec193642019-06-28 10:55:17 +02004231 const struct drm_plane_state *drm_plane_state;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004232 u64 total_data_rate = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07004233
4234 if (WARN_ON(!state))
4235 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004236
Matt Ropera1de91e2016-05-12 07:05:57 -07004237 /* Calculate and cache data rate for each plane */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004238 drm_atomic_crtc_state_for_each_plane_state(plane, drm_plane_state, &crtc_state->base) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004239 enum plane_id plane_id = to_intel_plane(plane)->id;
Maarten Lankhorstec193642019-06-28 10:55:17 +02004240 const struct intel_plane_state *plane_state =
4241 to_intel_plane_state(drm_plane_state);
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004242 u64 rate;
Matt Roper024c9042015-09-24 15:53:11 -07004243
Mahesh Kumarb879d582018-04-09 09:11:01 +05304244 /* packed/y */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004245 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004246 plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004247 total_data_rate += rate;
Matt Roper9c74d822016-05-12 07:05:58 -07004248
Mahesh Kumarb879d582018-04-09 09:11:01 +05304249 /* uv-plane */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004250 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 1);
Mahesh Kumarb879d582018-04-09 09:11:01 +05304251 uv_plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004252 total_data_rate += rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004253 }
4254
4255 return total_data_rate;
4256}
4257
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004258static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004259icl_get_total_relative_data_rate(struct intel_crtc_state *crtc_state,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004260 u64 *plane_data_rate)
4261{
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004262 struct drm_plane *plane;
Maarten Lankhorstec193642019-06-28 10:55:17 +02004263 const struct drm_plane_state *drm_plane_state;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004264 u64 total_data_rate = 0;
4265
Maarten Lankhorstec193642019-06-28 10:55:17 +02004266 if (WARN_ON(!crtc_state->base.state))
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004267 return 0;
4268
4269 /* Calculate and cache data rate for each plane */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004270 drm_atomic_crtc_state_for_each_plane_state(plane, drm_plane_state, &crtc_state->base) {
4271 const struct intel_plane_state *plane_state =
4272 to_intel_plane_state(drm_plane_state);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004273 enum plane_id plane_id = to_intel_plane(plane)->id;
4274 u64 rate;
4275
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004276 if (!plane_state->planar_linked_plane) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02004277 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004278 plane_data_rate[plane_id] = rate;
4279 total_data_rate += rate;
4280 } else {
4281 enum plane_id y_plane_id;
4282
4283 /*
4284 * The slave plane might not iterate in
4285 * drm_atomic_crtc_state_for_each_plane_state(),
4286 * and needs the master plane state which may be
4287 * NULL if we try get_new_plane_state(), so we
4288 * always calculate from the master.
4289 */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004290 if (plane_state->planar_slave)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004291 continue;
4292
4293 /* Y plane rate is calculated on the slave */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004294 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004295 y_plane_id = plane_state->planar_linked_plane->id;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004296 plane_data_rate[y_plane_id] = rate;
4297 total_data_rate += rate;
4298
Maarten Lankhorstec193642019-06-28 10:55:17 +02004299 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004300 plane_data_rate[plane_id] = rate;
4301 total_data_rate += rate;
4302 }
4303 }
4304
4305 return total_data_rate;
4306}
4307
Matt Roperc107acf2016-05-12 07:06:01 -07004308static int
Maarten Lankhorstec193642019-06-28 10:55:17 +02004309skl_allocate_pipe_ddb(struct intel_crtc_state *crtc_state,
Damien Lespiaub9cec072014-11-04 17:06:43 +00004310 struct skl_ddb_allocation *ddb /* out */)
4311{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004312 struct drm_atomic_state *state = crtc_state->base.state;
4313 struct drm_crtc *crtc = crtc_state->base.crtc;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004314 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004315 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstec193642019-06-28 10:55:17 +02004316 struct skl_ddb_entry *alloc = &crtc_state->wm.skl.ddb;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004317 u16 alloc_size, start = 0;
4318 u16 total[I915_MAX_PLANES] = {};
4319 u16 uv_total[I915_MAX_PLANES] = {};
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004320 u64 total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004321 enum plane_id plane_id;
Matt Roperc107acf2016-05-12 07:06:01 -07004322 int num_active;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004323 u64 plane_data_rate[I915_MAX_PLANES] = {};
4324 u64 uv_plane_data_rate[I915_MAX_PLANES] = {};
Ville Syrjälä0aded172019-02-05 17:50:53 +02004325 u32 blocks;
Matt Roperd8e87492018-12-11 09:31:07 -08004326 int level;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004327
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004328 /* Clear the partitioning for disabled planes. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004329 memset(crtc_state->wm.skl.plane_ddb_y, 0, sizeof(crtc_state->wm.skl.plane_ddb_y));
4330 memset(crtc_state->wm.skl.plane_ddb_uv, 0, sizeof(crtc_state->wm.skl.plane_ddb_uv));
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004331
Matt Ropera6d3460e2016-05-12 07:06:04 -07004332 if (WARN_ON(!state))
4333 return 0;
4334
Maarten Lankhorstec193642019-06-28 10:55:17 +02004335 if (!crtc_state->base.active) {
Lyudece0ba282016-09-15 10:46:35 -04004336 alloc->start = alloc->end = 0;
Matt Roperc107acf2016-05-12 07:06:01 -07004337 return 0;
4338 }
4339
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004340 if (INTEL_GEN(dev_priv) >= 11)
4341 total_data_rate =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004342 icl_get_total_relative_data_rate(crtc_state,
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004343 plane_data_rate);
4344 else
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004345 total_data_rate =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004346 skl_get_total_relative_data_rate(crtc_state,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004347 plane_data_rate,
4348 uv_plane_data_rate);
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004349
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004350
Maarten Lankhorstec193642019-06-28 10:55:17 +02004351 skl_ddb_get_pipe_allocation_limits(dev_priv, crtc_state, total_data_rate,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004352 ddb, alloc, &num_active);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00004353 alloc_size = skl_ddb_entry_size(alloc);
Kumar, Mahesh336031e2017-05-17 17:28:25 +05304354 if (alloc_size == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004355 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004356
Matt Roperd8e87492018-12-11 09:31:07 -08004357 /* Allocate fixed number of blocks for cursor. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004358 total[PLANE_CURSOR] = skl_cursor_allocation(crtc_state, num_active);
Matt Roperd8e87492018-12-11 09:31:07 -08004359 alloc_size -= total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02004360 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].start =
Matt Roperd8e87492018-12-11 09:31:07 -08004361 alloc->end - total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02004362 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].end = alloc->end;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004363
Matt Ropera1de91e2016-05-12 07:05:57 -07004364 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004365 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004366
Matt Roperd8e87492018-12-11 09:31:07 -08004367 /*
4368 * Find the highest watermark level for which we can satisfy the block
4369 * requirement of active planes.
4370 */
4371 for (level = ilk_wm_max_level(dev_priv); level >= 0; level--) {
Matt Roper25db2ea2018-12-12 11:17:20 -08004372 blocks = 0;
Matt Roperd8e87492018-12-11 09:31:07 -08004373 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004374 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004375 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä10a7e072019-03-12 22:58:40 +02004376
4377 if (plane_id == PLANE_CURSOR) {
4378 if (WARN_ON(wm->wm[level].min_ddb_alloc >
4379 total[PLANE_CURSOR])) {
4380 blocks = U32_MAX;
4381 break;
4382 }
4383 continue;
4384 }
4385
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004386 blocks += wm->wm[level].min_ddb_alloc;
4387 blocks += wm->uv_wm[level].min_ddb_alloc;
Matt Roperd8e87492018-12-11 09:31:07 -08004388 }
4389
Ville Syrjälä3cf963c2019-03-12 22:58:36 +02004390 if (blocks <= alloc_size) {
Matt Roperd8e87492018-12-11 09:31:07 -08004391 alloc_size -= blocks;
4392 break;
4393 }
4394 }
4395
4396 if (level < 0) {
4397 DRM_DEBUG_KMS("Requested display configuration exceeds system DDB limitations");
4398 DRM_DEBUG_KMS("minimum required %d/%d\n", blocks,
4399 alloc_size);
4400 return -EINVAL;
4401 }
4402
4403 /*
4404 * Grant each plane the blocks it requires at the highest achievable
4405 * watermark level, plus an extra share of the leftover blocks
4406 * proportional to its relative data rate.
4407 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004408 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004409 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004410 &crtc_state->wm.skl.optimal.planes[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08004411 u64 rate;
4412 u16 extra;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004413
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004414 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004415 continue;
4416
Damien Lespiaub9cec072014-11-04 17:06:43 +00004417 /*
Matt Roperd8e87492018-12-11 09:31:07 -08004418 * We've accounted for all active planes; remaining planes are
4419 * all disabled.
Damien Lespiaub9cec072014-11-04 17:06:43 +00004420 */
Matt Roperd8e87492018-12-11 09:31:07 -08004421 if (total_data_rate == 0)
4422 break;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004423
Matt Roperd8e87492018-12-11 09:31:07 -08004424 rate = plane_data_rate[plane_id];
4425 extra = min_t(u16, alloc_size,
4426 DIV64_U64_ROUND_UP(alloc_size * rate,
4427 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004428 total[plane_id] = wm->wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08004429 alloc_size -= extra;
4430 total_data_rate -= rate;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004431
Matt Roperd8e87492018-12-11 09:31:07 -08004432 if (total_data_rate == 0)
4433 break;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004434
Matt Roperd8e87492018-12-11 09:31:07 -08004435 rate = uv_plane_data_rate[plane_id];
4436 extra = min_t(u16, alloc_size,
4437 DIV64_U64_ROUND_UP(alloc_size * rate,
4438 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004439 uv_total[plane_id] = wm->uv_wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08004440 alloc_size -= extra;
4441 total_data_rate -= rate;
4442 }
4443 WARN_ON(alloc_size != 0 || total_data_rate != 0);
4444
4445 /* Set the actual DDB start/end points for each plane */
4446 start = alloc->start;
4447 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004448 struct skl_ddb_entry *plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004449 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004450 struct skl_ddb_entry *uv_plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004451 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08004452
4453 if (plane_id == PLANE_CURSOR)
4454 continue;
4455
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004456 /* Gen11+ uses a separate plane for UV watermarks */
Matt Roperd8e87492018-12-11 09:31:07 -08004457 WARN_ON(INTEL_GEN(dev_priv) >= 11 && uv_total[plane_id]);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004458
Matt Roperd8e87492018-12-11 09:31:07 -08004459 /* Leave disabled planes at (0,0) */
4460 if (total[plane_id]) {
4461 plane_alloc->start = start;
4462 start += total[plane_id];
4463 plane_alloc->end = start;
Matt Roperc107acf2016-05-12 07:06:01 -07004464 }
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004465
Matt Roperd8e87492018-12-11 09:31:07 -08004466 if (uv_total[plane_id]) {
4467 uv_plane_alloc->start = start;
4468 start += uv_total[plane_id];
4469 uv_plane_alloc->end = start;
4470 }
4471 }
4472
4473 /*
4474 * When we calculated watermark values we didn't know how high
4475 * of a level we'd actually be able to hit, so we just marked
4476 * all levels as "enabled." Go back now and disable the ones
4477 * that aren't actually possible.
4478 */
4479 for (level++; level <= ilk_wm_max_level(dev_priv); level++) {
4480 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004481 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004482 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjäläa301cb02019-03-12 22:58:41 +02004483
4484 /*
4485 * We only disable the watermarks for each plane if
4486 * they exceed the ddb allocation of said plane. This
4487 * is done so that we don't end up touching cursor
4488 * watermarks needlessly when some other plane reduces
4489 * our max possible watermark level.
4490 *
4491 * Bspec has this to say about the PLANE_WM enable bit:
4492 * "All the watermarks at this level for all enabled
4493 * planes must be enabled before the level will be used."
4494 * So this is actually safe to do.
4495 */
4496 if (wm->wm[level].min_ddb_alloc > total[plane_id] ||
4497 wm->uv_wm[level].min_ddb_alloc > uv_total[plane_id])
4498 memset(&wm->wm[level], 0, sizeof(wm->wm[level]));
Ville Syrjälä290248c2019-02-13 18:54:24 +02004499
Ville Syrjäläc384afe2019-02-28 19:36:39 +02004500 /*
Bob Paauwe39564ae2019-04-12 11:09:20 -07004501 * Wa_1408961008:icl, ehl
Ville Syrjäläc384afe2019-02-28 19:36:39 +02004502 * Underruns with WM1+ disabled
4503 */
Bob Paauwe39564ae2019-04-12 11:09:20 -07004504 if (IS_GEN(dev_priv, 11) &&
Ville Syrjälä290248c2019-02-13 18:54:24 +02004505 level == 1 && wm->wm[0].plane_en) {
4506 wm->wm[level].plane_res_b = wm->wm[0].plane_res_b;
Ville Syrjäläc384afe2019-02-28 19:36:39 +02004507 wm->wm[level].plane_res_l = wm->wm[0].plane_res_l;
4508 wm->wm[level].ignore_lines = wm->wm[0].ignore_lines;
Ville Syrjälä290248c2019-02-13 18:54:24 +02004509 }
Matt Roperd8e87492018-12-11 09:31:07 -08004510 }
4511 }
4512
4513 /*
4514 * Go back and disable the transition watermark if it turns out we
4515 * don't have enough DDB blocks for it.
4516 */
4517 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004518 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004519 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004520
Ville Syrjäläb19c9bc2018-12-21 19:14:31 +02004521 if (wm->trans_wm.plane_res_b >= total[plane_id])
Matt Roperd8e87492018-12-11 09:31:07 -08004522 memset(&wm->trans_wm, 0, sizeof(wm->trans_wm));
Damien Lespiaub9cec072014-11-04 17:06:43 +00004523 }
4524
Matt Roperc107acf2016-05-12 07:06:01 -07004525 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004526}
4527
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004528/*
4529 * 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 +02004530 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004531 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
4532 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
4533*/
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07004534static uint_fixed_16_16_t
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004535skl_wm_method1(const struct drm_i915_private *dev_priv, u32 pixel_rate,
4536 u8 cpp, u32 latency, u32 dbuf_block_size)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004537{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004538 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304539 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004540
4541 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304542 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004543
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304544 wm_intermediate_val = latency * pixel_rate * cpp;
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004545 ret = div_fixed16(wm_intermediate_val, 1000 * dbuf_block_size);
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07004546
4547 if (INTEL_GEN(dev_priv) >= 10)
4548 ret = add_fixed16_u32(ret, 1);
4549
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004550 return ret;
4551}
4552
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004553static uint_fixed_16_16_t
4554skl_wm_method2(u32 pixel_rate, u32 pipe_htotal, u32 latency,
4555 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004556{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004557 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304558 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004559
4560 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304561 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004562
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004563 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304564 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
4565 pipe_htotal * 1000);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304566 ret = mul_u32_fixed16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004567 return ret;
4568}
4569
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304570static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02004571intel_get_linetime_us(const struct intel_crtc_state *crtc_state)
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304572{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004573 u32 pixel_rate;
4574 u32 crtc_htotal;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304575 uint_fixed_16_16_t linetime_us;
4576
Maarten Lankhorstec193642019-06-28 10:55:17 +02004577 if (!crtc_state->base.active)
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304578 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304579
Maarten Lankhorstec193642019-06-28 10:55:17 +02004580 pixel_rate = crtc_state->pixel_rate;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304581
4582 if (WARN_ON(pixel_rate == 0))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304583 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304584
Maarten Lankhorstec193642019-06-28 10:55:17 +02004585 crtc_htotal = crtc_state->base.adjusted_mode.crtc_htotal;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304586 linetime_us = div_fixed16(crtc_htotal * 1000, pixel_rate);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304587
4588 return linetime_us;
4589}
4590
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004591static u32
Maarten Lankhorstec193642019-06-28 10:55:17 +02004592skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *crtc_state,
4593 const struct intel_plane_state *plane_state)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004594{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004595 u64 adjusted_pixel_rate;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304596 uint_fixed_16_16_t downscale_amount;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004597
4598 /* Shouldn't reach here on disabled planes... */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004599 if (WARN_ON(!intel_wm_plane_visible(crtc_state, plane_state)))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004600 return 0;
4601
4602 /*
4603 * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
4604 * with additional adjustments for plane-specific scaling.
4605 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004606 adjusted_pixel_rate = crtc_state->pixel_rate;
4607 downscale_amount = skl_plane_downscale_amount(crtc_state, plane_state);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004608
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304609 return mul_round_up_u32_fixed16(adjusted_pixel_rate,
4610 downscale_amount);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004611}
4612
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304613static int
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004614skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
4615 int width, const struct drm_format_info *format,
4616 u64 modifier, unsigned int rotation,
4617 u32 plane_pixel_rate, struct skl_wm_params *wp,
4618 int color_plane)
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304619{
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004620 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
4621 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004622 u32 interm_pbpl;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304623
Juha-Pekka Heikkiladf7d4152019-03-04 17:26:31 +05304624 /* only planar format has two planes */
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004625 if (color_plane == 1 && !drm_format_info_is_yuv_semiplanar(format)) {
Juha-Pekka Heikkiladf7d4152019-03-04 17:26:31 +05304626 DRM_DEBUG_KMS("Non planar format have single plane\n");
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05304627 return -EINVAL;
4628 }
4629
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004630 wp->y_tiled = modifier == I915_FORMAT_MOD_Y_TILED ||
4631 modifier == I915_FORMAT_MOD_Yf_TILED ||
4632 modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
4633 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
4634 wp->x_tiled = modifier == I915_FORMAT_MOD_X_TILED;
4635 wp->rc_surface = modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
4636 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004637 wp->is_planar = drm_format_info_is_yuv_semiplanar(format);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304638
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004639 wp->width = width;
Ville Syrjälä45bee432018-11-14 23:07:28 +02004640 if (color_plane == 1 && wp->is_planar)
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05304641 wp->width /= 2;
4642
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004643 wp->cpp = format->cpp[color_plane];
4644 wp->plane_pixel_rate = plane_pixel_rate;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304645
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004646 if (INTEL_GEN(dev_priv) >= 11 &&
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004647 modifier == I915_FORMAT_MOD_Yf_TILED && wp->cpp == 1)
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004648 wp->dbuf_block_size = 256;
4649 else
4650 wp->dbuf_block_size = 512;
4651
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004652 if (drm_rotation_90_or_270(rotation)) {
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304653 switch (wp->cpp) {
4654 case 1:
4655 wp->y_min_scanlines = 16;
4656 break;
4657 case 2:
4658 wp->y_min_scanlines = 8;
4659 break;
4660 case 4:
4661 wp->y_min_scanlines = 4;
4662 break;
4663 default:
4664 MISSING_CASE(wp->cpp);
4665 return -EINVAL;
4666 }
4667 } else {
4668 wp->y_min_scanlines = 4;
4669 }
4670
Ville Syrjälä60e983f2018-12-21 19:14:33 +02004671 if (skl_needs_memory_bw_wa(dev_priv))
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304672 wp->y_min_scanlines *= 2;
4673
4674 wp->plane_bytes_per_line = wp->width * wp->cpp;
4675 if (wp->y_tiled) {
4676 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line *
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004677 wp->y_min_scanlines,
4678 wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304679
4680 if (INTEL_GEN(dev_priv) >= 10)
4681 interm_pbpl++;
4682
4683 wp->plane_blocks_per_line = div_fixed16(interm_pbpl,
4684 wp->y_min_scanlines);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004685 } else if (wp->x_tiled && IS_GEN(dev_priv, 9)) {
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004686 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
4687 wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304688 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
4689 } else {
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004690 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
4691 wp->dbuf_block_size) + 1;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304692 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
4693 }
4694
4695 wp->y_tile_minimum = mul_u32_fixed16(wp->y_min_scanlines,
4696 wp->plane_blocks_per_line);
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004697
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304698 wp->linetime_us = fixed16_to_u32_round_up(
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004699 intel_get_linetime_us(crtc_state));
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304700
4701 return 0;
4702}
4703
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004704static int
4705skl_compute_plane_wm_params(const struct intel_crtc_state *crtc_state,
4706 const struct intel_plane_state *plane_state,
4707 struct skl_wm_params *wp, int color_plane)
4708{
4709 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
4710 const struct drm_framebuffer *fb = plane_state->base.fb;
4711 int width;
4712
4713 if (plane->id == PLANE_CURSOR) {
4714 width = plane_state->base.crtc_w;
4715 } else {
4716 /*
4717 * Src coordinates are already rotated by 270 degrees for
4718 * the 90/270 degree plane rotation cases (to match the
4719 * GTT mapping), hence no need to account for rotation here.
4720 */
4721 width = drm_rect_width(&plane_state->base.src) >> 16;
4722 }
4723
4724 return skl_compute_wm_params(crtc_state, width,
4725 fb->format, fb->modifier,
4726 plane_state->base.rotation,
4727 skl_adjusted_plane_pixel_rate(crtc_state, plane_state),
4728 wp, color_plane);
4729}
4730
Ville Syrjäläb52c2732018-12-21 19:14:28 +02004731static bool skl_wm_has_lines(struct drm_i915_private *dev_priv, int level)
4732{
4733 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
4734 return true;
4735
4736 /* The number of lines are ignored for the level 0 watermark. */
4737 return level > 0;
4738}
4739
Maarten Lankhorstec193642019-06-28 10:55:17 +02004740static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Matt Roperd8e87492018-12-11 09:31:07 -08004741 int level,
4742 const struct skl_wm_params *wp,
4743 const struct skl_wm_level *result_prev,
4744 struct skl_wm_level *result /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004745{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004746 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004747 u32 latency = dev_priv->wm.skl_latency[level];
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304748 uint_fixed_16_16_t method1, method2;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304749 uint_fixed_16_16_t selected_result;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004750 u32 res_blocks, res_lines, min_ddb_alloc = 0;
Ville Syrjäläce110ec2018-11-14 23:07:21 +02004751
Ville Syrjälä0aded172019-02-05 17:50:53 +02004752 if (latency == 0) {
4753 /* reject it */
4754 result->min_ddb_alloc = U16_MAX;
Ville Syrjälä692927f2018-12-21 19:14:29 +02004755 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02004756 }
Ville Syrjälä692927f2018-12-21 19:14:29 +02004757
Ville Syrjälä25312ef2019-05-03 20:38:05 +03004758 /*
4759 * WaIncreaseLatencyIPCEnabled: kbl,cfl
4760 * Display WA #1141: kbl,cfl
4761 */
Ville Syrjälä5a7d2022019-05-03 20:38:06 +03004762 if ((IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv)) ||
Rodrigo Vivi82525c12017-06-08 08:50:00 -07004763 dev_priv->ipc_enabled)
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05304764 latency += 4;
4765
Ville Syrjälä60e983f2018-12-21 19:14:33 +02004766 if (skl_needs_memory_bw_wa(dev_priv) && wp->x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03004767 latency += 15;
4768
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304769 method1 = skl_wm_method1(dev_priv, wp->plane_pixel_rate,
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004770 wp->cpp, latency, wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304771 method2 = skl_wm_method2(wp->plane_pixel_rate,
Maarten Lankhorstec193642019-06-28 10:55:17 +02004772 crtc_state->base.adjusted_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004773 latency,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304774 wp->plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004775
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304776 if (wp->y_tiled) {
4777 selected_result = max_fixed16(method2, wp->y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004778 } else {
Maarten Lankhorstec193642019-06-28 10:55:17 +02004779 if ((wp->cpp * crtc_state->base.adjusted_mode.crtc_htotal /
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004780 wp->dbuf_block_size < 1) &&
Paulo Zanoni077b5822018-10-04 16:15:57 -07004781 (wp->plane_bytes_per_line / wp->dbuf_block_size < 1)) {
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03004782 selected_result = method2;
Paulo Zanoni077b5822018-10-04 16:15:57 -07004783 } else if (latency >= wp->linetime_us) {
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004784 if (IS_GEN(dev_priv, 9) &&
Paulo Zanoni077b5822018-10-04 16:15:57 -07004785 !IS_GEMINILAKE(dev_priv))
4786 selected_result = min_fixed16(method1, method2);
4787 else
4788 selected_result = method2;
4789 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004790 selected_result = method1;
Paulo Zanoni077b5822018-10-04 16:15:57 -07004791 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004792 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004793
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304794 res_blocks = fixed16_to_u32_round_up(selected_result) + 1;
Kumar, Maheshd273ecc2017-05-17 17:28:22 +05304795 res_lines = div_round_up_fixed16(selected_result,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304796 wp->plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00004797
Paulo Zanonia5b79d32018-11-13 17:24:32 -08004798 if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv)) {
4799 /* Display WA #1125: skl,bxt,kbl */
4800 if (level == 0 && wp->rc_surface)
4801 res_blocks +=
4802 fixed16_to_u32_round_up(wp->y_tile_minimum);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07004803
Paulo Zanonia5b79d32018-11-13 17:24:32 -08004804 /* Display WA #1126: skl,bxt,kbl */
4805 if (level >= 1 && level <= 7) {
4806 if (wp->y_tiled) {
4807 res_blocks +=
4808 fixed16_to_u32_round_up(wp->y_tile_minimum);
4809 res_lines += wp->y_min_scanlines;
4810 } else {
4811 res_blocks++;
4812 }
4813
4814 /*
4815 * Make sure result blocks for higher latency levels are
4816 * atleast as high as level below the current level.
4817 * Assumption in DDB algorithm optimization for special
4818 * cases. Also covers Display WA #1125 for RC.
4819 */
4820 if (result_prev->plane_res_b > res_blocks)
4821 res_blocks = result_prev->plane_res_b;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004822 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004823 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004824
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004825 if (INTEL_GEN(dev_priv) >= 11) {
4826 if (wp->y_tiled) {
4827 int extra_lines;
4828
4829 if (res_lines % wp->y_min_scanlines == 0)
4830 extra_lines = wp->y_min_scanlines;
4831 else
4832 extra_lines = wp->y_min_scanlines * 2 -
4833 res_lines % wp->y_min_scanlines;
4834
4835 min_ddb_alloc = mul_round_up_u32_fixed16(res_lines + extra_lines,
4836 wp->plane_blocks_per_line);
4837 } else {
4838 min_ddb_alloc = res_blocks +
4839 DIV_ROUND_UP(res_blocks, 10);
4840 }
4841 }
4842
Ville Syrjäläb52c2732018-12-21 19:14:28 +02004843 if (!skl_wm_has_lines(dev_priv, level))
4844 res_lines = 0;
4845
Ville Syrjälä0aded172019-02-05 17:50:53 +02004846 if (res_lines > 31) {
4847 /* reject it */
4848 result->min_ddb_alloc = U16_MAX;
Matt Roperd8e87492018-12-11 09:31:07 -08004849 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02004850 }
Matt Roperd8e87492018-12-11 09:31:07 -08004851
4852 /*
4853 * If res_lines is valid, assume we can use this watermark level
4854 * for now. We'll come back and disable it after we calculate the
4855 * DDB allocation if it turns out we don't actually have enough
4856 * blocks to satisfy it.
4857 */
Mahesh Kumar62027b72018-04-09 09:11:05 +05304858 result->plane_res_b = res_blocks;
4859 result->plane_res_l = res_lines;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004860 /* Bspec says: value >= plane ddb allocation -> invalid, hence the +1 here */
4861 result->min_ddb_alloc = max(min_ddb_alloc, res_blocks) + 1;
Mahesh Kumar62027b72018-04-09 09:11:05 +05304862 result->plane_en = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004863}
4864
Matt Roperd8e87492018-12-11 09:31:07 -08004865static void
Maarten Lankhorstec193642019-06-28 10:55:17 +02004866skl_compute_wm_levels(const struct intel_crtc_state *crtc_state,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304867 const struct skl_wm_params *wm_params,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004868 struct skl_wm_level *levels)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004869{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004870 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304871 int level, max_level = ilk_wm_max_level(dev_priv);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004872 struct skl_wm_level *result_prev = &levels[0];
Lyudea62163e2016-10-04 14:28:20 -04004873
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304874 for (level = 0; level <= max_level; level++) {
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004875 struct skl_wm_level *result = &levels[level];
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304876
Maarten Lankhorstec193642019-06-28 10:55:17 +02004877 skl_compute_plane_wm(crtc_state, level, wm_params,
Matt Roperd8e87492018-12-11 09:31:07 -08004878 result_prev, result);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004879
4880 result_prev = result;
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304881 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004882}
4883
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004884static u32
Maarten Lankhorstec193642019-06-28 10:55:17 +02004885skl_compute_linetime_wm(const struct intel_crtc_state *crtc_state)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004886{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004887 struct drm_atomic_state *state = crtc_state->base.state;
Mahesh Kumara3a89862016-12-01 21:19:34 +05304888 struct drm_i915_private *dev_priv = to_i915(state->dev);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304889 uint_fixed_16_16_t linetime_us;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004890 u32 linetime_wm;
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03004891
Maarten Lankhorstec193642019-06-28 10:55:17 +02004892 linetime_us = intel_get_linetime_us(crtc_state);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304893 linetime_wm = fixed16_to_u32_round_up(mul_u32_fixed16(8, linetime_us));
Mahesh Kumara3a89862016-12-01 21:19:34 +05304894
Ville Syrjälä717671c2018-12-21 19:14:36 +02004895 /* Display WA #1135: BXT:ALL GLK:ALL */
4896 if (IS_GEN9_LP(dev_priv) && dev_priv->ipc_enabled)
Kumar, Mahesh446e8502017-08-17 19:15:25 +05304897 linetime_wm /= 2;
Mahesh Kumara3a89862016-12-01 21:19:34 +05304898
4899 return linetime_wm;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004900}
4901
Maarten Lankhorstec193642019-06-28 10:55:17 +02004902static void skl_compute_transition_wm(const struct intel_crtc_state *crtc_state,
Ville Syrjälä6a3c910b2018-11-14 23:07:23 +02004903 const struct skl_wm_params *wp,
Matt Roperd8e87492018-12-11 09:31:07 -08004904 struct skl_plane_wm *wm)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004905{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004906 struct drm_device *dev = crtc_state->base.crtc->dev;
Kumar, Maheshca476672017-08-17 19:15:24 +05304907 const struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004908 u16 trans_min, trans_y_tile_min;
4909 const u16 trans_amount = 10; /* This is configurable amount */
4910 u16 wm0_sel_res_b, trans_offset_b, res_blocks;
Damien Lespiau9414f562014-11-04 17:06:58 +00004911
Kumar, Maheshca476672017-08-17 19:15:24 +05304912 /* Transition WM are not recommended by HW team for GEN9 */
4913 if (INTEL_GEN(dev_priv) <= 9)
Ville Syrjälä14a43062018-11-14 23:07:22 +02004914 return;
Kumar, Maheshca476672017-08-17 19:15:24 +05304915
4916 /* Transition WM don't make any sense if ipc is disabled */
4917 if (!dev_priv->ipc_enabled)
Ville Syrjälä14a43062018-11-14 23:07:22 +02004918 return;
Kumar, Maheshca476672017-08-17 19:15:24 +05304919
Paulo Zanoni91961a82018-10-04 16:15:56 -07004920 trans_min = 14;
4921 if (INTEL_GEN(dev_priv) >= 11)
Kumar, Maheshca476672017-08-17 19:15:24 +05304922 trans_min = 4;
4923
4924 trans_offset_b = trans_min + trans_amount;
4925
Paulo Zanonicbacc792018-10-04 16:15:58 -07004926 /*
4927 * The spec asks for Selected Result Blocks for wm0 (the real value),
4928 * not Result Blocks (the integer value). Pay attention to the capital
4929 * letters. The value wm_l0->plane_res_b is actually Result Blocks, but
4930 * since Result Blocks is the ceiling of Selected Result Blocks plus 1,
4931 * and since we later will have to get the ceiling of the sum in the
4932 * transition watermarks calculation, we can just pretend Selected
4933 * Result Blocks is Result Blocks minus 1 and it should work for the
4934 * current platforms.
4935 */
Ville Syrjälä6a3c910b2018-11-14 23:07:23 +02004936 wm0_sel_res_b = wm->wm[0].plane_res_b - 1;
Paulo Zanonicbacc792018-10-04 16:15:58 -07004937
Kumar, Maheshca476672017-08-17 19:15:24 +05304938 if (wp->y_tiled) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004939 trans_y_tile_min =
4940 (u16)mul_round_up_u32_fixed16(2, wp->y_tile_minimum);
Paulo Zanonicbacc792018-10-04 16:15:58 -07004941 res_blocks = max(wm0_sel_res_b, trans_y_tile_min) +
Kumar, Maheshca476672017-08-17 19:15:24 +05304942 trans_offset_b;
4943 } else {
Paulo Zanonicbacc792018-10-04 16:15:58 -07004944 res_blocks = wm0_sel_res_b + trans_offset_b;
Kumar, Maheshca476672017-08-17 19:15:24 +05304945
4946 /* WA BUG:1938466 add one block for non y-tile planes */
4947 if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_A0))
4948 res_blocks += 1;
4949
4950 }
4951
Matt Roperd8e87492018-12-11 09:31:07 -08004952 /*
4953 * Just assume we can enable the transition watermark. After
4954 * computing the DDB we'll come back and disable it if that
4955 * assumption turns out to be false.
4956 */
4957 wm->trans_wm.plane_res_b = res_blocks + 1;
4958 wm->trans_wm.plane_en = true;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004959}
4960
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004961static int skl_build_plane_wm_single(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004962 const struct intel_plane_state *plane_state,
4963 enum plane_id plane_id, int color_plane)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004964{
Ville Syrjälä83158472018-11-27 18:57:26 +02004965 struct skl_plane_wm *wm = &crtc_state->wm.skl.optimal.planes[plane_id];
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004966 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004967 int ret;
4968
Ville Syrjälä51de9c62018-11-14 23:07:25 +02004969 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004970 &wm_params, color_plane);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004971 if (ret)
4972 return ret;
4973
Ville Syrjälä67155a62019-03-12 22:58:37 +02004974 skl_compute_wm_levels(crtc_state, &wm_params, wm->wm);
Matt Roperd8e87492018-12-11 09:31:07 -08004975 skl_compute_transition_wm(crtc_state, &wm_params, wm);
Ville Syrjälä83158472018-11-27 18:57:26 +02004976
4977 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004978}
4979
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004980static int skl_build_plane_wm_uv(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004981 const struct intel_plane_state *plane_state,
4982 enum plane_id plane_id)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004983{
Ville Syrjälä83158472018-11-27 18:57:26 +02004984 struct skl_plane_wm *wm = &crtc_state->wm.skl.optimal.planes[plane_id];
4985 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004986 int ret;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004987
Ville Syrjälä83158472018-11-27 18:57:26 +02004988 wm->is_planar = true;
4989
4990 /* uv plane watermarks must also be validated for NV12/Planar */
Ville Syrjälä51de9c62018-11-14 23:07:25 +02004991 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004992 &wm_params, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004993 if (ret)
4994 return ret;
4995
Ville Syrjälä67155a62019-03-12 22:58:37 +02004996 skl_compute_wm_levels(crtc_state, &wm_params, wm->uv_wm);
Ville Syrjälä83158472018-11-27 18:57:26 +02004997
4998 return 0;
4999}
5000
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005001static int skl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005002 const struct intel_plane_state *plane_state)
5003{
5004 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
5005 const struct drm_framebuffer *fb = plane_state->base.fb;
5006 enum plane_id plane_id = plane->id;
5007 int ret;
5008
5009 if (!intel_wm_plane_visible(crtc_state, plane_state))
5010 return 0;
5011
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005012 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005013 plane_id, 0);
5014 if (ret)
5015 return ret;
5016
5017 if (fb->format->is_yuv && fb->format->num_planes > 1) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005018 ret = skl_build_plane_wm_uv(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005019 plane_id);
5020 if (ret)
5021 return ret;
5022 }
5023
5024 return 0;
5025}
5026
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005027static int icl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005028 const struct intel_plane_state *plane_state)
5029{
5030 enum plane_id plane_id = to_intel_plane(plane_state->base.plane)->id;
5031 int ret;
5032
5033 /* Watermarks calculated in master */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005034 if (plane_state->planar_slave)
Ville Syrjälä83158472018-11-27 18:57:26 +02005035 return 0;
5036
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005037 if (plane_state->planar_linked_plane) {
Ville Syrjälä83158472018-11-27 18:57:26 +02005038 const struct drm_framebuffer *fb = plane_state->base.fb;
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005039 enum plane_id y_plane_id = plane_state->planar_linked_plane->id;
Ville Syrjälä83158472018-11-27 18:57:26 +02005040
5041 WARN_ON(!intel_wm_plane_visible(crtc_state, plane_state));
5042 WARN_ON(!fb->format->is_yuv ||
5043 fb->format->num_planes == 1);
5044
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005045 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005046 y_plane_id, 0);
5047 if (ret)
5048 return ret;
5049
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005050 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005051 plane_id, 1);
5052 if (ret)
5053 return ret;
5054 } else if (intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005055 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005056 plane_id, 0);
5057 if (ret)
5058 return ret;
5059 }
5060
5061 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005062}
5063
Maarten Lankhorstec193642019-06-28 10:55:17 +02005064static int skl_build_pipe_wm(struct intel_crtc_state *crtc_state)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005065{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005066 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
5067 struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05305068 struct drm_plane *plane;
Maarten Lankhorstec193642019-06-28 10:55:17 +02005069 const struct drm_plane_state *drm_plane_state;
Matt Roper55994c22016-05-12 07:06:08 -07005070 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005071
Lyudea62163e2016-10-04 14:28:20 -04005072 /*
5073 * We'll only calculate watermarks for planes that are actually
5074 * enabled, so make sure all other planes are set as disabled.
5075 */
5076 memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
5077
Maarten Lankhorstec193642019-06-28 10:55:17 +02005078 drm_atomic_crtc_state_for_each_plane_state(plane, drm_plane_state,
5079 &crtc_state->base) {
5080 const struct intel_plane_state *plane_state =
5081 to_intel_plane_state(drm_plane_state);
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05305082
Ville Syrjälä83158472018-11-27 18:57:26 +02005083 if (INTEL_GEN(dev_priv) >= 11)
Maarten Lankhorstec193642019-06-28 10:55:17 +02005084 ret = icl_build_plane_wm(crtc_state, plane_state);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005085 else
Maarten Lankhorstec193642019-06-28 10:55:17 +02005086 ret = skl_build_plane_wm(crtc_state, plane_state);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305087 if (ret)
5088 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005089 }
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305090
Maarten Lankhorstec193642019-06-28 10:55:17 +02005091 pipe_wm->linetime = skl_compute_linetime_wm(crtc_state);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005092
Matt Roper55994c22016-05-12 07:06:08 -07005093 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005094}
5095
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005096static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
5097 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00005098 const struct skl_ddb_entry *entry)
5099{
5100 if (entry->end)
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005101 I915_WRITE_FW(reg, (entry->end - 1) << 16 | entry->start);
Damien Lespiau16160e32014-11-04 17:06:53 +00005102 else
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005103 I915_WRITE_FW(reg, 0);
Damien Lespiau16160e32014-11-04 17:06:53 +00005104}
5105
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005106static void skl_write_wm_level(struct drm_i915_private *dev_priv,
5107 i915_reg_t reg,
5108 const struct skl_wm_level *level)
5109{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005110 u32 val = 0;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005111
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005112 if (level->plane_en)
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005113 val |= PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005114 if (level->ignore_lines)
5115 val |= PLANE_WM_IGNORE_LINES;
5116 val |= level->plane_res_b;
5117 val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005118
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005119 I915_WRITE_FW(reg, val);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005120}
5121
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005122void skl_write_plane_wm(struct intel_plane *plane,
5123 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005124{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005125 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005126 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005127 enum plane_id plane_id = plane->id;
5128 enum pipe pipe = plane->pipe;
5129 const struct skl_plane_wm *wm =
5130 &crtc_state->wm.skl.optimal.planes[plane_id];
5131 const struct skl_ddb_entry *ddb_y =
5132 &crtc_state->wm.skl.plane_ddb_y[plane_id];
5133 const struct skl_ddb_entry *ddb_uv =
5134 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005135
5136 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005137 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005138 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04005139 }
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005140 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005141 &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02005142
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005143 if (INTEL_GEN(dev_priv) >= 11) {
Mahesh Kumar234059d2018-01-30 11:49:13 -02005144 skl_ddb_entry_write(dev_priv,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005145 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5146 return;
Mahesh Kumarb879d582018-04-09 09:11:01 +05305147 }
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005148
5149 if (wm->is_planar)
5150 swap(ddb_y, ddb_uv);
5151
5152 skl_ddb_entry_write(dev_priv,
5153 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5154 skl_ddb_entry_write(dev_priv,
5155 PLANE_NV12_BUF_CFG(pipe, plane_id), ddb_uv);
Lyude62e0fb82016-08-22 12:50:08 -04005156}
5157
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005158void skl_write_cursor_wm(struct intel_plane *plane,
5159 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005160{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005161 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005162 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005163 enum plane_id plane_id = plane->id;
5164 enum pipe pipe = plane->pipe;
5165 const struct skl_plane_wm *wm =
5166 &crtc_state->wm.skl.optimal.planes[plane_id];
5167 const struct skl_ddb_entry *ddb =
5168 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005169
5170 for (level = 0; level <= max_level; level++) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005171 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
5172 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04005173 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005174 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02005175
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005176 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe), ddb);
Lyude62e0fb82016-08-22 12:50:08 -04005177}
5178
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005179bool skl_wm_level_equals(const struct skl_wm_level *l1,
5180 const struct skl_wm_level *l2)
5181{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005182 return l1->plane_en == l2->plane_en &&
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005183 l1->ignore_lines == l2->ignore_lines &&
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005184 l1->plane_res_l == l2->plane_res_l &&
5185 l1->plane_res_b == l2->plane_res_b;
5186}
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005187
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005188static bool skl_plane_wm_equals(struct drm_i915_private *dev_priv,
5189 const struct skl_plane_wm *wm1,
5190 const struct skl_plane_wm *wm2)
5191{
5192 int level, max_level = ilk_wm_max_level(dev_priv);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005193
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005194 for (level = 0; level <= max_level; level++) {
5195 if (!skl_wm_level_equals(&wm1->wm[level], &wm2->wm[level]) ||
5196 !skl_wm_level_equals(&wm1->uv_wm[level], &wm2->uv_wm[level]))
5197 return false;
5198 }
5199
5200 return skl_wm_level_equals(&wm1->trans_wm, &wm2->trans_wm);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005201}
5202
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005203static bool skl_pipe_wm_equals(struct intel_crtc *crtc,
5204 const struct skl_pipe_wm *wm1,
5205 const struct skl_pipe_wm *wm2)
5206{
5207 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5208 enum plane_id plane_id;
5209
5210 for_each_plane_id_on_crtc(crtc, plane_id) {
5211 if (!skl_plane_wm_equals(dev_priv,
5212 &wm1->planes[plane_id],
5213 &wm2->planes[plane_id]))
5214 return false;
5215 }
5216
5217 return wm1->linetime == wm2->linetime;
5218}
5219
Lyude27082492016-08-24 07:48:10 +02005220static inline bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
5221 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005222{
Lyude27082492016-08-24 07:48:10 +02005223 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005224}
5225
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005226bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry *ddb,
Jani Nikula696173b2019-04-05 14:00:15 +03005227 const struct skl_ddb_entry *entries,
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005228 int num_entries, int ignore_idx)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005229{
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005230 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005231
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005232 for (i = 0; i < num_entries; i++) {
5233 if (i != ignore_idx &&
5234 skl_ddb_entries_overlap(ddb, &entries[i]))
Lyude27082492016-08-24 07:48:10 +02005235 return true;
Mika Kahola2b685042017-10-10 13:17:03 +03005236 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005237
Lyude27082492016-08-24 07:48:10 +02005238 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005239}
5240
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005241static u32
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005242pipes_modified(struct intel_atomic_state *state)
Matt Roper9b613022016-06-27 16:42:44 -07005243{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005244 struct intel_crtc *crtc;
Maarten Lankhorstec193642019-06-28 10:55:17 +02005245 struct intel_crtc_state *crtc_state;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005246 u32 i, ret = 0;
Matt Roper9b613022016-06-27 16:42:44 -07005247
Maarten Lankhorstec193642019-06-28 10:55:17 +02005248 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i)
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005249 ret |= drm_crtc_mask(&crtc->base);
Matt Roper9b613022016-06-27 16:42:44 -07005250
5251 return ret;
5252}
5253
Jani Nikulabb7791b2016-10-04 12:29:17 +03005254static int
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005255skl_ddb_add_affected_planes(const struct intel_crtc_state *old_crtc_state,
5256 struct intel_crtc_state *new_crtc_state)
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005257{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005258 struct intel_atomic_state *state = to_intel_atomic_state(new_crtc_state->base.state);
5259 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
5260 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5261 struct intel_plane *plane;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005262
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005263 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5264 struct intel_plane_state *plane_state;
5265 enum plane_id plane_id = plane->id;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005266
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005267 if (skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_y[plane_id],
5268 &new_crtc_state->wm.skl.plane_ddb_y[plane_id]) &&
5269 skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_uv[plane_id],
5270 &new_crtc_state->wm.skl.plane_ddb_uv[plane_id]))
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005271 continue;
5272
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005273 plane_state = intel_atomic_get_plane_state(state, plane);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005274 if (IS_ERR(plane_state))
5275 return PTR_ERR(plane_state);
Maarten Lankhorst1ab554b2018-10-22 15:51:52 +02005276
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005277 new_crtc_state->update_planes |= BIT(plane_id);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005278 }
5279
5280 return 0;
5281}
5282
5283static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005284skl_compute_ddb(struct intel_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07005285{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005286 const struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5287 struct skl_ddb_allocation *ddb = &state->wm_results.ddb;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005288 struct intel_crtc_state *old_crtc_state;
5289 struct intel_crtc_state *new_crtc_state;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305290 struct intel_crtc *crtc;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305291 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07005292
Paulo Zanoni5a920b82016-10-04 14:37:32 -03005293 memcpy(ddb, &dev_priv->wm.skl_hw.ddb, sizeof(*ddb));
5294
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005295 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005296 new_crtc_state, i) {
5297 ret = skl_allocate_pipe_ddb(new_crtc_state, ddb);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005298 if (ret)
5299 return ret;
5300
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005301 ret = skl_ddb_add_affected_planes(old_crtc_state,
5302 new_crtc_state);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005303 if (ret)
5304 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07005305 }
5306
5307 return 0;
5308}
5309
Ville Syrjäläab98e942019-02-08 22:05:27 +02005310static char enast(bool enable)
5311{
5312 return enable ? '*' : ' ';
5313}
5314
Matt Roper2722efb2016-08-17 15:55:55 -04005315static void
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005316skl_print_wm_changes(struct intel_atomic_state *state)
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005317{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005318 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5319 const struct intel_crtc_state *old_crtc_state;
5320 const struct intel_crtc_state *new_crtc_state;
5321 struct intel_plane *plane;
5322 struct intel_crtc *crtc;
Maarten Lankhorst75704982016-11-01 12:04:10 +01005323 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005324
Ville Syrjäläab98e942019-02-08 22:05:27 +02005325 if ((drm_debug & DRM_UT_KMS) == 0)
5326 return;
5327
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005328 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
5329 new_crtc_state, i) {
Ville Syrjäläab98e942019-02-08 22:05:27 +02005330 const struct skl_pipe_wm *old_pipe_wm, *new_pipe_wm;
5331
5332 old_pipe_wm = &old_crtc_state->wm.skl.optimal;
5333 new_pipe_wm = &new_crtc_state->wm.skl.optimal;
5334
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005335 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5336 enum plane_id plane_id = plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005337 const struct skl_ddb_entry *old, *new;
5338
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005339 old = &old_crtc_state->wm.skl.plane_ddb_y[plane_id];
5340 new = &new_crtc_state->wm.skl.plane_ddb_y[plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005341
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005342 if (skl_ddb_entry_equal(old, new))
5343 continue;
5344
Ville Syrjäläab98e942019-02-08 22:05:27 +02005345 DRM_DEBUG_KMS("[PLANE:%d:%s] ddb (%4d - %4d) -> (%4d - %4d), size %4d -> %4d\n",
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005346 plane->base.base.id, plane->base.name,
Ville Syrjäläab98e942019-02-08 22:05:27 +02005347 old->start, old->end, new->start, new->end,
5348 skl_ddb_entry_size(old), skl_ddb_entry_size(new));
5349 }
5350
5351 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5352 enum plane_id plane_id = plane->id;
5353 const struct skl_plane_wm *old_wm, *new_wm;
5354
5355 old_wm = &old_pipe_wm->planes[plane_id];
5356 new_wm = &new_pipe_wm->planes[plane_id];
5357
5358 if (skl_plane_wm_equals(dev_priv, old_wm, new_wm))
5359 continue;
5360
5361 DRM_DEBUG_KMS("[PLANE:%d:%s] level %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm"
5362 " -> %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm\n",
5363 plane->base.base.id, plane->base.name,
5364 enast(old_wm->wm[0].plane_en), enast(old_wm->wm[1].plane_en),
5365 enast(old_wm->wm[2].plane_en), enast(old_wm->wm[3].plane_en),
5366 enast(old_wm->wm[4].plane_en), enast(old_wm->wm[5].plane_en),
5367 enast(old_wm->wm[6].plane_en), enast(old_wm->wm[7].plane_en),
5368 enast(old_wm->trans_wm.plane_en),
5369 enast(new_wm->wm[0].plane_en), enast(new_wm->wm[1].plane_en),
5370 enast(new_wm->wm[2].plane_en), enast(new_wm->wm[3].plane_en),
5371 enast(new_wm->wm[4].plane_en), enast(new_wm->wm[5].plane_en),
5372 enast(new_wm->wm[6].plane_en), enast(new_wm->wm[7].plane_en),
5373 enast(new_wm->trans_wm.plane_en));
5374
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005375 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"
5376 " -> %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 +02005377 plane->base.base.id, plane->base.name,
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005378 enast(old_wm->wm[0].ignore_lines), old_wm->wm[0].plane_res_l,
5379 enast(old_wm->wm[1].ignore_lines), old_wm->wm[1].plane_res_l,
5380 enast(old_wm->wm[2].ignore_lines), old_wm->wm[2].plane_res_l,
5381 enast(old_wm->wm[3].ignore_lines), old_wm->wm[3].plane_res_l,
5382 enast(old_wm->wm[4].ignore_lines), old_wm->wm[4].plane_res_l,
5383 enast(old_wm->wm[5].ignore_lines), old_wm->wm[5].plane_res_l,
5384 enast(old_wm->wm[6].ignore_lines), old_wm->wm[6].plane_res_l,
5385 enast(old_wm->wm[7].ignore_lines), old_wm->wm[7].plane_res_l,
5386 enast(old_wm->trans_wm.ignore_lines), old_wm->trans_wm.plane_res_l,
5387
5388 enast(new_wm->wm[0].ignore_lines), new_wm->wm[0].plane_res_l,
5389 enast(new_wm->wm[1].ignore_lines), new_wm->wm[1].plane_res_l,
5390 enast(new_wm->wm[2].ignore_lines), new_wm->wm[2].plane_res_l,
5391 enast(new_wm->wm[3].ignore_lines), new_wm->wm[3].plane_res_l,
5392 enast(new_wm->wm[4].ignore_lines), new_wm->wm[4].plane_res_l,
5393 enast(new_wm->wm[5].ignore_lines), new_wm->wm[5].plane_res_l,
5394 enast(new_wm->wm[6].ignore_lines), new_wm->wm[6].plane_res_l,
5395 enast(new_wm->wm[7].ignore_lines), new_wm->wm[7].plane_res_l,
5396 enast(new_wm->trans_wm.ignore_lines), new_wm->trans_wm.plane_res_l);
Ville Syrjäläab98e942019-02-08 22:05:27 +02005397
5398 DRM_DEBUG_KMS("[PLANE:%d:%s] blocks %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d"
5399 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d\n",
5400 plane->base.base.id, plane->base.name,
5401 old_wm->wm[0].plane_res_b, old_wm->wm[1].plane_res_b,
5402 old_wm->wm[2].plane_res_b, old_wm->wm[3].plane_res_b,
5403 old_wm->wm[4].plane_res_b, old_wm->wm[5].plane_res_b,
5404 old_wm->wm[6].plane_res_b, old_wm->wm[7].plane_res_b,
5405 old_wm->trans_wm.plane_res_b,
5406 new_wm->wm[0].plane_res_b, new_wm->wm[1].plane_res_b,
5407 new_wm->wm[2].plane_res_b, new_wm->wm[3].plane_res_b,
5408 new_wm->wm[4].plane_res_b, new_wm->wm[5].plane_res_b,
5409 new_wm->wm[6].plane_res_b, new_wm->wm[7].plane_res_b,
5410 new_wm->trans_wm.plane_res_b);
5411
5412 DRM_DEBUG_KMS("[PLANE:%d:%s] min_ddb %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d"
5413 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d\n",
5414 plane->base.base.id, plane->base.name,
5415 old_wm->wm[0].min_ddb_alloc, old_wm->wm[1].min_ddb_alloc,
5416 old_wm->wm[2].min_ddb_alloc, old_wm->wm[3].min_ddb_alloc,
5417 old_wm->wm[4].min_ddb_alloc, old_wm->wm[5].min_ddb_alloc,
5418 old_wm->wm[6].min_ddb_alloc, old_wm->wm[7].min_ddb_alloc,
5419 old_wm->trans_wm.min_ddb_alloc,
5420 new_wm->wm[0].min_ddb_alloc, new_wm->wm[1].min_ddb_alloc,
5421 new_wm->wm[2].min_ddb_alloc, new_wm->wm[3].min_ddb_alloc,
5422 new_wm->wm[4].min_ddb_alloc, new_wm->wm[5].min_ddb_alloc,
5423 new_wm->wm[6].min_ddb_alloc, new_wm->wm[7].min_ddb_alloc,
5424 new_wm->trans_wm.min_ddb_alloc);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005425 }
5426 }
5427}
5428
Matt Roper98d39492016-05-12 07:06:03 -07005429static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005430skl_ddb_add_affected_pipes(struct intel_atomic_state *state, bool *changed)
Matt Roper98d39492016-05-12 07:06:03 -07005431{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005432 struct drm_device *dev = state->base.dev;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305433 const struct drm_i915_private *dev_priv = to_i915(dev);
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005434 struct intel_crtc *crtc;
5435 struct intel_crtc_state *crtc_state;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005436 u32 realloc_pipes = pipes_modified(state);
Matt Roper734fa012016-05-12 15:11:40 -07005437 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07005438
5439 /*
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02005440 * When we distrust bios wm we always need to recompute to set the
5441 * expected DDB allocations for each CRTC.
5442 */
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305443 if (dev_priv->wm.distrust_bios_wm)
5444 (*changed) = true;
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02005445
5446 /*
Matt Roper98d39492016-05-12 07:06:03 -07005447 * If this transaction isn't actually touching any CRTC's, don't
5448 * bother with watermark calculation. Note that if we pass this
5449 * test, we're guaranteed to hold at least one CRTC state mutex,
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03005450 * which means we can safely use values like dev_priv->active_pipes
Matt Roper98d39492016-05-12 07:06:03 -07005451 * since any racing commits that want to update them would need to
5452 * hold _all_ CRTC state mutexes.
5453 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005454 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i)
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305455 (*changed) = true;
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02005456
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305457 if (!*changed)
Matt Roper98d39492016-05-12 07:06:03 -07005458 return 0;
5459
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305460 /*
5461 * If this is our first atomic update following hardware readout,
5462 * we can't trust the DDB that the BIOS programmed for us. Let's
5463 * pretend that all pipes switched active status so that we'll
5464 * ensure a full DDB recompute.
5465 */
5466 if (dev_priv->wm.distrust_bios_wm) {
5467 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005468 state->base.acquire_ctx);
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305469 if (ret)
5470 return ret;
5471
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005472 state->active_pipe_changes = ~0;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305473
5474 /*
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03005475 * We usually only initialize state->active_pipes if we
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305476 * we're doing a modeset; make sure this field is always
5477 * initialized during the sanitization process that happens
5478 * on the first commit too.
5479 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005480 if (!state->modeset)
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03005481 state->active_pipes = dev_priv->active_pipes;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305482 }
5483
5484 /*
5485 * If the modeset changes which CRTC's are active, we need to
5486 * recompute the DDB allocation for *all* active pipes, even
5487 * those that weren't otherwise being modified in any way by this
5488 * atomic commit. Due to the shrinking of the per-pipe allocations
5489 * when new active CRTC's are added, it's possible for a pipe that
5490 * we were already using and aren't changing at all here to suddenly
5491 * become invalid if its DDB needs exceeds its new allocation.
5492 *
5493 * Note that if we wind up doing a full DDB recompute, we can't let
5494 * any other display updates race with this transaction, so we need
5495 * to grab the lock on *all* CRTC's.
5496 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005497 if (state->active_pipe_changes || state->modeset) {
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305498 realloc_pipes = ~0;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005499 state->wm_results.dirty_pipes = ~0;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305500 }
5501
5502 /*
5503 * We're not recomputing for the pipes not included in the commit, so
5504 * make sure we start with the current state.
5505 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005506 for_each_intel_crtc_mask(dev, crtc, realloc_pipes) {
5507 crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
5508 if (IS_ERR(crtc_state))
5509 return PTR_ERR(crtc_state);
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305510 }
5511
5512 return 0;
5513}
5514
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005515/*
5516 * To make sure the cursor watermark registers are always consistent
5517 * with our computed state the following scenario needs special
5518 * treatment:
5519 *
5520 * 1. enable cursor
5521 * 2. move cursor entirely offscreen
5522 * 3. disable cursor
5523 *
5524 * Step 2. does call .disable_plane() but does not zero the watermarks
5525 * (since we consider an offscreen cursor still active for the purposes
5526 * of watermarks). Step 3. would not normally call .disable_plane()
5527 * because the actual plane visibility isn't changing, and we don't
5528 * deallocate the cursor ddb until the pipe gets disabled. So we must
5529 * force step 3. to call .disable_plane() to update the watermark
5530 * registers properly.
5531 *
5532 * Other planes do not suffer from this issues as their watermarks are
5533 * calculated based on the actual plane visibility. The only time this
5534 * can trigger for the other planes is during the initial readout as the
5535 * default value of the watermarks registers is not zero.
5536 */
5537static int skl_wm_add_affected_planes(struct intel_atomic_state *state,
5538 struct intel_crtc *crtc)
5539{
5540 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5541 const struct intel_crtc_state *old_crtc_state =
5542 intel_atomic_get_old_crtc_state(state, crtc);
5543 struct intel_crtc_state *new_crtc_state =
5544 intel_atomic_get_new_crtc_state(state, crtc);
5545 struct intel_plane *plane;
5546
5547 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5548 struct intel_plane_state *plane_state;
5549 enum plane_id plane_id = plane->id;
5550
5551 /*
5552 * Force a full wm update for every plane on modeset.
5553 * Required because the reset value of the wm registers
5554 * is non-zero, whereas we want all disabled planes to
5555 * have zero watermarks. So if we turn off the relevant
5556 * power well the hardware state will go out of sync
5557 * with the software state.
5558 */
5559 if (!drm_atomic_crtc_needs_modeset(&new_crtc_state->base) &&
5560 skl_plane_wm_equals(dev_priv,
5561 &old_crtc_state->wm.skl.optimal.planes[plane_id],
5562 &new_crtc_state->wm.skl.optimal.planes[plane_id]))
5563 continue;
5564
5565 plane_state = intel_atomic_get_plane_state(state, plane);
5566 if (IS_ERR(plane_state))
5567 return PTR_ERR(plane_state);
5568
5569 new_crtc_state->update_planes |= BIT(plane_id);
5570 }
5571
5572 return 0;
5573}
5574
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305575static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005576skl_compute_wm(struct intel_atomic_state *state)
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305577{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005578 struct intel_crtc *crtc;
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02005579 struct intel_crtc_state *new_crtc_state;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005580 struct intel_crtc_state *old_crtc_state;
5581 struct skl_ddb_values *results = &state->wm_results;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305582 bool changed = false;
5583 int ret, i;
5584
Matt Roper734fa012016-05-12 15:11:40 -07005585 /* Clear all dirty flags */
5586 results->dirty_pipes = 0;
5587
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305588 ret = skl_ddb_add_affected_pipes(state, &changed);
5589 if (ret || !changed)
5590 return ret;
5591
Matt Roper734fa012016-05-12 15:11:40 -07005592 /*
5593 * Calculate WM's for all pipes that are part of this transaction.
Matt Roperd8e87492018-12-11 09:31:07 -08005594 * Note that skl_ddb_add_affected_pipes may have added more CRTC's that
Matt Roper734fa012016-05-12 15:11:40 -07005595 * weren't otherwise being modified (and set bits in dirty_pipes) if
5596 * pipe allocations had to change.
Matt Roper734fa012016-05-12 15:11:40 -07005597 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005598 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02005599 new_crtc_state, i) {
5600 ret = skl_build_pipe_wm(new_crtc_state);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005601 if (ret)
5602 return ret;
5603
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005604 ret = skl_wm_add_affected_planes(state, crtc);
Matt Roper734fa012016-05-12 15:11:40 -07005605 if (ret)
5606 return ret;
5607
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02005608 if (!skl_pipe_wm_equals(crtc,
5609 &old_crtc_state->wm.skl.optimal,
5610 &new_crtc_state->wm.skl.optimal))
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005611 results->dirty_pipes |= drm_crtc_mask(&crtc->base);
Matt Roper734fa012016-05-12 15:11:40 -07005612 }
5613
Matt Roperd8e87492018-12-11 09:31:07 -08005614 ret = skl_compute_ddb(state);
5615 if (ret)
5616 return ret;
5617
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005618 skl_print_wm_changes(state);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005619
Matt Roper98d39492016-05-12 07:06:03 -07005620 return 0;
5621}
5622
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005623static void skl_atomic_update_crtc_wm(struct intel_atomic_state *state,
Maarten Lankhorstec193642019-06-28 10:55:17 +02005624 struct intel_crtc_state *crtc_state)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005625{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005626 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005627 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Maarten Lankhorstec193642019-06-28 10:55:17 +02005628 struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005629 enum pipe pipe = crtc->pipe;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005630
5631 if (!(state->wm_results.dirty_pipes & drm_crtc_mask(&crtc->base)))
5632 return;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005633
5634 I915_WRITE(PIPE_WM_LINETIME(pipe), pipe_wm->linetime);
5635}
5636
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005637static void skl_initial_wm(struct intel_atomic_state *state,
Maarten Lankhorstec193642019-06-28 10:55:17 +02005638 struct intel_crtc_state *crtc_state)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005639{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005640 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02005641 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005642 struct drm_i915_private *dev_priv = to_i915(dev);
Mahesh Kumar60f8e872018-04-09 09:11:00 +05305643 struct skl_ddb_values *results = &state->wm_results;
Bob Paauweadda50b2015-07-21 10:42:53 -07005644
Ville Syrjälä432081b2016-10-31 22:37:03 +02005645 if ((results->dirty_pipes & drm_crtc_mask(&intel_crtc->base)) == 0)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005646 return;
5647
Matt Roper734fa012016-05-12 15:11:40 -07005648 mutex_lock(&dev_priv->wm.wm_mutex);
5649
Maarten Lankhorstec193642019-06-28 10:55:17 +02005650 if (crtc_state->base.active_changed)
5651 skl_atomic_update_crtc_wm(state, crtc_state);
Lyude27082492016-08-24 07:48:10 +02005652
Matt Roper734fa012016-05-12 15:11:40 -07005653 mutex_unlock(&dev_priv->wm.wm_mutex);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005654}
5655
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005656static void ilk_compute_wm_config(struct drm_i915_private *dev_priv,
Ville Syrjäläd8905652016-01-14 14:53:35 +02005657 struct intel_wm_config *config)
5658{
5659 struct intel_crtc *crtc;
5660
5661 /* Compute the currently _active_ config */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005662 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläd8905652016-01-14 14:53:35 +02005663 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
5664
5665 if (!wm->pipe_enabled)
5666 continue;
5667
5668 config->sprites_enabled |= wm->sprites_enabled;
5669 config->sprites_scaled |= wm->sprites_scaled;
5670 config->num_pipes_active++;
5671 }
5672}
5673
Matt Ropered4a6a72016-02-23 17:20:13 -08005674static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03005675{
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005676 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02005677 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02005678 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02005679 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005680 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07005681
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005682 ilk_compute_wm_config(dev_priv, &config);
Ville Syrjäläd8905652016-01-14 14:53:35 +02005683
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005684 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_1_2, &max);
5685 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03005686
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005687 /* 5/6 split only in single pipe config on IVB+ */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00005688 if (INTEL_GEN(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02005689 config.num_pipes_active == 1 && config.sprites_enabled) {
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005690 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_5_6, &max);
5691 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005692
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005693 best_lp_wm = ilk_find_best_result(dev_priv, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03005694 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005695 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005696 }
5697
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005698 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005699 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005700
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005701 ilk_compute_wm_results(dev_priv, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03005702
Imre Deak820c1982013-12-17 14:46:36 +02005703 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03005704}
5705
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005706static void ilk_initial_watermarks(struct intel_atomic_state *state,
Maarten Lankhorstec193642019-06-28 10:55:17 +02005707 struct intel_crtc_state *crtc_state)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005708{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005709 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä88016a92019-07-01 19:05:45 +03005710 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005711
Matt Ropered4a6a72016-02-23 17:20:13 -08005712 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03005713 crtc->wm.active.ilk = crtc_state->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08005714 ilk_program_watermarks(dev_priv);
5715 mutex_unlock(&dev_priv->wm.wm_mutex);
5716}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005717
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005718static void ilk_optimize_watermarks(struct intel_atomic_state *state,
Maarten Lankhorstec193642019-06-28 10:55:17 +02005719 struct intel_crtc_state *crtc_state)
Matt Ropered4a6a72016-02-23 17:20:13 -08005720{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005721 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä88016a92019-07-01 19:05:45 +03005722 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
5723
5724 if (!crtc_state->wm.need_postvbl_update)
5725 return;
Matt Ropered4a6a72016-02-23 17:20:13 -08005726
5727 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03005728 crtc->wm.active.ilk = crtc_state->wm.ilk.optimal;
5729 ilk_program_watermarks(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08005730 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005731}
5732
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005733static inline void skl_wm_level_from_reg_val(u32 val,
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005734 struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00005735{
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005736 level->plane_en = val & PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005737 level->ignore_lines = val & PLANE_WM_IGNORE_LINES;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005738 level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
5739 level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
5740 PLANE_WM_LINES_MASK;
Pradeep Bhat30789992014-11-04 17:06:45 +00005741}
5742
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005743void skl_pipe_wm_get_hw_state(struct intel_crtc *crtc,
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005744 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00005745{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005746 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5747 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005748 int level, max_level;
5749 enum plane_id plane_id;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005750 u32 val;
Pradeep Bhat30789992014-11-04 17:06:45 +00005751
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005752 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00005753
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005754 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005755 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00005756
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005757 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005758 if (plane_id != PLANE_CURSOR)
5759 val = I915_READ(PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005760 else
5761 val = I915_READ(CUR_WM(pipe, level));
5762
5763 skl_wm_level_from_reg_val(val, &wm->wm[level]);
5764 }
5765
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005766 if (plane_id != PLANE_CURSOR)
5767 val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005768 else
5769 val = I915_READ(CUR_WM_TRANS(pipe));
5770
5771 skl_wm_level_from_reg_val(val, &wm->trans_wm);
5772 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005773
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005774 if (!crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00005775 return;
5776
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005777 out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00005778}
5779
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005780void skl_wm_get_hw_state(struct drm_i915_private *dev_priv)
Pradeep Bhat30789992014-11-04 17:06:45 +00005781{
Mahesh Kumar60f8e872018-04-09 09:11:00 +05305782 struct skl_ddb_values *hw = &dev_priv->wm.skl_hw;
Damien Lespiaua269c582014-11-04 17:06:49 +00005783 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005784 struct intel_crtc *crtc;
Maarten Lankhorstec193642019-06-28 10:55:17 +02005785 struct intel_crtc_state *crtc_state;
Pradeep Bhat30789992014-11-04 17:06:45 +00005786
Damien Lespiaua269c582014-11-04 17:06:49 +00005787 skl_ddb_get_hw_state(dev_priv, ddb);
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005788 for_each_intel_crtc(&dev_priv->drm, crtc) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02005789 crtc_state = to_intel_crtc_state(crtc->base.state);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005790
Maarten Lankhorstec193642019-06-28 10:55:17 +02005791 skl_pipe_wm_get_hw_state(crtc, &crtc_state->wm.skl.optimal);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005792
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005793 if (crtc->active)
5794 hw->dirty_pipes |= drm_crtc_mask(&crtc->base);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005795 }
Matt Ropera1de91e2016-05-12 07:05:57 -07005796
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03005797 if (dev_priv->active_pipes) {
Matt Roper279e99d2016-05-12 07:06:02 -07005798 /* Fully recompute DDB on first atomic commit */
5799 dev_priv->wm.distrust_bios_wm = true;
Matt Roper279e99d2016-05-12 07:06:02 -07005800 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005801}
5802
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005803static void ilk_pipe_wm_get_hw_state(struct intel_crtc *crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005804{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005805 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005806 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02005807 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Maarten Lankhorstec193642019-06-28 10:55:17 +02005808 struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
5809 struct intel_pipe_wm *active = &crtc_state->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005810 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005811 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005812 [PIPE_A] = WM0_PIPEA_ILK,
5813 [PIPE_B] = WM0_PIPEB_ILK,
5814 [PIPE_C] = WM0_PIPEC_IVB,
5815 };
5816
5817 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005818 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02005819 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005820
Ville Syrjälä15606532016-05-13 17:55:17 +03005821 memset(active, 0, sizeof(*active));
5822
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005823 active->pipe_enabled = crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02005824
5825 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005826 u32 tmp = hw->wm_pipe[pipe];
5827
5828 /*
5829 * For active pipes LP0 watermark is marked as
5830 * enabled, and LP1+ watermaks as disabled since
5831 * we can't really reverse compute them in case
5832 * multiple pipes are active.
5833 */
5834 active->wm[0].enable = true;
5835 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
5836 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
5837 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
5838 active->linetime = hw->wm_linetime[pipe];
5839 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005840 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005841
5842 /*
5843 * For inactive pipes, all watermark levels
5844 * should be marked as enabled but zeroed,
5845 * which is what we'd compute them to.
5846 */
5847 for (level = 0; level <= max_level; level++)
5848 active->wm[level].enable = true;
5849 }
Matt Roper4e0963c2015-09-24 15:53:15 -07005850
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005851 crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005852}
5853
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005854#define _FW_WM(value, plane) \
5855 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
5856#define _FW_WM_VLV(value, plane) \
5857 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
5858
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005859static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
5860 struct g4x_wm_values *wm)
5861{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005862 u32 tmp;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005863
5864 tmp = I915_READ(DSPFW1);
5865 wm->sr.plane = _FW_WM(tmp, SR);
5866 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5867 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
5868 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
5869
5870 tmp = I915_READ(DSPFW2);
5871 wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
5872 wm->sr.fbc = _FW_WM(tmp, FBC_SR);
5873 wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
5874 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
5875 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5876 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
5877
5878 tmp = I915_READ(DSPFW3);
5879 wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
5880 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5881 wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
5882 wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
5883}
5884
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005885static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
5886 struct vlv_wm_values *wm)
5887{
5888 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005889 u32 tmp;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005890
5891 for_each_pipe(dev_priv, pipe) {
5892 tmp = I915_READ(VLV_DDL(pipe));
5893
Ville Syrjälä1b313892016-11-28 19:37:08 +02005894 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005895 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005896 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005897 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005898 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005899 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005900 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005901 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
5902 }
5903
5904 tmp = I915_READ(DSPFW1);
5905 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005906 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5907 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
5908 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005909
5910 tmp = I915_READ(DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005911 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
5912 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5913 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005914
5915 tmp = I915_READ(DSPFW3);
5916 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5917
5918 if (IS_CHERRYVIEW(dev_priv)) {
5919 tmp = I915_READ(DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005920 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5921 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005922
5923 tmp = I915_READ(DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005924 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
5925 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005926
5927 tmp = I915_READ(DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005928 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
5929 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005930
5931 tmp = I915_READ(DSPHOWM);
5932 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005933 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
5934 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
5935 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
5936 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5937 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5938 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5939 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5940 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5941 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005942 } else {
5943 tmp = I915_READ(DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005944 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5945 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005946
5947 tmp = I915_READ(DSPHOWM);
5948 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005949 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5950 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5951 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5952 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5953 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5954 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005955 }
5956}
5957
5958#undef _FW_WM
5959#undef _FW_WM_VLV
5960
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005961void g4x_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005962{
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005963 struct g4x_wm_values *wm = &dev_priv->wm.g4x;
5964 struct intel_crtc *crtc;
5965
5966 g4x_read_wm_values(dev_priv, wm);
5967
5968 wm->cxsr = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
5969
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005970 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005971 struct intel_crtc_state *crtc_state =
5972 to_intel_crtc_state(crtc->base.state);
5973 struct g4x_wm_state *active = &crtc->wm.active.g4x;
5974 struct g4x_pipe_wm *raw;
5975 enum pipe pipe = crtc->pipe;
5976 enum plane_id plane_id;
5977 int level, max_level;
5978
5979 active->cxsr = wm->cxsr;
5980 active->hpll_en = wm->hpll_en;
5981 active->fbc_en = wm->fbc_en;
5982
5983 active->sr = wm->sr;
5984 active->hpll = wm->hpll;
5985
5986 for_each_plane_id_on_crtc(crtc, plane_id) {
5987 active->wm.plane[plane_id] =
5988 wm->pipe[pipe].plane[plane_id];
5989 }
5990
5991 if (wm->cxsr && wm->hpll_en)
5992 max_level = G4X_WM_LEVEL_HPLL;
5993 else if (wm->cxsr)
5994 max_level = G4X_WM_LEVEL_SR;
5995 else
5996 max_level = G4X_WM_LEVEL_NORMAL;
5997
5998 level = G4X_WM_LEVEL_NORMAL;
5999 raw = &crtc_state->wm.g4x.raw[level];
6000 for_each_plane_id_on_crtc(crtc, plane_id)
6001 raw->plane[plane_id] = active->wm.plane[plane_id];
6002
6003 if (++level > max_level)
6004 goto out;
6005
6006 raw = &crtc_state->wm.g4x.raw[level];
6007 raw->plane[PLANE_PRIMARY] = active->sr.plane;
6008 raw->plane[PLANE_CURSOR] = active->sr.cursor;
6009 raw->plane[PLANE_SPRITE0] = 0;
6010 raw->fbc = active->sr.fbc;
6011
6012 if (++level > max_level)
6013 goto out;
6014
6015 raw = &crtc_state->wm.g4x.raw[level];
6016 raw->plane[PLANE_PRIMARY] = active->hpll.plane;
6017 raw->plane[PLANE_CURSOR] = active->hpll.cursor;
6018 raw->plane[PLANE_SPRITE0] = 0;
6019 raw->fbc = active->hpll.fbc;
6020
6021 out:
6022 for_each_plane_id_on_crtc(crtc, plane_id)
6023 g4x_raw_plane_wm_set(crtc_state, level,
6024 plane_id, USHRT_MAX);
6025 g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
6026
6027 crtc_state->wm.g4x.optimal = *active;
6028 crtc_state->wm.g4x.intermediate = *active;
6029
6030 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
6031 pipe_name(pipe),
6032 wm->pipe[pipe].plane[PLANE_PRIMARY],
6033 wm->pipe[pipe].plane[PLANE_CURSOR],
6034 wm->pipe[pipe].plane[PLANE_SPRITE0]);
6035 }
6036
6037 DRM_DEBUG_KMS("Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
6038 wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
6039 DRM_DEBUG_KMS("Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
6040 wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
6041 DRM_DEBUG_KMS("Initial SR=%s HPLL=%s FBC=%s\n",
6042 yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
6043}
6044
6045void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
6046{
6047 struct intel_plane *plane;
6048 struct intel_crtc *crtc;
6049
6050 mutex_lock(&dev_priv->wm.wm_mutex);
6051
6052 for_each_intel_plane(&dev_priv->drm, plane) {
6053 struct intel_crtc *crtc =
6054 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
6055 struct intel_crtc_state *crtc_state =
6056 to_intel_crtc_state(crtc->base.state);
6057 struct intel_plane_state *plane_state =
6058 to_intel_plane_state(plane->base.state);
6059 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
6060 enum plane_id plane_id = plane->id;
6061 int level;
6062
6063 if (plane_state->base.visible)
6064 continue;
6065
6066 for (level = 0; level < 3; level++) {
6067 struct g4x_pipe_wm *raw =
6068 &crtc_state->wm.g4x.raw[level];
6069
6070 raw->plane[plane_id] = 0;
6071 wm_state->wm.plane[plane_id] = 0;
6072 }
6073
6074 if (plane_id == PLANE_PRIMARY) {
6075 for (level = 0; level < 3; level++) {
6076 struct g4x_pipe_wm *raw =
6077 &crtc_state->wm.g4x.raw[level];
6078 raw->fbc = 0;
6079 }
6080
6081 wm_state->sr.fbc = 0;
6082 wm_state->hpll.fbc = 0;
6083 wm_state->fbc_en = false;
6084 }
6085 }
6086
6087 for_each_intel_crtc(&dev_priv->drm, crtc) {
6088 struct intel_crtc_state *crtc_state =
6089 to_intel_crtc_state(crtc->base.state);
6090
6091 crtc_state->wm.g4x.intermediate =
6092 crtc_state->wm.g4x.optimal;
6093 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
6094 }
6095
6096 g4x_program_watermarks(dev_priv);
6097
6098 mutex_unlock(&dev_priv->wm.wm_mutex);
6099}
6100
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006101void vlv_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006102{
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006103 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02006104 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006105 u32 val;
6106
6107 vlv_read_wm_values(dev_priv, wm);
6108
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006109 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
6110 wm->level = VLV_WM_LEVEL_PM2;
6111
6112 if (IS_CHERRYVIEW(dev_priv)) {
Chris Wilson337fa6e2019-04-26 09:17:20 +01006113 vlv_punit_get(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006114
Ville Syrjäläc11b8132018-11-29 19:55:03 +02006115 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006116 if (val & DSP_MAXFIFO_PM5_ENABLE)
6117 wm->level = VLV_WM_LEVEL_PM5;
6118
Ville Syrjälä58590c12015-09-08 21:05:12 +03006119 /*
6120 * If DDR DVFS is disabled in the BIOS, Punit
6121 * will never ack the request. So if that happens
6122 * assume we don't have to enable/disable DDR DVFS
6123 * dynamically. To test that just set the REQ_ACK
6124 * bit to poke the Punit, but don't change the
6125 * HIGH/LOW bits so that we don't actually change
6126 * the current state.
6127 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006128 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03006129 val |= FORCE_DDR_FREQ_REQ_ACK;
6130 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
6131
6132 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
6133 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
6134 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
6135 "assuming DDR DVFS is disabled\n");
6136 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
6137 } else {
6138 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
6139 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
6140 wm->level = VLV_WM_LEVEL_DDR_DVFS;
6141 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006142
Chris Wilson337fa6e2019-04-26 09:17:20 +01006143 vlv_punit_put(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006144 }
6145
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006146 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02006147 struct intel_crtc_state *crtc_state =
6148 to_intel_crtc_state(crtc->base.state);
6149 struct vlv_wm_state *active = &crtc->wm.active.vlv;
6150 const struct vlv_fifo_state *fifo_state =
6151 &crtc_state->wm.vlv.fifo_state;
6152 enum pipe pipe = crtc->pipe;
6153 enum plane_id plane_id;
6154 int level;
6155
6156 vlv_get_fifo_size(crtc_state);
6157
6158 active->num_levels = wm->level + 1;
6159 active->cxsr = wm->cxsr;
6160
Ville Syrjäläff32c542017-03-02 19:14:57 +02006161 for (level = 0; level < active->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03006162 struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02006163 &crtc_state->wm.vlv.raw[level];
6164
6165 active->sr[level].plane = wm->sr.plane;
6166 active->sr[level].cursor = wm->sr.cursor;
6167
6168 for_each_plane_id_on_crtc(crtc, plane_id) {
6169 active->wm[level].plane[plane_id] =
6170 wm->pipe[pipe].plane[plane_id];
6171
6172 raw->plane[plane_id] =
6173 vlv_invert_wm_value(active->wm[level].plane[plane_id],
6174 fifo_state->plane[plane_id]);
6175 }
6176 }
6177
6178 for_each_plane_id_on_crtc(crtc, plane_id)
6179 vlv_raw_plane_wm_set(crtc_state, level,
6180 plane_id, USHRT_MAX);
6181 vlv_invalidate_wms(crtc, active, level);
6182
6183 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02006184 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02006185
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006186 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 +02006187 pipe_name(pipe),
6188 wm->pipe[pipe].plane[PLANE_PRIMARY],
6189 wm->pipe[pipe].plane[PLANE_CURSOR],
6190 wm->pipe[pipe].plane[PLANE_SPRITE0],
6191 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02006192 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006193
6194 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
6195 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
6196}
6197
Ville Syrjälä602ae832017-03-02 19:15:02 +02006198void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
6199{
6200 struct intel_plane *plane;
6201 struct intel_crtc *crtc;
6202
6203 mutex_lock(&dev_priv->wm.wm_mutex);
6204
6205 for_each_intel_plane(&dev_priv->drm, plane) {
6206 struct intel_crtc *crtc =
6207 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
6208 struct intel_crtc_state *crtc_state =
6209 to_intel_crtc_state(crtc->base.state);
6210 struct intel_plane_state *plane_state =
6211 to_intel_plane_state(plane->base.state);
6212 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
6213 const struct vlv_fifo_state *fifo_state =
6214 &crtc_state->wm.vlv.fifo_state;
6215 enum plane_id plane_id = plane->id;
6216 int level;
6217
6218 if (plane_state->base.visible)
6219 continue;
6220
6221 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03006222 struct g4x_pipe_wm *raw =
Ville Syrjälä602ae832017-03-02 19:15:02 +02006223 &crtc_state->wm.vlv.raw[level];
6224
6225 raw->plane[plane_id] = 0;
6226
6227 wm_state->wm[level].plane[plane_id] =
6228 vlv_invert_wm_value(raw->plane[plane_id],
6229 fifo_state->plane[plane_id]);
6230 }
6231 }
6232
6233 for_each_intel_crtc(&dev_priv->drm, crtc) {
6234 struct intel_crtc_state *crtc_state =
6235 to_intel_crtc_state(crtc->base.state);
6236
6237 crtc_state->wm.vlv.intermediate =
6238 crtc_state->wm.vlv.optimal;
6239 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
6240 }
6241
6242 vlv_program_watermarks(dev_priv);
6243
6244 mutex_unlock(&dev_priv->wm.wm_mutex);
6245}
6246
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02006247/*
6248 * FIXME should probably kill this and improve
6249 * the real watermark readout/sanitation instead
6250 */
6251static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
6252{
6253 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
6254 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
6255 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
6256
6257 /*
6258 * Don't touch WM1S_LP_EN here.
6259 * Doing so could cause underruns.
6260 */
6261}
6262
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006263void ilk_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006264{
Imre Deak820c1982013-12-17 14:46:36 +02006265 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006266 struct intel_crtc *crtc;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006267
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02006268 ilk_init_lp_watermarks(dev_priv);
6269
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006270 for_each_intel_crtc(&dev_priv->drm, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006271 ilk_pipe_wm_get_hw_state(crtc);
6272
6273 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
6274 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
6275 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
6276
6277 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00006278 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjäläcfa76982014-03-07 18:32:08 +02006279 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
6280 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
6281 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006282
Tvrtko Ursulin86527442016-10-13 11:03:00 +01006283 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02006284 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
6285 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01006286 else if (IS_IVYBRIDGE(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02006287 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
6288 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006289
6290 hw->enable_fbc_wm =
6291 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
6292}
6293
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006294/**
6295 * intel_update_watermarks - update FIFO watermark values based on current modes
Chris Wilson31383412018-02-14 14:03:03 +00006296 * @crtc: the #intel_crtc on which to compute the WM
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006297 *
6298 * Calculate watermark values for the various WM regs based on current mode
6299 * and plane configuration.
6300 *
6301 * There are several cases to deal with here:
6302 * - normal (i.e. non-self-refresh)
6303 * - self-refresh (SR) mode
6304 * - lines are large relative to FIFO size (buffer can hold up to 2)
6305 * - lines are small relative to FIFO size (buffer can hold more than 2
6306 * lines), so need to account for TLB latency
6307 *
6308 * The normal calculation is:
6309 * watermark = dotclock * bytes per pixel * latency
6310 * where latency is platform & configuration dependent (we assume pessimal
6311 * values here).
6312 *
6313 * The SR calculation is:
6314 * watermark = (trunc(latency/line time)+1) * surface width *
6315 * bytes per pixel
6316 * where
6317 * line time = htotal / dotclock
6318 * surface width = hdisplay for normal plane and 64 for cursor
6319 * and latency is assumed to be high, as above.
6320 *
6321 * The final value programmed to the register should always be rounded up,
6322 * and include an extra 2 entries to account for clock crossings.
6323 *
6324 * We don't use the sprite, so we can ignore that. And on Crestline we have
6325 * to set the non-SR watermarks to 8.
6326 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02006327void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006328{
Ville Syrjälä432081b2016-10-31 22:37:03 +02006329 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006330
6331 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03006332 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006333}
6334
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306335void intel_enable_ipc(struct drm_i915_private *dev_priv)
6336{
6337 u32 val;
6338
José Roberto de Souzafd847b82018-09-18 13:47:11 -07006339 if (!HAS_IPC(dev_priv))
6340 return;
6341
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306342 val = I915_READ(DISP_ARB_CTL2);
6343
6344 if (dev_priv->ipc_enabled)
6345 val |= DISP_IPC_ENABLE;
6346 else
6347 val &= ~DISP_IPC_ENABLE;
6348
6349 I915_WRITE(DISP_ARB_CTL2, val);
6350}
6351
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03006352static bool intel_can_enable_ipc(struct drm_i915_private *dev_priv)
6353{
6354 /* Display WA #0477 WaDisableIPC: skl */
6355 if (IS_SKYLAKE(dev_priv))
6356 return false;
6357
6358 /* Display WA #1141: SKL:all KBL:all CFL */
6359 if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv))
6360 return dev_priv->dram_info.symmetric_memory;
6361
6362 return true;
6363}
6364
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306365void intel_init_ipc(struct drm_i915_private *dev_priv)
6366{
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306367 if (!HAS_IPC(dev_priv))
6368 return;
6369
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03006370 dev_priv->ipc_enabled = intel_can_enable_ipc(dev_priv);
José Roberto de Souzac9b818d2018-09-18 13:47:13 -07006371
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306372 intel_enable_ipc(dev_priv);
6373}
6374
Jani Nikulae2828912016-01-18 09:19:47 +02006375/*
Daniel Vetter92703882012-08-09 16:46:01 +02006376 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02006377 */
6378DEFINE_SPINLOCK(mchdev_lock);
6379
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006380bool ironlake_set_drps(struct drm_i915_private *i915, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006381{
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006382 struct intel_uncore *uncore = &i915->uncore;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006383 u16 rgvswctl;
6384
Chris Wilson67520412017-03-02 13:28:01 +00006385 lockdep_assert_held(&mchdev_lock);
Daniel Vetter92703882012-08-09 16:46:01 +02006386
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006387 rgvswctl = intel_uncore_read16(uncore, MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006388 if (rgvswctl & MEMCTL_CMD_STS) {
6389 DRM_DEBUG("gpu busy, RCS change rejected\n");
6390 return false; /* still busy with another command */
6391 }
6392
6393 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
6394 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006395 intel_uncore_write16(uncore, MEMSWCTL, rgvswctl);
6396 intel_uncore_posting_read16(uncore, MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006397
6398 rgvswctl |= MEMCTL_CMD_STS;
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006399 intel_uncore_write16(uncore, MEMSWCTL, rgvswctl);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006400
6401 return true;
6402}
6403
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006404static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006405{
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006406 struct intel_uncore *uncore = &dev_priv->uncore;
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00006407 u32 rgvmodectl;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006408 u8 fmax, fmin, fstart, vstart;
6409
Daniel Vetter92703882012-08-09 16:46:01 +02006410 spin_lock_irq(&mchdev_lock);
6411
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006412 rgvmodectl = intel_uncore_read(uncore, MEMMODECTL);
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00006413
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006414 /* Enable temp reporting */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006415 intel_uncore_write16(uncore, PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
6416 intel_uncore_write16(uncore, TSC1, I915_READ(TSC1) | TSE);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006417
6418 /* 100ms RC evaluation intervals */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006419 intel_uncore_write(uncore, RCUPEI, 100000);
6420 intel_uncore_write(uncore, RCDNEI, 100000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006421
6422 /* Set max/min thresholds to 90ms and 80ms respectively */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006423 intel_uncore_write(uncore, RCBMAXAVG, 90000);
6424 intel_uncore_write(uncore, RCBMINAVG, 80000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006425
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006426 intel_uncore_write(uncore, MEMIHYST, 1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006427
6428 /* Set up min, max, and cur for interrupt handling */
6429 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
6430 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
6431 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
6432 MEMMODE_FSTART_SHIFT;
6433
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006434 vstart = (intel_uncore_read(uncore, PXVFREQ(fstart)) &
6435 PXVFREQ_PX_MASK) >> PXVFREQ_PX_SHIFT;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006436
Daniel Vetter20e4d402012-08-08 23:35:39 +02006437 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
6438 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006439
Daniel Vetter20e4d402012-08-08 23:35:39 +02006440 dev_priv->ips.max_delay = fstart;
6441 dev_priv->ips.min_delay = fmin;
6442 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006443
6444 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
6445 fmax, fmin, fstart);
6446
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006447 intel_uncore_write(uncore,
6448 MEMINTREN,
6449 MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006450
6451 /*
6452 * Interrupts will be enabled in ironlake_irq_postinstall
6453 */
6454
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006455 intel_uncore_write(uncore, VIDSTART, vstart);
6456 intel_uncore_posting_read(uncore, VIDSTART);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006457
6458 rgvmodectl |= MEMMODE_SWMODE_EN;
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006459 intel_uncore_write(uncore, MEMMODECTL, rgvmodectl);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006460
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006461 if (wait_for_atomic((intel_uncore_read(uncore, MEMSWCTL) &
6462 MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006463 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02006464 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006465
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006466 ironlake_set_drps(dev_priv, fstart);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006467
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006468 dev_priv->ips.last_count1 =
6469 intel_uncore_read(uncore, DMIEC) +
6470 intel_uncore_read(uncore, DDREC) +
6471 intel_uncore_read(uncore, CSIEC);
Daniel Vetter20e4d402012-08-08 23:35:39 +02006472 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006473 dev_priv->ips.last_count2 = intel_uncore_read(uncore, GFXEC);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00006474 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02006475
6476 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006477}
6478
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006479static void ironlake_disable_drps(struct drm_i915_private *i915)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006480{
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006481 struct intel_uncore *uncore = &i915->uncore;
Daniel Vetter92703882012-08-09 16:46:01 +02006482 u16 rgvswctl;
6483
6484 spin_lock_irq(&mchdev_lock);
6485
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006486 rgvswctl = intel_uncore_read16(uncore, MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006487
6488 /* Ack interrupts, disable EFC interrupt */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006489 intel_uncore_write(uncore,
6490 MEMINTREN,
6491 intel_uncore_read(uncore, MEMINTREN) &
6492 ~MEMINT_EVAL_CHG_EN);
6493 intel_uncore_write(uncore, MEMINTRSTS, MEMINT_EVAL_CHG);
6494 intel_uncore_write(uncore,
6495 DEIER,
6496 intel_uncore_read(uncore, DEIER) & ~DE_PCU_EVENT);
6497 intel_uncore_write(uncore, DEIIR, DE_PCU_EVENT);
6498 intel_uncore_write(uncore,
6499 DEIMR,
6500 intel_uncore_read(uncore, DEIMR) | DE_PCU_EVENT);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006501
6502 /* Go back to the starting frequency */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006503 ironlake_set_drps(i915, i915->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02006504 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006505 rgvswctl |= MEMCTL_CMD_STS;
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006506 intel_uncore_write(uncore, MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02006507 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006508
Daniel Vetter92703882012-08-09 16:46:01 +02006509 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006510}
6511
Daniel Vetteracbe9472012-07-26 11:50:05 +02006512/* There's a funny hw issue where the hw returns all 0 when reading from
6513 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
6514 * ourselves, instead of doing a rmw cycle (which might result in us clearing
6515 * all limits and the gpu stuck at whatever frequency it is at atm).
6516 */
Akash Goel74ef1172015-03-06 11:07:19 +05306517static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006518{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006519 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006520 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006521
Daniel Vetter20b46e52012-07-26 11:16:14 +02006522 /* Only set the down limit when we've reached the lowest level to avoid
6523 * getting more interrupts, otherwise leave this clear. This prevents a
6524 * race in the hw when coming out of rc6: There's a tiny window where
6525 * the hw runs at the minimal clock before selecting the desired
6526 * frequency, if the down threshold expires in that window we will not
6527 * receive a down interrupt. */
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006528 if (INTEL_GEN(dev_priv) >= 9) {
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006529 limits = (rps->max_freq_softlimit) << 23;
6530 if (val <= rps->min_freq_softlimit)
6531 limits |= (rps->min_freq_softlimit) << 14;
Akash Goel74ef1172015-03-06 11:07:19 +05306532 } else {
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006533 limits = rps->max_freq_softlimit << 24;
6534 if (val <= rps->min_freq_softlimit)
6535 limits |= rps->min_freq_softlimit << 16;
Akash Goel74ef1172015-03-06 11:07:19 +05306536 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02006537
6538 return limits;
6539}
6540
Chris Wilson60548c52018-07-31 14:26:29 +01006541static void rps_set_power(struct drm_i915_private *dev_priv, int new_power)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006542{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006543 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Akash Goel8a586432015-03-06 11:07:18 +05306544 u32 threshold_up = 0, threshold_down = 0; /* in % */
6545 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006546
Chris Wilson60548c52018-07-31 14:26:29 +01006547 lockdep_assert_held(&rps->power.mutex);
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006548
Chris Wilson60548c52018-07-31 14:26:29 +01006549 if (new_power == rps->power.mode)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006550 return;
6551
6552 /* Note the units here are not exactly 1us, but 1280ns. */
6553 switch (new_power) {
6554 case LOW_POWER:
6555 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05306556 ei_up = 16000;
6557 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006558
6559 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306560 ei_down = 32000;
6561 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006562 break;
6563
6564 case BETWEEN:
6565 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05306566 ei_up = 13000;
6567 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006568
6569 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306570 ei_down = 32000;
6571 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006572 break;
6573
6574 case HIGH_POWER:
6575 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05306576 ei_up = 10000;
6577 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006578
6579 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306580 ei_down = 32000;
6581 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006582 break;
6583 }
6584
Mika Kuoppala6067a272017-02-15 15:52:59 +02006585 /* When byt can survive without system hang with dynamic
6586 * sw freq adjustments, this restriction can be lifted.
6587 */
6588 if (IS_VALLEYVIEW(dev_priv))
6589 goto skip_hw_write;
6590
Akash Goel8a586432015-03-06 11:07:18 +05306591 I915_WRITE(GEN6_RP_UP_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01006592 GT_INTERVAL_FROM_US(dev_priv, ei_up));
Akash Goel8a586432015-03-06 11:07:18 +05306593 I915_WRITE(GEN6_RP_UP_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01006594 GT_INTERVAL_FROM_US(dev_priv,
6595 ei_up * threshold_up / 100));
Akash Goel8a586432015-03-06 11:07:18 +05306596
6597 I915_WRITE(GEN6_RP_DOWN_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01006598 GT_INTERVAL_FROM_US(dev_priv, ei_down));
Akash Goel8a586432015-03-06 11:07:18 +05306599 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01006600 GT_INTERVAL_FROM_US(dev_priv,
6601 ei_down * threshold_down / 100));
Akash Goel8a586432015-03-06 11:07:18 +05306602
Chris Wilsona72b5622016-07-02 15:35:59 +01006603 I915_WRITE(GEN6_RP_CONTROL,
Mika Kuoppala1071d0f2019-04-10 16:24:36 +03006604 (INTEL_GEN(dev_priv) > 9 ? 0 : GEN6_RP_MEDIA_TURBO) |
Chris Wilsona72b5622016-07-02 15:35:59 +01006605 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6606 GEN6_RP_MEDIA_IS_GFX |
6607 GEN6_RP_ENABLE |
6608 GEN6_RP_UP_BUSY_AVG |
6609 GEN6_RP_DOWN_IDLE_AVG);
Akash Goel8a586432015-03-06 11:07:18 +05306610
Mika Kuoppala6067a272017-02-15 15:52:59 +02006611skip_hw_write:
Chris Wilson60548c52018-07-31 14:26:29 +01006612 rps->power.mode = new_power;
6613 rps->power.up_threshold = threshold_up;
6614 rps->power.down_threshold = threshold_down;
6615}
6616
6617static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
6618{
6619 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6620 int new_power;
6621
6622 new_power = rps->power.mode;
6623 switch (rps->power.mode) {
6624 case LOW_POWER:
6625 if (val > rps->efficient_freq + 1 &&
6626 val > rps->cur_freq)
6627 new_power = BETWEEN;
6628 break;
6629
6630 case BETWEEN:
6631 if (val <= rps->efficient_freq &&
6632 val < rps->cur_freq)
6633 new_power = LOW_POWER;
6634 else if (val >= rps->rp0_freq &&
6635 val > rps->cur_freq)
6636 new_power = HIGH_POWER;
6637 break;
6638
6639 case HIGH_POWER:
6640 if (val < (rps->rp1_freq + rps->rp0_freq) >> 1 &&
6641 val < rps->cur_freq)
6642 new_power = BETWEEN;
6643 break;
6644 }
6645 /* Max/min bins are special */
6646 if (val <= rps->min_freq_softlimit)
6647 new_power = LOW_POWER;
6648 if (val >= rps->max_freq_softlimit)
6649 new_power = HIGH_POWER;
6650
6651 mutex_lock(&rps->power.mutex);
6652 if (rps->power.interactive)
6653 new_power = HIGH_POWER;
6654 rps_set_power(dev_priv, new_power);
6655 mutex_unlock(&rps->power.mutex);
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006656}
6657
Chris Wilson60548c52018-07-31 14:26:29 +01006658void intel_rps_mark_interactive(struct drm_i915_private *i915, bool interactive)
6659{
6660 struct intel_rps *rps = &i915->gt_pm.rps;
6661
6662 if (INTEL_GEN(i915) < 6)
6663 return;
6664
6665 mutex_lock(&rps->power.mutex);
6666 if (interactive) {
6667 if (!rps->power.interactive++ && READ_ONCE(i915->gt.awake))
6668 rps_set_power(i915, HIGH_POWER);
6669 } else {
6670 GEM_BUG_ON(!rps->power.interactive);
6671 rps->power.interactive--;
6672 }
6673 mutex_unlock(&rps->power.mutex);
6674}
6675
Chris Wilson2876ce72014-03-28 08:03:34 +00006676static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
6677{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006678 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Chris Wilson2876ce72014-03-28 08:03:34 +00006679 u32 mask = 0;
6680
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006681 /* We use UP_EI_EXPIRED interupts for both up/down in manual mode */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006682 if (val > rps->min_freq_softlimit)
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006683 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006684 if (val < rps->max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00006685 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00006686
Chris Wilson7b3c29f2014-07-10 20:31:19 +01006687 mask &= dev_priv->pm_rps_events;
6688
Imre Deak59d02a12014-12-19 19:33:26 +02006689 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00006690}
6691
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006692/* gen6_set_rps is called to update the frequency request, but should also be
6693 * called when the range (min_delay and max_delay) is modified so that we can
6694 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006695static int gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02006696{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006697 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6698
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006699 /* min/max delay may still have been modified so be sure to
6700 * write the limits value.
6701 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006702 if (val != rps->cur_freq) {
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006703 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006704
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006705 if (INTEL_GEN(dev_priv) >= 9)
Akash Goel57041952015-03-06 11:07:17 +05306706 I915_WRITE(GEN6_RPNSWREQ,
6707 GEN9_FREQUENCY(val));
Chris Wilsondc979972016-05-10 14:10:04 +01006708 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006709 I915_WRITE(GEN6_RPNSWREQ,
6710 HSW_FREQUENCY(val));
6711 else
6712 I915_WRITE(GEN6_RPNSWREQ,
6713 GEN6_FREQUENCY(val) |
6714 GEN6_OFFSET(0) |
6715 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006716 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006717
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006718 /* Make sure we continue to get interrupts
6719 * until we hit the minimum or maximum frequencies.
6720 */
Akash Goel74ef1172015-03-06 11:07:19 +05306721 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00006722 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006723
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006724 rps->cur_freq = val;
Mika Kuoppala0f945922015-11-17 18:14:26 +02006725 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006726
6727 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006728}
6729
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006730static int valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006731{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006732 int err;
6733
Chris Wilsondc979972016-05-10 14:10:04 +01006734 if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006735 "Odd GPU freq value\n"))
6736 val &= ~1;
6737
Deepak Scd25dd52015-07-10 18:31:40 +05306738 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
6739
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006740 if (val != dev_priv->gt_pm.rps.cur_freq) {
Chris Wilson337fa6e2019-04-26 09:17:20 +01006741 vlv_punit_get(dev_priv);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006742 err = vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
Chris Wilson337fa6e2019-04-26 09:17:20 +01006743 vlv_punit_put(dev_priv);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006744 if (err)
6745 return err;
6746
Chris Wilsondb4c5e02017-02-10 15:03:46 +00006747 gen6_set_rps_thresholds(dev_priv, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01006748 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006749
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006750 dev_priv->gt_pm.rps.cur_freq = val;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006751 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006752
6753 return 0;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006754}
6755
Deepak Sa7f6e232015-05-09 18:04:44 +05306756/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05306757 *
6758 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05306759 * 1. Forcewake Media well.
6760 * 2. Request idle freq.
6761 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05306762*/
6763static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
6764{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006765 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6766 u32 val = rps->idle_freq;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006767 int err;
Deepak S5549d252014-06-28 11:26:11 +05306768
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006769 if (rps->cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05306770 return;
6771
Chris Wilsonc9efef72017-01-02 15:28:45 +00006772 /* The punit delays the write of the frequency and voltage until it
6773 * determines the GPU is awake. During normal usage we don't want to
6774 * waste power changing the frequency if the GPU is sleeping (rc6).
6775 * However, the GPU and driver is now idle and we do not want to delay
6776 * switching to minimum voltage (reducing power whilst idle) as we do
6777 * not expect to be woken in the near future and so must flush the
6778 * change by waking the device.
6779 *
6780 * We choose to take the media powerwell (either would do to trick the
6781 * punit into committing the voltage change) as that takes a lot less
6782 * power than the render powerwell.
6783 */
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07006784 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006785 err = valleyview_set_rps(dev_priv, val);
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07006786 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006787
6788 if (err)
6789 DRM_ERROR("Failed to set RPS for idle\n");
Deepak S76c3552f2014-01-30 23:08:16 +05306790}
6791
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006792void gen6_rps_busy(struct drm_i915_private *dev_priv)
6793{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006794 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6795
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006796 mutex_lock(&rps->lock);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006797 if (rps->enabled) {
Chris Wilsonbd648182017-02-10 15:03:48 +00006798 u8 freq;
6799
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006800 if (dev_priv->pm_rps_events & GEN6_PM_RP_UP_EI_EXPIRED)
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006801 gen6_rps_reset_ei(dev_priv);
6802 I915_WRITE(GEN6_PMINTRMSK,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006803 gen6_rps_pm_mask(dev_priv, rps->cur_freq));
Michał Winiarski2b83c4c2016-06-20 11:58:27 +02006804
Chris Wilsonc33d2472016-07-04 08:08:36 +01006805 gen6_enable_rps_interrupts(dev_priv);
6806
Chris Wilsonbd648182017-02-10 15:03:48 +00006807 /* Use the user's desired frequency as a guide, but for better
6808 * performance, jump directly to RPe as our starting frequency.
6809 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006810 freq = max(rps->cur_freq,
6811 rps->efficient_freq);
Chris Wilsonbd648182017-02-10 15:03:48 +00006812
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006813 if (intel_set_rps(dev_priv,
Chris Wilsonbd648182017-02-10 15:03:48 +00006814 clamp(freq,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006815 rps->min_freq_softlimit,
6816 rps->max_freq_softlimit)))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006817 DRM_DEBUG_DRIVER("Failed to set idle frequency\n");
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006818 }
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006819 mutex_unlock(&rps->lock);
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006820}
6821
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006822void gen6_rps_idle(struct drm_i915_private *dev_priv)
6823{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006824 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6825
Chris Wilsonc33d2472016-07-04 08:08:36 +01006826 /* Flush our bottom-half so that it does not race with us
6827 * setting the idle frequency and so that it is bounded by
6828 * our rpm wakeref. And then disable the interrupts to stop any
6829 * futher RPS reclocking whilst we are asleep.
6830 */
6831 gen6_disable_rps_interrupts(dev_priv);
6832
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006833 mutex_lock(&rps->lock);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006834 if (rps->enabled) {
Chris Wilsondc979972016-05-10 14:10:04 +01006835 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Deepak S76c3552f2014-01-30 23:08:16 +05306836 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02006837 else
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006838 gen6_set_rps(dev_priv, rps->idle_freq);
6839 rps->last_adj = 0;
Ville Syrjälä12c100b2016-05-23 17:42:48 +03006840 I915_WRITE(GEN6_PMINTRMSK,
6841 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Chris Wilsonc0951f02013-10-10 21:58:50 +01006842 }
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006843 mutex_unlock(&rps->lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006844}
6845
Chris Wilson62eb3c22019-02-13 09:25:04 +00006846void gen6_rps_boost(struct i915_request *rq)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006847{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006848 struct intel_rps *rps = &rq->i915->gt_pm.rps;
Chris Wilson74d290f2017-08-17 13:37:06 +01006849 unsigned long flags;
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006850 bool boost;
6851
Chris Wilson8d3afd72015-05-21 21:01:47 +01006852 /* This is intentionally racy! We peek at the state here, then
6853 * validate inside the RPS worker.
6854 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006855 if (!rps->enabled)
Chris Wilson8d3afd72015-05-21 21:01:47 +01006856 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006857
Chris Wilson0e218342019-01-21 22:21:02 +00006858 if (i915_request_signaled(rq))
Chris Wilson253a2812018-02-06 14:31:37 +00006859 return;
6860
Chris Wilsone61e0f52018-02-21 09:56:36 +00006861 /* Serializes with i915_request_retire() */
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006862 boost = false;
Chris Wilson74d290f2017-08-17 13:37:06 +01006863 spin_lock_irqsave(&rq->lock, flags);
Lionel Landwerlin2a98f4e2019-07-09 17:42:27 +01006864 if (!i915_request_has_waitboost(rq) &&
6865 !dma_fence_is_signaled_locked(&rq->fence)) {
Chris Wilson253a2812018-02-06 14:31:37 +00006866 boost = !atomic_fetch_inc(&rps->num_waiters);
Lionel Landwerlin2a98f4e2019-07-09 17:42:27 +01006867 rq->flags |= I915_REQUEST_WAITBOOST;
Chris Wilsonc0951f02013-10-10 21:58:50 +01006868 }
Chris Wilson74d290f2017-08-17 13:37:06 +01006869 spin_unlock_irqrestore(&rq->lock, flags);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006870 if (!boost)
6871 return;
6872
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006873 if (READ_ONCE(rps->cur_freq) < rps->boost_freq)
6874 schedule_work(&rps->work);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006875
Chris Wilson62eb3c22019-02-13 09:25:04 +00006876 atomic_inc(&rps->boosts);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006877}
6878
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006879int intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006880{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006881 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006882 int err;
6883
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006884 lockdep_assert_held(&rps->lock);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006885 GEM_BUG_ON(val > rps->max_freq);
6886 GEM_BUG_ON(val < rps->min_freq);
Chris Wilsoncfd1c482017-02-20 09:47:07 +00006887
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006888 if (!rps->enabled) {
6889 rps->cur_freq = val;
Chris Wilson76e4e4b2017-02-20 09:47:08 +00006890 return 0;
6891 }
6892
Chris Wilsondc979972016-05-10 14:10:04 +01006893 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006894 err = valleyview_set_rps(dev_priv, val);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006895 else
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006896 err = gen6_set_rps(dev_priv, val);
6897
6898 return err;
Jesse Barnes0a073b82013-04-17 15:54:58 -07006899}
6900
Chris Wilsondc979972016-05-10 14:10:04 +01006901static void gen9_disable_rps(struct drm_i915_private *dev_priv)
Akash Goel2030d682016-04-23 00:05:45 +05306902{
Akash Goel2030d682016-04-23 00:05:45 +05306903 I915_WRITE(GEN6_RP_CONTROL, 0);
6904}
6905
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01006906static void gen6_disable_rps(struct drm_i915_private *dev_priv)
6907{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006908 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Akash Goel2030d682016-04-23 00:05:45 +05306909 I915_WRITE(GEN6_RP_CONTROL, 0);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006910}
6911
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01006912static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
6913{
6914 I915_WRITE(GEN6_RP_CONTROL, 0);
6915}
6916
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01006917static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
6918{
6919 I915_WRITE(GEN6_RP_CONTROL, 0);
6920}
6921
Chris Wilsondc979972016-05-10 14:10:04 +01006922static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
Imre Deake6069ca2014-04-18 16:01:02 +03006923{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006924 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6925
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006926 /* All of these values are in units of 50MHz */
Chris Wilson773ea9a2016-07-13 09:10:33 +01006927
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006928 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02006929 if (IS_GEN9_LP(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006930 u32 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006931 rps->rp0_freq = (rp_state_cap >> 16) & 0xff;
6932 rps->rp1_freq = (rp_state_cap >> 8) & 0xff;
6933 rps->min_freq = (rp_state_cap >> 0) & 0xff;
Bob Paauwe35040562015-06-25 14:54:07 -07006934 } else {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006935 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006936 rps->rp0_freq = (rp_state_cap >> 0) & 0xff;
6937 rps->rp1_freq = (rp_state_cap >> 8) & 0xff;
6938 rps->min_freq = (rp_state_cap >> 16) & 0xff;
Bob Paauwe35040562015-06-25 14:54:07 -07006939 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006940 /* hw_max = RP0 until we check for overclocking */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006941 rps->max_freq = rps->rp0_freq;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006942
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006943 rps->efficient_freq = rps->rp1_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01006944 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
Oscar Mateo2b2874e2018-04-05 17:00:52 +03006945 IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006946 u32 ddcc_status = 0;
6947
6948 if (sandybridge_pcode_read(dev_priv,
6949 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
Ville Syrjäläd284d512019-05-21 19:40:24 +03006950 &ddcc_status, NULL) == 0)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006951 rps->efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08006952 clamp_t(u8,
6953 ((ddcc_status >> 8) & 0xff),
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006954 rps->min_freq,
6955 rps->max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006956 }
6957
Oscar Mateo2b2874e2018-04-05 17:00:52 +03006958 if (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
Akash Goelc5e06882015-06-29 14:50:19 +05306959 /* Store the frequency values in 16.66 MHZ units, which is
Chris Wilson773ea9a2016-07-13 09:10:33 +01006960 * the natural hardware unit for SKL
6961 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006962 rps->rp0_freq *= GEN9_FREQ_SCALER;
6963 rps->rp1_freq *= GEN9_FREQ_SCALER;
6964 rps->min_freq *= GEN9_FREQ_SCALER;
6965 rps->max_freq *= GEN9_FREQ_SCALER;
6966 rps->efficient_freq *= GEN9_FREQ_SCALER;
Akash Goelc5e06882015-06-29 14:50:19 +05306967 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006968}
6969
Chris Wilson3a45b052016-07-13 09:10:32 +01006970static void reset_rps(struct drm_i915_private *dev_priv,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006971 int (*set)(struct drm_i915_private *, u8))
Chris Wilson3a45b052016-07-13 09:10:32 +01006972{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006973 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6974 u8 freq = rps->cur_freq;
Chris Wilson3a45b052016-07-13 09:10:32 +01006975
6976 /* force a reset */
Chris Wilson60548c52018-07-31 14:26:29 +01006977 rps->power.mode = -1;
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006978 rps->cur_freq = -1;
Chris Wilson3a45b052016-07-13 09:10:32 +01006979
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006980 if (set(dev_priv, freq))
6981 DRM_ERROR("Failed to reset RPS to initial values\n");
Chris Wilson3a45b052016-07-13 09:10:32 +01006982}
6983
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006984/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Chris Wilsondc979972016-05-10 14:10:04 +01006985static void gen9_enable_rps(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00006986{
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07006987 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006988
David Weinehall36fe7782017-11-17 10:01:46 +02006989 /* Program defaults and thresholds for RPS */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08006990 if (IS_GEN(dev_priv, 9))
David Weinehall36fe7782017-11-17 10:01:46 +02006991 I915_WRITE(GEN6_RC_VIDEO_FREQ,
6992 GEN9_FREQUENCY(dev_priv->gt_pm.rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006993
Akash Goel0beb0592015-03-06 11:07:20 +05306994 /* 1 second timeout*/
6995 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
6996 GT_INTERVAL_FROM_US(dev_priv, 1000000));
6997
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006998 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006999
Akash Goel0beb0592015-03-06 11:07:20 +05307000 /* Leaning on the below call to gen6_set_rps to program/setup the
7001 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
7002 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
Chris Wilson3a45b052016-07-13 09:10:32 +01007003 reset_rps(dev_priv, gen6_set_rps);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00007004
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007005 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00007006}
7007
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01007008static void gen8_enable_rps(struct drm_i915_private *dev_priv)
7009{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007010 struct intel_rps *rps = &dev_priv->gt_pm.rps;
7011
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007012 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01007013
7014 /* 1 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07007015 I915_WRITE(GEN6_RPNSWREQ,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007016 HSW_FREQUENCY(rps->rp1_freq));
Ben Widawskyf9bdc582014-03-31 17:16:41 -07007017 I915_WRITE(GEN6_RC_VIDEO_FREQ,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007018 HSW_FREQUENCY(rps->rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02007019 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
7020 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007021
Daniel Vetter7526ed72014-09-29 15:07:19 +02007022 /* Docs recommend 900MHz, and 300 MHz respectively */
7023 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007024 rps->max_freq_softlimit << 24 |
7025 rps->min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007026
Daniel Vetter7526ed72014-09-29 15:07:19 +02007027 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
7028 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
7029 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
7030 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007031
Daniel Vetter7526ed72014-09-29 15:07:19 +02007032 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007033
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01007034 /* 2: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02007035 I915_WRITE(GEN6_RP_CONTROL,
7036 GEN6_RP_MEDIA_TURBO |
7037 GEN6_RP_MEDIA_HW_NORMAL_MODE |
7038 GEN6_RP_MEDIA_IS_GFX |
7039 GEN6_RP_ENABLE |
7040 GEN6_RP_UP_BUSY_AVG |
7041 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007042
Chris Wilson3a45b052016-07-13 09:10:32 +01007043 reset_rps(dev_priv, gen6_set_rps);
Daniel Vetter7526ed72014-09-29 15:07:19 +02007044
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007045 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007046}
7047
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01007048static void gen6_enable_rps(struct drm_i915_private *dev_priv)
7049{
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01007050 /* Here begins a magic sequence of register writes to enable
7051 * auto-downclocking.
7052 *
7053 * Perhaps there might be some value in exposing these to
7054 * userspace...
7055 */
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007056 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01007057
7058 /* Power down if completely idle for over 50ms */
7059 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
7060 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7061
7062 reset_rps(dev_priv, gen6_set_rps);
7063
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007064 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01007065}
7066
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007067static void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007068{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007069 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Mika Kuoppala66c1f772018-03-20 17:17:33 +02007070 const int min_freq = 15;
7071 const int scaling_factor = 180;
Chris Wilson3ebecd02013-04-12 19:10:13 +01007072 unsigned int gpu_freq;
7073 unsigned int max_ia_freq, min_ring_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05307074 unsigned int max_gpu_freq, min_gpu_freq;
Ben Widawskyeda79642013-10-07 17:15:48 -03007075 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007076
Chris Wilsonebb5eb72019-04-26 09:17:21 +01007077 lockdep_assert_held(&rps->lock);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02007078
Mika Kuoppala66c1f772018-03-20 17:17:33 +02007079 if (rps->max_freq <= rps->min_freq)
7080 return;
7081
Ben Widawskyeda79642013-10-07 17:15:48 -03007082 policy = cpufreq_cpu_get(0);
7083 if (policy) {
7084 max_ia_freq = policy->cpuinfo.max_freq;
7085 cpufreq_cpu_put(policy);
7086 } else {
7087 /*
7088 * Default to measured freq if none found, PCU will ensure we
7089 * don't go over
7090 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007091 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03007092 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007093
7094 /* Convert from kHz to MHz */
7095 max_ia_freq /= 1000;
7096
Ben Widawsky153b4b952013-10-22 22:05:09 -07007097 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07007098 /* convert DDR frequency from units of 266.6MHz to bandwidth */
7099 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01007100
Chris Wilsond586b5f2018-03-08 14:26:48 +00007101 min_gpu_freq = rps->min_freq;
7102 max_gpu_freq = rps->max_freq;
Oscar Mateo2b2874e2018-04-05 17:00:52 +03007103 if (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
Akash Goel4c8c7742015-06-29 14:50:20 +05307104 /* Convert GT frequency to 50 HZ units */
Chris Wilsond586b5f2018-03-08 14:26:48 +00007105 min_gpu_freq /= GEN9_FREQ_SCALER;
7106 max_gpu_freq /= GEN9_FREQ_SCALER;
Akash Goel4c8c7742015-06-29 14:50:20 +05307107 }
7108
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007109 /*
7110 * For each potential GPU frequency, load a ring frequency we'd like
7111 * to use for memory access. We do this by specifying the IA frequency
7112 * the PCU should use as a reference to determine the ring frequency.
7113 */
Akash Goel4c8c7742015-06-29 14:50:20 +05307114 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
Mika Kuoppala66c1f772018-03-20 17:17:33 +02007115 const int diff = max_gpu_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01007116 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007117
Oscar Mateo2b2874e2018-04-05 17:00:52 +03007118 if (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
Akash Goel4c8c7742015-06-29 14:50:20 +05307119 /*
7120 * ring_freq = 2 * GT. ring_freq is in 100MHz units
7121 * No floor required for ring frequency on SKL.
7122 */
7123 ring_freq = gpu_freq;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00007124 } else if (INTEL_GEN(dev_priv) >= 8) {
Ben Widawsky46c764d2013-11-02 21:07:49 -07007125 /* max(2 * GT, DDR). NB: GT is 50MHz units */
7126 ring_freq = max(min_ring_freq, gpu_freq);
Chris Wilsondc979972016-05-10 14:10:04 +01007127 } else if (IS_HASWELL(dev_priv)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07007128 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01007129 ring_freq = max(min_ring_freq, ring_freq);
7130 /* leave ia_freq as the default, chosen by cpufreq */
7131 } else {
7132 /* On older processors, there is no separate ring
7133 * clock domain, so in order to boost the bandwidth
7134 * of the ring, we need to upclock the CPU (ia_freq).
7135 *
7136 * For GPU frequencies less than 750MHz,
7137 * just use the lowest ring freq.
7138 */
7139 if (gpu_freq < min_freq)
7140 ia_freq = 800;
7141 else
7142 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
7143 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
7144 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007145
Ben Widawsky42c05262012-09-26 10:34:00 -07007146 sandybridge_pcode_write(dev_priv,
7147 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01007148 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
7149 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
7150 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007151 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007152}
7153
Ville Syrjälä03af2042014-06-28 02:03:53 +03007154static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05307155{
7156 u32 val, rp0;
7157
Jani Nikula5b5929c2015-10-07 11:17:46 +03007158 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05307159
Jani Nikula02584042018-12-31 16:56:41 +02007160 switch (RUNTIME_INFO(dev_priv)->sseu.eu_total) {
Jani Nikula5b5929c2015-10-07 11:17:46 +03007161 case 8:
7162 /* (2 * 4) config */
7163 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
7164 break;
7165 case 12:
7166 /* (2 * 6) config */
7167 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
7168 break;
7169 case 16:
7170 /* (2 * 8) config */
7171 default:
7172 /* Setting (2 * 8) Min RP0 for any other combination */
7173 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
7174 break;
Deepak S095acd52015-01-17 11:05:59 +05307175 }
Jani Nikula5b5929c2015-10-07 11:17:46 +03007176
7177 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
7178
Deepak S2b6b3a02014-05-27 15:59:30 +05307179 return rp0;
7180}
7181
7182static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
7183{
7184 u32 val, rpe;
7185
7186 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
7187 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
7188
7189 return rpe;
7190}
7191
Deepak S7707df42014-07-12 18:46:14 +05307192static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
7193{
7194 u32 val, rp1;
7195
Jani Nikula5b5929c2015-10-07 11:17:46 +03007196 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
7197 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
7198
Deepak S7707df42014-07-12 18:46:14 +05307199 return rp1;
7200}
7201
Deepak S96676fe2016-08-12 18:46:41 +05307202static u32 cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
7203{
7204 u32 val, rpn;
7205
7206 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
7207 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
7208 FB_GFX_FREQ_FUSE_MASK);
7209
7210 return rpn;
7211}
7212
Deepak Sf8f2b002014-07-10 13:16:21 +05307213static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
7214{
7215 u32 val, rp1;
7216
7217 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
7218
7219 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
7220
7221 return rp1;
7222}
7223
Ville Syrjälä03af2042014-06-28 02:03:53 +03007224static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07007225{
7226 u32 val, rp0;
7227
Jani Nikula64936252013-05-22 15:36:20 +03007228 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007229
7230 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
7231 /* Clamp to max */
7232 rp0 = min_t(u32, rp0, 0xea);
7233
7234 return rp0;
7235}
7236
7237static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
7238{
7239 u32 val, rpe;
7240
Jani Nikula64936252013-05-22 15:36:20 +03007241 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007242 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03007243 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007244 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
7245
7246 return rpe;
7247}
7248
Ville Syrjälä03af2042014-06-28 02:03:53 +03007249static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07007250{
Imre Deak36146032014-12-04 18:39:35 +02007251 u32 val;
7252
7253 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
7254 /*
7255 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
7256 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
7257 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
7258 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
7259 * to make sure it matches what Punit accepts.
7260 */
7261 return max_t(u32, val, 0xc0);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007262}
7263
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007264static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
7265{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007266 dev_priv->gt_pm.rps.gpll_ref_freq =
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007267 vlv_get_cck_clock(dev_priv, "GPLL ref",
7268 CCK_GPLL_CLOCK_CONTROL,
7269 dev_priv->czclk_freq);
7270
7271 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007272 dev_priv->gt_pm.rps.gpll_ref_freq);
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007273}
7274
Chris Wilsondc979972016-05-10 14:10:04 +01007275static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03007276{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007277 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007278 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03007279
Chris Wilson337fa6e2019-04-26 09:17:20 +01007280 vlv_iosf_sb_get(dev_priv,
7281 BIT(VLV_IOSF_SB_PUNIT) |
7282 BIT(VLV_IOSF_SB_NC) |
7283 BIT(VLV_IOSF_SB_CCK));
7284
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007285 vlv_init_gpll_ref_freq(dev_priv);
7286
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007287 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
7288 switch ((val >> 6) & 3) {
7289 case 0:
7290 case 1:
7291 dev_priv->mem_freq = 800;
7292 break;
7293 case 2:
7294 dev_priv->mem_freq = 1066;
7295 break;
7296 case 3:
7297 dev_priv->mem_freq = 1333;
7298 break;
7299 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02007300 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007301
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007302 rps->max_freq = valleyview_rps_max_freq(dev_priv);
7303 rps->rp0_freq = rps->max_freq;
Imre Deak4e805192014-04-14 20:24:41 +03007304 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007305 intel_gpu_freq(dev_priv, rps->max_freq),
7306 rps->max_freq);
Imre Deak4e805192014-04-14 20:24:41 +03007307
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007308 rps->efficient_freq = valleyview_rps_rpe_freq(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007309 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007310 intel_gpu_freq(dev_priv, rps->efficient_freq),
7311 rps->efficient_freq);
Imre Deak4e805192014-04-14 20:24:41 +03007312
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007313 rps->rp1_freq = valleyview_rps_guar_freq(dev_priv);
Deepak Sf8f2b002014-07-10 13:16:21 +05307314 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007315 intel_gpu_freq(dev_priv, rps->rp1_freq),
7316 rps->rp1_freq);
Deepak Sf8f2b002014-07-10 13:16:21 +05307317
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007318 rps->min_freq = valleyview_rps_min_freq(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007319 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007320 intel_gpu_freq(dev_priv, rps->min_freq),
7321 rps->min_freq);
Chris Wilson337fa6e2019-04-26 09:17:20 +01007322
7323 vlv_iosf_sb_put(dev_priv,
7324 BIT(VLV_IOSF_SB_PUNIT) |
7325 BIT(VLV_IOSF_SB_NC) |
7326 BIT(VLV_IOSF_SB_CCK));
Imre Deak4e805192014-04-14 20:24:41 +03007327}
7328
Chris Wilsondc979972016-05-10 14:10:04 +01007329static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05307330{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007331 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007332 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05307333
Chris Wilson337fa6e2019-04-26 09:17:20 +01007334 vlv_iosf_sb_get(dev_priv,
7335 BIT(VLV_IOSF_SB_PUNIT) |
7336 BIT(VLV_IOSF_SB_NC) |
7337 BIT(VLV_IOSF_SB_CCK));
7338
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007339 vlv_init_gpll_ref_freq(dev_priv);
7340
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02007341 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02007342
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007343 switch ((val >> 2) & 0x7) {
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007344 case 3:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007345 dev_priv->mem_freq = 2000;
7346 break;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03007347 default:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007348 dev_priv->mem_freq = 1600;
7349 break;
7350 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02007351 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007352
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007353 rps->max_freq = cherryview_rps_max_freq(dev_priv);
7354 rps->rp0_freq = rps->max_freq;
Deepak S2b6b3a02014-05-27 15:59:30 +05307355 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007356 intel_gpu_freq(dev_priv, rps->max_freq),
7357 rps->max_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05307358
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007359 rps->efficient_freq = cherryview_rps_rpe_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307360 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007361 intel_gpu_freq(dev_priv, rps->efficient_freq),
7362 rps->efficient_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05307363
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007364 rps->rp1_freq = cherryview_rps_guar_freq(dev_priv);
Deepak S7707df42014-07-12 18:46:14 +05307365 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007366 intel_gpu_freq(dev_priv, rps->rp1_freq),
7367 rps->rp1_freq);
Deepak S7707df42014-07-12 18:46:14 +05307368
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007369 rps->min_freq = cherryview_rps_min_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307370 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007371 intel_gpu_freq(dev_priv, rps->min_freq),
7372 rps->min_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05307373
Chris Wilson337fa6e2019-04-26 09:17:20 +01007374 vlv_iosf_sb_put(dev_priv,
7375 BIT(VLV_IOSF_SB_PUNIT) |
7376 BIT(VLV_IOSF_SB_NC) |
7377 BIT(VLV_IOSF_SB_CCK));
7378
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007379 WARN_ONCE((rps->max_freq | rps->efficient_freq | rps->rp1_freq |
7380 rps->min_freq) & 1,
Ville Syrjälä1c147622014-08-18 14:42:43 +03007381 "Odd GPU freq values\n");
Deepak S38807742014-05-23 21:00:15 +05307382}
7383
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007384static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
7385{
7386 u32 val;
7387
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007388 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007389
7390 /* 1: Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02007391 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05307392 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
7393 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
7394 I915_WRITE(GEN6_RP_UP_EI, 66000);
7395 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
7396
7397 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7398
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007399 /* 2: Enable RPS */
Deepak S2b6b3a02014-05-27 15:59:30 +05307400 I915_WRITE(GEN6_RP_CONTROL,
7401 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02007402 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05307403 GEN6_RP_ENABLE |
7404 GEN6_RP_UP_BUSY_AVG |
7405 GEN6_RP_DOWN_IDLE_AVG);
7406
Deepak S3ef62342015-04-29 08:36:24 +05307407 /* Setting Fixed Bias */
Chris Wilson337fa6e2019-04-26 09:17:20 +01007408 vlv_punit_get(dev_priv);
7409
7410 val = VLV_OVERRIDE_EN | VLV_SOC_TDP_EN | CHV_BIAS_CPU_50_SOC_50;
Deepak S3ef62342015-04-29 08:36:24 +05307411 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
7412
Deepak S2b6b3a02014-05-27 15:59:30 +05307413 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
7414
Chris Wilson337fa6e2019-04-26 09:17:20 +01007415 vlv_punit_put(dev_priv);
7416
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02007417 /* RPS code assumes GPLL is used */
7418 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
7419
Jani Nikula742f4912015-09-03 11:16:09 +03007420 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05307421 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7422
Chris Wilson3a45b052016-07-13 09:10:32 +01007423 reset_rps(dev_priv, valleyview_set_rps);
Deepak S2b6b3a02014-05-27 15:59:30 +05307424
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007425 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05307426}
7427
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01007428static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
7429{
7430 u32 val;
7431
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007432 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01007433
Ville Syrjäläcad725f2015-01-19 13:50:48 +02007434 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007435 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
7436 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
7437 I915_WRITE(GEN6_RP_UP_EI, 66000);
7438 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
7439
7440 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7441
7442 I915_WRITE(GEN6_RP_CONTROL,
7443 GEN6_RP_MEDIA_TURBO |
7444 GEN6_RP_MEDIA_HW_NORMAL_MODE |
7445 GEN6_RP_MEDIA_IS_GFX |
7446 GEN6_RP_ENABLE |
7447 GEN6_RP_UP_BUSY_AVG |
7448 GEN6_RP_DOWN_IDLE_CONT);
7449
Chris Wilson337fa6e2019-04-26 09:17:20 +01007450 vlv_punit_get(dev_priv);
7451
Deepak S3ef62342015-04-29 08:36:24 +05307452 /* Setting Fixed Bias */
Chris Wilson337fa6e2019-04-26 09:17:20 +01007453 val = VLV_OVERRIDE_EN | VLV_SOC_TDP_EN | VLV_BIAS_CPU_125_SOC_875;
Deepak S3ef62342015-04-29 08:36:24 +05307454 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
7455
Jani Nikula64936252013-05-22 15:36:20 +03007456 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007457
Chris Wilson337fa6e2019-04-26 09:17:20 +01007458 vlv_punit_put(dev_priv);
7459
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02007460 /* RPS code assumes GPLL is used */
7461 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
7462
Jani Nikula742f4912015-09-03 11:16:09 +03007463 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07007464 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7465
Chris Wilson3a45b052016-07-13 09:10:32 +01007466 reset_rps(dev_priv, valleyview_set_rps);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007467
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007468 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007469}
7470
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007471static unsigned long intel_pxfreq(u32 vidfreq)
7472{
7473 unsigned long freq;
7474 int div = (vidfreq & 0x3f0000) >> 16;
7475 int post = (vidfreq & 0x3000) >> 12;
7476 int pre = (vidfreq & 0x7);
7477
7478 if (!pre)
7479 return 0;
7480
7481 freq = ((div * 133333) / ((1<<post) * pre));
7482
7483 return freq;
7484}
7485
Daniel Vettereb48eb02012-04-26 23:28:12 +02007486static const struct cparams {
7487 u16 i;
7488 u16 t;
7489 u16 m;
7490 u16 c;
7491} cparams[] = {
7492 { 1, 1333, 301, 28664 },
7493 { 1, 1066, 294, 24460 },
7494 { 1, 800, 294, 25192 },
7495 { 0, 1333, 276, 27605 },
7496 { 0, 1066, 276, 27605 },
7497 { 0, 800, 231, 23784 },
7498};
7499
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007500static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007501{
7502 u64 total_count, diff, ret;
7503 u32 count1, count2, count3, m = 0, c = 0;
7504 unsigned long now = jiffies_to_msecs(jiffies), diff1;
7505 int i;
7506
Chris Wilson67520412017-03-02 13:28:01 +00007507 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007508
Daniel Vetter20e4d402012-08-08 23:35:39 +02007509 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007510
7511 /* Prevent division-by-zero if we are asking too fast.
7512 * Also, we don't get interesting results if we are polling
7513 * faster than once in 10ms, so just return the saved value
7514 * in such cases.
7515 */
7516 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02007517 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007518
7519 count1 = I915_READ(DMIEC);
7520 count2 = I915_READ(DDREC);
7521 count3 = I915_READ(CSIEC);
7522
7523 total_count = count1 + count2 + count3;
7524
7525 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02007526 if (total_count < dev_priv->ips.last_count1) {
7527 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007528 diff += total_count;
7529 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007530 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007531 }
7532
7533 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007534 if (cparams[i].i == dev_priv->ips.c_m &&
7535 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02007536 m = cparams[i].m;
7537 c = cparams[i].c;
7538 break;
7539 }
7540 }
7541
7542 diff = div_u64(diff, diff1);
7543 ret = ((m * diff) + c);
7544 ret = div_u64(ret, 10);
7545
Daniel Vetter20e4d402012-08-08 23:35:39 +02007546 dev_priv->ips.last_count1 = total_count;
7547 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007548
Daniel Vetter20e4d402012-08-08 23:35:39 +02007549 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007550
7551 return ret;
7552}
7553
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007554unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
7555{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007556 intel_wakeref_t wakeref;
7557 unsigned long val = 0;
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007558
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007559 if (!IS_GEN(dev_priv, 5))
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007560 return 0;
7561
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -07007562 with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref) {
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007563 spin_lock_irq(&mchdev_lock);
7564 val = __i915_chipset_val(dev_priv);
7565 spin_unlock_irq(&mchdev_lock);
7566 }
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007567
7568 return val;
7569}
7570
Tvrtko Ursulinc54f0ba2019-06-11 11:45:43 +01007571unsigned long i915_mch_val(struct drm_i915_private *i915)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007572{
7573 unsigned long m, x, b;
7574 u32 tsfs;
7575
Tvrtko Ursulinc54f0ba2019-06-11 11:45:43 +01007576 tsfs = intel_uncore_read(&i915->uncore, TSFS);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007577
7578 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
Tvrtko Ursulinc54f0ba2019-06-11 11:45:43 +01007579 x = intel_uncore_read8(&i915->uncore, TR1);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007580
7581 b = tsfs & TSFS_INTR_MASK;
7582
7583 return ((m * x) / 127) - b;
7584}
7585
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007586static int _pxvid_to_vd(u8 pxvid)
7587{
7588 if (pxvid == 0)
7589 return 0;
7590
7591 if (pxvid >= 8 && pxvid < 31)
7592 pxvid = 31;
7593
7594 return (pxvid + 2) * 125;
7595}
7596
7597static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007598{
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007599 const int vd = _pxvid_to_vd(pxvid);
7600 const int vm = vd - 1125;
7601
Chris Wilsondc979972016-05-10 14:10:04 +01007602 if (INTEL_INFO(dev_priv)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007603 return vm > 0 ? vm : 0;
7604
7605 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007606}
7607
Daniel Vetter02d71952012-08-09 16:44:54 +02007608static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007609{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00007610 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007611 u32 count;
7612
Chris Wilson67520412017-03-02 13:28:01 +00007613 lockdep_assert_held(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007614
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00007615 now = ktime_get_raw_ns();
7616 diffms = now - dev_priv->ips.last_time2;
7617 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007618
7619 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02007620 if (!diffms)
7621 return;
7622
7623 count = I915_READ(GFXEC);
7624
Daniel Vetter20e4d402012-08-08 23:35:39 +02007625 if (count < dev_priv->ips.last_count2) {
7626 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007627 diff += count;
7628 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007629 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007630 }
7631
Daniel Vetter20e4d402012-08-08 23:35:39 +02007632 dev_priv->ips.last_count2 = count;
7633 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007634
7635 /* More magic constants... */
7636 diff = diff * 1181;
7637 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02007638 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007639}
7640
Daniel Vetter02d71952012-08-09 16:44:54 +02007641void i915_update_gfx_val(struct drm_i915_private *dev_priv)
7642{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007643 intel_wakeref_t wakeref;
7644
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007645 if (!IS_GEN(dev_priv, 5))
Daniel Vetter02d71952012-08-09 16:44:54 +02007646 return;
7647
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -07007648 with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref) {
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007649 spin_lock_irq(&mchdev_lock);
7650 __i915_update_gfx_val(dev_priv);
7651 spin_unlock_irq(&mchdev_lock);
7652 }
Daniel Vetter02d71952012-08-09 16:44:54 +02007653}
7654
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007655static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007656{
7657 unsigned long t, corr, state1, corr2, state2;
7658 u32 pxvid, ext_v;
7659
Chris Wilson67520412017-03-02 13:28:01 +00007660 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007661
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007662 pxvid = I915_READ(PXVFREQ(dev_priv->gt_pm.rps.cur_freq));
Daniel Vettereb48eb02012-04-26 23:28:12 +02007663 pxvid = (pxvid >> 24) & 0x7f;
7664 ext_v = pvid_to_extvid(dev_priv, pxvid);
7665
7666 state1 = ext_v;
7667
7668 t = i915_mch_val(dev_priv);
7669
7670 /* Revel in the empirically derived constants */
7671
7672 /* Correction factor in 1/100000 units */
7673 if (t > 80)
7674 corr = ((t * 2349) + 135940);
7675 else if (t >= 50)
7676 corr = ((t * 964) + 29317);
7677 else /* < 50 */
7678 corr = ((t * 301) + 1004);
7679
7680 corr = corr * ((150142 * state1) / 10000 - 78642);
7681 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02007682 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007683
7684 state2 = (corr2 * state1) / 10000;
7685 state2 /= 100; /* convert to mW */
7686
Daniel Vetter02d71952012-08-09 16:44:54 +02007687 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007688
Daniel Vetter20e4d402012-08-08 23:35:39 +02007689 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007690}
7691
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007692unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
7693{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007694 intel_wakeref_t wakeref;
7695 unsigned long val = 0;
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007696
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007697 if (!IS_GEN(dev_priv, 5))
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007698 return 0;
7699
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -07007700 with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref) {
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007701 spin_lock_irq(&mchdev_lock);
7702 val = __i915_gfx_val(dev_priv);
7703 spin_unlock_irq(&mchdev_lock);
7704 }
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007705
7706 return val;
7707}
7708
Chris Wilsonadc674c2019-04-12 09:53:22 +01007709static struct drm_i915_private __rcu *i915_mch_dev;
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007710
7711static struct drm_i915_private *mchdev_get(void)
7712{
7713 struct drm_i915_private *i915;
7714
7715 rcu_read_lock();
Chris Wilsonadc674c2019-04-12 09:53:22 +01007716 i915 = rcu_dereference(i915_mch_dev);
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007717 if (!kref_get_unless_zero(&i915->drm.ref))
7718 i915 = NULL;
7719 rcu_read_unlock();
7720
7721 return i915;
7722}
7723
Daniel Vettereb48eb02012-04-26 23:28:12 +02007724/**
7725 * i915_read_mch_val - return value for IPS use
7726 *
7727 * Calculate and return a value for the IPS driver to use when deciding whether
7728 * we have thermal and power headroom to increase CPU or GPU power budget.
7729 */
7730unsigned long i915_read_mch_val(void)
7731{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007732 struct drm_i915_private *i915;
7733 unsigned long chipset_val = 0;
7734 unsigned long graphics_val = 0;
7735 intel_wakeref_t wakeref;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007736
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007737 i915 = mchdev_get();
7738 if (!i915)
7739 return 0;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007740
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -07007741 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007742 spin_lock_irq(&mchdev_lock);
7743 chipset_val = __i915_chipset_val(i915);
7744 graphics_val = __i915_gfx_val(i915);
7745 spin_unlock_irq(&mchdev_lock);
7746 }
Daniel Vettereb48eb02012-04-26 23:28:12 +02007747
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007748 drm_dev_put(&i915->drm);
7749 return chipset_val + graphics_val;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007750}
7751EXPORT_SYMBOL_GPL(i915_read_mch_val);
7752
7753/**
7754 * i915_gpu_raise - raise GPU frequency limit
7755 *
7756 * Raise the limit; IPS indicates we have thermal headroom.
7757 */
7758bool i915_gpu_raise(void)
7759{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007760 struct drm_i915_private *i915;
7761
7762 i915 = mchdev_get();
7763 if (!i915)
7764 return false;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007765
Daniel Vetter92703882012-08-09 16:46:01 +02007766 spin_lock_irq(&mchdev_lock);
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007767 if (i915->ips.max_delay > i915->ips.fmax)
7768 i915->ips.max_delay--;
Daniel Vetter92703882012-08-09 16:46:01 +02007769 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007770
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007771 drm_dev_put(&i915->drm);
7772 return true;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007773}
7774EXPORT_SYMBOL_GPL(i915_gpu_raise);
7775
7776/**
7777 * i915_gpu_lower - lower GPU frequency limit
7778 *
7779 * IPS indicates we're close to a thermal limit, so throttle back the GPU
7780 * frequency maximum.
7781 */
7782bool i915_gpu_lower(void)
7783{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007784 struct drm_i915_private *i915;
7785
7786 i915 = mchdev_get();
7787 if (!i915)
7788 return false;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007789
Daniel Vetter92703882012-08-09 16:46:01 +02007790 spin_lock_irq(&mchdev_lock);
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007791 if (i915->ips.max_delay < i915->ips.min_delay)
7792 i915->ips.max_delay++;
Daniel Vetter92703882012-08-09 16:46:01 +02007793 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007794
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007795 drm_dev_put(&i915->drm);
7796 return true;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007797}
7798EXPORT_SYMBOL_GPL(i915_gpu_lower);
7799
7800/**
7801 * i915_gpu_busy - indicate GPU business to IPS
7802 *
7803 * Tell the IPS driver whether or not the GPU is busy.
7804 */
7805bool i915_gpu_busy(void)
7806{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007807 struct drm_i915_private *i915;
7808 bool ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007809
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007810 i915 = mchdev_get();
7811 if (!i915)
7812 return false;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007813
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007814 ret = i915->gt.awake;
7815
7816 drm_dev_put(&i915->drm);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007817 return ret;
7818}
7819EXPORT_SYMBOL_GPL(i915_gpu_busy);
7820
7821/**
7822 * i915_gpu_turbo_disable - disable graphics turbo
7823 *
7824 * Disable graphics turbo by resetting the max frequency and setting the
7825 * current frequency to the default.
7826 */
7827bool i915_gpu_turbo_disable(void)
7828{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007829 struct drm_i915_private *i915;
7830 bool ret;
7831
7832 i915 = mchdev_get();
7833 if (!i915)
7834 return false;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007835
Daniel Vetter92703882012-08-09 16:46:01 +02007836 spin_lock_irq(&mchdev_lock);
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007837 i915->ips.max_delay = i915->ips.fstart;
7838 ret = ironlake_set_drps(i915, i915->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02007839 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007840
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007841 drm_dev_put(&i915->drm);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007842 return ret;
7843}
7844EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
7845
7846/**
7847 * Tells the intel_ips driver that the i915 driver is now loaded, if
7848 * IPS got loaded first.
7849 *
7850 * This awkward dance is so that neither module has to depend on the
7851 * other in order for IPS to do the appropriate communication of
7852 * GPU turbo limits to i915.
7853 */
7854static void
7855ips_ping_for_i915_load(void)
7856{
7857 void (*link)(void);
7858
7859 link = symbol_get(ips_link_to_i915_driver);
7860 if (link) {
7861 link();
7862 symbol_put(ips_link_to_i915_driver);
7863 }
7864}
7865
7866void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
7867{
Daniel Vetter02d71952012-08-09 16:44:54 +02007868 /* We only register the i915 ips part with intel-ips once everything is
7869 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007870 rcu_assign_pointer(i915_mch_dev, dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007871
7872 ips_ping_for_i915_load();
7873}
7874
7875void intel_gpu_ips_teardown(void)
7876{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007877 rcu_assign_pointer(i915_mch_dev, NULL);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007878}
Deepak S76c3552f2014-01-30 23:08:16 +05307879
Chris Wilsondc979972016-05-10 14:10:04 +01007880static void intel_init_emon(struct drm_i915_private *dev_priv)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007881{
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007882 u32 lcfuse;
7883 u8 pxw[16];
7884 int i;
7885
7886 /* Disable to program */
7887 I915_WRITE(ECR, 0);
7888 POSTING_READ(ECR);
7889
7890 /* Program energy weights for various events */
7891 I915_WRITE(SDEW, 0x15040d00);
7892 I915_WRITE(CSIEW0, 0x007f0000);
7893 I915_WRITE(CSIEW1, 0x1e220004);
7894 I915_WRITE(CSIEW2, 0x04000004);
7895
7896 for (i = 0; i < 5; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007897 I915_WRITE(PEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007898 for (i = 0; i < 3; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007899 I915_WRITE(DEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007900
7901 /* Program P-state weights to account for frequency power adjustment */
7902 for (i = 0; i < 16; i++) {
Ville Syrjälä616847e2015-09-18 20:03:19 +03007903 u32 pxvidfreq = I915_READ(PXVFREQ(i));
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007904 unsigned long freq = intel_pxfreq(pxvidfreq);
7905 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
7906 PXVFREQ_PX_SHIFT;
7907 unsigned long val;
7908
7909 val = vid * vid;
7910 val *= (freq / 1000);
7911 val *= 255;
7912 val /= (127*127*900);
7913 if (val > 0xff)
7914 DRM_ERROR("bad pxval: %ld\n", val);
7915 pxw[i] = val;
7916 }
7917 /* Render standby states get 0 weight */
7918 pxw[14] = 0;
7919 pxw[15] = 0;
7920
7921 for (i = 0; i < 4; i++) {
7922 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
7923 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
Ville Syrjälä616847e2015-09-18 20:03:19 +03007924 I915_WRITE(PXW(i), val);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007925 }
7926
7927 /* Adjust magic regs to magic values (more experimental results) */
7928 I915_WRITE(OGW0, 0);
7929 I915_WRITE(OGW1, 0);
7930 I915_WRITE(EG0, 0x00007f00);
7931 I915_WRITE(EG1, 0x0000000e);
7932 I915_WRITE(EG2, 0x000e0000);
7933 I915_WRITE(EG3, 0x68000300);
7934 I915_WRITE(EG4, 0x42000000);
7935 I915_WRITE(EG5, 0x00140031);
7936 I915_WRITE(EG6, 0);
7937 I915_WRITE(EG7, 0);
7938
7939 for (i = 0; i < 8; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007940 I915_WRITE(PXWL(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007941
7942 /* Enable PMON + select events */
7943 I915_WRITE(ECR, 0x80000019);
7944
7945 lcfuse = I915_READ(LCFUSE02);
7946
Daniel Vetter20e4d402012-08-08 23:35:39 +02007947 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007948}
7949
Chris Wilsondc979972016-05-10 14:10:04 +01007950void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007951{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007952 struct intel_rps *rps = &dev_priv->gt_pm.rps;
7953
Andi Shytic1132362019-09-27 12:08:49 +01007954 /* Powersaving is controlled by the host when inside a VM */
7955 if (intel_vgpu_active(dev_priv))
7956 mkwrite_device_info(dev_priv)->has_rps = false;
Imre Deake6069ca2014-04-18 16:01:02 +03007957
Chris Wilson773ea9a2016-07-13 09:10:33 +01007958 /* Initialize RPS limits (for userspace) */
Chris Wilsondc979972016-05-10 14:10:04 +01007959 if (IS_CHERRYVIEW(dev_priv))
7960 cherryview_init_gt_powersave(dev_priv);
7961 else if (IS_VALLEYVIEW(dev_priv))
7962 valleyview_init_gt_powersave(dev_priv);
Chris Wilson2a13ae72016-08-02 11:15:27 +01007963 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson773ea9a2016-07-13 09:10:33 +01007964 gen6_init_rps_frequencies(dev_priv);
7965
7966 /* Derive initial user preferences/limits from the hardware limits */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007967 rps->max_freq_softlimit = rps->max_freq;
7968 rps->min_freq_softlimit = rps->min_freq;
Chris Wilson773ea9a2016-07-13 09:10:33 +01007969
Chris Wilson99ac9612016-07-13 09:10:34 +01007970 /* After setting max-softlimit, find the overclock max freq */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007971 if (IS_GEN(dev_priv, 6) ||
Chris Wilson99ac9612016-07-13 09:10:34 +01007972 IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
7973 u32 params = 0;
7974
Ville Syrjäläd284d512019-05-21 19:40:24 +03007975 sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS,
7976 &params, NULL);
Chris Wilson99ac9612016-07-13 09:10:34 +01007977 if (params & BIT(31)) { /* OC supported */
7978 DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007979 (rps->max_freq & 0xff) * 50,
Chris Wilson99ac9612016-07-13 09:10:34 +01007980 (params & 0xff) * 50);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007981 rps->max_freq = params & 0xff;
Chris Wilson99ac9612016-07-13 09:10:34 +01007982 }
7983 }
7984
Chris Wilson29ecd78d2016-07-13 09:10:35 +01007985 /* Finally allow us to boost to max by default */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007986 rps->boost_freq = rps->max_freq;
Chris Wilson844e3312019-04-18 21:53:58 +01007987 rps->idle_freq = rps->min_freq;
7988 rps->cur_freq = rps->idle_freq;
Imre Deakae484342014-03-31 15:10:44 +03007989}
7990
Chris Wilsonb7137e02016-07-13 09:10:37 +01007991void intel_sanitize_gt_powersave(struct drm_i915_private *dev_priv)
7992{
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01007993 dev_priv->gt_pm.rps.enabled = true; /* force RPS disabling */
Chris Wilsonb7137e02016-07-13 09:10:37 +01007994 intel_disable_gt_powersave(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007995
Oscar Mateod02b98b2018-04-05 17:00:50 +03007996 if (INTEL_GEN(dev_priv) >= 11)
7997 gen11_reset_rps_interrupts(dev_priv);
Chris Wilson61e1e372018-08-12 23:36:30 +01007998 else if (INTEL_GEN(dev_priv) >= 6)
Oscar Mateod02b98b2018-04-05 17:00:50 +03007999 gen6_reset_rps_interrupts(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07008000}
8001
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008002static inline void intel_disable_llc_pstate(struct drm_i915_private *i915)
8003{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008004 lockdep_assert_held(&i915->gt_pm.rps.lock);
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008005
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008006 if (!i915->gt_pm.llc_pstate.enabled)
8007 return;
8008
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008009 /* Currently there is no HW configuration to be done to disable. */
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008010
8011 i915->gt_pm.llc_pstate.enabled = false;
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008012}
8013
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008014static void intel_disable_rps(struct drm_i915_private *dev_priv)
8015{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008016 lockdep_assert_held(&dev_priv->gt_pm.rps.lock);
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008017
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008018 if (!dev_priv->gt_pm.rps.enabled)
8019 return;
8020
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008021 if (INTEL_GEN(dev_priv) >= 9)
8022 gen9_disable_rps(dev_priv);
8023 else if (IS_CHERRYVIEW(dev_priv))
8024 cherryview_disable_rps(dev_priv);
8025 else if (IS_VALLEYVIEW(dev_priv))
8026 valleyview_disable_rps(dev_priv);
8027 else if (INTEL_GEN(dev_priv) >= 6)
8028 gen6_disable_rps(dev_priv);
8029 else if (IS_IRONLAKE_M(dev_priv))
8030 ironlake_disable_drps(dev_priv);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008031
8032 dev_priv->gt_pm.rps.enabled = false;
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008033}
8034
Chris Wilsondc979972016-05-10 14:10:04 +01008035void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
Daniel Vetter8090c6b2012-06-24 16:42:32 +02008036{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008037 mutex_lock(&dev_priv->gt_pm.rps.lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07008038
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008039 intel_disable_rps(dev_priv);
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008040 if (HAS_LLC(dev_priv))
8041 intel_disable_llc_pstate(dev_priv);
8042
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008043 mutex_unlock(&dev_priv->gt_pm.rps.lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01008044}
8045
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008046static inline void intel_enable_llc_pstate(struct drm_i915_private *i915)
8047{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008048 lockdep_assert_held(&i915->gt_pm.rps.lock);
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008049
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008050 if (i915->gt_pm.llc_pstate.enabled)
8051 return;
8052
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008053 gen6_update_ring_freq(i915);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008054
8055 i915->gt_pm.llc_pstate.enabled = true;
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008056}
8057
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008058static void intel_enable_rps(struct drm_i915_private *dev_priv)
8059{
8060 struct intel_rps *rps = &dev_priv->gt_pm.rps;
8061
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008062 lockdep_assert_held(&rps->lock);
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008063
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008064 if (rps->enabled)
8065 return;
8066
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008067 if (IS_CHERRYVIEW(dev_priv)) {
8068 cherryview_enable_rps(dev_priv);
8069 } else if (IS_VALLEYVIEW(dev_priv)) {
8070 valleyview_enable_rps(dev_priv);
8071 } else if (INTEL_GEN(dev_priv) >= 9) {
8072 gen9_enable_rps(dev_priv);
8073 } else if (IS_BROADWELL(dev_priv)) {
8074 gen8_enable_rps(dev_priv);
8075 } else if (INTEL_GEN(dev_priv) >= 6) {
8076 gen6_enable_rps(dev_priv);
8077 } else if (IS_IRONLAKE_M(dev_priv)) {
8078 ironlake_enable_drps(dev_priv);
8079 intel_init_emon(dev_priv);
8080 }
8081
8082 WARN_ON(rps->max_freq < rps->min_freq);
8083 WARN_ON(rps->idle_freq > rps->max_freq);
8084
8085 WARN_ON(rps->efficient_freq < rps->min_freq);
8086 WARN_ON(rps->efficient_freq > rps->max_freq);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008087
8088 rps->enabled = true;
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008089}
8090
Chris Wilsonb7137e02016-07-13 09:10:37 +01008091void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
8092{
Chris Wilsonb7137e02016-07-13 09:10:37 +01008093 /* Powersaving is controlled by the host when inside a VM */
8094 if (intel_vgpu_active(dev_priv))
8095 return;
8096
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008097 mutex_lock(&dev_priv->gt_pm.rps.lock);
Imre Deak3cc134e2014-11-19 15:30:03 +02008098
Chris Wilson91cbdb82019-04-19 14:48:36 +01008099 if (HAS_RPS(dev_priv))
8100 intel_enable_rps(dev_priv);
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008101 if (HAS_LLC(dev_priv))
8102 intel_enable_llc_pstate(dev_priv);
8103
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008104 mutex_unlock(&dev_priv->gt_pm.rps.lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01008105}
Imre Deakc6df39b2014-04-14 20:24:29 +03008106
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008107static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01008108{
Daniel Vetter3107bd42012-10-31 22:52:31 +01008109 /*
8110 * On Ibex Peak and Cougar Point, we need to disable clock
8111 * gating for the panel power sequencer or it will fail to
8112 * start up when no ports are active.
8113 */
8114 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
8115}
8116
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008117static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008118{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03008119 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008120
Damien Lespiau055e3932014-08-18 13:49:10 +01008121 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008122 I915_WRITE(DSPCNTR(pipe),
8123 I915_READ(DSPCNTR(pipe)) |
8124 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03008125
8126 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
8127 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008128 }
8129}
8130
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008131static void ilk_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008132{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008133 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008134
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01008135 /*
8136 * Required for FBC
8137 * WaFbcDisableDpfcClockGating:ilk
8138 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008139 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
8140 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
8141 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008142
8143 I915_WRITE(PCH_3DCGDIS0,
8144 MARIUNIT_CLOCK_GATE_DISABLE |
8145 SVSMUNIT_CLOCK_GATE_DISABLE);
8146 I915_WRITE(PCH_3DCGDIS1,
8147 VFMUNIT_CLOCK_GATE_DISABLE);
8148
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008149 /*
8150 * According to the spec the following bits should be set in
8151 * order to enable memory self-refresh
8152 * The bit 22/21 of 0x42004
8153 * The bit 5 of 0x42020
8154 * The bit 15 of 0x45000
8155 */
8156 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8157 (I915_READ(ILK_DISPLAY_CHICKEN2) |
8158 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008159 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008160 I915_WRITE(DISP_ARB_CTL,
8161 (I915_READ(DISP_ARB_CTL) |
8162 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02008163
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008164 /*
8165 * Based on the document from hardware guys the following bits
8166 * should be set unconditionally in order to enable FBC.
8167 * The bit 22 of 0x42000
8168 * The bit 22 of 0x42004
8169 * The bit 7,8,9 of 0x42020.
8170 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008171 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01008172 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008173 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8174 I915_READ(ILK_DISPLAY_CHICKEN1) |
8175 ILK_FBCQ_DIS);
8176 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8177 I915_READ(ILK_DISPLAY_CHICKEN2) |
8178 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008179 }
8180
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008181 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
8182
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008183 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8184 I915_READ(ILK_DISPLAY_CHICKEN2) |
8185 ILK_ELPIN_409_SELECT);
8186 I915_WRITE(_3D_CHICKEN2,
8187 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
8188 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02008189
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008190 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02008191 I915_WRITE(CACHE_MODE_0,
8192 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01008193
Akash Goel4e046322014-04-04 17:14:38 +05308194 /* WaDisable_RenderCache_OperationalFlush:ilk */
8195 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8196
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008197 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03008198
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008199 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008200}
8201
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008202static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01008203{
Ville Syrjäläd048a262019-08-21 20:30:31 +03008204 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008205 u32 val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01008206
8207 /*
8208 * On Ibex Peak and Cougar Point, we need to disable clock
8209 * gating for the panel power sequencer or it will fail to
8210 * start up when no ports are active.
8211 */
Jesse Barnescd664072013-10-02 10:34:19 -07008212 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
8213 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
8214 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008215 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
8216 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01008217 /* The below fixes the weird display corruption, a few pixels shifted
8218 * downward, on (only) LVDS of some HP laptops with IVY.
8219 */
Damien Lespiau055e3932014-08-18 13:49:10 +01008220 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03008221 val = I915_READ(TRANS_CHICKEN2(pipe));
8222 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
8223 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008224 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008225 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03008226 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
8227 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
8228 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008229 I915_WRITE(TRANS_CHICKEN2(pipe), val);
8230 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01008231 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01008232 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01008233 I915_WRITE(TRANS_CHICKEN1(pipe),
8234 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
8235 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008236}
8237
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008238static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008239{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008240 u32 tmp;
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008241
8242 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02008243 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
8244 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
8245 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008246}
8247
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008248static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008249{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008250 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008251
Damien Lespiau231e54f2012-10-19 17:55:41 +01008252 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008253
8254 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8255 I915_READ(ILK_DISPLAY_CHICKEN2) |
8256 ILK_ELPIN_409_SELECT);
8257
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008258 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01008259 I915_WRITE(_3D_CHICKEN,
8260 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
8261
Akash Goel4e046322014-04-04 17:14:38 +05308262 /* WaDisable_RenderCache_OperationalFlush:snb */
8263 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8264
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008265 /*
8266 * BSpec recoomends 8x4 when MSAA is used,
8267 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008268 *
8269 * Note that PS/WM thread counts depend on the WIZ hashing
8270 * disable bit, which we don't touch here, but it's good
8271 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008272 */
8273 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008274 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008275
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008276 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02008277 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008278
8279 I915_WRITE(GEN6_UCGCTL1,
8280 I915_READ(GEN6_UCGCTL1) |
8281 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
8282 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
8283
8284 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
8285 * gating disable must be set. Failure to set it results in
8286 * flickering pixels due to Z write ordering failures after
8287 * some amount of runtime in the Mesa "fire" demo, and Unigine
8288 * Sanctuary and Tropics, and apparently anything else with
8289 * alpha test or pixel discard.
8290 *
8291 * According to the spec, bit 11 (RCCUNIT) must also be set,
8292 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008293 *
Ville Syrjäläef593182014-01-22 21:32:47 +02008294 * WaDisableRCCUnitClockGating:snb
8295 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008296 */
8297 I915_WRITE(GEN6_UCGCTL2,
8298 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
8299 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
8300
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02008301 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02008302 I915_WRITE(_3D_CHICKEN3,
8303 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008304
8305 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02008306 * Bspec says:
8307 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
8308 * 3DSTATE_SF number of SF output attributes is more than 16."
8309 */
8310 I915_WRITE(_3D_CHICKEN3,
8311 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
8312
8313 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008314 * According to the spec the following bits should be
8315 * set in order to enable memory self-refresh and fbc:
8316 * The bit21 and bit22 of 0x42000
8317 * The bit21 and bit22 of 0x42004
8318 * The bit5 and bit7 of 0x42020
8319 * The bit14 of 0x70180
8320 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01008321 *
8322 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008323 */
8324 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8325 I915_READ(ILK_DISPLAY_CHICKEN1) |
8326 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
8327 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8328 I915_READ(ILK_DISPLAY_CHICKEN2) |
8329 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01008330 I915_WRITE(ILK_DSPCLK_GATE_D,
8331 I915_READ(ILK_DSPCLK_GATE_D) |
8332 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
8333 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008334
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008335 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07008336
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008337 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008338
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008339 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008340}
8341
8342static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
8343{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008344 u32 reg = I915_READ(GEN7_FF_THREAD_MODE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008345
Ville Syrjälä3aad9052014-01-22 21:32:59 +02008346 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02008347 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02008348 *
8349 * This actually overrides the dispatch
8350 * mode for all thread types.
8351 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008352 reg &= ~GEN7_FF_SCHED_MASK;
8353 reg |= GEN7_FF_TS_SCHED_HW;
8354 reg |= GEN7_FF_VS_SCHED_HW;
8355 reg |= GEN7_FF_DS_SCHED_HW;
8356
8357 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
8358}
8359
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008360static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008361{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008362 /*
8363 * TODO: this bit should only be enabled when really needed, then
8364 * disabled when not needed anymore in order to save power.
8365 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008366 if (HAS_PCH_LPT_LP(dev_priv))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008367 I915_WRITE(SOUTH_DSPCLK_GATE_D,
8368 I915_READ(SOUTH_DSPCLK_GATE_D) |
8369 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03008370
8371 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03008372 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
8373 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03008374 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008375}
8376
Ville Syrjälä712bf362016-10-31 22:37:23 +02008377static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03008378{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008379 if (HAS_PCH_LPT_LP(dev_priv)) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008380 u32 val = I915_READ(SOUTH_DSPCLK_GATE_D);
Imre Deak7d708ee2013-04-17 14:04:50 +03008381
8382 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8383 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8384 }
8385}
8386
Imre Deak450174f2016-05-03 15:54:21 +03008387static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
8388 int general_prio_credits,
8389 int high_prio_credits)
8390{
8391 u32 misccpctl;
Oscar Mateo930a7842017-10-17 13:25:45 -07008392 u32 val;
Imre Deak450174f2016-05-03 15:54:21 +03008393
8394 /* WaTempDisableDOPClkGating:bdw */
8395 misccpctl = I915_READ(GEN7_MISCCPCTL);
8396 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
8397
Oscar Mateo930a7842017-10-17 13:25:45 -07008398 val = I915_READ(GEN8_L3SQCREG1);
8399 val &= ~L3_PRIO_CREDITS_MASK;
8400 val |= L3_GENERAL_PRIO_CREDITS(general_prio_credits);
8401 val |= L3_HIGH_PRIO_CREDITS(high_prio_credits);
8402 I915_WRITE(GEN8_L3SQCREG1, val);
Imre Deak450174f2016-05-03 15:54:21 +03008403
8404 /*
8405 * Wait at least 100 clocks before re-enabling clock gating.
8406 * See the definition of L3SQCREG1 in BSpec.
8407 */
8408 POSTING_READ(GEN8_L3SQCREG1);
8409 udelay(1);
8410 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
8411}
8412
Oscar Mateod65dc3e2018-05-08 14:29:24 -07008413static void icl_init_clock_gating(struct drm_i915_private *dev_priv)
8414{
8415 /* This is not an Wa. Enable to reduce Sampler power */
8416 I915_WRITE(GEN10_DFR_RATIO_EN_AND_CHICKEN,
8417 I915_READ(GEN10_DFR_RATIO_EN_AND_CHICKEN) & ~DFR_DISABLE);
Radhakrishna Sripada622b3f62018-10-30 01:45:01 -07008418
8419 /* WaEnable32PlaneMode:icl */
8420 I915_WRITE(GEN9_CSFE_CHICKEN1_RCS,
8421 _MASKED_BIT_ENABLE(GEN11_ENABLE_32_PLANE_MODE));
Oscar Mateod65dc3e2018-05-08 14:29:24 -07008422}
8423
Michel Thierry5d869232019-08-23 01:20:34 -07008424static void tgl_init_clock_gating(struct drm_i915_private *dev_priv)
8425{
8426 u32 vd_pg_enable = 0;
8427 unsigned int i;
8428
8429 /* This is not a WA. Enable VD HCP & MFX_ENC powergate */
8430 for (i = 0; i < I915_MAX_VCS; i++) {
8431 if (HAS_ENGINE(dev_priv, _VCS(i)))
8432 vd_pg_enable |= VDN_HCP_POWERGATE_ENABLE(i) |
8433 VDN_MFX_POWERGATE_ENABLE(i);
8434 }
8435
8436 I915_WRITE(POWERGATE_ENABLE,
8437 I915_READ(POWERGATE_ENABLE) | vd_pg_enable);
8438}
8439
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008440static void cnp_init_clock_gating(struct drm_i915_private *dev_priv)
8441{
8442 if (!HAS_PCH_CNP(dev_priv))
8443 return;
8444
Rodrigo Vivi470e7c62018-03-05 17:28:12 -08008445 /* Display WA #1181 WaSouthDisplayDisablePWMCGEGating: cnp */
Rodrigo Vivi4cc6feb2017-09-08 16:45:33 -07008446 I915_WRITE(SOUTH_DSPCLK_GATE_D, I915_READ(SOUTH_DSPCLK_GATE_D) |
8447 CNP_PWM_CGE_GATING_DISABLE);
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008448}
8449
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008450static void cnl_init_clock_gating(struct drm_i915_private *dev_priv)
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008451{
Rodrigo Vivi8f067832017-09-05 12:30:13 -07008452 u32 val;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008453 cnp_init_clock_gating(dev_priv);
8454
Rodrigo Vivi1a25db62017-08-15 16:16:51 -07008455 /* This is not an Wa. Enable for better image quality */
8456 I915_WRITE(_3D_CHICKEN3,
8457 _MASKED_BIT_ENABLE(_3D_CHICKEN3_AA_LINE_QUALITY_FIX_ENABLE));
8458
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008459 /* WaEnableChickenDCPR:cnl */
8460 I915_WRITE(GEN8_CHICKEN_DCPR_1,
8461 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
8462
8463 /* WaFbcWakeMemOn:cnl */
8464 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
8465 DISP_FBC_MEMORY_WAKE);
8466
Chris Wilson34991bd2017-11-11 10:03:36 +00008467 val = I915_READ(SLICE_UNIT_LEVEL_CLKGATE);
8468 /* ReadHitWriteOnlyDisable:cnl */
8469 val |= RCCUNIT_CLKGATE_DIS;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008470 /* WaSarbUnitClockGatingDisable:cnl (pre-prod) */
8471 if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_B0))
Chris Wilson34991bd2017-11-11 10:03:36 +00008472 val |= SARBUNIT_CLKGATE_DIS;
8473 I915_WRITE(SLICE_UNIT_LEVEL_CLKGATE, val);
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08008474
Rodrigo Vivia4713c52018-03-07 14:09:12 -08008475 /* Wa_2201832410:cnl */
8476 val = I915_READ(SUBSLICE_UNIT_LEVEL_CLKGATE);
8477 val |= GWUNIT_CLKGATE_DIS;
8478 I915_WRITE(SUBSLICE_UNIT_LEVEL_CLKGATE, val);
8479
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08008480 /* WaDisableVFclkgate:cnl */
Rodrigo Vivi14941b62018-03-05 17:20:00 -08008481 /* WaVFUnitClockGatingDisable:cnl */
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08008482 val = I915_READ(UNSLICE_UNIT_LEVEL_CLKGATE);
8483 val |= VFUNIT_CLKGATE_DIS;
8484 I915_WRITE(UNSLICE_UNIT_LEVEL_CLKGATE, val);
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008485}
8486
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008487static void cfl_init_clock_gating(struct drm_i915_private *dev_priv)
8488{
8489 cnp_init_clock_gating(dev_priv);
8490 gen9_init_clock_gating(dev_priv);
8491
8492 /* WaFbcNukeOnHostModify:cfl */
8493 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8494 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
8495}
8496
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008497static void kbl_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008498{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008499 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008500
8501 /* WaDisableSDEUnitClockGating:kbl */
8502 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
8503 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8504 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03008505
8506 /* WaDisableGamClockGating:kbl */
8507 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
8508 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
8509 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008510
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008511 /* WaFbcNukeOnHostModify:kbl */
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008512 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8513 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008514}
8515
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008516static void skl_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008517{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008518 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03008519
8520 /* WAC6entrylatency:skl */
8521 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
8522 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008523
8524 /* WaFbcNukeOnHostModify:skl */
8525 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8526 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008527}
8528
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008529static void bdw_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008530{
Damien Lespiau07d27e22014-03-03 17:31:46 +00008531 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008532
Ben Widawskyab57fff2013-12-12 15:28:04 -08008533 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07008534 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008535
Ben Widawskyab57fff2013-12-12 15:28:04 -08008536 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008537 I915_WRITE(CHICKEN_PAR1_1,
8538 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
8539
Ben Widawskyab57fff2013-12-12 15:28:04 -08008540 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01008541 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00008542 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02008543 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02008544 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008545 }
Ben Widawsky63801f22013-12-12 17:26:03 -08008546
Ben Widawskyab57fff2013-12-12 15:28:04 -08008547 /* WaVSRefCountFullforceMissDisable:bdw */
8548 /* WaDSRefCountFullforceMissDisable:bdw */
8549 I915_WRITE(GEN7_FF_THREAD_MODE,
8550 I915_READ(GEN7_FF_THREAD_MODE) &
8551 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02008552
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02008553 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
8554 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02008555
8556 /* WaDisableSDEUnitClockGating:bdw */
8557 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8558 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00008559
Imre Deak450174f2016-05-03 15:54:21 +03008560 /* WaProgramL3SqcReg1Default:bdw */
8561 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03008562
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03008563 /* WaKVMNotificationOnConfigChange:bdw */
8564 I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
8565 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
8566
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008567 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00008568
8569 /* WaDisableDopClockGating:bdw
8570 *
8571 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
8572 * clock gating.
8573 */
8574 I915_WRITE(GEN6_UCGCTL1,
8575 I915_READ(GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008576}
8577
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008578static void hsw_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008579{
Francisco Jerezf3fc4882013-10-02 15:53:16 -07008580 /* L3 caching of data atomics doesn't work -- disable it. */
8581 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
8582 I915_WRITE(HSW_ROW_CHICKEN3,
8583 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
8584
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008585 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008586 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8587 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8588 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8589
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02008590 /* WaVSRefCountFullforceMissDisable:hsw */
8591 I915_WRITE(GEN7_FF_THREAD_MODE,
8592 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008593
Akash Goel4e046322014-04-04 17:14:38 +05308594 /* WaDisable_RenderCache_OperationalFlush:hsw */
8595 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8596
Chia-I Wufe27c602014-01-28 13:29:33 +08008597 /* enable HiZ Raw Stall Optimization */
8598 I915_WRITE(CACHE_MODE_0_GEN7,
8599 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
8600
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008601 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008602 I915_WRITE(CACHE_MODE_1,
8603 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03008604
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008605 /*
8606 * BSpec recommends 8x4 when MSAA is used,
8607 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008608 *
8609 * Note that PS/WM thread counts depend on the WIZ hashing
8610 * disable bit, which we don't touch here, but it's good
8611 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008612 */
8613 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008614 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008615
Kenneth Graunke94411592014-12-31 16:23:00 -08008616 /* WaSampleCChickenBitEnable:hsw */
8617 I915_WRITE(HALF_SLICE_CHICKEN3,
8618 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
8619
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008620 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07008621 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
8622
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008623 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008624}
8625
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008626static void ivb_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008627{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008628 u32 snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008629
Damien Lespiau231e54f2012-10-19 17:55:41 +01008630 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008631
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008632 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05008633 I915_WRITE(_3D_CHICKEN3,
8634 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
8635
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008636 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008637 I915_WRITE(IVB_CHICKEN3,
8638 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8639 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8640
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008641 /* WaDisablePSDDualDispatchEnable:ivb */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008642 if (IS_IVB_GT1(dev_priv))
Jesse Barnes12f33822012-10-25 12:15:45 -07008643 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
8644 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07008645
Akash Goel4e046322014-04-04 17:14:38 +05308646 /* WaDisable_RenderCache_OperationalFlush:ivb */
8647 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8648
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008649 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008650 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
8651 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
8652
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008653 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008654 I915_WRITE(GEN7_L3CNTLREG1,
8655 GEN7_WA_FOR_GEN7_L3_CONTROL);
8656 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07008657 GEN7_WA_L3_CHICKEN_MODE);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008658 if (IS_IVB_GT1(dev_priv))
Jesse Barnes8ab43972012-10-25 12:15:42 -07008659 I915_WRITE(GEN7_ROW_CHICKEN2,
8660 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02008661 else {
8662 /* must write both registers */
8663 I915_WRITE(GEN7_ROW_CHICKEN2,
8664 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07008665 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
8666 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02008667 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008668
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008669 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05008670 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
8671 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
8672
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02008673 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07008674 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008675 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008676 */
8677 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02008678 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07008679
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008680 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008681 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8682 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8683 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8684
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008685 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008686
8687 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02008688
Chris Wilson22721342014-03-04 09:41:43 +00008689 if (0) { /* causes HiZ corruption on ivb:gt1 */
8690 /* enable HiZ Raw Stall Optimization */
8691 I915_WRITE(CACHE_MODE_0_GEN7,
8692 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
8693 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08008694
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008695 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02008696 I915_WRITE(CACHE_MODE_1,
8697 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07008698
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008699 /*
8700 * BSpec recommends 8x4 when MSAA is used,
8701 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008702 *
8703 * Note that PS/WM thread counts depend on the WIZ hashing
8704 * disable bit, which we don't touch here, but it's good
8705 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008706 */
8707 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008708 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008709
Ben Widawsky20848222012-05-04 18:58:59 -07008710 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
8711 snpcr &= ~GEN6_MBC_SNPCR_MASK;
8712 snpcr |= GEN6_MBC_SNPCR_MED;
8713 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008714
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008715 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008716 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008717
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008718 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008719}
8720
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008721static void vlv_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008722{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008723 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05008724 I915_WRITE(_3D_CHICKEN3,
8725 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
8726
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008727 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008728 I915_WRITE(IVB_CHICKEN3,
8729 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8730 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8731
Ville Syrjäläfad7d362014-01-22 21:32:39 +02008732 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008733 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07008734 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08008735 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
8736 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07008737
Akash Goel4e046322014-04-04 17:14:38 +05308738 /* WaDisable_RenderCache_OperationalFlush:vlv */
8739 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8740
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008741 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05008742 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
8743 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
8744
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008745 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07008746 I915_WRITE(GEN7_ROW_CHICKEN2,
8747 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
8748
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008749 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008750 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8751 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8752 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8753
Ville Syrjälä46680e02014-01-22 21:33:01 +02008754 gen7_setup_fixed_func_scheduler(dev_priv);
8755
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02008756 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07008757 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008758 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008759 */
8760 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02008761 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07008762
Akash Goelc98f5062014-03-24 23:00:07 +05308763 /* WaDisableL3Bank2xClockGate:vlv
8764 * Disabling L3 clock gating- MMIO 940c[25] = 1
8765 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
8766 I915_WRITE(GEN7_UCGCTL4,
8767 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07008768
Ville Syrjäläafd58e72014-01-22 21:33:03 +02008769 /*
8770 * BSpec says this must be set, even though
8771 * WaDisable4x2SubspanOptimization isn't listed for VLV.
8772 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02008773 I915_WRITE(CACHE_MODE_1,
8774 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07008775
8776 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02008777 * BSpec recommends 8x4 when MSAA is used,
8778 * however in practice 16x4 seems fastest.
8779 *
8780 * Note that PS/WM thread counts depend on the WIZ hashing
8781 * disable bit, which we don't touch here, but it's good
8782 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
8783 */
8784 I915_WRITE(GEN7_GT_MODE,
8785 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
8786
8787 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02008788 * WaIncreaseL3CreditsForVLVB0:vlv
8789 * This is the hardware default actually.
8790 */
8791 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
8792
8793 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008794 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07008795 * Disable clock gating on th GCFG unit to prevent a delay
8796 * in the reporting of vblank events.
8797 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02008798 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008799}
8800
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008801static void chv_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03008802{
Ville Syrjälä232ce332014-04-09 13:28:35 +03008803 /* WaVSRefCountFullforceMissDisable:chv */
8804 /* WaDSRefCountFullforceMissDisable:chv */
8805 I915_WRITE(GEN7_FF_THREAD_MODE,
8806 I915_READ(GEN7_FF_THREAD_MODE) &
8807 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03008808
8809 /* WaDisableSemaphoreAndSyncFlipWait:chv */
8810 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
8811 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03008812
8813 /* WaDisableCSUnitClockGating:chv */
8814 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
8815 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03008816
8817 /* WaDisableSDEUnitClockGating:chv */
8818 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8819 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008820
8821 /*
Imre Deak450174f2016-05-03 15:54:21 +03008822 * WaProgramL3SqcReg1Default:chv
8823 * See gfxspecs/Related Documents/Performance Guide/
8824 * LSQC Setting Recommendations.
8825 */
8826 gen8_set_l3sqc_credits(dev_priv, 38, 2);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03008827}
8828
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008829static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008830{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008831 u32 dspclk_gate;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008832
8833 I915_WRITE(RENCLK_GATE_D1, 0);
8834 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
8835 GS_UNIT_CLOCK_GATE_DISABLE |
8836 CL_UNIT_CLOCK_GATE_DISABLE);
8837 I915_WRITE(RAMCLK_GATE_D, 0);
8838 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
8839 OVRUNIT_CLOCK_GATE_DISABLE |
8840 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008841 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008842 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
8843 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02008844
8845 /* WaDisableRenderCachePipelinedFlush */
8846 I915_WRITE(CACHE_MODE_0,
8847 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03008848
Akash Goel4e046322014-04-04 17:14:38 +05308849 /* WaDisable_RenderCache_OperationalFlush:g4x */
8850 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8851
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008852 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008853}
8854
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008855static void i965gm_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008856{
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01008857 struct intel_uncore *uncore = &dev_priv->uncore;
8858
8859 intel_uncore_write(uncore, RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
8860 intel_uncore_write(uncore, RENCLK_GATE_D2, 0);
8861 intel_uncore_write(uncore, DSPCLK_GATE_D, 0);
8862 intel_uncore_write(uncore, RAMCLK_GATE_D, 0);
8863 intel_uncore_write16(uncore, DEUC, 0);
8864 intel_uncore_write(uncore,
8865 MI_ARB_STATE,
8866 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05308867
8868 /* WaDisable_RenderCache_OperationalFlush:gen4 */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01008869 intel_uncore_write(uncore,
8870 CACHE_MODE_0,
8871 _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008872}
8873
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008874static void i965g_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008875{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008876 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
8877 I965_RCC_CLOCK_GATE_DISABLE |
8878 I965_RCPB_CLOCK_GATE_DISABLE |
8879 I965_ISC_CLOCK_GATE_DISABLE |
8880 I965_FBC_CLOCK_GATE_DISABLE);
8881 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03008882 I915_WRITE(MI_ARB_STATE,
8883 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05308884
8885 /* WaDisable_RenderCache_OperationalFlush:gen4 */
8886 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008887}
8888
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008889static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008890{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008891 u32 dstate = I915_READ(D_STATE);
8892
8893 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
8894 DSTATE_DOT_CLOCK_GATING;
8895 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01008896
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008897 if (IS_PINEVIEW(dev_priv))
Chris Wilson13a86b82012-04-24 14:51:43 +01008898 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02008899
8900 /* IIR "flip pending" means done if this bit is set */
8901 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02008902
8903 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02008904 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02008905
8906 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
8907 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03008908
8909 I915_WRITE(MI_ARB_STATE,
8910 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008911}
8912
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008913static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008914{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008915 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02008916
8917 /* interrupts should cause a wake up from C3 */
8918 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
8919 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03008920
8921 I915_WRITE(MEM_MODE,
8922 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008923}
8924
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008925static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008926{
Ville Syrjälä10383922014-08-15 01:21:54 +03008927 I915_WRITE(MEM_MODE,
8928 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
8929 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008930}
8931
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008932void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008933{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008934 dev_priv->display.init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008935}
8936
Ville Syrjälä712bf362016-10-31 22:37:23 +02008937void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03008938{
Ville Syrjälä712bf362016-10-31 22:37:23 +02008939 if (HAS_PCH_LPT(dev_priv))
8940 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03008941}
8942
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008943static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02008944{
8945 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
8946}
8947
8948/**
8949 * intel_init_clock_gating_hooks - setup the clock gating hooks
8950 * @dev_priv: device private
8951 *
8952 * Setup the hooks that configure which clocks of a given platform can be
8953 * gated and also apply various GT and display specific workarounds for these
8954 * platforms. Note that some GT specific workarounds are applied separately
8955 * when GPU contexts or batchbuffers start their execution.
8956 */
8957void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
8958{
Lucas De Marchi13e53c52019-08-17 02:38:42 -07008959 if (IS_GEN(dev_priv, 12))
Michel Thierry5d869232019-08-23 01:20:34 -07008960 dev_priv->display.init_clock_gating = tgl_init_clock_gating;
Lucas De Marchi13e53c52019-08-17 02:38:42 -07008961 else if (IS_GEN(dev_priv, 11))
Oscar Mateod65dc3e2018-05-08 14:29:24 -07008962 dev_priv->display.init_clock_gating = icl_init_clock_gating;
Oscar Mateocc38cae2018-05-08 14:29:23 -07008963 else if (IS_CANNONLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008964 dev_priv->display.init_clock_gating = cnl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008965 else if (IS_COFFEELAKE(dev_priv))
8966 dev_priv->display.init_clock_gating = cfl_init_clock_gating;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008967 else if (IS_SKYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008968 dev_priv->display.init_clock_gating = skl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008969 else if (IS_KABYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008970 dev_priv->display.init_clock_gating = kbl_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02008971 else if (IS_BROXTON(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02008972 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02008973 else if (IS_GEMINILAKE(dev_priv))
8974 dev_priv->display.init_clock_gating = glk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008975 else if (IS_BROADWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008976 dev_priv->display.init_clock_gating = bdw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008977 else if (IS_CHERRYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008978 dev_priv->display.init_clock_gating = chv_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008979 else if (IS_HASWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008980 dev_priv->display.init_clock_gating = hsw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008981 else if (IS_IVYBRIDGE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008982 dev_priv->display.init_clock_gating = ivb_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008983 else if (IS_VALLEYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008984 dev_priv->display.init_clock_gating = vlv_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008985 else if (IS_GEN(dev_priv, 6))
Imre Deakbb400da2016-03-16 13:38:54 +02008986 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008987 else if (IS_GEN(dev_priv, 5))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008988 dev_priv->display.init_clock_gating = ilk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008989 else if (IS_G4X(dev_priv))
8990 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02008991 else if (IS_I965GM(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008992 dev_priv->display.init_clock_gating = i965gm_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02008993 else if (IS_I965G(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008994 dev_priv->display.init_clock_gating = i965g_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008995 else if (IS_GEN(dev_priv, 3))
Imre Deakbb400da2016-03-16 13:38:54 +02008996 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
8997 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
8998 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008999 else if (IS_GEN(dev_priv, 2))
Imre Deakbb400da2016-03-16 13:38:54 +02009000 dev_priv->display.init_clock_gating = i830_init_clock_gating;
9001 else {
9002 MISSING_CASE(INTEL_DEVID(dev_priv));
9003 dev_priv->display.init_clock_gating = nop_init_clock_gating;
9004 }
9005}
9006
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009007/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02009008void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009009{
Daniel Vetterc921aba2012-04-26 23:28:17 +02009010 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02009011 if (IS_PINEVIEW(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02009012 i915_pineview_get_mem_freq(dev_priv);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009013 else if (IS_GEN(dev_priv, 5))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02009014 i915_ironlake_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02009015
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009016 /* For FIFO watermark updates */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02009017 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02009018 skl_setup_wm_latency(dev_priv);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01009019 dev_priv->display.initial_watermarks = skl_initial_wm;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01009020 dev_priv->display.atomic_update_watermarks = skl_atomic_update_crtc_wm;
Matt Roper98d39492016-05-12 07:06:03 -07009021 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01009022 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02009023 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03009024
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009025 if ((IS_GEN(dev_priv, 5) && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02009026 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009027 (!IS_GEN(dev_priv, 5) && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02009028 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07009029 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08009030 dev_priv->display.compute_intermediate_wm =
9031 ilk_compute_intermediate_wm;
9032 dev_priv->display.initial_watermarks =
9033 ilk_initial_watermarks;
9034 dev_priv->display.optimize_watermarks =
9035 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02009036 } else {
9037 DRM_DEBUG_KMS("Failed to read display plane latency. "
9038 "Disable CxSR\n");
9039 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02009040 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02009041 vlv_setup_wm_latency(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02009042 dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
Ville Syrjälä4841da52017-03-02 19:14:59 +02009043 dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02009044 dev_priv->display.initial_watermarks = vlv_initial_watermarks;
Ville Syrjälä4841da52017-03-02 19:14:59 +02009045 dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
Ville Syrjäläff32c542017-03-02 19:14:57 +02009046 dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03009047 } else if (IS_G4X(dev_priv)) {
9048 g4x_setup_wm_latency(dev_priv);
9049 dev_priv->display.compute_pipe_wm = g4x_compute_pipe_wm;
9050 dev_priv->display.compute_intermediate_wm = g4x_compute_intermediate_wm;
9051 dev_priv->display.initial_watermarks = g4x_initial_watermarks;
9052 dev_priv->display.optimize_watermarks = g4x_optimize_watermarks;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02009053 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +00009054 if (!intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009055 dev_priv->is_ddr3,
9056 dev_priv->fsb_freq,
9057 dev_priv->mem_freq)) {
9058 DRM_INFO("failed to find known CxSR latency "
9059 "(found ddr%s fsb freq %d, mem freq %d), "
9060 "disabling CxSR\n",
9061 (dev_priv->is_ddr3 == 1) ? "3" : "2",
9062 dev_priv->fsb_freq, dev_priv->mem_freq);
9063 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03009064 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009065 dev_priv->display.update_wm = NULL;
9066 } else
9067 dev_priv->display.update_wm = pineview_update_wm;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009068 } else if (IS_GEN(dev_priv, 4)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009069 dev_priv->display.update_wm = i965_update_wm;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009070 } else if (IS_GEN(dev_priv, 3)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009071 dev_priv->display.update_wm = i9xx_update_wm;
9072 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009073 } else if (IS_GEN(dev_priv, 2)) {
Jani Nikula24977872019-09-11 12:26:08 +03009074 if (INTEL_NUM_PIPES(dev_priv) == 1) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009075 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009076 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009077 } else {
9078 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009079 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009080 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009081 } else {
9082 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009083 }
9084}
9085
Ville Syrjälädd06f882014-11-10 22:55:12 +02009086static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
9087{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009088 struct intel_rps *rps = &dev_priv->gt_pm.rps;
9089
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009090 /*
9091 * N = val - 0xb7
9092 * Slow = Fast = GPLL ref * N
9093 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009094 return DIV_ROUND_CLOSEST(rps->gpll_ref_freq * (val - 0xb7), 1000);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009095}
9096
Fengguang Wub55dd642014-07-12 11:21:39 +02009097static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009098{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009099 struct intel_rps *rps = &dev_priv->gt_pm.rps;
9100
9101 return DIV_ROUND_CLOSEST(1000 * val, rps->gpll_ref_freq) + 0xb7;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009102}
9103
Fengguang Wub55dd642014-07-12 11:21:39 +02009104static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05309105{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009106 struct intel_rps *rps = &dev_priv->gt_pm.rps;
9107
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009108 /*
9109 * N = val / 2
9110 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
9111 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009112 return DIV_ROUND_CLOSEST(rps->gpll_ref_freq * val, 2 * 2 * 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05309113}
9114
Fengguang Wub55dd642014-07-12 11:21:39 +02009115static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05309116{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009117 struct intel_rps *rps = &dev_priv->gt_pm.rps;
9118
Ville Syrjälä1c147622014-08-18 14:42:43 +03009119 /* CHV needs even values */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009120 return DIV_ROUND_CLOSEST(2 * 1000 * val, rps->gpll_ref_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05309121}
9122
Ville Syrjälä616bc822015-01-23 21:04:25 +02009123int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
9124{
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07009125 if (INTEL_GEN(dev_priv) >= 9)
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009126 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
9127 GEN9_FREQ_SCALER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009128 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009129 return chv_gpu_freq(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009130 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009131 return byt_gpu_freq(dev_priv, val);
9132 else
9133 return val * GT_FREQUENCY_MULTIPLIER;
9134}
9135
Ville Syrjälä616bc822015-01-23 21:04:25 +02009136int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
9137{
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07009138 if (INTEL_GEN(dev_priv) >= 9)
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009139 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
9140 GT_FREQUENCY_MULTIPLIER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009141 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009142 return chv_freq_opcode(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009143 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009144 return byt_freq_opcode(dev_priv, val);
9145 else
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009146 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
Deepak S22b1b2f2014-07-12 14:54:33 +05309147}
9148
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00009149void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01009150{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01009151 mutex_init(&dev_priv->gt_pm.rps.lock);
Chris Wilson60548c52018-07-31 14:26:29 +01009152 mutex_init(&dev_priv->gt_pm.rps.power.mutex);
Daniel Vetterf742a552013-12-06 10:17:53 +01009153
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009154 atomic_set(&dev_priv->gt_pm.rps.num_waiters, 0);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03009155
Sagar Arun Kamblead1443f2017-10-10 22:30:04 +01009156 dev_priv->runtime_pm.suspended = false;
9157 atomic_set(&dev_priv->runtime_pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01009158}
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009159
Tvrtko Ursulinc84b2702017-11-21 18:18:44 +00009160u32 intel_get_cagf(struct drm_i915_private *dev_priv, u32 rpstat)
9161{
9162 u32 cagf;
9163
9164 if (INTEL_GEN(dev_priv) >= 9)
9165 cagf = (rpstat & GEN9_CAGF_MASK) >> GEN9_CAGF_SHIFT;
9166 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
9167 cagf = (rpstat & HSW_CAGF_MASK) >> HSW_CAGF_SHIFT;
9168 else
9169 cagf = (rpstat & GEN6_CAGF_MASK) >> GEN6_CAGF_SHIFT;
9170
9171 return cagf;
9172}