blob: 3536c2e975e7525f199883acb35cfa3bdcccc15a [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
Maarten Lankhorst3a612762019-10-04 13:34:54 +02001120 width = drm_rect_width(&plane_state->base.dst);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001121
1122 if (plane->id == PLANE_CURSOR) {
1123 wm = intel_wm_method2(clock, htotal, width, cpp, latency);
1124 } else if (plane->id == PLANE_PRIMARY &&
1125 level == G4X_WM_LEVEL_NORMAL) {
1126 wm = intel_wm_method1(clock, cpp, latency);
1127 } else {
Chris Wilson1a1f1282017-11-07 14:03:38 +00001128 unsigned int small, large;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001129
1130 small = intel_wm_method1(clock, cpp, latency);
1131 large = intel_wm_method2(clock, htotal, width, cpp, latency);
1132
1133 wm = min(small, large);
1134 }
1135
1136 wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
1137 width, cpp);
1138
1139 wm = DIV_ROUND_UP(wm, 64) + 2;
1140
Chris Wilson1a1f1282017-11-07 14:03:38 +00001141 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001142}
1143
1144static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1145 int level, enum plane_id plane_id, u16 value)
1146{
1147 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1148 bool dirty = false;
1149
1150 for (; level < intel_wm_num_levels(dev_priv); level++) {
1151 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1152
1153 dirty |= raw->plane[plane_id] != value;
1154 raw->plane[plane_id] = value;
1155 }
1156
1157 return dirty;
1158}
1159
1160static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
1161 int level, u16 value)
1162{
1163 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1164 bool dirty = false;
1165
1166 /* NORMAL level doesn't have an FBC watermark */
1167 level = max(level, G4X_WM_LEVEL_SR);
1168
1169 for (; level < intel_wm_num_levels(dev_priv); level++) {
1170 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1171
1172 dirty |= raw->fbc != value;
1173 raw->fbc = value;
1174 }
1175
1176 return dirty;
1177}
1178
Maarten Lankhorstec193642019-06-28 10:55:17 +02001179static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
1180 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001181 u32 pri_val);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001182
1183static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1184 const struct intel_plane_state *plane_state)
1185{
1186 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1187 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
1188 enum plane_id plane_id = plane->id;
1189 bool dirty = false;
1190 int level;
1191
1192 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1193 dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1194 if (plane_id == PLANE_PRIMARY)
1195 dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
1196 goto out;
1197 }
1198
1199 for (level = 0; level < num_levels; level++) {
1200 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1201 int wm, max_wm;
1202
1203 wm = g4x_compute_wm(crtc_state, plane_state, level);
1204 max_wm = g4x_plane_fifo_size(plane_id, level);
1205
1206 if (wm > max_wm)
1207 break;
1208
1209 dirty |= raw->plane[plane_id] != wm;
1210 raw->plane[plane_id] = wm;
1211
1212 if (plane_id != PLANE_PRIMARY ||
1213 level == G4X_WM_LEVEL_NORMAL)
1214 continue;
1215
1216 wm = ilk_compute_fbc_wm(crtc_state, plane_state,
1217 raw->plane[plane_id]);
1218 max_wm = g4x_fbc_fifo_size(level);
1219
1220 /*
1221 * FBC wm is not mandatory as we
1222 * can always just disable its use.
1223 */
1224 if (wm > max_wm)
1225 wm = USHRT_MAX;
1226
1227 dirty |= raw->fbc != wm;
1228 raw->fbc = wm;
1229 }
1230
1231 /* mark watermarks as invalid */
1232 dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1233
1234 if (plane_id == PLANE_PRIMARY)
1235 dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
1236
1237 out:
1238 if (dirty) {
1239 DRM_DEBUG_KMS("%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
1240 plane->base.name,
1241 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
1242 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
1243 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
1244
1245 if (plane_id == PLANE_PRIMARY)
1246 DRM_DEBUG_KMS("FBC watermarks: SR=%d, HPLL=%d\n",
1247 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
1248 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
1249 }
1250
1251 return dirty;
1252}
1253
1254static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1255 enum plane_id plane_id, int level)
1256{
1257 const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1258
1259 return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
1260}
1261
1262static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
1263 int level)
1264{
1265 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1266
1267 if (level > dev_priv->wm.max_level)
1268 return false;
1269
1270 return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1271 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1272 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1273}
1274
1275/* mark all levels starting from 'level' as invalid */
1276static void g4x_invalidate_wms(struct intel_crtc *crtc,
1277 struct g4x_wm_state *wm_state, int level)
1278{
1279 if (level <= G4X_WM_LEVEL_NORMAL) {
1280 enum plane_id plane_id;
1281
1282 for_each_plane_id_on_crtc(crtc, plane_id)
1283 wm_state->wm.plane[plane_id] = USHRT_MAX;
1284 }
1285
1286 if (level <= G4X_WM_LEVEL_SR) {
1287 wm_state->cxsr = false;
1288 wm_state->sr.cursor = USHRT_MAX;
1289 wm_state->sr.plane = USHRT_MAX;
1290 wm_state->sr.fbc = USHRT_MAX;
1291 }
1292
1293 if (level <= G4X_WM_LEVEL_HPLL) {
1294 wm_state->hpll_en = false;
1295 wm_state->hpll.cursor = USHRT_MAX;
1296 wm_state->hpll.plane = USHRT_MAX;
1297 wm_state->hpll.fbc = USHRT_MAX;
1298 }
1299}
1300
1301static int g4x_compute_pipe_wm(struct intel_crtc_state *crtc_state)
1302{
1303 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1304 struct intel_atomic_state *state =
1305 to_intel_atomic_state(crtc_state->base.state);
1306 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001307 int num_active_planes = hweight8(crtc_state->active_planes &
1308 ~BIT(PLANE_CURSOR));
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001309 const struct g4x_pipe_wm *raw;
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001310 const struct intel_plane_state *old_plane_state;
1311 const struct intel_plane_state *new_plane_state;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001312 struct intel_plane *plane;
1313 enum plane_id plane_id;
1314 int i, level;
1315 unsigned int dirty = 0;
1316
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001317 for_each_oldnew_intel_plane_in_state(state, plane,
1318 old_plane_state,
1319 new_plane_state, i) {
1320 if (new_plane_state->base.crtc != &crtc->base &&
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001321 old_plane_state->base.crtc != &crtc->base)
1322 continue;
1323
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001324 if (g4x_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001325 dirty |= BIT(plane->id);
1326 }
1327
1328 if (!dirty)
1329 return 0;
1330
1331 level = G4X_WM_LEVEL_NORMAL;
1332 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1333 goto out;
1334
1335 raw = &crtc_state->wm.g4x.raw[level];
1336 for_each_plane_id_on_crtc(crtc, plane_id)
1337 wm_state->wm.plane[plane_id] = raw->plane[plane_id];
1338
1339 level = G4X_WM_LEVEL_SR;
1340
1341 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1342 goto out;
1343
1344 raw = &crtc_state->wm.g4x.raw[level];
1345 wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
1346 wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
1347 wm_state->sr.fbc = raw->fbc;
1348
1349 wm_state->cxsr = num_active_planes == BIT(PLANE_PRIMARY);
1350
1351 level = G4X_WM_LEVEL_HPLL;
1352
1353 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1354 goto out;
1355
1356 raw = &crtc_state->wm.g4x.raw[level];
1357 wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
1358 wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
1359 wm_state->hpll.fbc = raw->fbc;
1360
1361 wm_state->hpll_en = wm_state->cxsr;
1362
1363 level++;
1364
1365 out:
1366 if (level == G4X_WM_LEVEL_NORMAL)
1367 return -EINVAL;
1368
1369 /* invalidate the higher levels */
1370 g4x_invalidate_wms(crtc, wm_state, level);
1371
1372 /*
1373 * Determine if the FBC watermark(s) can be used. IF
1374 * this isn't the case we prefer to disable the FBC
1375 ( watermark(s) rather than disable the SR/HPLL
1376 * level(s) entirely.
1377 */
1378 wm_state->fbc_en = level > G4X_WM_LEVEL_NORMAL;
1379
1380 if (level >= G4X_WM_LEVEL_SR &&
1381 wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
1382 wm_state->fbc_en = false;
1383 else if (level >= G4X_WM_LEVEL_HPLL &&
1384 wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
1385 wm_state->fbc_en = false;
1386
1387 return 0;
1388}
1389
Matt Ropercd1d3ee2018-12-10 13:54:14 -08001390static int g4x_compute_intermediate_wm(struct intel_crtc_state *new_crtc_state)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001391{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08001392 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001393 struct g4x_wm_state *intermediate = &new_crtc_state->wm.g4x.intermediate;
1394 const struct g4x_wm_state *optimal = &new_crtc_state->wm.g4x.optimal;
1395 struct intel_atomic_state *intel_state =
1396 to_intel_atomic_state(new_crtc_state->base.state);
1397 const struct intel_crtc_state *old_crtc_state =
1398 intel_atomic_get_old_crtc_state(intel_state, crtc);
1399 const struct g4x_wm_state *active = &old_crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001400 enum plane_id plane_id;
1401
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001402 if (!new_crtc_state->base.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->base)) {
1403 *intermediate = *optimal;
1404
1405 intermediate->cxsr = false;
1406 intermediate->hpll_en = false;
1407 goto out;
1408 }
1409
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001410 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001411 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001412 intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001413 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001414 intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
1415
1416 for_each_plane_id_on_crtc(crtc, plane_id) {
1417 intermediate->wm.plane[plane_id] =
1418 max(optimal->wm.plane[plane_id],
1419 active->wm.plane[plane_id]);
1420
1421 WARN_ON(intermediate->wm.plane[plane_id] >
1422 g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
1423 }
1424
1425 intermediate->sr.plane = max(optimal->sr.plane,
1426 active->sr.plane);
1427 intermediate->sr.cursor = max(optimal->sr.cursor,
1428 active->sr.cursor);
1429 intermediate->sr.fbc = max(optimal->sr.fbc,
1430 active->sr.fbc);
1431
1432 intermediate->hpll.plane = max(optimal->hpll.plane,
1433 active->hpll.plane);
1434 intermediate->hpll.cursor = max(optimal->hpll.cursor,
1435 active->hpll.cursor);
1436 intermediate->hpll.fbc = max(optimal->hpll.fbc,
1437 active->hpll.fbc);
1438
1439 WARN_ON((intermediate->sr.plane >
1440 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
1441 intermediate->sr.cursor >
1442 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
1443 intermediate->cxsr);
1444 WARN_ON((intermediate->sr.plane >
1445 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
1446 intermediate->sr.cursor >
1447 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
1448 intermediate->hpll_en);
1449
1450 WARN_ON(intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
1451 intermediate->fbc_en && intermediate->cxsr);
1452 WARN_ON(intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
1453 intermediate->fbc_en && intermediate->hpll_en);
1454
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001455out:
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001456 /*
1457 * If our intermediate WM are identical to the final WM, then we can
1458 * omit the post-vblank programming; only update if it's different.
1459 */
1460 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001461 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001462
1463 return 0;
1464}
1465
1466static void g4x_merge_wm(struct drm_i915_private *dev_priv,
1467 struct g4x_wm_values *wm)
1468{
1469 struct intel_crtc *crtc;
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001470 int num_active_pipes = 0;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001471
1472 wm->cxsr = true;
1473 wm->hpll_en = true;
1474 wm->fbc_en = true;
1475
1476 for_each_intel_crtc(&dev_priv->drm, crtc) {
1477 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1478
1479 if (!crtc->active)
1480 continue;
1481
1482 if (!wm_state->cxsr)
1483 wm->cxsr = false;
1484 if (!wm_state->hpll_en)
1485 wm->hpll_en = false;
1486 if (!wm_state->fbc_en)
1487 wm->fbc_en = false;
1488
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001489 num_active_pipes++;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001490 }
1491
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001492 if (num_active_pipes != 1) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001493 wm->cxsr = false;
1494 wm->hpll_en = false;
1495 wm->fbc_en = false;
1496 }
1497
1498 for_each_intel_crtc(&dev_priv->drm, crtc) {
1499 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1500 enum pipe pipe = crtc->pipe;
1501
1502 wm->pipe[pipe] = wm_state->wm;
1503 if (crtc->active && wm->cxsr)
1504 wm->sr = wm_state->sr;
1505 if (crtc->active && wm->hpll_en)
1506 wm->hpll = wm_state->hpll;
1507 }
1508}
1509
1510static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
1511{
1512 struct g4x_wm_values *old_wm = &dev_priv->wm.g4x;
1513 struct g4x_wm_values new_wm = {};
1514
1515 g4x_merge_wm(dev_priv, &new_wm);
1516
1517 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
1518 return;
1519
1520 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
1521 _intel_set_memory_cxsr(dev_priv, false);
1522
1523 g4x_write_wm_values(dev_priv, &new_wm);
1524
1525 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
1526 _intel_set_memory_cxsr(dev_priv, true);
1527
1528 *old_wm = new_wm;
1529}
1530
1531static void g4x_initial_watermarks(struct intel_atomic_state *state,
1532 struct intel_crtc_state *crtc_state)
1533{
1534 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1535 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1536
1537 mutex_lock(&dev_priv->wm.wm_mutex);
1538 crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
1539 g4x_program_watermarks(dev_priv);
1540 mutex_unlock(&dev_priv->wm.wm_mutex);
1541}
1542
1543static void g4x_optimize_watermarks(struct intel_atomic_state *state,
1544 struct intel_crtc_state *crtc_state)
1545{
1546 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä88016a92019-07-01 19:05:45 +03001547 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001548
1549 if (!crtc_state->wm.need_postvbl_update)
1550 return;
1551
1552 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03001553 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001554 g4x_program_watermarks(dev_priv);
1555 mutex_unlock(&dev_priv->wm.wm_mutex);
1556}
1557
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001558/* latency must be in 0.1us units. */
1559static unsigned int vlv_wm_method2(unsigned int pixel_rate,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001560 unsigned int htotal,
1561 unsigned int width,
Ville Syrjäläac484962016-01-20 21:05:26 +02001562 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001563 unsigned int latency)
1564{
1565 unsigned int ret;
1566
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001567 ret = intel_wm_method2(pixel_rate, htotal,
1568 width, cpp, latency);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001569 ret = DIV_ROUND_UP(ret, 64);
1570
1571 return ret;
1572}
1573
Ville Syrjäläbb726512016-10-31 22:37:24 +02001574static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001575{
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001576 /* all latencies in usec */
1577 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1578
Ville Syrjälä58590c12015-09-08 21:05:12 +03001579 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
1580
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001581 if (IS_CHERRYVIEW(dev_priv)) {
1582 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1583 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001584
1585 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001586 }
1587}
1588
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001589static u16 vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1590 const struct intel_plane_state *plane_state,
1591 int level)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001592{
Ville Syrjäläe339d672016-11-28 19:37:17 +02001593 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001594 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläe339d672016-11-28 19:37:17 +02001595 const struct drm_display_mode *adjusted_mode =
1596 &crtc_state->base.adjusted_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001597 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001598
1599 if (dev_priv->wm.pri_latency[level] == 0)
1600 return USHRT_MAX;
1601
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001602 if (!intel_wm_plane_visible(crtc_state, plane_state))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001603 return 0;
1604
Daniel Vetteref426c12017-01-04 11:41:10 +01001605 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjäläe339d672016-11-28 19:37:17 +02001606 clock = adjusted_mode->crtc_clock;
1607 htotal = adjusted_mode->crtc_htotal;
1608 width = crtc_state->pipe_src_w;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001609
Ville Syrjälä709f3fc2017-03-03 17:19:26 +02001610 if (plane->id == PLANE_CURSOR) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001611 /*
1612 * FIXME the formula gives values that are
1613 * too big for the cursor FIFO, and hence we
1614 * would never be able to use cursors. For
1615 * now just hardcode the watermark.
1616 */
1617 wm = 63;
1618 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +02001619 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001620 dev_priv->wm.pri_latency[level] * 10);
1621 }
1622
Chris Wilson1a1f1282017-11-07 14:03:38 +00001623 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001624}
1625
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001626static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1627{
1628 return (active_planes & (BIT(PLANE_SPRITE0) |
1629 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1630}
1631
Ville Syrjälä5012e602017-03-02 19:14:56 +02001632static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001633{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001634 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001635 const struct g4x_pipe_wm *raw =
Ville Syrjälä5012e602017-03-02 19:14:56 +02001636 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001637 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001638 unsigned int active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001639 int num_active_planes = hweight8(active_planes);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001640 const int fifo_size = 511;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001641 int fifo_extra, fifo_left = fifo_size;
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001642 int sprite0_fifo_extra = 0;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001643 unsigned int total_rate;
1644 enum plane_id plane_id;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001645
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001646 /*
1647 * When enabling sprite0 after sprite1 has already been enabled
1648 * we tend to get an underrun unless sprite0 already has some
1649 * FIFO space allcoated. Hence we always allocate at least one
1650 * cacheline for sprite0 whenever sprite1 is enabled.
1651 *
1652 * All other plane enable sequences appear immune to this problem.
1653 */
1654 if (vlv_need_sprite0_fifo_workaround(active_planes))
1655 sprite0_fifo_extra = 1;
1656
Ville Syrjälä5012e602017-03-02 19:14:56 +02001657 total_rate = raw->plane[PLANE_PRIMARY] +
1658 raw->plane[PLANE_SPRITE0] +
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001659 raw->plane[PLANE_SPRITE1] +
1660 sprite0_fifo_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001661
Ville Syrjälä5012e602017-03-02 19:14:56 +02001662 if (total_rate > fifo_size)
1663 return -EINVAL;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001664
Ville Syrjälä5012e602017-03-02 19:14:56 +02001665 if (total_rate == 0)
1666 total_rate = 1;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001667
Ville Syrjälä5012e602017-03-02 19:14:56 +02001668 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001669 unsigned int rate;
1670
Ville Syrjälä5012e602017-03-02 19:14:56 +02001671 if ((active_planes & BIT(plane_id)) == 0) {
1672 fifo_state->plane[plane_id] = 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001673 continue;
1674 }
1675
Ville Syrjälä5012e602017-03-02 19:14:56 +02001676 rate = raw->plane[plane_id];
1677 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1678 fifo_left -= fifo_state->plane[plane_id];
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001679 }
1680
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001681 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1682 fifo_left -= sprite0_fifo_extra;
1683
Ville Syrjälä5012e602017-03-02 19:14:56 +02001684 fifo_state->plane[PLANE_CURSOR] = 63;
1685
1686 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001687
1688 /* spread the remainder evenly */
Ville Syrjälä5012e602017-03-02 19:14:56 +02001689 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001690 int plane_extra;
1691
1692 if (fifo_left == 0)
1693 break;
1694
Ville Syrjälä5012e602017-03-02 19:14:56 +02001695 if ((active_planes & BIT(plane_id)) == 0)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001696 continue;
1697
1698 plane_extra = min(fifo_extra, fifo_left);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001699 fifo_state->plane[plane_id] += plane_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001700 fifo_left -= plane_extra;
1701 }
1702
Ville Syrjälä5012e602017-03-02 19:14:56 +02001703 WARN_ON(active_planes != 0 && fifo_left != 0);
1704
1705 /* give it all to the first plane if none are active */
1706 if (active_planes == 0) {
1707 WARN_ON(fifo_left != fifo_size);
1708 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1709 }
1710
1711 return 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001712}
1713
Ville Syrjäläff32c542017-03-02 19:14:57 +02001714/* mark all levels starting from 'level' as invalid */
1715static void vlv_invalidate_wms(struct intel_crtc *crtc,
1716 struct vlv_wm_state *wm_state, int level)
1717{
1718 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1719
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001720 for (; level < intel_wm_num_levels(dev_priv); level++) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02001721 enum plane_id plane_id;
1722
1723 for_each_plane_id_on_crtc(crtc, plane_id)
1724 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1725
1726 wm_state->sr[level].cursor = USHRT_MAX;
1727 wm_state->sr[level].plane = USHRT_MAX;
1728 }
1729}
1730
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001731static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1732{
1733 if (wm > fifo_size)
1734 return USHRT_MAX;
1735 else
1736 return fifo_size - wm;
1737}
1738
Ville Syrjäläff32c542017-03-02 19:14:57 +02001739/*
1740 * Starting from 'level' set all higher
1741 * levels to 'value' in the "raw" watermarks.
1742 */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001743static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001744 int level, enum plane_id plane_id, u16 value)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001745{
Ville Syrjäläff32c542017-03-02 19:14:57 +02001746 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001747 int num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001748 bool dirty = false;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001749
Ville Syrjäläff32c542017-03-02 19:14:57 +02001750 for (; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001751 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001752
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001753 dirty |= raw->plane[plane_id] != value;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001754 raw->plane[plane_id] = value;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001755 }
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001756
1757 return dirty;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001758}
1759
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001760static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1761 const struct intel_plane_state *plane_state)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001762{
1763 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1764 enum plane_id plane_id = plane->id;
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001765 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
Ville Syrjäläff32c542017-03-02 19:14:57 +02001766 int level;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001767 bool dirty = false;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001768
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001769 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001770 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1771 goto out;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001772 }
1773
1774 for (level = 0; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001775 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001776 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1777 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1778
Ville Syrjäläff32c542017-03-02 19:14:57 +02001779 if (wm > max_wm)
1780 break;
1781
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001782 dirty |= raw->plane[plane_id] != wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001783 raw->plane[plane_id] = wm;
1784 }
1785
1786 /* mark all higher levels as invalid */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001787 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001788
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001789out:
1790 if (dirty)
Ville Syrjälä57a65282017-04-21 21:14:22 +03001791 DRM_DEBUG_KMS("%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001792 plane->base.name,
1793 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1794 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1795 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
1796
1797 return dirty;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001798}
1799
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001800static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1801 enum plane_id plane_id, int level)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001802{
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001803 const struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02001804 &crtc_state->wm.vlv.raw[level];
1805 const struct vlv_fifo_state *fifo_state =
1806 &crtc_state->wm.vlv.fifo_state;
1807
1808 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1809}
1810
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001811static 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 +02001812{
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001813 return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1814 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1815 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1816 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001817}
1818
1819static int vlv_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001820{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001821 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001822 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001823 struct intel_atomic_state *state =
1824 to_intel_atomic_state(crtc_state->base.state);
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001825 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001826 const struct vlv_fifo_state *fifo_state =
1827 &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001828 int num_active_planes = hweight8(crtc_state->active_planes &
1829 ~BIT(PLANE_CURSOR));
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001830 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->base);
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001831 const struct intel_plane_state *old_plane_state;
1832 const struct intel_plane_state *new_plane_state;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001833 struct intel_plane *plane;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001834 enum plane_id plane_id;
1835 int level, ret, i;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001836 unsigned int dirty = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001837
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001838 for_each_oldnew_intel_plane_in_state(state, plane,
1839 old_plane_state,
1840 new_plane_state, i) {
1841 if (new_plane_state->base.crtc != &crtc->base &&
Ville Syrjäläff32c542017-03-02 19:14:57 +02001842 old_plane_state->base.crtc != &crtc->base)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001843 continue;
1844
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001845 if (vlv_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001846 dirty |= BIT(plane->id);
1847 }
1848
1849 /*
1850 * DSPARB registers may have been reset due to the
1851 * power well being turned off. Make sure we restore
1852 * them to a consistent state even if no primary/sprite
1853 * planes are initially active.
1854 */
1855 if (needs_modeset)
1856 crtc_state->fifo_changed = true;
1857
1858 if (!dirty)
1859 return 0;
1860
1861 /* cursor changes don't warrant a FIFO recompute */
1862 if (dirty & ~BIT(PLANE_CURSOR)) {
1863 const struct intel_crtc_state *old_crtc_state =
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001864 intel_atomic_get_old_crtc_state(state, crtc);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001865 const struct vlv_fifo_state *old_fifo_state =
1866 &old_crtc_state->wm.vlv.fifo_state;
1867
1868 ret = vlv_compute_fifo(crtc_state);
1869 if (ret)
1870 return ret;
1871
1872 if (needs_modeset ||
1873 memcmp(old_fifo_state, fifo_state,
1874 sizeof(*fifo_state)) != 0)
1875 crtc_state->fifo_changed = true;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001876 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001877
Ville Syrjäläff32c542017-03-02 19:14:57 +02001878 /* initially allow all levels */
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001879 wm_state->num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001880 /*
1881 * Note that enabling cxsr with no primary/sprite planes
1882 * enabled can wedge the pipe. Hence we only allow cxsr
1883 * with exactly one enabled primary/sprite plane.
1884 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001885 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001886
Ville Syrjälä5012e602017-03-02 19:14:56 +02001887 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001888 const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Jani Nikula24977872019-09-11 12:26:08 +03001889 const int sr_fifo_size = INTEL_NUM_PIPES(dev_priv) * 512 - 1;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001890
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001891 if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
Ville Syrjäläff32c542017-03-02 19:14:57 +02001892 break;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001893
Ville Syrjäläff32c542017-03-02 19:14:57 +02001894 for_each_plane_id_on_crtc(crtc, plane_id) {
1895 wm_state->wm[level].plane[plane_id] =
1896 vlv_invert_wm_value(raw->plane[plane_id],
1897 fifo_state->plane[plane_id]);
1898 }
1899
1900 wm_state->sr[level].plane =
1901 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
Ville Syrjälä5012e602017-03-02 19:14:56 +02001902 raw->plane[PLANE_SPRITE0],
Ville Syrjäläff32c542017-03-02 19:14:57 +02001903 raw->plane[PLANE_SPRITE1]),
1904 sr_fifo_size);
1905
1906 wm_state->sr[level].cursor =
1907 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1908 63);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001909 }
1910
Ville Syrjäläff32c542017-03-02 19:14:57 +02001911 if (level == 0)
1912 return -EINVAL;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001913
Ville Syrjäläff32c542017-03-02 19:14:57 +02001914 /* limit to only levels we can actually handle */
1915 wm_state->num_levels = level;
1916
1917 /* invalidate the higher levels */
1918 vlv_invalidate_wms(crtc, wm_state, level);
1919
1920 return 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001921}
1922
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001923#define VLV_FIFO(plane, value) \
1924 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1925
Ville Syrjäläff32c542017-03-02 19:14:57 +02001926static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
1927 struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001928{
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001929 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001930 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001931 struct intel_uncore *uncore = &dev_priv->uncore;
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001932 const struct vlv_fifo_state *fifo_state =
1933 &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001934 int sprite0_start, sprite1_start, fifo_size;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001935
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001936 if (!crtc_state->fifo_changed)
1937 return;
1938
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001939 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
1940 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
1941 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001942
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001943 WARN_ON(fifo_state->plane[PLANE_CURSOR] != 63);
1944 WARN_ON(fifo_size != 511);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001945
Ville Syrjäläc137d662017-03-02 19:15:06 +02001946 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
1947
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001948 /*
1949 * uncore.lock serves a double purpose here. It allows us to
1950 * use the less expensive I915_{READ,WRITE}_FW() functions, and
1951 * it protects the DSPARB registers from getting clobbered by
1952 * parallel updates from multiple pipes.
1953 *
1954 * intel_pipe_update_start() has already disabled interrupts
1955 * for us, so a plain spin_lock() is sufficient here.
1956 */
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001957 spin_lock(&uncore->lock);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001958
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001959 switch (crtc->pipe) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001960 u32 dsparb, dsparb2, dsparb3;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001961 case PIPE_A:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001962 dsparb = intel_uncore_read_fw(uncore, DSPARB);
1963 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001964
1965 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1966 VLV_FIFO(SPRITEB, 0xff));
1967 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1968 VLV_FIFO(SPRITEB, sprite1_start));
1969
1970 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1971 VLV_FIFO(SPRITEB_HI, 0x1));
1972 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1973 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1974
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001975 intel_uncore_write_fw(uncore, DSPARB, dsparb);
1976 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001977 break;
1978 case PIPE_B:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001979 dsparb = intel_uncore_read_fw(uncore, DSPARB);
1980 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001981
1982 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1983 VLV_FIFO(SPRITED, 0xff));
1984 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1985 VLV_FIFO(SPRITED, sprite1_start));
1986
1987 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
1988 VLV_FIFO(SPRITED_HI, 0xff));
1989 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
1990 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
1991
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001992 intel_uncore_write_fw(uncore, DSPARB, dsparb);
1993 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001994 break;
1995 case PIPE_C:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001996 dsparb3 = intel_uncore_read_fw(uncore, DSPARB3);
1997 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001998
1999 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
2000 VLV_FIFO(SPRITEF, 0xff));
2001 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
2002 VLV_FIFO(SPRITEF, sprite1_start));
2003
2004 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
2005 VLV_FIFO(SPRITEF_HI, 0xff));
2006 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
2007 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
2008
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002009 intel_uncore_write_fw(uncore, DSPARB3, dsparb3);
2010 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002011 break;
2012 default:
2013 break;
2014 }
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002015
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002016 intel_uncore_posting_read_fw(uncore, DSPARB);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002017
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002018 spin_unlock(&uncore->lock);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002019}
2020
2021#undef VLV_FIFO
2022
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002023static int vlv_compute_intermediate_wm(struct intel_crtc_state *new_crtc_state)
Ville Syrjälä4841da52017-03-02 19:14:59 +02002024{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002025 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002026 struct vlv_wm_state *intermediate = &new_crtc_state->wm.vlv.intermediate;
2027 const struct vlv_wm_state *optimal = &new_crtc_state->wm.vlv.optimal;
2028 struct intel_atomic_state *intel_state =
2029 to_intel_atomic_state(new_crtc_state->base.state);
2030 const struct intel_crtc_state *old_crtc_state =
2031 intel_atomic_get_old_crtc_state(intel_state, crtc);
2032 const struct vlv_wm_state *active = &old_crtc_state->wm.vlv.optimal;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002033 int level;
2034
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002035 if (!new_crtc_state->base.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->base)) {
2036 *intermediate = *optimal;
2037
2038 intermediate->cxsr = false;
2039 goto out;
2040 }
2041
Ville Syrjälä4841da52017-03-02 19:14:59 +02002042 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002043 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002044 !new_crtc_state->disable_cxsr;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002045
2046 for (level = 0; level < intermediate->num_levels; level++) {
2047 enum plane_id plane_id;
2048
2049 for_each_plane_id_on_crtc(crtc, plane_id) {
2050 intermediate->wm[level].plane[plane_id] =
2051 min(optimal->wm[level].plane[plane_id],
2052 active->wm[level].plane[plane_id]);
2053 }
2054
2055 intermediate->sr[level].plane = min(optimal->sr[level].plane,
2056 active->sr[level].plane);
2057 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
2058 active->sr[level].cursor);
2059 }
2060
2061 vlv_invalidate_wms(crtc, intermediate, level);
2062
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002063out:
Ville Syrjälä4841da52017-03-02 19:14:59 +02002064 /*
2065 * If our intermediate WM are identical to the final WM, then we can
2066 * omit the post-vblank programming; only update if it's different.
2067 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002068 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002069 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002070
2071 return 0;
2072}
2073
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002074static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002075 struct vlv_wm_values *wm)
2076{
2077 struct intel_crtc *crtc;
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002078 int num_active_pipes = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002079
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002080 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002081 wm->cxsr = true;
2082
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002083 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002084 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002085
2086 if (!crtc->active)
2087 continue;
2088
2089 if (!wm_state->cxsr)
2090 wm->cxsr = false;
2091
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002092 num_active_pipes++;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002093 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
2094 }
2095
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002096 if (num_active_pipes != 1)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002097 wm->cxsr = false;
2098
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002099 if (num_active_pipes > 1)
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03002100 wm->level = VLV_WM_LEVEL_PM2;
2101
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002102 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002103 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002104 enum pipe pipe = crtc->pipe;
2105
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002106 wm->pipe[pipe] = wm_state->wm[wm->level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02002107 if (crtc->active && wm->cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002108 wm->sr = wm_state->sr[wm->level];
2109
Ville Syrjälä1b313892016-11-28 19:37:08 +02002110 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
2111 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
2112 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
2113 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002114 }
2115}
2116
Ville Syrjäläff32c542017-03-02 19:14:57 +02002117static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002118{
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002119 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
2120 struct vlv_wm_values new_wm = {};
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002121
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002122 vlv_merge_wm(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002123
Ville Syrjäläff32c542017-03-02 19:14:57 +02002124 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002125 return;
2126
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002127 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002128 chv_set_memory_dvfs(dev_priv, false);
2129
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002130 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002131 chv_set_memory_pm5(dev_priv, false);
2132
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002133 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002134 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002135
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002136 vlv_write_wm_values(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002137
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002138 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002139 _intel_set_memory_cxsr(dev_priv, true);
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->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002142 chv_set_memory_pm5(dev_priv, true);
2143
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002144 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002145 chv_set_memory_dvfs(dev_priv, true);
2146
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002147 *old_wm = new_wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03002148}
2149
Ville Syrjäläff32c542017-03-02 19:14:57 +02002150static void vlv_initial_watermarks(struct intel_atomic_state *state,
2151 struct intel_crtc_state *crtc_state)
2152{
2153 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2154 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
2155
2156 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002157 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
2158 vlv_program_watermarks(dev_priv);
2159 mutex_unlock(&dev_priv->wm.wm_mutex);
2160}
2161
2162static void vlv_optimize_watermarks(struct intel_atomic_state *state,
2163 struct intel_crtc_state *crtc_state)
2164{
2165 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä88016a92019-07-01 19:05:45 +03002166 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002167
2168 if (!crtc_state->wm.need_postvbl_update)
2169 return;
2170
2171 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03002172 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02002173 vlv_program_watermarks(dev_priv);
2174 mutex_unlock(&dev_priv->wm.wm_mutex);
2175}
2176
Ville Syrjälä432081b2016-10-31 22:37:03 +02002177static void i965_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002178{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002179 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002180 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002181 int srwm = 1;
2182 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03002183 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002184
2185 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002186 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002187 if (crtc) {
2188 /* self-refresh has much higher latency */
2189 static const int sr_latency_ns = 12000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002190 const struct drm_display_mode *adjusted_mode =
2191 &crtc->config->base.adjusted_mode;
2192 const struct drm_framebuffer *fb =
2193 crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002194 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002195 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002196 int hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002197 int cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002198 int entries;
2199
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002200 entries = intel_wm_method2(clock, htotal,
2201 hdisplay, cpp, sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002202 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
2203 srwm = I965_FIFO_SIZE - entries;
2204 if (srwm < 0)
2205 srwm = 1;
2206 srwm &= 0x1ff;
2207 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
2208 entries, srwm);
2209
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002210 entries = intel_wm_method2(clock, htotal,
2211 crtc->base.cursor->state->crtc_w, 4,
2212 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002213 entries = DIV_ROUND_UP(entries,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002214 i965_cursor_wm_info.cacheline_size) +
2215 i965_cursor_wm_info.guard_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002216
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002217 cursor_sr = i965_cursor_wm_info.fifo_size - entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002218 if (cursor_sr > i965_cursor_wm_info.max_wm)
2219 cursor_sr = i965_cursor_wm_info.max_wm;
2220
2221 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
2222 "cursor %d\n", srwm, cursor_sr);
2223
Imre Deak98584252014-06-13 14:54:20 +03002224 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002225 } else {
Imre Deak98584252014-06-13 14:54:20 +03002226 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002227 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03002228 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002229 }
2230
2231 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2232 srwm);
2233
2234 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002235 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
2236 FW_WM(8, CURSORB) |
2237 FW_WM(8, PLANEB) |
2238 FW_WM(8, PLANEA));
2239 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
2240 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002241 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002242 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03002243
2244 if (cxsr_enabled)
2245 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002246}
2247
Ville Syrjäläf4998962015-03-10 17:02:21 +02002248#undef FW_WM
2249
Ville Syrjälä432081b2016-10-31 22:37:03 +02002250static void i9xx_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002251{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002252 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002253 const struct intel_watermark_params *wm_info;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002254 u32 fwater_lo;
2255 u32 fwater_hi;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002256 int cwm, srwm = 1;
2257 int fifo_size;
2258 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002259 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002260
Ville Syrjäläa9097be2016-10-31 22:37:20 +02002261 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002262 wm_info = &i945_wm_info;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002263 else if (!IS_GEN(dev_priv, 2))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002264 wm_info = &i915_wm_info;
2265 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03002266 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002267
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002268 fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_A);
2269 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_A);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002270 if (intel_crtc_active(crtc)) {
2271 const struct drm_display_mode *adjusted_mode =
2272 &crtc->config->base.adjusted_mode;
2273 const struct drm_framebuffer *fb =
2274 crtc->base.primary->state->fb;
2275 int cpp;
2276
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002277 if (IS_GEN(dev_priv, 2))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002278 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002279 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002280 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002281
Damien Lespiau241bfc32013-09-25 16:45:37 +01002282 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002283 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002284 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002285 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002286 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002287 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002288 if (planea_wm > (long)wm_info->max_wm)
2289 planea_wm = wm_info->max_wm;
2290 }
2291
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002292 if (IS_GEN(dev_priv, 2))
Ville Syrjälä9d539102014-08-15 01:21:53 +03002293 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002294
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002295 fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_B);
2296 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_B);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002297 if (intel_crtc_active(crtc)) {
2298 const struct drm_display_mode *adjusted_mode =
2299 &crtc->config->base.adjusted_mode;
2300 const struct drm_framebuffer *fb =
2301 crtc->base.primary->state->fb;
2302 int cpp;
2303
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002304 if (IS_GEN(dev_priv, 2))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002305 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002306 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002307 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002308
Damien Lespiau241bfc32013-09-25 16:45:37 +01002309 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002310 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002311 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002312 if (enabled == NULL)
2313 enabled = crtc;
2314 else
2315 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002316 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002317 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002318 if (planeb_wm > (long)wm_info->max_wm)
2319 planeb_wm = wm_info->max_wm;
2320 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002321
2322 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
2323
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002324 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07002325 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002326
Ville Syrjäläefc26112016-10-31 22:37:04 +02002327 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002328
2329 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01002330 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002331 enabled = NULL;
2332 }
2333
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002334 /*
2335 * Overlay gets an aggressive default since video jitter is bad.
2336 */
2337 cwm = 2;
2338
2339 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03002340 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002341
2342 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02002343 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002344 /* self-refresh has much higher latency */
2345 static const int sr_latency_ns = 6000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002346 const struct drm_display_mode *adjusted_mode =
2347 &enabled->config->base.adjusted_mode;
2348 const struct drm_framebuffer *fb =
2349 enabled->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002350 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002351 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002352 int hdisplay = enabled->config->pipe_src_w;
2353 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002354 int entries;
2355
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002356 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002357 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002358 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002359 cpp = fb->format->cpp[0];
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002360
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002361 entries = intel_wm_method2(clock, htotal, hdisplay, cpp,
2362 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002363 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
2364 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
2365 srwm = wm_info->fifo_size - entries;
2366 if (srwm < 0)
2367 srwm = 1;
2368
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002369 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002370 I915_WRITE(FW_BLC_SELF,
2371 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03002372 else
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002373 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
2374 }
2375
2376 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
2377 planea_wm, planeb_wm, cwm, srwm);
2378
2379 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2380 fwater_hi = (cwm & 0x1f);
2381
2382 /* Set request length to 8 cachelines per fetch */
2383 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2384 fwater_hi = fwater_hi | (1 << 8);
2385
2386 I915_WRITE(FW_BLC, fwater_lo);
2387 I915_WRITE(FW_BLC2, fwater_hi);
2388
Imre Deak5209b1f2014-07-01 12:36:17 +03002389 if (enabled)
2390 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002391}
2392
Ville Syrjälä432081b2016-10-31 22:37:03 +02002393static void i845_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002394{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002395 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002396 struct intel_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002397 const struct drm_display_mode *adjusted_mode;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002398 u32 fwater_lo;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002399 int planea_wm;
2400
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002401 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002402 if (crtc == NULL)
2403 return;
2404
Ville Syrjäläefc26112016-10-31 22:37:04 +02002405 adjusted_mode = &crtc->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002406 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02002407 &i845_wm_info,
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002408 dev_priv->display.get_fifo_size(dev_priv, PLANE_A),
Chris Wilson5aef6002014-09-03 11:56:07 +01002409 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002410 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
2411 fwater_lo |= (3<<8) | planea_wm;
2412
2413 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
2414
2415 I915_WRITE(FW_BLC, fwater_lo);
2416}
2417
Ville Syrjälä37126462013-08-01 16:18:55 +03002418/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002419static unsigned int ilk_wm_method1(unsigned int pixel_rate,
2420 unsigned int cpp,
2421 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002422{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002423 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002424
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002425 ret = intel_wm_method1(pixel_rate, cpp, latency);
2426 ret = DIV_ROUND_UP(ret, 64) + 2;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002427
2428 return ret;
2429}
2430
Ville Syrjälä37126462013-08-01 16:18:55 +03002431/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002432static unsigned int ilk_wm_method2(unsigned int pixel_rate,
2433 unsigned int htotal,
2434 unsigned int width,
2435 unsigned int cpp,
2436 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002437{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002438 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002439
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002440 ret = intel_wm_method2(pixel_rate, htotal,
2441 width, cpp, latency);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002442 ret = DIV_ROUND_UP(ret, 64) + 2;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002443
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002444 return ret;
2445}
2446
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002447static u32 ilk_wm_fbc(u32 pri_val, u32 horiz_pixels, u8 cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002448{
Matt Roper15126882015-12-03 11:37:40 -08002449 /*
2450 * Neither of these should be possible since this function shouldn't be
2451 * called if the CRTC is off or the plane is invisible. But let's be
2452 * extra paranoid to avoid a potential divide-by-zero if we screw up
2453 * elsewhere in the driver.
2454 */
Ville Syrjäläac484962016-01-20 21:05:26 +02002455 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08002456 return 0;
2457 if (WARN_ON(!horiz_pixels))
2458 return 0;
2459
Ville Syrjäläac484962016-01-20 21:05:26 +02002460 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002461}
2462
Imre Deak820c1982013-12-17 14:46:36 +02002463struct ilk_wm_maximums {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002464 u16 pri;
2465 u16 spr;
2466 u16 cur;
2467 u16 fbc;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002468};
2469
Ville Syrjälä37126462013-08-01 16:18:55 +03002470/*
2471 * For both WM_PIPE and WM_LP.
2472 * mem_value must be in 0.1us units.
2473 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002474static u32 ilk_compute_pri_wm(const struct intel_crtc_state *crtc_state,
2475 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002476 u32 mem_value, bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002477{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002478 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002479 int cpp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002480
Ville Syrjälä03981c62018-11-14 19:34:40 +02002481 if (mem_value == 0)
2482 return U32_MAX;
2483
Maarten Lankhorstec193642019-06-28 10:55:17 +02002484 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002485 return 0;
2486
Maarten Lankhorstec193642019-06-28 10:55:17 +02002487 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002488
Maarten Lankhorstec193642019-06-28 10:55:17 +02002489 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002490
2491 if (!is_lp)
2492 return method1;
2493
Maarten Lankhorstec193642019-06-28 10:55:17 +02002494 method2 = ilk_wm_method2(crtc_state->pixel_rate,
2495 crtc_state->base.adjusted_mode.crtc_htotal,
2496 drm_rect_width(&plane_state->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002497 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002498
2499 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002500}
2501
Ville Syrjälä37126462013-08-01 16:18:55 +03002502/*
2503 * For both WM_PIPE and WM_LP.
2504 * mem_value must be in 0.1us units.
2505 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002506static u32 ilk_compute_spr_wm(const struct intel_crtc_state *crtc_state,
2507 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002508 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002509{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002510 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002511 int cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002512
Ville Syrjälä03981c62018-11-14 19:34:40 +02002513 if (mem_value == 0)
2514 return U32_MAX;
2515
Maarten Lankhorstec193642019-06-28 10:55:17 +02002516 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002517 return 0;
2518
Maarten Lankhorstec193642019-06-28 10:55:17 +02002519 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002520
Maarten Lankhorstec193642019-06-28 10:55:17 +02002521 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
2522 method2 = ilk_wm_method2(crtc_state->pixel_rate,
2523 crtc_state->base.adjusted_mode.crtc_htotal,
2524 drm_rect_width(&plane_state->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002525 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002526 return min(method1, method2);
2527}
2528
Ville Syrjälä37126462013-08-01 16:18:55 +03002529/*
2530 * For both WM_PIPE and WM_LP.
2531 * mem_value must be in 0.1us units.
2532 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002533static u32 ilk_compute_cur_wm(const struct intel_crtc_state *crtc_state,
2534 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002535 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002536{
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002537 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002538
Ville Syrjälä03981c62018-11-14 19:34:40 +02002539 if (mem_value == 0)
2540 return U32_MAX;
2541
Maarten Lankhorstec193642019-06-28 10:55:17 +02002542 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002543 return 0;
2544
Maarten Lankhorstec193642019-06-28 10:55:17 +02002545 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002546
Maarten Lankhorstec193642019-06-28 10:55:17 +02002547 return ilk_wm_method2(crtc_state->pixel_rate,
2548 crtc_state->base.adjusted_mode.crtc_htotal,
Maarten Lankhorst3a612762019-10-04 13:34:54 +02002549 drm_rect_width(&plane_state->base.dst),
2550 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002551}
2552
Paulo Zanonicca32e92013-05-31 11:45:06 -03002553/* Only for WM_LP. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002554static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
2555 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002556 u32 pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002557{
Ville Syrjälä83054942016-11-18 21:53:00 +02002558 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002559
Maarten Lankhorstec193642019-06-28 10:55:17 +02002560 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanonicca32e92013-05-31 11:45:06 -03002561 return 0;
2562
Maarten Lankhorstec193642019-06-28 10:55:17 +02002563 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002564
Maarten Lankhorstec193642019-06-28 10:55:17 +02002565 return ilk_wm_fbc(pri_val, drm_rect_width(&plane_state->base.dst), cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002566}
2567
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002568static unsigned int
2569ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002570{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002571 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002572 return 3072;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002573 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002574 return 768;
2575 else
2576 return 512;
2577}
2578
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002579static unsigned int
2580ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2581 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002582{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002583 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002584 /* BDW primary/sprite plane watermarks */
2585 return level == 0 ? 255 : 2047;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002586 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002587 /* IVB/HSW primary/sprite plane watermarks */
2588 return level == 0 ? 127 : 1023;
2589 else if (!is_sprite)
2590 /* ILK/SNB primary plane watermarks */
2591 return level == 0 ? 127 : 511;
2592 else
2593 /* ILK/SNB sprite plane watermarks */
2594 return level == 0 ? 63 : 255;
2595}
2596
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002597static unsigned int
2598ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002599{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002600 if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002601 return level == 0 ? 63 : 255;
2602 else
2603 return level == 0 ? 31 : 63;
2604}
2605
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002606static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002607{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002608 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002609 return 31;
2610 else
2611 return 15;
2612}
2613
Ville Syrjälä158ae642013-08-07 13:28:19 +03002614/* Calculate the maximum primary/sprite plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002615static unsigned int ilk_plane_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002616 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002617 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002618 enum intel_ddb_partitioning ddb_partitioning,
2619 bool is_sprite)
2620{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002621 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002622
2623 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002624 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002625 return 0;
2626
2627 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002628 if (level == 0 || config->num_pipes_active > 1) {
Jani Nikula24977872019-09-11 12:26:08 +03002629 fifo_size /= INTEL_NUM_PIPES(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002630
2631 /*
2632 * For some reason the non self refresh
2633 * FIFO size is only half of the self
2634 * refresh FIFO size on ILK/SNB.
2635 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002636 if (INTEL_GEN(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002637 fifo_size /= 2;
2638 }
2639
Ville Syrjälä240264f2013-08-07 13:29:12 +03002640 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002641 /* level 0 is always calculated with 1:1 split */
2642 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2643 if (is_sprite)
2644 fifo_size *= 5;
2645 fifo_size /= 6;
2646 } else {
2647 fifo_size /= 2;
2648 }
2649 }
2650
2651 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002652 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002653}
2654
2655/* Calculate the maximum cursor plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002656static unsigned int ilk_cursor_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002657 int level,
2658 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002659{
2660 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002661 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002662 return 64;
2663
2664 /* otherwise just report max that registers can hold */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002665 return ilk_cursor_wm_reg_max(dev_priv, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002666}
2667
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002668static void ilk_compute_wm_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002669 int level,
2670 const struct intel_wm_config *config,
2671 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002672 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002673{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002674 max->pri = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, false);
2675 max->spr = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, true);
2676 max->cur = ilk_cursor_wm_max(dev_priv, level, config);
2677 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002678}
2679
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002680static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002681 int level,
2682 struct ilk_wm_maximums *max)
2683{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002684 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2685 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2686 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2687 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002688}
2689
Ville Syrjäläd9395652013-10-09 19:18:10 +03002690static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002691 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002692 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002693{
2694 bool ret;
2695
2696 /* already determined to be invalid? */
2697 if (!result->enable)
2698 return false;
2699
2700 result->enable = result->pri_val <= max->pri &&
2701 result->spr_val <= max->spr &&
2702 result->cur_val <= max->cur;
2703
2704 ret = result->enable;
2705
2706 /*
2707 * HACK until we can pre-compute everything,
2708 * and thus fail gracefully if LP0 watermarks
2709 * are exceeded...
2710 */
2711 if (level == 0 && !result->enable) {
2712 if (result->pri_val > max->pri)
2713 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2714 level, result->pri_val, max->pri);
2715 if (result->spr_val > max->spr)
2716 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2717 level, result->spr_val, max->spr);
2718 if (result->cur_val > max->cur)
2719 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2720 level, result->cur_val, max->cur);
2721
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002722 result->pri_val = min_t(u32, result->pri_val, max->pri);
2723 result->spr_val = min_t(u32, result->spr_val, max->spr);
2724 result->cur_val = min_t(u32, result->cur_val, max->cur);
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002725 result->enable = true;
2726 }
2727
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002728 return ret;
2729}
2730
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002731static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Matt Roper43d59ed2015-09-24 15:53:07 -07002732 const struct intel_crtc *intel_crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002733 int level,
Maarten Lankhorstec193642019-06-28 10:55:17 +02002734 struct intel_crtc_state *crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02002735 const struct intel_plane_state *pristate,
2736 const struct intel_plane_state *sprstate,
2737 const struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002738 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002739{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002740 u16 pri_latency = dev_priv->wm.pri_latency[level];
2741 u16 spr_latency = dev_priv->wm.spr_latency[level];
2742 u16 cur_latency = dev_priv->wm.cur_latency[level];
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002743
2744 /* WM1+ latency values stored in 0.5us units */
2745 if (level > 0) {
2746 pri_latency *= 5;
2747 spr_latency *= 5;
2748 cur_latency *= 5;
2749 }
2750
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002751 if (pristate) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02002752 result->pri_val = ilk_compute_pri_wm(crtc_state, pristate,
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002753 pri_latency, level);
Maarten Lankhorstec193642019-06-28 10:55:17 +02002754 result->fbc_val = ilk_compute_fbc_wm(crtc_state, pristate, result->pri_val);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002755 }
2756
2757 if (sprstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002758 result->spr_val = ilk_compute_spr_wm(crtc_state, sprstate, spr_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002759
2760 if (curstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002761 result->cur_val = ilk_compute_cur_wm(crtc_state, curstate, cur_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002762
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002763 result->enable = true;
2764}
2765
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002766static u32
Maarten Lankhorstec193642019-06-28 10:55:17 +02002767hsw_compute_linetime_wm(const struct intel_crtc_state *crtc_state)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002768{
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002769 const struct intel_atomic_state *intel_state =
Maarten Lankhorstec193642019-06-28 10:55:17 +02002770 to_intel_atomic_state(crtc_state->base.state);
Matt Roperee91a152015-12-03 11:37:39 -08002771 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorstec193642019-06-28 10:55:17 +02002772 &crtc_state->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002773 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002774
Maarten Lankhorstec193642019-06-28 10:55:17 +02002775 if (!crtc_state->base.active)
Matt Roperee91a152015-12-03 11:37:39 -08002776 return 0;
2777 if (WARN_ON(adjusted_mode->crtc_clock == 0))
2778 return 0;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002779 if (WARN_ON(intel_state->cdclk.logical.cdclk == 0))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002780 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002781
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002782 /* The WM are computed with base on how long it takes to fill a single
2783 * row at the given clock rate, multiplied by 8.
2784 * */
Ville Syrjälä124abe02015-09-08 13:40:45 +03002785 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2786 adjusted_mode->crtc_clock);
2787 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002788 intel_state->cdclk.logical.cdclk);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002789
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002790 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2791 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002792}
2793
Ville Syrjäläbb726512016-10-31 22:37:24 +02002794static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002795 u16 wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002796{
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002797 struct intel_uncore *uncore = &dev_priv->uncore;
2798
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002799 if (INTEL_GEN(dev_priv) >= 9) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002800 u32 val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002801 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002802 int level, max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002803
2804 /* read the first set of memory latencies[0:3] */
2805 val = 0; /* data0 to be programmed to 0 for first set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002806 ret = sandybridge_pcode_read(dev_priv,
2807 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002808 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002809
2810 if (ret) {
2811 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2812 return;
2813 }
2814
2815 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2816 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2817 GEN9_MEM_LATENCY_LEVEL_MASK;
2818 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2819 GEN9_MEM_LATENCY_LEVEL_MASK;
2820 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2821 GEN9_MEM_LATENCY_LEVEL_MASK;
2822
2823 /* read the second set of memory latencies[4:7] */
2824 val = 1; /* data0 to be programmed to 1 for second set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002825 ret = sandybridge_pcode_read(dev_priv,
2826 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002827 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002828 if (ret) {
2829 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2830 return;
2831 }
2832
2833 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2834 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2835 GEN9_MEM_LATENCY_LEVEL_MASK;
2836 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2837 GEN9_MEM_LATENCY_LEVEL_MASK;
2838 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2839 GEN9_MEM_LATENCY_LEVEL_MASK;
2840
Vandana Kannan367294b2014-11-04 17:06:46 +00002841 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002842 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2843 * need to be disabled. We make sure to sanitize the values out
2844 * of the punit to satisfy this requirement.
2845 */
2846 for (level = 1; level <= max_level; level++) {
2847 if (wm[level] == 0) {
2848 for (i = level + 1; i <= max_level; i++)
2849 wm[i] = 0;
2850 break;
2851 }
2852 }
2853
2854 /*
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002855 * WaWmMemoryReadLatency:skl+,glk
Damien Lespiau6f972352015-02-09 19:33:07 +00002856 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002857 * punit doesn't take into account the read latency so we need
Paulo Zanoni0727e402016-09-22 18:00:30 -03002858 * to add 2us to the various latency levels we retrieve from the
2859 * punit when level 0 response data us 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002860 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002861 if (wm[0] == 0) {
2862 wm[0] += 2;
2863 for (level = 1; level <= max_level; level++) {
2864 if (wm[level] == 0)
2865 break;
Vandana Kannan367294b2014-11-04 17:06:46 +00002866 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002867 }
Paulo Zanoni0727e402016-09-22 18:00:30 -03002868 }
2869
Mahesh Kumar86b59282018-08-31 16:39:42 +05302870 /*
2871 * WA Level-0 adjustment for 16GB DIMMs: SKL+
2872 * If we could not get dimm info enable this WA to prevent from
2873 * any underrun. If not able to get Dimm info assume 16GB dimm
2874 * to avoid any underrun.
2875 */
Ville Syrjälä5d6f36b2018-10-23 21:21:02 +03002876 if (dev_priv->dram_info.is_16gb_dimm)
Mahesh Kumar86b59282018-08-31 16:39:42 +05302877 wm[0] += 1;
2878
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002879 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002880 u64 sskpd = intel_uncore_read64(uncore, MCH_SSKPD);
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002881
2882 wm[0] = (sskpd >> 56) & 0xFF;
2883 if (wm[0] == 0)
2884 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002885 wm[1] = (sskpd >> 4) & 0xFF;
2886 wm[2] = (sskpd >> 12) & 0xFF;
2887 wm[3] = (sskpd >> 20) & 0x1FF;
2888 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002889 } else if (INTEL_GEN(dev_priv) >= 6) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002890 u32 sskpd = intel_uncore_read(uncore, MCH_SSKPD);
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002891
2892 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2893 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2894 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2895 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002896 } else if (INTEL_GEN(dev_priv) >= 5) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002897 u32 mltr = intel_uncore_read(uncore, MLTR_ILK);
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002898
2899 /* ILK primary LP0 latency is 700 ns */
2900 wm[0] = 7;
2901 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2902 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002903 } else {
2904 MISSING_CASE(INTEL_DEVID(dev_priv));
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002905 }
2906}
2907
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002908static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002909 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002910{
2911 /* ILK sprite LP0 latency is 1300 ns */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002912 if (IS_GEN(dev_priv, 5))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002913 wm[0] = 13;
2914}
2915
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002916static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002917 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002918{
2919 /* ILK cursor LP0 latency is 1300 ns */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002920 if (IS_GEN(dev_priv, 5))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002921 wm[0] = 13;
Ville Syrjälä53615a52013-08-01 16:18:50 +03002922}
2923
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002924int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002925{
2926 /* how many WM levels are we expecting */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002927 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002928 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002929 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002930 return 4;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002931 else if (INTEL_GEN(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002932 return 3;
2933 else
2934 return 2;
2935}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002936
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002937static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002938 const char *name,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002939 const u16 wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002940{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002941 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002942
2943 for (level = 0; level <= max_level; level++) {
2944 unsigned int latency = wm[level];
2945
2946 if (latency == 0) {
Chris Wilson86c1c872018-07-26 17:15:27 +01002947 DRM_DEBUG_KMS("%s WM%d latency not provided\n",
2948 name, level);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002949 continue;
2950 }
2951
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002952 /*
2953 * - latencies are in us on gen9.
2954 * - before then, WM1+ latency values are in 0.5us units
2955 */
Paulo Zanonidfc267a2017-08-09 13:52:46 -07002956 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002957 latency *= 10;
2958 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002959 latency *= 5;
2960
2961 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2962 name, level, wm[level],
2963 latency / 10, latency % 10);
2964 }
2965}
2966
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002967static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002968 u16 wm[5], u16 min)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002969{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002970 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002971
2972 if (wm[0] >= min)
2973 return false;
2974
2975 wm[0] = max(wm[0], min);
2976 for (level = 1; level <= max_level; level++)
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002977 wm[level] = max_t(u16, wm[level], DIV_ROUND_UP(min, 5));
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002978
2979 return true;
2980}
2981
Ville Syrjäläbb726512016-10-31 22:37:24 +02002982static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002983{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002984 bool changed;
2985
2986 /*
2987 * The BIOS provided WM memory latency values are often
2988 * inadequate for high resolution displays. Adjust them.
2989 */
2990 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2991 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2992 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2993
2994 if (!changed)
2995 return;
2996
2997 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002998 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
2999 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3000 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003001}
3002
Ville Syrjälä03981c62018-11-14 19:34:40 +02003003static void snb_wm_lp3_irq_quirk(struct drm_i915_private *dev_priv)
3004{
3005 /*
3006 * On some SNB machines (Thinkpad X220 Tablet at least)
3007 * LP3 usage can cause vblank interrupts to be lost.
3008 * The DEIIR bit will go high but it looks like the CPU
3009 * never gets interrupted.
3010 *
3011 * It's not clear whether other interrupt source could
3012 * be affected or if this is somehow limited to vblank
3013 * interrupts only. To play it safe we disable LP3
3014 * watermarks entirely.
3015 */
3016 if (dev_priv->wm.pri_latency[3] == 0 &&
3017 dev_priv->wm.spr_latency[3] == 0 &&
3018 dev_priv->wm.cur_latency[3] == 0)
3019 return;
3020
3021 dev_priv->wm.pri_latency[3] = 0;
3022 dev_priv->wm.spr_latency[3] = 0;
3023 dev_priv->wm.cur_latency[3] = 0;
3024
3025 DRM_DEBUG_KMS("LP3 watermarks disabled due to potential for lost interrupts\n");
3026 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3027 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3028 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
3029}
3030
Ville Syrjäläbb726512016-10-31 22:37:24 +02003031static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03003032{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003033 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03003034
3035 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
3036 sizeof(dev_priv->wm.pri_latency));
3037 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
3038 sizeof(dev_priv->wm.pri_latency));
3039
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003040 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003041 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003042
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003043 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3044 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3045 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003046
Lucas De Marchicf819ef2018-12-12 10:10:43 -08003047 if (IS_GEN(dev_priv, 6)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02003048 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä03981c62018-11-14 19:34:40 +02003049 snb_wm_lp3_irq_quirk(dev_priv);
3050 }
Ville Syrjälä53615a52013-08-01 16:18:50 +03003051}
3052
Ville Syrjäläbb726512016-10-31 22:37:24 +02003053static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003054{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003055 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003056 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003057}
3058
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003059static bool ilk_validate_pipe_wm(const struct drm_i915_private *dev_priv,
Matt Ropered4a6a72016-02-23 17:20:13 -08003060 struct intel_pipe_wm *pipe_wm)
3061{
3062 /* LP0 watermark maximums depend on this pipe alone */
3063 const struct intel_wm_config config = {
3064 .num_pipes_active = 1,
3065 .sprites_enabled = pipe_wm->sprites_enabled,
3066 .sprites_scaled = pipe_wm->sprites_scaled,
3067 };
3068 struct ilk_wm_maximums max;
3069
3070 /* LP0 watermarks always use 1/2 DDB partitioning */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003071 ilk_compute_wm_maximums(dev_priv, 0, &config, INTEL_DDB_PART_1_2, &max);
Matt Ropered4a6a72016-02-23 17:20:13 -08003072
3073 /* At least LP0 must be valid */
3074 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
3075 DRM_DEBUG_KMS("LP0 watermark invalid\n");
3076 return false;
3077 }
3078
3079 return true;
3080}
3081
Matt Roper261a27d2015-10-08 15:28:25 -07003082/* Compute new watermarks for the pipe */
Maarten Lankhorstec193642019-06-28 10:55:17 +02003083static int ilk_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Matt Roper261a27d2015-10-08 15:28:25 -07003084{
Maarten Lankhorstec193642019-06-28 10:55:17 +02003085 struct drm_atomic_state *state = crtc_state->base.state;
3086 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07003087 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003088 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003089 const struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02003090 struct intel_plane *plane;
3091 const struct intel_plane_state *plane_state;
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003092 const struct intel_plane_state *pristate = NULL;
3093 const struct intel_plane_state *sprstate = NULL;
3094 const struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003095 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02003096 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003097
Maarten Lankhorstec193642019-06-28 10:55:17 +02003098 pipe_wm = &crtc_state->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003099
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02003100 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
3101 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
3102 pristate = plane_state;
3103 else if (plane->base.type == DRM_PLANE_TYPE_OVERLAY)
3104 sprstate = plane_state;
3105 else if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
3106 curstate = plane_state;
Matt Roper43d59ed2015-09-24 15:53:07 -07003107 }
3108
Maarten Lankhorstec193642019-06-28 10:55:17 +02003109 pipe_wm->pipe_enabled = crtc_state->base.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003110 if (sprstate) {
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003111 pipe_wm->sprites_enabled = sprstate->base.visible;
3112 pipe_wm->sprites_scaled = sprstate->base.visible &&
3113 (drm_rect_width(&sprstate->base.dst) != drm_rect_width(&sprstate->base.src) >> 16 ||
3114 drm_rect_height(&sprstate->base.dst) != drm_rect_height(&sprstate->base.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003115 }
3116
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003117 usable_level = max_level;
3118
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003119 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003120 if (INTEL_GEN(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003121 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003122
3123 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08003124 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003125 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003126
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003127 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
Maarten Lankhorstec193642019-06-28 10:55:17 +02003128 ilk_compute_wm_level(dev_priv, intel_crtc, 0, crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003129 pristate, sprstate, curstate, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003130
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003131 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Maarten Lankhorstec193642019-06-28 10:55:17 +02003132 pipe_wm->linetime = hsw_compute_linetime_wm(crtc_state);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003133
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003134 if (!ilk_validate_pipe_wm(dev_priv, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01003135 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003136
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003137 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003138
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003139 for (level = 1; level <= usable_level; level++) {
3140 struct intel_wm_level *wm = &pipe_wm->wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003141
Maarten Lankhorstec193642019-06-28 10:55:17 +02003142 ilk_compute_wm_level(dev_priv, intel_crtc, level, crtc_state,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003143 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003144
3145 /*
3146 * Disable any watermark level that exceeds the
3147 * register maximums since such watermarks are
3148 * always invalid.
3149 */
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003150 if (!ilk_validate_wm_level(level, &max, wm)) {
3151 memset(wm, 0, sizeof(*wm));
3152 break;
3153 }
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003154 }
3155
Matt Roper86c8bbb2015-09-24 15:53:16 -07003156 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003157}
3158
3159/*
Matt Ropered4a6a72016-02-23 17:20:13 -08003160 * Build a set of 'intermediate' watermark values that satisfy both the old
3161 * state and the new state. These can be programmed to the hardware
3162 * immediately.
3163 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003164static int ilk_compute_intermediate_wm(struct intel_crtc_state *newstate)
Matt Ropered4a6a72016-02-23 17:20:13 -08003165{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003166 struct intel_crtc *intel_crtc = to_intel_crtc(newstate->base.crtc);
3167 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Matt Ropere8f1f022016-05-12 07:05:55 -07003168 struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003169 struct intel_atomic_state *intel_state =
3170 to_intel_atomic_state(newstate->base.state);
3171 const struct intel_crtc_state *oldstate =
3172 intel_atomic_get_old_crtc_state(intel_state, intel_crtc);
3173 const struct intel_pipe_wm *b = &oldstate->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003174 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08003175
3176 /*
3177 * Start with the final, target watermarks, then combine with the
3178 * currently active watermarks to get values that are safe both before
3179 * and after the vblank.
3180 */
Matt Ropere8f1f022016-05-12 07:05:55 -07003181 *a = newstate->wm.ilk.optimal;
Ville Syrjäläf255c622018-11-08 17:10:13 +02003182 if (!newstate->base.active || drm_atomic_crtc_needs_modeset(&newstate->base) ||
3183 intel_state->skip_intermediate_wm)
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003184 return 0;
3185
Matt Ropered4a6a72016-02-23 17:20:13 -08003186 a->pipe_enabled |= b->pipe_enabled;
3187 a->sprites_enabled |= b->sprites_enabled;
3188 a->sprites_scaled |= b->sprites_scaled;
3189
3190 for (level = 0; level <= max_level; level++) {
3191 struct intel_wm_level *a_wm = &a->wm[level];
3192 const struct intel_wm_level *b_wm = &b->wm[level];
3193
3194 a_wm->enable &= b_wm->enable;
3195 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
3196 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
3197 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
3198 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
3199 }
3200
3201 /*
3202 * We need to make sure that these merged watermark values are
3203 * actually a valid configuration themselves. If they're not,
3204 * there's no safe way to transition from the old state to
3205 * the new state, so we need to fail the atomic transaction.
3206 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003207 if (!ilk_validate_pipe_wm(dev_priv, a))
Matt Ropered4a6a72016-02-23 17:20:13 -08003208 return -EINVAL;
3209
3210 /*
3211 * If our intermediate WM are identical to the final WM, then we can
3212 * omit the post-vblank programming; only update if it's different.
3213 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02003214 if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) != 0)
3215 newstate->wm.need_postvbl_update = true;
Matt Ropered4a6a72016-02-23 17:20:13 -08003216
3217 return 0;
3218}
3219
3220/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003221 * Merge the watermarks from all active pipes for a specific level.
3222 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003223static void ilk_merge_wm_level(struct drm_i915_private *dev_priv,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003224 int level,
3225 struct intel_wm_level *ret_wm)
3226{
3227 const struct intel_crtc *intel_crtc;
3228
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003229 ret_wm->enable = true;
3230
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003231 for_each_intel_crtc(&dev_priv->drm, intel_crtc) {
Matt Ropered4a6a72016-02-23 17:20:13 -08003232 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02003233 const struct intel_wm_level *wm = &active->wm[level];
3234
3235 if (!active->pipe_enabled)
3236 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003237
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003238 /*
3239 * The watermark values may have been used in the past,
3240 * so we must maintain them in the registers for some
3241 * time even if the level is now disabled.
3242 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003243 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003244 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003245
3246 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
3247 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
3248 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
3249 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
3250 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003251}
3252
3253/*
3254 * Merge all low power watermarks for all active pipes.
3255 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003256static void ilk_wm_merge(struct drm_i915_private *dev_priv,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003257 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02003258 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003259 struct intel_pipe_wm *merged)
3260{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003261 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003262 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003263
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003264 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003265 if ((INTEL_GEN(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003266 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03003267 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003268
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003269 /* ILK: FBC WM must be disabled always */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003270 merged->fbc_wm_enabled = INTEL_GEN(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003271
3272 /* merge each WM1+ level */
3273 for (level = 1; level <= max_level; level++) {
3274 struct intel_wm_level *wm = &merged->wm[level];
3275
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003276 ilk_merge_wm_level(dev_priv, level, wm);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003277
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003278 if (level > last_enabled_level)
3279 wm->enable = false;
3280 else if (!ilk_validate_wm_level(level, max, wm))
3281 /* make sure all following levels get disabled */
3282 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003283
3284 /*
3285 * The spec says it is preferred to disable
3286 * FBC WMs instead of disabling a WM level.
3287 */
3288 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003289 if (wm->enable)
3290 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003291 wm->fbc_val = 0;
3292 }
3293 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003294
3295 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
3296 /*
3297 * FIXME this is racy. FBC might get enabled later.
3298 * What we should check here is whether FBC can be
3299 * enabled sometime later.
3300 */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08003301 if (IS_GEN(dev_priv, 5) && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03003302 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003303 for (level = 2; level <= max_level; level++) {
3304 struct intel_wm_level *wm = &merged->wm[level];
3305
3306 wm->enable = false;
3307 }
3308 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003309}
3310
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003311static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
3312{
3313 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
3314 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
3315}
3316
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003317/* The value we need to program into the WM_LPx latency field */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003318static unsigned int ilk_wm_lp_latency(struct drm_i915_private *dev_priv,
3319 int level)
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003320{
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003321 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003322 return 2 * level;
3323 else
3324 return dev_priv->wm.pri_latency[level];
3325}
3326
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003327static void ilk_compute_wm_results(struct drm_i915_private *dev_priv,
Ville Syrjälä0362c782013-10-09 19:17:57 +03003328 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03003329 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02003330 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003331{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003332 struct intel_crtc *intel_crtc;
3333 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003334
Ville Syrjälä0362c782013-10-09 19:17:57 +03003335 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03003336 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003337
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003338 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03003339 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03003340 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003341
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003342 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003343
Ville Syrjälä0362c782013-10-09 19:17:57 +03003344 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03003345
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003346 /*
3347 * Maintain the watermark values even if the level is
3348 * disabled. Doing otherwise could cause underruns.
3349 */
3350 results->wm_lp[wm_lp - 1] =
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003351 (ilk_wm_lp_latency(dev_priv, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07003352 (r->pri_val << WM1_LP_SR_SHIFT) |
3353 r->cur_val;
3354
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003355 if (r->enable)
3356 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
3357
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003358 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07003359 results->wm_lp[wm_lp - 1] |=
3360 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
3361 else
3362 results->wm_lp[wm_lp - 1] |=
3363 r->fbc_val << WM1_LP_FBC_SHIFT;
3364
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003365 /*
3366 * Always set WM1S_LP_EN when spr_val != 0, even if the
3367 * level is disabled. Doing otherwise could cause underruns.
3368 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003369 if (INTEL_GEN(dev_priv) <= 6 && r->spr_val) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003370 WARN_ON(wm_lp != 1);
3371 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
3372 } else
3373 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003374 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003375
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003376 /* LP0 register values */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003377 for_each_intel_crtc(&dev_priv->drm, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003378 enum pipe pipe = intel_crtc->pipe;
Matt Ropered4a6a72016-02-23 17:20:13 -08003379 const struct intel_wm_level *r =
3380 &intel_crtc->wm.active.ilk.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003381
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003382 if (WARN_ON(!r->enable))
3383 continue;
3384
Matt Ropered4a6a72016-02-23 17:20:13 -08003385 results->wm_linetime[pipe] = intel_crtc->wm.active.ilk.linetime;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003386
3387 results->wm_pipe[pipe] =
3388 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
3389 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
3390 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003391 }
3392}
3393
Paulo Zanoni861f3382013-05-31 10:19:21 -03003394/* Find the result with the highest level enabled. Check for enable_fbc_wm in
3395 * case both are at the same level. Prefer r1 in case they're the same. */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003396static struct intel_pipe_wm *
3397ilk_find_best_result(struct drm_i915_private *dev_priv,
3398 struct intel_pipe_wm *r1,
3399 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003400{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003401 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003402 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003403
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003404 for (level = 1; level <= max_level; level++) {
3405 if (r1->wm[level].enable)
3406 level1 = level;
3407 if (r2->wm[level].enable)
3408 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003409 }
3410
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003411 if (level1 == level2) {
3412 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003413 return r2;
3414 else
3415 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003416 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03003417 return r1;
3418 } else {
3419 return r2;
3420 }
3421}
3422
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003423/* dirty bits used to track which watermarks need changes */
3424#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
3425#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
3426#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
3427#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
3428#define WM_DIRTY_FBC (1 << 24)
3429#define WM_DIRTY_DDB (1 << 25)
3430
Damien Lespiau055e3932014-08-18 13:49:10 +01003431static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02003432 const struct ilk_wm_values *old,
3433 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003434{
3435 unsigned int dirty = 0;
3436 enum pipe pipe;
3437 int wm_lp;
3438
Damien Lespiau055e3932014-08-18 13:49:10 +01003439 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003440 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
3441 dirty |= WM_DIRTY_LINETIME(pipe);
3442 /* Must disable LP1+ watermarks too */
3443 dirty |= WM_DIRTY_LP_ALL;
3444 }
3445
3446 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
3447 dirty |= WM_DIRTY_PIPE(pipe);
3448 /* Must disable LP1+ watermarks too */
3449 dirty |= WM_DIRTY_LP_ALL;
3450 }
3451 }
3452
3453 if (old->enable_fbc_wm != new->enable_fbc_wm) {
3454 dirty |= WM_DIRTY_FBC;
3455 /* Must disable LP1+ watermarks too */
3456 dirty |= WM_DIRTY_LP_ALL;
3457 }
3458
3459 if (old->partitioning != new->partitioning) {
3460 dirty |= WM_DIRTY_DDB;
3461 /* Must disable LP1+ watermarks too */
3462 dirty |= WM_DIRTY_LP_ALL;
3463 }
3464
3465 /* LP1+ watermarks already deemed dirty, no need to continue */
3466 if (dirty & WM_DIRTY_LP_ALL)
3467 return dirty;
3468
3469 /* Find the lowest numbered LP1+ watermark in need of an update... */
3470 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3471 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
3472 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
3473 break;
3474 }
3475
3476 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
3477 for (; wm_lp <= 3; wm_lp++)
3478 dirty |= WM_DIRTY_LP(wm_lp);
3479
3480 return dirty;
3481}
3482
Ville Syrjälä8553c182013-12-05 15:51:39 +02003483static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
3484 unsigned int dirty)
3485{
Imre Deak820c1982013-12-17 14:46:36 +02003486 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02003487 bool changed = false;
3488
3489 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
3490 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
3491 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
3492 changed = true;
3493 }
3494 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
3495 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
3496 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
3497 changed = true;
3498 }
3499 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
3500 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
3501 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
3502 changed = true;
3503 }
3504
3505 /*
3506 * Don't touch WM1S_LP_EN here.
3507 * Doing so could cause underruns.
3508 */
3509
3510 return changed;
3511}
3512
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003513/*
3514 * The spec says we shouldn't write when we don't need, because every write
3515 * causes WMs to be re-evaluated, expending some power.
3516 */
Imre Deak820c1982013-12-17 14:46:36 +02003517static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
3518 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003519{
Imre Deak820c1982013-12-17 14:46:36 +02003520 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003521 unsigned int dirty;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003522 u32 val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003523
Damien Lespiau055e3932014-08-18 13:49:10 +01003524 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003525 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003526 return;
3527
Ville Syrjälä8553c182013-12-05 15:51:39 +02003528 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003529
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003530 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003531 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003532 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003533 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003534 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003535 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
3536
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003537 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003538 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003539 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003540 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003541 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003542 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
3543
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003544 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003545 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003546 val = I915_READ(WM_MISC);
3547 if (results->partitioning == INTEL_DDB_PART_1_2)
3548 val &= ~WM_MISC_DATA_PARTITION_5_6;
3549 else
3550 val |= WM_MISC_DATA_PARTITION_5_6;
3551 I915_WRITE(WM_MISC, val);
3552 } else {
3553 val = I915_READ(DISP_ARB_CTL2);
3554 if (results->partitioning == INTEL_DDB_PART_1_2)
3555 val &= ~DISP_DATA_PARTITION_5_6;
3556 else
3557 val |= DISP_DATA_PARTITION_5_6;
3558 I915_WRITE(DISP_ARB_CTL2, val);
3559 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003560 }
3561
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003562 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03003563 val = I915_READ(DISP_ARB_CTL);
3564 if (results->enable_fbc_wm)
3565 val &= ~DISP_FBC_WM_DIS;
3566 else
3567 val |= DISP_FBC_WM_DIS;
3568 I915_WRITE(DISP_ARB_CTL, val);
3569 }
3570
Imre Deak954911e2013-12-17 14:46:34 +02003571 if (dirty & WM_DIRTY_LP(1) &&
3572 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
3573 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
3574
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003575 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003576 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
3577 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
3578 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
3579 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
3580 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003581
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003582 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003583 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003584 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003585 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003586 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003587 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003588
3589 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003590}
3591
Matt Ropered4a6a72016-02-23 17:20:13 -08003592bool ilk_disable_lp_wm(struct drm_device *dev)
Ville Syrjälä8553c182013-12-05 15:51:39 +02003593{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003594 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003595
3596 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3597}
3598
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303599static u8 intel_enabled_dbuf_slices_num(struct drm_i915_private *dev_priv)
3600{
3601 u8 enabled_slices;
3602
3603 /* Slice 1 will always be enabled */
3604 enabled_slices = 1;
3605
3606 /* Gen prior to GEN11 have only one DBuf slice */
3607 if (INTEL_GEN(dev_priv) < 11)
3608 return enabled_slices;
3609
Imre Deak209d7352019-03-07 12:32:35 +02003610 /*
3611 * FIXME: for now we'll only ever use 1 slice; pretend that we have
3612 * only that 1 slice enabled until we have a proper way for on-demand
3613 * toggling of the second slice.
3614 */
3615 if (0 && I915_READ(DBUF_CTL_S2) & DBUF_POWER_STATE)
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303616 enabled_slices++;
3617
3618 return enabled_slices;
3619}
3620
Matt Roper024c9042015-09-24 15:53:11 -07003621/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003622 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3623 * so assume we'll always need it in order to avoid underruns.
3624 */
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003625static bool skl_needs_memory_bw_wa(struct drm_i915_private *dev_priv)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003626{
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003627 return IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003628}
3629
Paulo Zanoni56feca92016-09-22 18:00:28 -03003630static bool
3631intel_has_sagv(struct drm_i915_private *dev_priv)
3632{
Lucas De Marchi8ffa4392019-09-04 14:34:18 -07003633 /* HACK! */
3634 if (IS_GEN(dev_priv, 12))
3635 return false;
3636
Rodrigo Vivi1ca2b062018-10-26 13:03:17 -07003637 return (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) &&
3638 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED;
Paulo Zanoni56feca92016-09-22 18:00:28 -03003639}
3640
James Ausmusb068a862019-10-09 10:23:14 -07003641static void
3642skl_setup_sagv_block_time(struct drm_i915_private *dev_priv)
3643{
James Ausmusda80f042019-10-09 10:23:15 -07003644 if (INTEL_GEN(dev_priv) >= 12) {
3645 u32 val = 0;
3646 int ret;
3647
3648 ret = sandybridge_pcode_read(dev_priv,
3649 GEN12_PCODE_READ_SAGV_BLOCK_TIME_US,
3650 &val, NULL);
3651 if (!ret) {
3652 dev_priv->sagv_block_time_us = val;
3653 return;
3654 }
3655
3656 DRM_DEBUG_DRIVER("Couldn't read SAGV block time!\n");
3657 } else if (IS_GEN(dev_priv, 11)) {
James Ausmusb068a862019-10-09 10:23:14 -07003658 dev_priv->sagv_block_time_us = 10;
3659 return;
3660 } else if (IS_GEN(dev_priv, 10)) {
3661 dev_priv->sagv_block_time_us = 20;
3662 return;
3663 } else if (IS_GEN(dev_priv, 9)) {
3664 dev_priv->sagv_block_time_us = 30;
3665 return;
3666 } else {
3667 MISSING_CASE(INTEL_GEN(dev_priv));
3668 }
3669
3670 /* Default to an unusable block time */
3671 dev_priv->sagv_block_time_us = -1;
3672}
3673
Lyude656d1b82016-08-17 15:55:54 -04003674/*
3675 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3676 * depending on power and performance requirements. The display engine access
3677 * to system memory is blocked during the adjustment time. Because of the
3678 * blocking time, having this enabled can cause full system hangs and/or pipe
3679 * underruns if we don't meet all of the following requirements:
3680 *
3681 * - <= 1 pipe enabled
3682 * - All planes can enable watermarks for latencies >= SAGV engine block time
3683 * - We're not using an interlaced display configuration
3684 */
3685int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003686intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003687{
3688 int ret;
3689
Paulo Zanoni56feca92016-09-22 18:00:28 -03003690 if (!intel_has_sagv(dev_priv))
3691 return 0;
3692
3693 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003694 return 0;
3695
Ville Syrjäläff61a972018-12-21 19:14:34 +02003696 DRM_DEBUG_KMS("Enabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003697 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3698 GEN9_SAGV_ENABLE);
3699
Ville Syrjäläff61a972018-12-21 19:14:34 +02003700 /* We don't need to wait for SAGV when enabling */
Lyude656d1b82016-08-17 15:55:54 -04003701
3702 /*
3703 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003704 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003705 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003706 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003707 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003708 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003709 return 0;
3710 } else if (ret < 0) {
Ville Syrjäläff61a972018-12-21 19:14:34 +02003711 DRM_ERROR("Failed to enable SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003712 return ret;
3713 }
3714
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003715 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003716 return 0;
3717}
3718
Lyude656d1b82016-08-17 15:55:54 -04003719int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003720intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003721{
Imre Deakb3b8e992016-12-05 18:27:38 +02003722 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003723
Paulo Zanoni56feca92016-09-22 18:00:28 -03003724 if (!intel_has_sagv(dev_priv))
3725 return 0;
3726
3727 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003728 return 0;
3729
Ville Syrjäläff61a972018-12-21 19:14:34 +02003730 DRM_DEBUG_KMS("Disabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003731 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003732 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3733 GEN9_SAGV_DISABLE,
3734 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3735 1);
Lyude656d1b82016-08-17 15:55:54 -04003736 /*
3737 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003738 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003739 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003740 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003741 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003742 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003743 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003744 } else if (ret < 0) {
Ville Syrjäläff61a972018-12-21 19:14:34 +02003745 DRM_ERROR("Failed to disable SAGV (%d)\n", ret);
Imre Deakb3b8e992016-12-05 18:27:38 +02003746 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003747 }
3748
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003749 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003750 return 0;
3751}
3752
Maarten Lankhorst855e0d62019-06-28 10:55:13 +02003753bool intel_can_enable_sagv(struct intel_atomic_state *state)
Lyude656d1b82016-08-17 15:55:54 -04003754{
Maarten Lankhorst855e0d62019-06-28 10:55:13 +02003755 struct drm_device *dev = state->base.dev;
Lyude656d1b82016-08-17 15:55:54 -04003756 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003757 struct intel_crtc *crtc;
3758 struct intel_plane *plane;
Maarten Lankhorstec193642019-06-28 10:55:17 +02003759 struct intel_crtc_state *crtc_state;
Lyude656d1b82016-08-17 15:55:54 -04003760 enum pipe pipe;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003761 int level, latency;
Lyude656d1b82016-08-17 15:55:54 -04003762
Paulo Zanoni56feca92016-09-22 18:00:28 -03003763 if (!intel_has_sagv(dev_priv))
3764 return false;
3765
Lyude656d1b82016-08-17 15:55:54 -04003766 /*
Lyude656d1b82016-08-17 15:55:54 -04003767 * If there are no active CRTCs, no additional checks need be performed
3768 */
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003769 if (hweight8(state->active_pipes) == 0)
Lyude656d1b82016-08-17 15:55:54 -04003770 return true;
Lucas De Marchida172232019-04-04 16:04:26 -07003771
3772 /*
3773 * SKL+ workaround: bspec recommends we disable SAGV when we have
3774 * more then one pipe enabled
3775 */
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003776 if (hweight8(state->active_pipes) > 1)
Lyude656d1b82016-08-17 15:55:54 -04003777 return false;
3778
3779 /* Since we're now guaranteed to only have one active CRTC... */
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03003780 pipe = ffs(state->active_pipes) - 1;
Ville Syrjälä98187832016-10-31 22:37:10 +02003781 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Maarten Lankhorstec193642019-06-28 10:55:17 +02003782 crtc_state = to_intel_crtc_state(crtc->base.state);
Lyude656d1b82016-08-17 15:55:54 -04003783
Paulo Zanonic89cadd2016-10-10 17:30:59 -03003784 if (crtc->base.state->adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003785 return false;
3786
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003787 for_each_intel_plane_on_crtc(dev, crtc, plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003788 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02003789 &crtc_state->wm.skl.optimal.planes[plane->id];
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003790
Lyude656d1b82016-08-17 15:55:54 -04003791 /* Skip this plane if it's not enabled */
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003792 if (!wm->wm[0].plane_en)
Lyude656d1b82016-08-17 15:55:54 -04003793 continue;
3794
3795 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003796 for (level = ilk_wm_max_level(dev_priv);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003797 !wm->wm[level].plane_en; --level)
Lyude656d1b82016-08-17 15:55:54 -04003798 { }
3799
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003800 latency = dev_priv->wm.skl_latency[level];
3801
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003802 if (skl_needs_memory_bw_wa(dev_priv) &&
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003803 plane->base.state->fb->modifier ==
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003804 I915_FORMAT_MOD_X_TILED)
3805 latency += 15;
3806
Lyude656d1b82016-08-17 15:55:54 -04003807 /*
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003808 * If any of the planes on this pipe don't enable wm levels that
3809 * incur memory latencies higher than sagv_block_time_us we
Ville Syrjäläff61a972018-12-21 19:14:34 +02003810 * can't enable SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003811 */
James Ausmusb068a862019-10-09 10:23:14 -07003812 if (latency < dev_priv->sagv_block_time_us)
Lyude656d1b82016-08-17 15:55:54 -04003813 return false;
3814 }
3815
3816 return true;
3817}
3818
Mahesh Kumaraaa02372018-07-31 19:54:44 +05303819static u16 intel_get_ddb_size(struct drm_i915_private *dev_priv,
Maarten Lankhorstec193642019-06-28 10:55:17 +02003820 const struct intel_crtc_state *crtc_state,
Maarten Lankhorst24719e92018-10-22 12:20:00 +02003821 const u64 total_data_rate,
Mahesh Kumaraaa02372018-07-31 19:54:44 +05303822 const int num_active,
3823 struct skl_ddb_allocation *ddb)
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303824{
3825 const struct drm_display_mode *adjusted_mode;
3826 u64 total_data_bw;
3827 u16 ddb_size = INTEL_INFO(dev_priv)->ddb_size;
3828
3829 WARN_ON(ddb_size == 0);
3830
3831 if (INTEL_GEN(dev_priv) < 11)
3832 return ddb_size - 4; /* 4 blocks for bypass path allocation */
3833
Maarten Lankhorstec193642019-06-28 10:55:17 +02003834 adjusted_mode = &crtc_state->base.adjusted_mode;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02003835 total_data_bw = total_data_rate * drm_mode_vrefresh(adjusted_mode);
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303836
3837 /*
3838 * 12GB/s is maximum BW supported by single DBuf slice.
Ville Syrjäläad3e7b82019-01-30 17:51:10 +02003839 *
3840 * FIXME dbuf slice code is broken:
3841 * - must wait for planes to stop using the slice before powering it off
3842 * - plane straddling both slices is illegal in multi-pipe scenarios
3843 * - should validate we stay within the hw bandwidth limits
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303844 */
Ville Syrjäläad3e7b82019-01-30 17:51:10 +02003845 if (0 && (num_active > 1 || total_data_bw >= GBps(12))) {
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303846 ddb->enabled_slices = 2;
3847 } else {
3848 ddb->enabled_slices = 1;
3849 ddb_size /= 2;
3850 }
3851
3852 return ddb_size;
3853}
3854
Damien Lespiaub9cec072014-11-04 17:06:43 +00003855static void
Maarten Lankhorstb048a002018-10-18 13:51:30 +02003856skl_ddb_get_pipe_allocation_limits(struct drm_i915_private *dev_priv,
Maarten Lankhorstec193642019-06-28 10:55:17 +02003857 const struct intel_crtc_state *crtc_state,
Maarten Lankhorst24719e92018-10-22 12:20:00 +02003858 const u64 total_data_rate,
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303859 struct skl_ddb_allocation *ddb,
Matt Roperc107acf2016-05-12 07:06:01 -07003860 struct skl_ddb_entry *alloc, /* out */
3861 int *num_active /* out */)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003862{
Maarten Lankhorstec193642019-06-28 10:55:17 +02003863 struct drm_atomic_state *state = crtc_state->base.state;
Matt Roperc107acf2016-05-12 07:06:01 -07003864 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorstec193642019-06-28 10:55:17 +02003865 struct drm_crtc *for_crtc = crtc_state->base.crtc;
3866 const struct intel_crtc *crtc;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303867 u32 pipe_width = 0, total_width = 0, width_before_pipe = 0;
3868 enum pipe for_pipe = to_intel_crtc(for_crtc)->pipe;
3869 u16 ddb_size;
3870 u32 i;
Matt Roperc107acf2016-05-12 07:06:01 -07003871
Maarten Lankhorstec193642019-06-28 10:55:17 +02003872 if (WARN_ON(!state) || !crtc_state->base.active) {
Damien Lespiaub9cec072014-11-04 17:06:43 +00003873 alloc->start = 0;
3874 alloc->end = 0;
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003875 *num_active = hweight8(dev_priv->active_pipes);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003876 return;
3877 }
3878
Matt Ropera6d3460e2016-05-12 07:06:04 -07003879 if (intel_state->active_pipe_changes)
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003880 *num_active = hweight8(intel_state->active_pipes);
Matt Ropera6d3460e2016-05-12 07:06:04 -07003881 else
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003882 *num_active = hweight8(dev_priv->active_pipes);
Matt Ropera6d3460e2016-05-12 07:06:04 -07003883
Maarten Lankhorstec193642019-06-28 10:55:17 +02003884 ddb_size = intel_get_ddb_size(dev_priv, crtc_state, total_data_rate,
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303885 *num_active, ddb);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003886
Matt Roperc107acf2016-05-12 07:06:01 -07003887 /*
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303888 * If the state doesn't change the active CRTC's or there is no
3889 * modeset request, then there's no need to recalculate;
3890 * the existing pipe allocation limits should remain unchanged.
3891 * Note that we're safe from racing commits since any racing commit
3892 * that changes the active CRTC list or do modeset would need to
3893 * grab _all_ crtc locks, including the one we currently hold.
Matt Roperc107acf2016-05-12 07:06:01 -07003894 */
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303895 if (!intel_state->active_pipe_changes && !intel_state->modeset) {
Maarten Lankhorst512b5522016-11-08 13:55:34 +01003896 /*
3897 * alloc may be cleared by clear_intel_crtc_state,
3898 * copy from old state to be sure
3899 */
3900 *alloc = to_intel_crtc_state(for_crtc->state)->wm.skl.ddb;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003901 return;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003902 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07003903
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303904 /*
3905 * Watermark/ddb requirement highly depends upon width of the
3906 * framebuffer, So instead of allocating DDB equally among pipes
3907 * distribute DDB based on resolution/width of the display.
3908 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02003909 for_each_new_intel_crtc_in_state(intel_state, crtc, crtc_state, i) {
3910 const struct drm_display_mode *adjusted_mode =
3911 &crtc_state->base.adjusted_mode;
3912 enum pipe pipe = crtc->pipe;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303913 int hdisplay, vdisplay;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303914
Maarten Lankhorstec193642019-06-28 10:55:17 +02003915 if (!crtc_state->base.enable)
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303916 continue;
3917
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303918 drm_mode_get_hv_timing(adjusted_mode, &hdisplay, &vdisplay);
3919 total_width += hdisplay;
3920
3921 if (pipe < for_pipe)
3922 width_before_pipe += hdisplay;
3923 else if (pipe == for_pipe)
3924 pipe_width = hdisplay;
3925 }
3926
3927 alloc->start = ddb_size * width_before_pipe / total_width;
3928 alloc->end = ddb_size * (width_before_pipe + pipe_width) / total_width;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003929}
3930
Ville Syrjälädf331de2019-03-19 18:03:11 +02003931static int skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
3932 int width, const struct drm_format_info *format,
3933 u64 modifier, unsigned int rotation,
3934 u32 plane_pixel_rate, struct skl_wm_params *wp,
3935 int color_plane);
Maarten Lankhorstec193642019-06-28 10:55:17 +02003936static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Ville Syrjälädf331de2019-03-19 18:03:11 +02003937 int level,
3938 const struct skl_wm_params *wp,
3939 const struct skl_wm_level *result_prev,
3940 struct skl_wm_level *result /* out */);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003941
Ville Syrjälädf331de2019-03-19 18:03:11 +02003942static unsigned int
3943skl_cursor_allocation(const struct intel_crtc_state *crtc_state,
3944 int num_active)
3945{
3946 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
3947 int level, max_level = ilk_wm_max_level(dev_priv);
3948 struct skl_wm_level wm = {};
3949 int ret, min_ddb_alloc = 0;
3950 struct skl_wm_params wp;
3951
3952 ret = skl_compute_wm_params(crtc_state, 256,
3953 drm_format_info(DRM_FORMAT_ARGB8888),
3954 DRM_FORMAT_MOD_LINEAR,
3955 DRM_MODE_ROTATE_0,
3956 crtc_state->pixel_rate, &wp, 0);
3957 WARN_ON(ret);
3958
3959 for (level = 0; level <= max_level; level++) {
Ville Syrjälä6086e472019-03-21 19:51:28 +02003960 skl_compute_plane_wm(crtc_state, level, &wp, &wm, &wm);
Ville Syrjälädf331de2019-03-19 18:03:11 +02003961 if (wm.min_ddb_alloc == U16_MAX)
3962 break;
3963
3964 min_ddb_alloc = wm.min_ddb_alloc;
3965 }
3966
3967 return max(num_active == 1 ? 32 : 8, min_ddb_alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003968}
3969
Mahesh Kumar37cde112018-04-26 19:55:17 +05303970static void skl_ddb_entry_init_from_hw(struct drm_i915_private *dev_priv,
3971 struct skl_ddb_entry *entry, u32 reg)
Damien Lespiaua269c582014-11-04 17:06:49 +00003972{
Mahesh Kumar37cde112018-04-26 19:55:17 +05303973
Ville Syrjäläd7e449a2019-02-05 22:50:56 +02003974 entry->start = reg & DDB_ENTRY_MASK;
3975 entry->end = (reg >> DDB_ENTRY_END_SHIFT) & DDB_ENTRY_MASK;
Mahesh Kumar37cde112018-04-26 19:55:17 +05303976
Damien Lespiau16160e32014-11-04 17:06:53 +00003977 if (entry->end)
3978 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00003979}
3980
Mahesh Kumarddf34312018-04-09 09:11:03 +05303981static void
3982skl_ddb_get_hw_plane_state(struct drm_i915_private *dev_priv,
3983 const enum pipe pipe,
3984 const enum plane_id plane_id,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02003985 struct skl_ddb_entry *ddb_y,
3986 struct skl_ddb_entry *ddb_uv)
Mahesh Kumarddf34312018-04-09 09:11:03 +05303987{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02003988 u32 val, val2;
3989 u32 fourcc = 0;
Mahesh Kumarddf34312018-04-09 09:11:03 +05303990
3991 /* Cursor doesn't support NV12/planar, so no extra calculation needed */
3992 if (plane_id == PLANE_CURSOR) {
3993 val = I915_READ(CUR_BUF_CFG(pipe));
Ville Syrjäläff43bc32018-11-27 18:59:00 +02003994 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
Mahesh Kumarddf34312018-04-09 09:11:03 +05303995 return;
3996 }
3997
3998 val = I915_READ(PLANE_CTL(pipe, plane_id));
3999
4000 /* No DDB allocated for disabled planes */
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004001 if (val & PLANE_CTL_ENABLE)
4002 fourcc = skl_format_to_fourcc(val & PLANE_CTL_FORMAT_MASK,
4003 val & PLANE_CTL_ORDER_RGBX,
4004 val & PLANE_CTL_ALPHA_MASK);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304005
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004006 if (INTEL_GEN(dev_priv) >= 11) {
4007 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
4008 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4009 } else {
4010 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
Paulo Zanoni12a6c932018-07-31 17:46:14 -07004011 val2 = I915_READ(PLANE_NV12_BUF_CFG(pipe, plane_id));
Mahesh Kumarddf34312018-04-09 09:11:03 +05304012
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004013 if (fourcc &&
4014 drm_format_info_is_yuv_semiplanar(drm_format_info(fourcc)))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004015 swap(val, val2);
4016
4017 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4018 skl_ddb_entry_init_from_hw(dev_priv, ddb_uv, val2);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304019 }
4020}
4021
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004022void skl_pipe_ddb_get_hw_state(struct intel_crtc *crtc,
4023 struct skl_ddb_entry *ddb_y,
4024 struct skl_ddb_entry *ddb_uv)
4025{
4026 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4027 enum intel_display_power_domain power_domain;
4028 enum pipe pipe = crtc->pipe;
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004029 intel_wakeref_t wakeref;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004030 enum plane_id plane_id;
4031
4032 power_domain = POWER_DOMAIN_PIPE(pipe);
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004033 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
4034 if (!wakeref)
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004035 return;
4036
4037 for_each_plane_id_on_crtc(crtc, plane_id)
4038 skl_ddb_get_hw_plane_state(dev_priv, pipe,
4039 plane_id,
4040 &ddb_y[plane_id],
4041 &ddb_uv[plane_id]);
4042
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004043 intel_display_power_put(dev_priv, power_domain, wakeref);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004044}
4045
Damien Lespiau08db6652014-11-04 17:06:52 +00004046void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
4047 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00004048{
Mahesh Kumar74bd8002018-04-26 19:55:15 +05304049 ddb->enabled_slices = intel_enabled_dbuf_slices_num(dev_priv);
Damien Lespiaua269c582014-11-04 17:06:49 +00004050}
4051
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004052/*
4053 * Determines the downscale amount of a plane for the purposes of watermark calculations.
4054 * The bspec defines downscale amount as:
4055 *
4056 * """
4057 * Horizontal down scale amount = maximum[1, Horizontal source size /
4058 * Horizontal destination size]
4059 * Vertical down scale amount = maximum[1, Vertical source size /
4060 * Vertical destination size]
4061 * Total down scale amount = Horizontal down scale amount *
4062 * Vertical down scale amount
4063 * """
4064 *
4065 * Return value is provided in 16.16 fixed point form to retain fractional part.
4066 * Caller should take care of dividing & rounding off the value.
4067 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304068static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02004069skl_plane_downscale_amount(const struct intel_crtc_state *crtc_state,
4070 const struct intel_plane_state *plane_state)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004071{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004072 u32 src_w, src_h, dst_w, dst_h;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304073 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
4074 uint_fixed_16_16_t downscale_h, downscale_w;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004075
Maarten Lankhorstec193642019-06-28 10:55:17 +02004076 if (WARN_ON(!intel_wm_plane_visible(crtc_state, plane_state)))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304077 return u32_to_fixed16(0);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004078
Maarten Lankhorst3a612762019-10-04 13:34:54 +02004079 /*
4080 * Src coordinates are already rotated by 270 degrees for
4081 * the 90/270 degree plane rotation cases (to match the
4082 * GTT mapping), hence no need to account for rotation here.
4083 *
4084 * n.b., src is 16.16 fixed point, dst is whole integer.
4085 */
4086 src_w = drm_rect_width(&plane_state->base.src) >> 16;
4087 src_h = drm_rect_height(&plane_state->base.src) >> 16;
4088 dst_w = drm_rect_width(&plane_state->base.dst);
4089 dst_h = drm_rect_height(&plane_state->base.dst);
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004090
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304091 fp_w_ratio = div_fixed16(src_w, dst_w);
4092 fp_h_ratio = div_fixed16(src_h, dst_h);
4093 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
4094 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004095
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304096 return mul_fixed16(downscale_w, downscale_h);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004097}
4098
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304099static uint_fixed_16_16_t
4100skl_pipe_downscale_amount(const struct intel_crtc_state *crtc_state)
4101{
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304102 uint_fixed_16_16_t pipe_downscale = u32_to_fixed16(1);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304103
4104 if (!crtc_state->base.enable)
4105 return pipe_downscale;
4106
4107 if (crtc_state->pch_pfit.enabled) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004108 u32 src_w, src_h, dst_w, dst_h;
4109 u32 pfit_size = crtc_state->pch_pfit.size;
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304110 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
4111 uint_fixed_16_16_t downscale_h, downscale_w;
4112
4113 src_w = crtc_state->pipe_src_w;
4114 src_h = crtc_state->pipe_src_h;
4115 dst_w = pfit_size >> 16;
4116 dst_h = pfit_size & 0xffff;
4117
4118 if (!dst_w || !dst_h)
4119 return pipe_downscale;
4120
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304121 fp_w_ratio = div_fixed16(src_w, dst_w);
4122 fp_h_ratio = div_fixed16(src_h, dst_h);
4123 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
4124 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304125
4126 pipe_downscale = mul_fixed16(downscale_w, downscale_h);
4127 }
4128
4129 return pipe_downscale;
4130}
4131
4132int skl_check_pipe_max_pixel_rate(struct intel_crtc *intel_crtc,
Maarten Lankhorstec193642019-06-28 10:55:17 +02004133 struct intel_crtc_state *crtc_state)
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304134{
Rodrigo Vivi43037c82017-10-03 15:31:42 -07004135 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Maarten Lankhorstec193642019-06-28 10:55:17 +02004136 struct drm_atomic_state *state = crtc_state->base.state;
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004137 const struct intel_plane_state *plane_state;
4138 struct intel_plane *plane;
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02004139 int crtc_clock, dotclk;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004140 u32 pipe_max_pixel_rate;
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304141 uint_fixed_16_16_t pipe_downscale;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304142 uint_fixed_16_16_t max_downscale = u32_to_fixed16(1);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304143
Maarten Lankhorstec193642019-06-28 10:55:17 +02004144 if (!crtc_state->base.enable)
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304145 return 0;
4146
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004147 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304148 uint_fixed_16_16_t plane_downscale;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304149 uint_fixed_16_16_t fp_9_div_8 = div_fixed16(9, 8);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304150 int bpp;
4151
Maarten Lankhorstec193642019-06-28 10:55:17 +02004152 if (!intel_wm_plane_visible(crtc_state, plane_state))
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304153 continue;
4154
Maarten Lankhorstec193642019-06-28 10:55:17 +02004155 if (WARN_ON(!plane_state->base.fb))
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304156 return -EINVAL;
4157
Maarten Lankhorstec193642019-06-28 10:55:17 +02004158 plane_downscale = skl_plane_downscale_amount(crtc_state, plane_state);
4159 bpp = plane_state->base.fb->format->cpp[0] * 8;
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304160 if (bpp == 64)
4161 plane_downscale = mul_fixed16(plane_downscale,
4162 fp_9_div_8);
4163
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304164 max_downscale = max_fixed16(plane_downscale, max_downscale);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304165 }
Maarten Lankhorstec193642019-06-28 10:55:17 +02004166 pipe_downscale = skl_pipe_downscale_amount(crtc_state);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304167
4168 pipe_downscale = mul_fixed16(pipe_downscale, max_downscale);
4169
Maarten Lankhorstec193642019-06-28 10:55:17 +02004170 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02004171 dotclk = to_intel_atomic_state(state)->cdclk.logical.cdclk;
4172
Rodrigo Vivi43037c82017-10-03 15:31:42 -07004173 if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10)
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02004174 dotclk *= 2;
4175
4176 pipe_max_pixel_rate = div_round_up_u32_fixed16(dotclk, pipe_downscale);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304177
4178 if (pipe_max_pixel_rate < crtc_clock) {
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02004179 DRM_DEBUG_KMS("Max supported pixel clock with scaling exceeded\n");
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304180 return -EINVAL;
4181 }
4182
4183 return 0;
4184}
4185
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004186static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004187skl_plane_relative_data_rate(const struct intel_crtc_state *crtc_state,
4188 const struct intel_plane_state *plane_state,
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004189 int color_plane)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004190{
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004191 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
4192 const struct drm_framebuffer *fb = plane_state->base.fb;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004193 u32 data_rate;
4194 u32 width = 0, height = 0;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304195 uint_fixed_16_16_t down_scale_amount;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004196 u64 rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07004197
Maarten Lankhorstec193642019-06-28 10:55:17 +02004198 if (!plane_state->base.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07004199 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02004200
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004201 if (plane->id == PLANE_CURSOR)
Matt Ropera1de91e2016-05-12 07:05:57 -07004202 return 0;
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004203
4204 if (color_plane == 1 &&
4205 !drm_format_info_is_yuv_semiplanar(fb->format))
Matt Ropera1de91e2016-05-12 07:05:57 -07004206 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004207
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004208 /*
4209 * Src coordinates are already rotated by 270 degrees for
4210 * the 90/270 degree plane rotation cases (to match the
4211 * GTT mapping), hence no need to account for rotation here.
4212 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004213 width = drm_rect_width(&plane_state->base.src) >> 16;
4214 height = drm_rect_height(&plane_state->base.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004215
Mahesh Kumarb879d582018-04-09 09:11:01 +05304216 /* UV plane does 1/2 pixel sub-sampling */
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004217 if (color_plane == 1) {
Mahesh Kumarb879d582018-04-09 09:11:01 +05304218 width /= 2;
4219 height /= 2;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004220 }
4221
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004222 data_rate = width * height;
Mahesh Kumarb879d582018-04-09 09:11:01 +05304223
Maarten Lankhorstec193642019-06-28 10:55:17 +02004224 down_scale_amount = skl_plane_downscale_amount(crtc_state, plane_state);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004225
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004226 rate = mul_round_up_u32_fixed16(data_rate, down_scale_amount);
4227
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004228 rate *= fb->format->cpp[color_plane];
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004229 return rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004230}
4231
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004232static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004233skl_get_total_relative_data_rate(struct intel_crtc_state *crtc_state,
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004234 u64 *plane_data_rate,
4235 u64 *uv_plane_data_rate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004236{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004237 struct drm_atomic_state *state = crtc_state->base.state;
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004238 struct intel_plane *plane;
4239 const struct intel_plane_state *plane_state;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004240 u64 total_data_rate = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07004241
4242 if (WARN_ON(!state))
4243 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004244
Matt Ropera1de91e2016-05-12 07:05:57 -07004245 /* Calculate and cache data rate for each plane */
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004246 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
4247 enum plane_id plane_id = plane->id;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004248 u64 rate;
Matt Roper024c9042015-09-24 15:53:11 -07004249
Mahesh Kumarb879d582018-04-09 09:11:01 +05304250 /* packed/y */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004251 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004252 plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004253 total_data_rate += rate;
Matt Roper9c74d822016-05-12 07:05:58 -07004254
Mahesh Kumarb879d582018-04-09 09:11:01 +05304255 /* uv-plane */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004256 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 1);
Mahesh Kumarb879d582018-04-09 09:11:01 +05304257 uv_plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004258 total_data_rate += rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004259 }
4260
4261 return total_data_rate;
4262}
4263
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004264static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004265icl_get_total_relative_data_rate(struct intel_crtc_state *crtc_state,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004266 u64 *plane_data_rate)
4267{
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004268 struct intel_plane *plane;
4269 const struct intel_plane_state *plane_state;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004270 u64 total_data_rate = 0;
4271
Maarten Lankhorstec193642019-06-28 10:55:17 +02004272 if (WARN_ON(!crtc_state->base.state))
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004273 return 0;
4274
4275 /* Calculate and cache data rate for each plane */
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004276 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
4277 enum plane_id plane_id = plane->id;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004278 u64 rate;
4279
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004280 if (!plane_state->planar_linked_plane) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02004281 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004282 plane_data_rate[plane_id] = rate;
4283 total_data_rate += rate;
4284 } else {
4285 enum plane_id y_plane_id;
4286
4287 /*
4288 * The slave plane might not iterate in
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004289 * intel_atomic_crtc_state_for_each_plane_state(),
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004290 * and needs the master plane state which may be
4291 * NULL if we try get_new_plane_state(), so we
4292 * always calculate from the master.
4293 */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004294 if (plane_state->planar_slave)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004295 continue;
4296
4297 /* Y plane rate is calculated on the slave */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004298 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004299 y_plane_id = plane_state->planar_linked_plane->id;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004300 plane_data_rate[y_plane_id] = rate;
4301 total_data_rate += rate;
4302
Maarten Lankhorstec193642019-06-28 10:55:17 +02004303 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004304 plane_data_rate[plane_id] = rate;
4305 total_data_rate += rate;
4306 }
4307 }
4308
4309 return total_data_rate;
4310}
4311
Matt Roperc107acf2016-05-12 07:06:01 -07004312static int
Maarten Lankhorstec193642019-06-28 10:55:17 +02004313skl_allocate_pipe_ddb(struct intel_crtc_state *crtc_state,
Damien Lespiaub9cec072014-11-04 17:06:43 +00004314 struct skl_ddb_allocation *ddb /* out */)
4315{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004316 struct drm_atomic_state *state = crtc_state->base.state;
4317 struct drm_crtc *crtc = crtc_state->base.crtc;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004318 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004319 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstec193642019-06-28 10:55:17 +02004320 struct skl_ddb_entry *alloc = &crtc_state->wm.skl.ddb;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004321 u16 alloc_size, start = 0;
4322 u16 total[I915_MAX_PLANES] = {};
4323 u16 uv_total[I915_MAX_PLANES] = {};
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004324 u64 total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004325 enum plane_id plane_id;
Matt Roperc107acf2016-05-12 07:06:01 -07004326 int num_active;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004327 u64 plane_data_rate[I915_MAX_PLANES] = {};
4328 u64 uv_plane_data_rate[I915_MAX_PLANES] = {};
Ville Syrjälä0aded172019-02-05 17:50:53 +02004329 u32 blocks;
Matt Roperd8e87492018-12-11 09:31:07 -08004330 int level;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004331
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004332 /* Clear the partitioning for disabled planes. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004333 memset(crtc_state->wm.skl.plane_ddb_y, 0, sizeof(crtc_state->wm.skl.plane_ddb_y));
4334 memset(crtc_state->wm.skl.plane_ddb_uv, 0, sizeof(crtc_state->wm.skl.plane_ddb_uv));
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004335
Matt Ropera6d3460e2016-05-12 07:06:04 -07004336 if (WARN_ON(!state))
4337 return 0;
4338
Maarten Lankhorstec193642019-06-28 10:55:17 +02004339 if (!crtc_state->base.active) {
Lyudece0ba282016-09-15 10:46:35 -04004340 alloc->start = alloc->end = 0;
Matt Roperc107acf2016-05-12 07:06:01 -07004341 return 0;
4342 }
4343
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004344 if (INTEL_GEN(dev_priv) >= 11)
4345 total_data_rate =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004346 icl_get_total_relative_data_rate(crtc_state,
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004347 plane_data_rate);
4348 else
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004349 total_data_rate =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004350 skl_get_total_relative_data_rate(crtc_state,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004351 plane_data_rate,
4352 uv_plane_data_rate);
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004353
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004354
Maarten Lankhorstec193642019-06-28 10:55:17 +02004355 skl_ddb_get_pipe_allocation_limits(dev_priv, crtc_state, total_data_rate,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004356 ddb, alloc, &num_active);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00004357 alloc_size = skl_ddb_entry_size(alloc);
Kumar, Mahesh336031e2017-05-17 17:28:25 +05304358 if (alloc_size == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004359 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004360
Matt Roperd8e87492018-12-11 09:31:07 -08004361 /* Allocate fixed number of blocks for cursor. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004362 total[PLANE_CURSOR] = skl_cursor_allocation(crtc_state, num_active);
Matt Roperd8e87492018-12-11 09:31:07 -08004363 alloc_size -= total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02004364 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].start =
Matt Roperd8e87492018-12-11 09:31:07 -08004365 alloc->end - total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02004366 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].end = alloc->end;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004367
Matt Ropera1de91e2016-05-12 07:05:57 -07004368 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004369 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004370
Matt Roperd8e87492018-12-11 09:31:07 -08004371 /*
4372 * Find the highest watermark level for which we can satisfy the block
4373 * requirement of active planes.
4374 */
4375 for (level = ilk_wm_max_level(dev_priv); level >= 0; level--) {
Matt Roper25db2ea2018-12-12 11:17:20 -08004376 blocks = 0;
Matt Roperd8e87492018-12-11 09:31:07 -08004377 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004378 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004379 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä10a7e072019-03-12 22:58:40 +02004380
4381 if (plane_id == PLANE_CURSOR) {
4382 if (WARN_ON(wm->wm[level].min_ddb_alloc >
4383 total[PLANE_CURSOR])) {
4384 blocks = U32_MAX;
4385 break;
4386 }
4387 continue;
4388 }
4389
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004390 blocks += wm->wm[level].min_ddb_alloc;
4391 blocks += wm->uv_wm[level].min_ddb_alloc;
Matt Roperd8e87492018-12-11 09:31:07 -08004392 }
4393
Ville Syrjälä3cf963c2019-03-12 22:58:36 +02004394 if (blocks <= alloc_size) {
Matt Roperd8e87492018-12-11 09:31:07 -08004395 alloc_size -= blocks;
4396 break;
4397 }
4398 }
4399
4400 if (level < 0) {
4401 DRM_DEBUG_KMS("Requested display configuration exceeds system DDB limitations");
4402 DRM_DEBUG_KMS("minimum required %d/%d\n", blocks,
4403 alloc_size);
4404 return -EINVAL;
4405 }
4406
4407 /*
4408 * Grant each plane the blocks it requires at the highest achievable
4409 * watermark level, plus an extra share of the leftover blocks
4410 * proportional to its relative data rate.
4411 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004412 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004413 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004414 &crtc_state->wm.skl.optimal.planes[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08004415 u64 rate;
4416 u16 extra;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004417
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004418 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004419 continue;
4420
Damien Lespiaub9cec072014-11-04 17:06:43 +00004421 /*
Matt Roperd8e87492018-12-11 09:31:07 -08004422 * We've accounted for all active planes; remaining planes are
4423 * all disabled.
Damien Lespiaub9cec072014-11-04 17:06:43 +00004424 */
Matt Roperd8e87492018-12-11 09:31:07 -08004425 if (total_data_rate == 0)
4426 break;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004427
Matt Roperd8e87492018-12-11 09:31:07 -08004428 rate = plane_data_rate[plane_id];
4429 extra = min_t(u16, alloc_size,
4430 DIV64_U64_ROUND_UP(alloc_size * rate,
4431 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004432 total[plane_id] = wm->wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08004433 alloc_size -= extra;
4434 total_data_rate -= rate;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004435
Matt Roperd8e87492018-12-11 09:31:07 -08004436 if (total_data_rate == 0)
4437 break;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004438
Matt Roperd8e87492018-12-11 09:31:07 -08004439 rate = uv_plane_data_rate[plane_id];
4440 extra = min_t(u16, alloc_size,
4441 DIV64_U64_ROUND_UP(alloc_size * rate,
4442 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004443 uv_total[plane_id] = wm->uv_wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08004444 alloc_size -= extra;
4445 total_data_rate -= rate;
4446 }
4447 WARN_ON(alloc_size != 0 || total_data_rate != 0);
4448
4449 /* Set the actual DDB start/end points for each plane */
4450 start = alloc->start;
4451 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004452 struct skl_ddb_entry *plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004453 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004454 struct skl_ddb_entry *uv_plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004455 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08004456
4457 if (plane_id == PLANE_CURSOR)
4458 continue;
4459
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004460 /* Gen11+ uses a separate plane for UV watermarks */
Matt Roperd8e87492018-12-11 09:31:07 -08004461 WARN_ON(INTEL_GEN(dev_priv) >= 11 && uv_total[plane_id]);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004462
Matt Roperd8e87492018-12-11 09:31:07 -08004463 /* Leave disabled planes at (0,0) */
4464 if (total[plane_id]) {
4465 plane_alloc->start = start;
4466 start += total[plane_id];
4467 plane_alloc->end = start;
Matt Roperc107acf2016-05-12 07:06:01 -07004468 }
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004469
Matt Roperd8e87492018-12-11 09:31:07 -08004470 if (uv_total[plane_id]) {
4471 uv_plane_alloc->start = start;
4472 start += uv_total[plane_id];
4473 uv_plane_alloc->end = start;
4474 }
4475 }
4476
4477 /*
4478 * When we calculated watermark values we didn't know how high
4479 * of a level we'd actually be able to hit, so we just marked
4480 * all levels as "enabled." Go back now and disable the ones
4481 * that aren't actually possible.
4482 */
4483 for (level++; level <= ilk_wm_max_level(dev_priv); level++) {
4484 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004485 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004486 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjäläa301cb02019-03-12 22:58:41 +02004487
4488 /*
4489 * We only disable the watermarks for each plane if
4490 * they exceed the ddb allocation of said plane. This
4491 * is done so that we don't end up touching cursor
4492 * watermarks needlessly when some other plane reduces
4493 * our max possible watermark level.
4494 *
4495 * Bspec has this to say about the PLANE_WM enable bit:
4496 * "All the watermarks at this level for all enabled
4497 * planes must be enabled before the level will be used."
4498 * So this is actually safe to do.
4499 */
4500 if (wm->wm[level].min_ddb_alloc > total[plane_id] ||
4501 wm->uv_wm[level].min_ddb_alloc > uv_total[plane_id])
4502 memset(&wm->wm[level], 0, sizeof(wm->wm[level]));
Ville Syrjälä290248c2019-02-13 18:54:24 +02004503
Ville Syrjäläc384afe2019-02-28 19:36:39 +02004504 /*
Bob Paauwe39564ae2019-04-12 11:09:20 -07004505 * Wa_1408961008:icl, ehl
Ville Syrjäläc384afe2019-02-28 19:36:39 +02004506 * Underruns with WM1+ disabled
4507 */
Bob Paauwe39564ae2019-04-12 11:09:20 -07004508 if (IS_GEN(dev_priv, 11) &&
Ville Syrjälä290248c2019-02-13 18:54:24 +02004509 level == 1 && wm->wm[0].plane_en) {
4510 wm->wm[level].plane_res_b = wm->wm[0].plane_res_b;
Ville Syrjäläc384afe2019-02-28 19:36:39 +02004511 wm->wm[level].plane_res_l = wm->wm[0].plane_res_l;
4512 wm->wm[level].ignore_lines = wm->wm[0].ignore_lines;
Ville Syrjälä290248c2019-02-13 18:54:24 +02004513 }
Matt Roperd8e87492018-12-11 09:31:07 -08004514 }
4515 }
4516
4517 /*
4518 * Go back and disable the transition watermark if it turns out we
4519 * don't have enough DDB blocks for it.
4520 */
4521 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004522 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004523 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004524
Ville Syrjäläb19c9bc2018-12-21 19:14:31 +02004525 if (wm->trans_wm.plane_res_b >= total[plane_id])
Matt Roperd8e87492018-12-11 09:31:07 -08004526 memset(&wm->trans_wm, 0, sizeof(wm->trans_wm));
Damien Lespiaub9cec072014-11-04 17:06:43 +00004527 }
4528
Matt Roperc107acf2016-05-12 07:06:01 -07004529 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004530}
4531
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004532/*
4533 * 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 +02004534 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004535 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
4536 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
4537*/
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07004538static uint_fixed_16_16_t
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004539skl_wm_method1(const struct drm_i915_private *dev_priv, u32 pixel_rate,
4540 u8 cpp, u32 latency, u32 dbuf_block_size)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004541{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004542 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304543 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004544
4545 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304546 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004547
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304548 wm_intermediate_val = latency * pixel_rate * cpp;
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004549 ret = div_fixed16(wm_intermediate_val, 1000 * dbuf_block_size);
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07004550
4551 if (INTEL_GEN(dev_priv) >= 10)
4552 ret = add_fixed16_u32(ret, 1);
4553
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004554 return ret;
4555}
4556
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004557static uint_fixed_16_16_t
4558skl_wm_method2(u32 pixel_rate, u32 pipe_htotal, u32 latency,
4559 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004560{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004561 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304562 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004563
4564 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304565 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004566
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004567 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304568 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
4569 pipe_htotal * 1000);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304570 ret = mul_u32_fixed16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004571 return ret;
4572}
4573
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304574static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02004575intel_get_linetime_us(const struct intel_crtc_state *crtc_state)
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304576{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004577 u32 pixel_rate;
4578 u32 crtc_htotal;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304579 uint_fixed_16_16_t linetime_us;
4580
Maarten Lankhorstec193642019-06-28 10:55:17 +02004581 if (!crtc_state->base.active)
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304582 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304583
Maarten Lankhorstec193642019-06-28 10:55:17 +02004584 pixel_rate = crtc_state->pixel_rate;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304585
4586 if (WARN_ON(pixel_rate == 0))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304587 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304588
Maarten Lankhorstec193642019-06-28 10:55:17 +02004589 crtc_htotal = crtc_state->base.adjusted_mode.crtc_htotal;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304590 linetime_us = div_fixed16(crtc_htotal * 1000, pixel_rate);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304591
4592 return linetime_us;
4593}
4594
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004595static u32
Maarten Lankhorstec193642019-06-28 10:55:17 +02004596skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *crtc_state,
4597 const struct intel_plane_state *plane_state)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004598{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004599 u64 adjusted_pixel_rate;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304600 uint_fixed_16_16_t downscale_amount;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004601
4602 /* Shouldn't reach here on disabled planes... */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004603 if (WARN_ON(!intel_wm_plane_visible(crtc_state, plane_state)))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004604 return 0;
4605
4606 /*
4607 * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
4608 * with additional adjustments for plane-specific scaling.
4609 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004610 adjusted_pixel_rate = crtc_state->pixel_rate;
4611 downscale_amount = skl_plane_downscale_amount(crtc_state, plane_state);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004612
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304613 return mul_round_up_u32_fixed16(adjusted_pixel_rate,
4614 downscale_amount);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004615}
4616
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304617static int
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004618skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
4619 int width, const struct drm_format_info *format,
4620 u64 modifier, unsigned int rotation,
4621 u32 plane_pixel_rate, struct skl_wm_params *wp,
4622 int color_plane)
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304623{
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004624 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
4625 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004626 u32 interm_pbpl;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304627
Juha-Pekka Heikkiladf7d4152019-03-04 17:26:31 +05304628 /* only planar format has two planes */
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004629 if (color_plane == 1 && !drm_format_info_is_yuv_semiplanar(format)) {
Juha-Pekka Heikkiladf7d4152019-03-04 17:26:31 +05304630 DRM_DEBUG_KMS("Non planar format have single plane\n");
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05304631 return -EINVAL;
4632 }
4633
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004634 wp->y_tiled = modifier == I915_FORMAT_MOD_Y_TILED ||
4635 modifier == I915_FORMAT_MOD_Yf_TILED ||
4636 modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
4637 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
4638 wp->x_tiled = modifier == I915_FORMAT_MOD_X_TILED;
4639 wp->rc_surface = modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
4640 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004641 wp->is_planar = drm_format_info_is_yuv_semiplanar(format);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304642
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004643 wp->width = width;
Ville Syrjälä45bee432018-11-14 23:07:28 +02004644 if (color_plane == 1 && wp->is_planar)
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05304645 wp->width /= 2;
4646
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004647 wp->cpp = format->cpp[color_plane];
4648 wp->plane_pixel_rate = plane_pixel_rate;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304649
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004650 if (INTEL_GEN(dev_priv) >= 11 &&
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004651 modifier == I915_FORMAT_MOD_Yf_TILED && wp->cpp == 1)
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004652 wp->dbuf_block_size = 256;
4653 else
4654 wp->dbuf_block_size = 512;
4655
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004656 if (drm_rotation_90_or_270(rotation)) {
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304657 switch (wp->cpp) {
4658 case 1:
4659 wp->y_min_scanlines = 16;
4660 break;
4661 case 2:
4662 wp->y_min_scanlines = 8;
4663 break;
4664 case 4:
4665 wp->y_min_scanlines = 4;
4666 break;
4667 default:
4668 MISSING_CASE(wp->cpp);
4669 return -EINVAL;
4670 }
4671 } else {
4672 wp->y_min_scanlines = 4;
4673 }
4674
Ville Syrjälä60e983f2018-12-21 19:14:33 +02004675 if (skl_needs_memory_bw_wa(dev_priv))
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304676 wp->y_min_scanlines *= 2;
4677
4678 wp->plane_bytes_per_line = wp->width * wp->cpp;
4679 if (wp->y_tiled) {
4680 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line *
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004681 wp->y_min_scanlines,
4682 wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304683
4684 if (INTEL_GEN(dev_priv) >= 10)
4685 interm_pbpl++;
4686
4687 wp->plane_blocks_per_line = div_fixed16(interm_pbpl,
4688 wp->y_min_scanlines);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004689 } else if (wp->x_tiled && IS_GEN(dev_priv, 9)) {
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004690 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
4691 wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304692 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
4693 } else {
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004694 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
4695 wp->dbuf_block_size) + 1;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304696 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
4697 }
4698
4699 wp->y_tile_minimum = mul_u32_fixed16(wp->y_min_scanlines,
4700 wp->plane_blocks_per_line);
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004701
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304702 wp->linetime_us = fixed16_to_u32_round_up(
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004703 intel_get_linetime_us(crtc_state));
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304704
4705 return 0;
4706}
4707
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004708static int
4709skl_compute_plane_wm_params(const struct intel_crtc_state *crtc_state,
4710 const struct intel_plane_state *plane_state,
4711 struct skl_wm_params *wp, int color_plane)
4712{
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004713 const struct drm_framebuffer *fb = plane_state->base.fb;
4714 int width;
4715
Maarten Lankhorst3a612762019-10-04 13:34:54 +02004716 /*
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;
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004722
4723 return skl_compute_wm_params(crtc_state, width,
4724 fb->format, fb->modifier,
4725 plane_state->base.rotation,
4726 skl_adjusted_plane_pixel_rate(crtc_state, plane_state),
4727 wp, color_plane);
4728}
4729
Ville Syrjäläb52c2732018-12-21 19:14:28 +02004730static bool skl_wm_has_lines(struct drm_i915_private *dev_priv, int level)
4731{
4732 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
4733 return true;
4734
4735 /* The number of lines are ignored for the level 0 watermark. */
4736 return level > 0;
4737}
4738
Maarten Lankhorstec193642019-06-28 10:55:17 +02004739static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Matt Roperd8e87492018-12-11 09:31:07 -08004740 int level,
4741 const struct skl_wm_params *wp,
4742 const struct skl_wm_level *result_prev,
4743 struct skl_wm_level *result /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004744{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004745 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004746 u32 latency = dev_priv->wm.skl_latency[level];
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304747 uint_fixed_16_16_t method1, method2;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304748 uint_fixed_16_16_t selected_result;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004749 u32 res_blocks, res_lines, min_ddb_alloc = 0;
Ville Syrjäläce110ec2018-11-14 23:07:21 +02004750
Ville Syrjälä0aded172019-02-05 17:50:53 +02004751 if (latency == 0) {
4752 /* reject it */
4753 result->min_ddb_alloc = U16_MAX;
Ville Syrjälä692927f2018-12-21 19:14:29 +02004754 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02004755 }
Ville Syrjälä692927f2018-12-21 19:14:29 +02004756
Ville Syrjälä25312ef2019-05-03 20:38:05 +03004757 /*
4758 * WaIncreaseLatencyIPCEnabled: kbl,cfl
4759 * Display WA #1141: kbl,cfl
4760 */
Ville Syrjälä5a7d2022019-05-03 20:38:06 +03004761 if ((IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv)) ||
Rodrigo Vivi82525c12017-06-08 08:50:00 -07004762 dev_priv->ipc_enabled)
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05304763 latency += 4;
4764
Ville Syrjälä60e983f2018-12-21 19:14:33 +02004765 if (skl_needs_memory_bw_wa(dev_priv) && wp->x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03004766 latency += 15;
4767
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304768 method1 = skl_wm_method1(dev_priv, wp->plane_pixel_rate,
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004769 wp->cpp, latency, wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304770 method2 = skl_wm_method2(wp->plane_pixel_rate,
Maarten Lankhorstec193642019-06-28 10:55:17 +02004771 crtc_state->base.adjusted_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004772 latency,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304773 wp->plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004774
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304775 if (wp->y_tiled) {
4776 selected_result = max_fixed16(method2, wp->y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004777 } else {
Maarten Lankhorstec193642019-06-28 10:55:17 +02004778 if ((wp->cpp * crtc_state->base.adjusted_mode.crtc_htotal /
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004779 wp->dbuf_block_size < 1) &&
Paulo Zanoni077b5822018-10-04 16:15:57 -07004780 (wp->plane_bytes_per_line / wp->dbuf_block_size < 1)) {
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03004781 selected_result = method2;
Paulo Zanoni077b5822018-10-04 16:15:57 -07004782 } else if (latency >= wp->linetime_us) {
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004783 if (IS_GEN(dev_priv, 9) &&
Paulo Zanoni077b5822018-10-04 16:15:57 -07004784 !IS_GEMINILAKE(dev_priv))
4785 selected_result = min_fixed16(method1, method2);
4786 else
4787 selected_result = method2;
4788 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004789 selected_result = method1;
Paulo Zanoni077b5822018-10-04 16:15:57 -07004790 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004791 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004792
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304793 res_blocks = fixed16_to_u32_round_up(selected_result) + 1;
Kumar, Maheshd273ecc2017-05-17 17:28:22 +05304794 res_lines = div_round_up_fixed16(selected_result,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304795 wp->plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00004796
Paulo Zanonia5b79d32018-11-13 17:24:32 -08004797 if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv)) {
4798 /* Display WA #1125: skl,bxt,kbl */
4799 if (level == 0 && wp->rc_surface)
4800 res_blocks +=
4801 fixed16_to_u32_round_up(wp->y_tile_minimum);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07004802
Paulo Zanonia5b79d32018-11-13 17:24:32 -08004803 /* Display WA #1126: skl,bxt,kbl */
4804 if (level >= 1 && level <= 7) {
4805 if (wp->y_tiled) {
4806 res_blocks +=
4807 fixed16_to_u32_round_up(wp->y_tile_minimum);
4808 res_lines += wp->y_min_scanlines;
4809 } else {
4810 res_blocks++;
4811 }
4812
4813 /*
4814 * Make sure result blocks for higher latency levels are
4815 * atleast as high as level below the current level.
4816 * Assumption in DDB algorithm optimization for special
4817 * cases. Also covers Display WA #1125 for RC.
4818 */
4819 if (result_prev->plane_res_b > res_blocks)
4820 res_blocks = result_prev->plane_res_b;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004821 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004822 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004823
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004824 if (INTEL_GEN(dev_priv) >= 11) {
4825 if (wp->y_tiled) {
4826 int extra_lines;
4827
4828 if (res_lines % wp->y_min_scanlines == 0)
4829 extra_lines = wp->y_min_scanlines;
4830 else
4831 extra_lines = wp->y_min_scanlines * 2 -
4832 res_lines % wp->y_min_scanlines;
4833
4834 min_ddb_alloc = mul_round_up_u32_fixed16(res_lines + extra_lines,
4835 wp->plane_blocks_per_line);
4836 } else {
4837 min_ddb_alloc = res_blocks +
4838 DIV_ROUND_UP(res_blocks, 10);
4839 }
4840 }
4841
Ville Syrjäläb52c2732018-12-21 19:14:28 +02004842 if (!skl_wm_has_lines(dev_priv, level))
4843 res_lines = 0;
4844
Ville Syrjälä0aded172019-02-05 17:50:53 +02004845 if (res_lines > 31) {
4846 /* reject it */
4847 result->min_ddb_alloc = U16_MAX;
Matt Roperd8e87492018-12-11 09:31:07 -08004848 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02004849 }
Matt Roperd8e87492018-12-11 09:31:07 -08004850
4851 /*
4852 * If res_lines is valid, assume we can use this watermark level
4853 * for now. We'll come back and disable it after we calculate the
4854 * DDB allocation if it turns out we don't actually have enough
4855 * blocks to satisfy it.
4856 */
Mahesh Kumar62027b72018-04-09 09:11:05 +05304857 result->plane_res_b = res_blocks;
4858 result->plane_res_l = res_lines;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004859 /* Bspec says: value >= plane ddb allocation -> invalid, hence the +1 here */
4860 result->min_ddb_alloc = max(min_ddb_alloc, res_blocks) + 1;
Mahesh Kumar62027b72018-04-09 09:11:05 +05304861 result->plane_en = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004862}
4863
Matt Roperd8e87492018-12-11 09:31:07 -08004864static void
Maarten Lankhorstec193642019-06-28 10:55:17 +02004865skl_compute_wm_levels(const struct intel_crtc_state *crtc_state,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304866 const struct skl_wm_params *wm_params,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004867 struct skl_wm_level *levels)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004868{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004869 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304870 int level, max_level = ilk_wm_max_level(dev_priv);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004871 struct skl_wm_level *result_prev = &levels[0];
Lyudea62163e2016-10-04 14:28:20 -04004872
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304873 for (level = 0; level <= max_level; level++) {
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004874 struct skl_wm_level *result = &levels[level];
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304875
Maarten Lankhorstec193642019-06-28 10:55:17 +02004876 skl_compute_plane_wm(crtc_state, level, wm_params,
Matt Roperd8e87492018-12-11 09:31:07 -08004877 result_prev, result);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004878
4879 result_prev = result;
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304880 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004881}
4882
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004883static u32
Maarten Lankhorstec193642019-06-28 10:55:17 +02004884skl_compute_linetime_wm(const struct intel_crtc_state *crtc_state)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004885{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004886 struct drm_atomic_state *state = crtc_state->base.state;
Mahesh Kumara3a89862016-12-01 21:19:34 +05304887 struct drm_i915_private *dev_priv = to_i915(state->dev);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304888 uint_fixed_16_16_t linetime_us;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004889 u32 linetime_wm;
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03004890
Maarten Lankhorstec193642019-06-28 10:55:17 +02004891 linetime_us = intel_get_linetime_us(crtc_state);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304892 linetime_wm = fixed16_to_u32_round_up(mul_u32_fixed16(8, linetime_us));
Mahesh Kumara3a89862016-12-01 21:19:34 +05304893
Ville Syrjälä717671c2018-12-21 19:14:36 +02004894 /* Display WA #1135: BXT:ALL GLK:ALL */
4895 if (IS_GEN9_LP(dev_priv) && dev_priv->ipc_enabled)
Kumar, Mahesh446e8502017-08-17 19:15:25 +05304896 linetime_wm /= 2;
Mahesh Kumara3a89862016-12-01 21:19:34 +05304897
4898 return linetime_wm;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004899}
4900
Maarten Lankhorstec193642019-06-28 10:55:17 +02004901static void skl_compute_transition_wm(const struct intel_crtc_state *crtc_state,
Ville Syrjälä6a3c910b2018-11-14 23:07:23 +02004902 const struct skl_wm_params *wp,
Matt Roperd8e87492018-12-11 09:31:07 -08004903 struct skl_plane_wm *wm)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004904{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004905 struct drm_device *dev = crtc_state->base.crtc->dev;
Kumar, Maheshca476672017-08-17 19:15:24 +05304906 const struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004907 u16 trans_min, trans_y_tile_min;
4908 const u16 trans_amount = 10; /* This is configurable amount */
4909 u16 wm0_sel_res_b, trans_offset_b, res_blocks;
Damien Lespiau9414f562014-11-04 17:06:58 +00004910
Kumar, Maheshca476672017-08-17 19:15:24 +05304911 /* Transition WM are not recommended by HW team for GEN9 */
4912 if (INTEL_GEN(dev_priv) <= 9)
Ville Syrjälä14a43062018-11-14 23:07:22 +02004913 return;
Kumar, Maheshca476672017-08-17 19:15:24 +05304914
4915 /* Transition WM don't make any sense if ipc is disabled */
4916 if (!dev_priv->ipc_enabled)
Ville Syrjälä14a43062018-11-14 23:07:22 +02004917 return;
Kumar, Maheshca476672017-08-17 19:15:24 +05304918
Paulo Zanoni91961a82018-10-04 16:15:56 -07004919 trans_min = 14;
4920 if (INTEL_GEN(dev_priv) >= 11)
Kumar, Maheshca476672017-08-17 19:15:24 +05304921 trans_min = 4;
4922
4923 trans_offset_b = trans_min + trans_amount;
4924
Paulo Zanonicbacc792018-10-04 16:15:58 -07004925 /*
4926 * The spec asks for Selected Result Blocks for wm0 (the real value),
4927 * not Result Blocks (the integer value). Pay attention to the capital
4928 * letters. The value wm_l0->plane_res_b is actually Result Blocks, but
4929 * since Result Blocks is the ceiling of Selected Result Blocks plus 1,
4930 * and since we later will have to get the ceiling of the sum in the
4931 * transition watermarks calculation, we can just pretend Selected
4932 * Result Blocks is Result Blocks minus 1 and it should work for the
4933 * current platforms.
4934 */
Ville Syrjälä6a3c910b2018-11-14 23:07:23 +02004935 wm0_sel_res_b = wm->wm[0].plane_res_b - 1;
Paulo Zanonicbacc792018-10-04 16:15:58 -07004936
Kumar, Maheshca476672017-08-17 19:15:24 +05304937 if (wp->y_tiled) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004938 trans_y_tile_min =
4939 (u16)mul_round_up_u32_fixed16(2, wp->y_tile_minimum);
Paulo Zanonicbacc792018-10-04 16:15:58 -07004940 res_blocks = max(wm0_sel_res_b, trans_y_tile_min) +
Kumar, Maheshca476672017-08-17 19:15:24 +05304941 trans_offset_b;
4942 } else {
Paulo Zanonicbacc792018-10-04 16:15:58 -07004943 res_blocks = wm0_sel_res_b + trans_offset_b;
Kumar, Maheshca476672017-08-17 19:15:24 +05304944
4945 /* WA BUG:1938466 add one block for non y-tile planes */
4946 if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_A0))
4947 res_blocks += 1;
4948
4949 }
4950
Matt Roperd8e87492018-12-11 09:31:07 -08004951 /*
4952 * Just assume we can enable the transition watermark. After
4953 * computing the DDB we'll come back and disable it if that
4954 * assumption turns out to be false.
4955 */
4956 wm->trans_wm.plane_res_b = res_blocks + 1;
4957 wm->trans_wm.plane_en = true;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004958}
4959
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004960static int skl_build_plane_wm_single(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004961 const struct intel_plane_state *plane_state,
4962 enum plane_id plane_id, int color_plane)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004963{
Ville Syrjälä83158472018-11-27 18:57:26 +02004964 struct skl_plane_wm *wm = &crtc_state->wm.skl.optimal.planes[plane_id];
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004965 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004966 int ret;
4967
Ville Syrjälä51de9c62018-11-14 23:07:25 +02004968 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004969 &wm_params, color_plane);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004970 if (ret)
4971 return ret;
4972
Ville Syrjälä67155a62019-03-12 22:58:37 +02004973 skl_compute_wm_levels(crtc_state, &wm_params, wm->wm);
Matt Roperd8e87492018-12-11 09:31:07 -08004974 skl_compute_transition_wm(crtc_state, &wm_params, wm);
Ville Syrjälä83158472018-11-27 18:57:26 +02004975
4976 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004977}
4978
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004979static int skl_build_plane_wm_uv(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004980 const struct intel_plane_state *plane_state,
4981 enum plane_id plane_id)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004982{
Ville Syrjälä83158472018-11-27 18:57:26 +02004983 struct skl_plane_wm *wm = &crtc_state->wm.skl.optimal.planes[plane_id];
4984 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004985 int ret;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004986
Ville Syrjälä83158472018-11-27 18:57:26 +02004987 wm->is_planar = true;
4988
4989 /* uv plane watermarks must also be validated for NV12/Planar */
Ville Syrjälä51de9c62018-11-14 23:07:25 +02004990 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004991 &wm_params, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004992 if (ret)
4993 return ret;
4994
Ville Syrjälä67155a62019-03-12 22:58:37 +02004995 skl_compute_wm_levels(crtc_state, &wm_params, wm->uv_wm);
Ville Syrjälä83158472018-11-27 18:57:26 +02004996
4997 return 0;
4998}
4999
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005000static int skl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005001 const struct intel_plane_state *plane_state)
5002{
5003 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
5004 const struct drm_framebuffer *fb = plane_state->base.fb;
5005 enum plane_id plane_id = plane->id;
5006 int ret;
5007
5008 if (!intel_wm_plane_visible(crtc_state, plane_state))
5009 return 0;
5010
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005011 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005012 plane_id, 0);
5013 if (ret)
5014 return ret;
5015
5016 if (fb->format->is_yuv && fb->format->num_planes > 1) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005017 ret = skl_build_plane_wm_uv(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005018 plane_id);
5019 if (ret)
5020 return ret;
5021 }
5022
5023 return 0;
5024}
5025
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005026static int icl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005027 const struct intel_plane_state *plane_state)
5028{
5029 enum plane_id plane_id = to_intel_plane(plane_state->base.plane)->id;
5030 int ret;
5031
5032 /* Watermarks calculated in master */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005033 if (plane_state->planar_slave)
Ville Syrjälä83158472018-11-27 18:57:26 +02005034 return 0;
5035
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005036 if (plane_state->planar_linked_plane) {
Ville Syrjälä83158472018-11-27 18:57:26 +02005037 const struct drm_framebuffer *fb = plane_state->base.fb;
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005038 enum plane_id y_plane_id = plane_state->planar_linked_plane->id;
Ville Syrjälä83158472018-11-27 18:57:26 +02005039
5040 WARN_ON(!intel_wm_plane_visible(crtc_state, plane_state));
5041 WARN_ON(!fb->format->is_yuv ||
5042 fb->format->num_planes == 1);
5043
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005044 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005045 y_plane_id, 0);
5046 if (ret)
5047 return ret;
5048
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005049 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005050 plane_id, 1);
5051 if (ret)
5052 return ret;
5053 } else if (intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005054 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005055 plane_id, 0);
5056 if (ret)
5057 return ret;
5058 }
5059
5060 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005061}
5062
Maarten Lankhorstec193642019-06-28 10:55:17 +02005063static int skl_build_pipe_wm(struct intel_crtc_state *crtc_state)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005064{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005065 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
5066 struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02005067 struct intel_plane *plane;
5068 const struct intel_plane_state *plane_state;
Matt Roper55994c22016-05-12 07:06:08 -07005069 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005070
Lyudea62163e2016-10-04 14:28:20 -04005071 /*
5072 * We'll only calculate watermarks for planes that are actually
5073 * enabled, so make sure all other planes are set as disabled.
5074 */
5075 memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
5076
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02005077 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state,
5078 crtc_state) {
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05305079
Ville Syrjälä83158472018-11-27 18:57:26 +02005080 if (INTEL_GEN(dev_priv) >= 11)
Maarten Lankhorstec193642019-06-28 10:55:17 +02005081 ret = icl_build_plane_wm(crtc_state, plane_state);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005082 else
Maarten Lankhorstec193642019-06-28 10:55:17 +02005083 ret = skl_build_plane_wm(crtc_state, plane_state);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305084 if (ret)
5085 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005086 }
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305087
Maarten Lankhorstec193642019-06-28 10:55:17 +02005088 pipe_wm->linetime = skl_compute_linetime_wm(crtc_state);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005089
Matt Roper55994c22016-05-12 07:06:08 -07005090 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005091}
5092
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005093static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
5094 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00005095 const struct skl_ddb_entry *entry)
5096{
5097 if (entry->end)
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005098 I915_WRITE_FW(reg, (entry->end - 1) << 16 | entry->start);
Damien Lespiau16160e32014-11-04 17:06:53 +00005099 else
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005100 I915_WRITE_FW(reg, 0);
Damien Lespiau16160e32014-11-04 17:06:53 +00005101}
5102
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005103static void skl_write_wm_level(struct drm_i915_private *dev_priv,
5104 i915_reg_t reg,
5105 const struct skl_wm_level *level)
5106{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005107 u32 val = 0;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005108
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005109 if (level->plane_en)
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005110 val |= PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005111 if (level->ignore_lines)
5112 val |= PLANE_WM_IGNORE_LINES;
5113 val |= level->plane_res_b;
5114 val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005115
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005116 I915_WRITE_FW(reg, val);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005117}
5118
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005119void skl_write_plane_wm(struct intel_plane *plane,
5120 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005121{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005122 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005123 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005124 enum plane_id plane_id = plane->id;
5125 enum pipe pipe = plane->pipe;
5126 const struct skl_plane_wm *wm =
5127 &crtc_state->wm.skl.optimal.planes[plane_id];
5128 const struct skl_ddb_entry *ddb_y =
5129 &crtc_state->wm.skl.plane_ddb_y[plane_id];
5130 const struct skl_ddb_entry *ddb_uv =
5131 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005132
5133 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005134 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005135 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04005136 }
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005137 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005138 &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02005139
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005140 if (INTEL_GEN(dev_priv) >= 11) {
Mahesh Kumar234059d2018-01-30 11:49:13 -02005141 skl_ddb_entry_write(dev_priv,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005142 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5143 return;
Mahesh Kumarb879d582018-04-09 09:11:01 +05305144 }
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005145
5146 if (wm->is_planar)
5147 swap(ddb_y, ddb_uv);
5148
5149 skl_ddb_entry_write(dev_priv,
5150 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5151 skl_ddb_entry_write(dev_priv,
5152 PLANE_NV12_BUF_CFG(pipe, plane_id), ddb_uv);
Lyude62e0fb82016-08-22 12:50:08 -04005153}
5154
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005155void skl_write_cursor_wm(struct intel_plane *plane,
5156 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005157{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005158 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005159 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005160 enum plane_id plane_id = plane->id;
5161 enum pipe pipe = plane->pipe;
5162 const struct skl_plane_wm *wm =
5163 &crtc_state->wm.skl.optimal.planes[plane_id];
5164 const struct skl_ddb_entry *ddb =
5165 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005166
5167 for (level = 0; level <= max_level; level++) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005168 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
5169 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04005170 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005171 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02005172
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005173 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe), ddb);
Lyude62e0fb82016-08-22 12:50:08 -04005174}
5175
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005176bool skl_wm_level_equals(const struct skl_wm_level *l1,
5177 const struct skl_wm_level *l2)
5178{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005179 return l1->plane_en == l2->plane_en &&
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005180 l1->ignore_lines == l2->ignore_lines &&
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005181 l1->plane_res_l == l2->plane_res_l &&
5182 l1->plane_res_b == l2->plane_res_b;
5183}
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005184
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005185static bool skl_plane_wm_equals(struct drm_i915_private *dev_priv,
5186 const struct skl_plane_wm *wm1,
5187 const struct skl_plane_wm *wm2)
5188{
5189 int level, max_level = ilk_wm_max_level(dev_priv);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005190
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005191 for (level = 0; level <= max_level; level++) {
5192 if (!skl_wm_level_equals(&wm1->wm[level], &wm2->wm[level]) ||
5193 !skl_wm_level_equals(&wm1->uv_wm[level], &wm2->uv_wm[level]))
5194 return false;
5195 }
5196
5197 return skl_wm_level_equals(&wm1->trans_wm, &wm2->trans_wm);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005198}
5199
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005200static bool skl_pipe_wm_equals(struct intel_crtc *crtc,
5201 const struct skl_pipe_wm *wm1,
5202 const struct skl_pipe_wm *wm2)
5203{
5204 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5205 enum plane_id plane_id;
5206
5207 for_each_plane_id_on_crtc(crtc, plane_id) {
5208 if (!skl_plane_wm_equals(dev_priv,
5209 &wm1->planes[plane_id],
5210 &wm2->planes[plane_id]))
5211 return false;
5212 }
5213
5214 return wm1->linetime == wm2->linetime;
5215}
5216
Lyude27082492016-08-24 07:48:10 +02005217static inline bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
5218 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005219{
Lyude27082492016-08-24 07:48:10 +02005220 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005221}
5222
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005223bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry *ddb,
Jani Nikula696173b2019-04-05 14:00:15 +03005224 const struct skl_ddb_entry *entries,
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005225 int num_entries, int ignore_idx)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005226{
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005227 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005228
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005229 for (i = 0; i < num_entries; i++) {
5230 if (i != ignore_idx &&
5231 skl_ddb_entries_overlap(ddb, &entries[i]))
Lyude27082492016-08-24 07:48:10 +02005232 return true;
Mika Kahola2b685042017-10-10 13:17:03 +03005233 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005234
Lyude27082492016-08-24 07:48:10 +02005235 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005236}
5237
Jani Nikulabb7791b2016-10-04 12:29:17 +03005238static int
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005239skl_ddb_add_affected_planes(const struct intel_crtc_state *old_crtc_state,
5240 struct intel_crtc_state *new_crtc_state)
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005241{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005242 struct intel_atomic_state *state = to_intel_atomic_state(new_crtc_state->base.state);
5243 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
5244 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5245 struct intel_plane *plane;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005246
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005247 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5248 struct intel_plane_state *plane_state;
5249 enum plane_id plane_id = plane->id;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005250
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005251 if (skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_y[plane_id],
5252 &new_crtc_state->wm.skl.plane_ddb_y[plane_id]) &&
5253 skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_uv[plane_id],
5254 &new_crtc_state->wm.skl.plane_ddb_uv[plane_id]))
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005255 continue;
5256
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005257 plane_state = intel_atomic_get_plane_state(state, plane);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005258 if (IS_ERR(plane_state))
5259 return PTR_ERR(plane_state);
Maarten Lankhorst1ab554b2018-10-22 15:51:52 +02005260
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005261 new_crtc_state->update_planes |= BIT(plane_id);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005262 }
5263
5264 return 0;
5265}
5266
5267static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005268skl_compute_ddb(struct intel_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07005269{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005270 const struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5271 struct skl_ddb_allocation *ddb = &state->wm_results.ddb;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005272 struct intel_crtc_state *old_crtc_state;
5273 struct intel_crtc_state *new_crtc_state;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305274 struct intel_crtc *crtc;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305275 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07005276
Paulo Zanoni5a920b82016-10-04 14:37:32 -03005277 memcpy(ddb, &dev_priv->wm.skl_hw.ddb, sizeof(*ddb));
5278
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005279 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005280 new_crtc_state, i) {
5281 ret = skl_allocate_pipe_ddb(new_crtc_state, ddb);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005282 if (ret)
5283 return ret;
5284
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005285 ret = skl_ddb_add_affected_planes(old_crtc_state,
5286 new_crtc_state);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005287 if (ret)
5288 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07005289 }
5290
5291 return 0;
5292}
5293
Ville Syrjäläab98e942019-02-08 22:05:27 +02005294static char enast(bool enable)
5295{
5296 return enable ? '*' : ' ';
5297}
5298
Matt Roper2722efb2016-08-17 15:55:55 -04005299static void
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005300skl_print_wm_changes(struct intel_atomic_state *state)
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005301{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005302 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5303 const struct intel_crtc_state *old_crtc_state;
5304 const struct intel_crtc_state *new_crtc_state;
5305 struct intel_plane *plane;
5306 struct intel_crtc *crtc;
Maarten Lankhorst75704982016-11-01 12:04:10 +01005307 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005308
Ville Syrjäläab98e942019-02-08 22:05:27 +02005309 if ((drm_debug & DRM_UT_KMS) == 0)
5310 return;
5311
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005312 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
5313 new_crtc_state, i) {
Ville Syrjäläab98e942019-02-08 22:05:27 +02005314 const struct skl_pipe_wm *old_pipe_wm, *new_pipe_wm;
5315
5316 old_pipe_wm = &old_crtc_state->wm.skl.optimal;
5317 new_pipe_wm = &new_crtc_state->wm.skl.optimal;
5318
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005319 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5320 enum plane_id plane_id = plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005321 const struct skl_ddb_entry *old, *new;
5322
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005323 old = &old_crtc_state->wm.skl.plane_ddb_y[plane_id];
5324 new = &new_crtc_state->wm.skl.plane_ddb_y[plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005325
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005326 if (skl_ddb_entry_equal(old, new))
5327 continue;
5328
Ville Syrjäläab98e942019-02-08 22:05:27 +02005329 DRM_DEBUG_KMS("[PLANE:%d:%s] ddb (%4d - %4d) -> (%4d - %4d), size %4d -> %4d\n",
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005330 plane->base.base.id, plane->base.name,
Ville Syrjäläab98e942019-02-08 22:05:27 +02005331 old->start, old->end, new->start, new->end,
5332 skl_ddb_entry_size(old), skl_ddb_entry_size(new));
5333 }
5334
5335 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5336 enum plane_id plane_id = plane->id;
5337 const struct skl_plane_wm *old_wm, *new_wm;
5338
5339 old_wm = &old_pipe_wm->planes[plane_id];
5340 new_wm = &new_pipe_wm->planes[plane_id];
5341
5342 if (skl_plane_wm_equals(dev_priv, old_wm, new_wm))
5343 continue;
5344
5345 DRM_DEBUG_KMS("[PLANE:%d:%s] level %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm"
5346 " -> %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm\n",
5347 plane->base.base.id, plane->base.name,
5348 enast(old_wm->wm[0].plane_en), enast(old_wm->wm[1].plane_en),
5349 enast(old_wm->wm[2].plane_en), enast(old_wm->wm[3].plane_en),
5350 enast(old_wm->wm[4].plane_en), enast(old_wm->wm[5].plane_en),
5351 enast(old_wm->wm[6].plane_en), enast(old_wm->wm[7].plane_en),
5352 enast(old_wm->trans_wm.plane_en),
5353 enast(new_wm->wm[0].plane_en), enast(new_wm->wm[1].plane_en),
5354 enast(new_wm->wm[2].plane_en), enast(new_wm->wm[3].plane_en),
5355 enast(new_wm->wm[4].plane_en), enast(new_wm->wm[5].plane_en),
5356 enast(new_wm->wm[6].plane_en), enast(new_wm->wm[7].plane_en),
5357 enast(new_wm->trans_wm.plane_en));
5358
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005359 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"
5360 " -> %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 +02005361 plane->base.base.id, plane->base.name,
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005362 enast(old_wm->wm[0].ignore_lines), old_wm->wm[0].plane_res_l,
5363 enast(old_wm->wm[1].ignore_lines), old_wm->wm[1].plane_res_l,
5364 enast(old_wm->wm[2].ignore_lines), old_wm->wm[2].plane_res_l,
5365 enast(old_wm->wm[3].ignore_lines), old_wm->wm[3].plane_res_l,
5366 enast(old_wm->wm[4].ignore_lines), old_wm->wm[4].plane_res_l,
5367 enast(old_wm->wm[5].ignore_lines), old_wm->wm[5].plane_res_l,
5368 enast(old_wm->wm[6].ignore_lines), old_wm->wm[6].plane_res_l,
5369 enast(old_wm->wm[7].ignore_lines), old_wm->wm[7].plane_res_l,
5370 enast(old_wm->trans_wm.ignore_lines), old_wm->trans_wm.plane_res_l,
5371
5372 enast(new_wm->wm[0].ignore_lines), new_wm->wm[0].plane_res_l,
5373 enast(new_wm->wm[1].ignore_lines), new_wm->wm[1].plane_res_l,
5374 enast(new_wm->wm[2].ignore_lines), new_wm->wm[2].plane_res_l,
5375 enast(new_wm->wm[3].ignore_lines), new_wm->wm[3].plane_res_l,
5376 enast(new_wm->wm[4].ignore_lines), new_wm->wm[4].plane_res_l,
5377 enast(new_wm->wm[5].ignore_lines), new_wm->wm[5].plane_res_l,
5378 enast(new_wm->wm[6].ignore_lines), new_wm->wm[6].plane_res_l,
5379 enast(new_wm->wm[7].ignore_lines), new_wm->wm[7].plane_res_l,
5380 enast(new_wm->trans_wm.ignore_lines), new_wm->trans_wm.plane_res_l);
Ville Syrjäläab98e942019-02-08 22:05:27 +02005381
5382 DRM_DEBUG_KMS("[PLANE:%d:%s] blocks %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d"
5383 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d\n",
5384 plane->base.base.id, plane->base.name,
5385 old_wm->wm[0].plane_res_b, old_wm->wm[1].plane_res_b,
5386 old_wm->wm[2].plane_res_b, old_wm->wm[3].plane_res_b,
5387 old_wm->wm[4].plane_res_b, old_wm->wm[5].plane_res_b,
5388 old_wm->wm[6].plane_res_b, old_wm->wm[7].plane_res_b,
5389 old_wm->trans_wm.plane_res_b,
5390 new_wm->wm[0].plane_res_b, new_wm->wm[1].plane_res_b,
5391 new_wm->wm[2].plane_res_b, new_wm->wm[3].plane_res_b,
5392 new_wm->wm[4].plane_res_b, new_wm->wm[5].plane_res_b,
5393 new_wm->wm[6].plane_res_b, new_wm->wm[7].plane_res_b,
5394 new_wm->trans_wm.plane_res_b);
5395
5396 DRM_DEBUG_KMS("[PLANE:%d:%s] min_ddb %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d"
5397 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d\n",
5398 plane->base.base.id, plane->base.name,
5399 old_wm->wm[0].min_ddb_alloc, old_wm->wm[1].min_ddb_alloc,
5400 old_wm->wm[2].min_ddb_alloc, old_wm->wm[3].min_ddb_alloc,
5401 old_wm->wm[4].min_ddb_alloc, old_wm->wm[5].min_ddb_alloc,
5402 old_wm->wm[6].min_ddb_alloc, old_wm->wm[7].min_ddb_alloc,
5403 old_wm->trans_wm.min_ddb_alloc,
5404 new_wm->wm[0].min_ddb_alloc, new_wm->wm[1].min_ddb_alloc,
5405 new_wm->wm[2].min_ddb_alloc, new_wm->wm[3].min_ddb_alloc,
5406 new_wm->wm[4].min_ddb_alloc, new_wm->wm[5].min_ddb_alloc,
5407 new_wm->wm[6].min_ddb_alloc, new_wm->wm[7].min_ddb_alloc,
5408 new_wm->trans_wm.min_ddb_alloc);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005409 }
5410 }
5411}
5412
Ville Syrjälä49e0ed32019-10-11 23:09:43 +03005413static int intel_add_all_pipes(struct intel_atomic_state *state)
5414{
5415 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5416 struct intel_crtc *crtc;
5417
5418 for_each_intel_crtc(&dev_priv->drm, crtc) {
5419 struct intel_crtc_state *crtc_state;
5420
5421 crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
5422 if (IS_ERR(crtc_state))
5423 return PTR_ERR(crtc_state);
5424 }
5425
5426 return 0;
5427}
5428
Matt Roper98d39492016-05-12 07:06:03 -07005429static int
Ville Syrjäläd7a14582019-10-11 23:09:42 +03005430skl_ddb_add_affected_pipes(struct intel_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07005431{
Ville Syrjälä49e0ed32019-10-11 23:09:43 +03005432 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Ville Syrjäläd7a14582019-10-11 23:09:42 +03005433 int ret;
Matt Roper98d39492016-05-12 07:06:03 -07005434
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305435 /*
5436 * If this is our first atomic update following hardware readout,
5437 * we can't trust the DDB that the BIOS programmed for us. Let's
5438 * pretend that all pipes switched active status so that we'll
5439 * ensure a full DDB recompute.
5440 */
5441 if (dev_priv->wm.distrust_bios_wm) {
Ville Syrjälä49e0ed32019-10-11 23:09:43 +03005442 ret = drm_modeset_lock(&dev_priv->drm.mode_config.connection_mutex,
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005443 state->base.acquire_ctx);
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305444 if (ret)
5445 return ret;
5446
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005447 state->active_pipe_changes = ~0;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305448
5449 /*
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03005450 * We usually only initialize state->active_pipes if we
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305451 * we're doing a modeset; make sure this field is always
5452 * initialized during the sanitization process that happens
5453 * on the first commit too.
5454 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005455 if (!state->modeset)
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03005456 state->active_pipes = dev_priv->active_pipes;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305457 }
5458
5459 /*
5460 * If the modeset changes which CRTC's are active, we need to
5461 * recompute the DDB allocation for *all* active pipes, even
5462 * those that weren't otherwise being modified in any way by this
5463 * atomic commit. Due to the shrinking of the per-pipe allocations
5464 * when new active CRTC's are added, it's possible for a pipe that
5465 * we were already using and aren't changing at all here to suddenly
5466 * become invalid if its DDB needs exceeds its new allocation.
5467 *
5468 * Note that if we wind up doing a full DDB recompute, we can't let
5469 * any other display updates race with this transaction, so we need
5470 * to grab the lock on *all* CRTC's.
5471 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005472 if (state->active_pipe_changes || state->modeset) {
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005473 state->wm_results.dirty_pipes = ~0;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305474
Ville Syrjälä49e0ed32019-10-11 23:09:43 +03005475 ret = intel_add_all_pipes(state);
5476 if (ret)
5477 return ret;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305478 }
5479
5480 return 0;
5481}
5482
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005483/*
5484 * To make sure the cursor watermark registers are always consistent
5485 * with our computed state the following scenario needs special
5486 * treatment:
5487 *
5488 * 1. enable cursor
5489 * 2. move cursor entirely offscreen
5490 * 3. disable cursor
5491 *
5492 * Step 2. does call .disable_plane() but does not zero the watermarks
5493 * (since we consider an offscreen cursor still active for the purposes
5494 * of watermarks). Step 3. would not normally call .disable_plane()
5495 * because the actual plane visibility isn't changing, and we don't
5496 * deallocate the cursor ddb until the pipe gets disabled. So we must
5497 * force step 3. to call .disable_plane() to update the watermark
5498 * registers properly.
5499 *
5500 * Other planes do not suffer from this issues as their watermarks are
5501 * calculated based on the actual plane visibility. The only time this
5502 * can trigger for the other planes is during the initial readout as the
5503 * default value of the watermarks registers is not zero.
5504 */
5505static int skl_wm_add_affected_planes(struct intel_atomic_state *state,
5506 struct intel_crtc *crtc)
5507{
5508 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5509 const struct intel_crtc_state *old_crtc_state =
5510 intel_atomic_get_old_crtc_state(state, crtc);
5511 struct intel_crtc_state *new_crtc_state =
5512 intel_atomic_get_new_crtc_state(state, crtc);
5513 struct intel_plane *plane;
5514
5515 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5516 struct intel_plane_state *plane_state;
5517 enum plane_id plane_id = plane->id;
5518
5519 /*
5520 * Force a full wm update for every plane on modeset.
5521 * Required because the reset value of the wm registers
5522 * is non-zero, whereas we want all disabled planes to
5523 * have zero watermarks. So if we turn off the relevant
5524 * power well the hardware state will go out of sync
5525 * with the software state.
5526 */
5527 if (!drm_atomic_crtc_needs_modeset(&new_crtc_state->base) &&
5528 skl_plane_wm_equals(dev_priv,
5529 &old_crtc_state->wm.skl.optimal.planes[plane_id],
5530 &new_crtc_state->wm.skl.optimal.planes[plane_id]))
5531 continue;
5532
5533 plane_state = intel_atomic_get_plane_state(state, plane);
5534 if (IS_ERR(plane_state))
5535 return PTR_ERR(plane_state);
5536
5537 new_crtc_state->update_planes |= BIT(plane_id);
5538 }
5539
5540 return 0;
5541}
5542
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305543static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005544skl_compute_wm(struct intel_atomic_state *state)
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305545{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005546 struct intel_crtc *crtc;
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02005547 struct intel_crtc_state *new_crtc_state;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005548 struct intel_crtc_state *old_crtc_state;
5549 struct skl_ddb_values *results = &state->wm_results;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305550 int ret, i;
5551
Matt Roper734fa012016-05-12 15:11:40 -07005552 /* Clear all dirty flags */
5553 results->dirty_pipes = 0;
5554
Ville Syrjäläd7a14582019-10-11 23:09:42 +03005555 ret = skl_ddb_add_affected_pipes(state);
5556 if (ret)
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305557 return ret;
5558
Matt Roper734fa012016-05-12 15:11:40 -07005559 /*
5560 * Calculate WM's for all pipes that are part of this transaction.
Matt Roperd8e87492018-12-11 09:31:07 -08005561 * Note that skl_ddb_add_affected_pipes may have added more CRTC's that
Matt Roper734fa012016-05-12 15:11:40 -07005562 * weren't otherwise being modified (and set bits in dirty_pipes) if
5563 * pipe allocations had to change.
Matt Roper734fa012016-05-12 15:11:40 -07005564 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005565 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02005566 new_crtc_state, i) {
5567 ret = skl_build_pipe_wm(new_crtc_state);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005568 if (ret)
5569 return ret;
5570
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005571 ret = skl_wm_add_affected_planes(state, crtc);
Matt Roper734fa012016-05-12 15:11:40 -07005572 if (ret)
5573 return ret;
5574
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02005575 if (!skl_pipe_wm_equals(crtc,
5576 &old_crtc_state->wm.skl.optimal,
5577 &new_crtc_state->wm.skl.optimal))
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005578 results->dirty_pipes |= drm_crtc_mask(&crtc->base);
Matt Roper734fa012016-05-12 15:11:40 -07005579 }
5580
Matt Roperd8e87492018-12-11 09:31:07 -08005581 ret = skl_compute_ddb(state);
5582 if (ret)
5583 return ret;
5584
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005585 skl_print_wm_changes(state);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005586
Matt Roper98d39492016-05-12 07:06:03 -07005587 return 0;
5588}
5589
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005590static void skl_atomic_update_crtc_wm(struct intel_atomic_state *state,
Maarten Lankhorstec193642019-06-28 10:55:17 +02005591 struct intel_crtc_state *crtc_state)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005592{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005593 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005594 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Maarten Lankhorstec193642019-06-28 10:55:17 +02005595 struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005596 enum pipe pipe = crtc->pipe;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005597
5598 if (!(state->wm_results.dirty_pipes & drm_crtc_mask(&crtc->base)))
5599 return;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005600
5601 I915_WRITE(PIPE_WM_LINETIME(pipe), pipe_wm->linetime);
5602}
5603
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005604static void skl_initial_wm(struct intel_atomic_state *state,
Maarten Lankhorstec193642019-06-28 10:55:17 +02005605 struct intel_crtc_state *crtc_state)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005606{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005607 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02005608 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005609 struct drm_i915_private *dev_priv = to_i915(dev);
Mahesh Kumar60f8e872018-04-09 09:11:00 +05305610 struct skl_ddb_values *results = &state->wm_results;
Bob Paauweadda50b2015-07-21 10:42:53 -07005611
Ville Syrjälä432081b2016-10-31 22:37:03 +02005612 if ((results->dirty_pipes & drm_crtc_mask(&intel_crtc->base)) == 0)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005613 return;
5614
Matt Roper734fa012016-05-12 15:11:40 -07005615 mutex_lock(&dev_priv->wm.wm_mutex);
5616
Maarten Lankhorstec193642019-06-28 10:55:17 +02005617 if (crtc_state->base.active_changed)
5618 skl_atomic_update_crtc_wm(state, crtc_state);
Lyude27082492016-08-24 07:48:10 +02005619
Matt Roper734fa012016-05-12 15:11:40 -07005620 mutex_unlock(&dev_priv->wm.wm_mutex);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005621}
5622
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005623static void ilk_compute_wm_config(struct drm_i915_private *dev_priv,
Ville Syrjäläd8905652016-01-14 14:53:35 +02005624 struct intel_wm_config *config)
5625{
5626 struct intel_crtc *crtc;
5627
5628 /* Compute the currently _active_ config */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005629 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläd8905652016-01-14 14:53:35 +02005630 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
5631
5632 if (!wm->pipe_enabled)
5633 continue;
5634
5635 config->sprites_enabled |= wm->sprites_enabled;
5636 config->sprites_scaled |= wm->sprites_scaled;
5637 config->num_pipes_active++;
5638 }
5639}
5640
Matt Ropered4a6a72016-02-23 17:20:13 -08005641static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03005642{
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005643 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02005644 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02005645 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02005646 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005647 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07005648
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005649 ilk_compute_wm_config(dev_priv, &config);
Ville Syrjäläd8905652016-01-14 14:53:35 +02005650
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005651 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_1_2, &max);
5652 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03005653
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005654 /* 5/6 split only in single pipe config on IVB+ */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00005655 if (INTEL_GEN(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02005656 config.num_pipes_active == 1 && config.sprites_enabled) {
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005657 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_5_6, &max);
5658 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005659
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005660 best_lp_wm = ilk_find_best_result(dev_priv, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03005661 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005662 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005663 }
5664
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005665 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005666 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005667
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005668 ilk_compute_wm_results(dev_priv, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03005669
Imre Deak820c1982013-12-17 14:46:36 +02005670 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03005671}
5672
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005673static void ilk_initial_watermarks(struct intel_atomic_state *state,
Maarten Lankhorstec193642019-06-28 10:55:17 +02005674 struct intel_crtc_state *crtc_state)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005675{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005676 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä88016a92019-07-01 19:05:45 +03005677 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005678
Matt Ropered4a6a72016-02-23 17:20:13 -08005679 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03005680 crtc->wm.active.ilk = crtc_state->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08005681 ilk_program_watermarks(dev_priv);
5682 mutex_unlock(&dev_priv->wm.wm_mutex);
5683}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005684
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005685static void ilk_optimize_watermarks(struct intel_atomic_state *state,
Maarten Lankhorstec193642019-06-28 10:55:17 +02005686 struct intel_crtc_state *crtc_state)
Matt Ropered4a6a72016-02-23 17:20:13 -08005687{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005688 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä88016a92019-07-01 19:05:45 +03005689 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
5690
5691 if (!crtc_state->wm.need_postvbl_update)
5692 return;
Matt Ropered4a6a72016-02-23 17:20:13 -08005693
5694 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03005695 crtc->wm.active.ilk = crtc_state->wm.ilk.optimal;
5696 ilk_program_watermarks(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08005697 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005698}
5699
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005700static inline void skl_wm_level_from_reg_val(u32 val,
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005701 struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00005702{
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005703 level->plane_en = val & PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005704 level->ignore_lines = val & PLANE_WM_IGNORE_LINES;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005705 level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
5706 level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
5707 PLANE_WM_LINES_MASK;
Pradeep Bhat30789992014-11-04 17:06:45 +00005708}
5709
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005710void skl_pipe_wm_get_hw_state(struct intel_crtc *crtc,
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005711 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00005712{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005713 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5714 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005715 int level, max_level;
5716 enum plane_id plane_id;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005717 u32 val;
Pradeep Bhat30789992014-11-04 17:06:45 +00005718
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005719 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00005720
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005721 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005722 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00005723
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005724 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005725 if (plane_id != PLANE_CURSOR)
5726 val = I915_READ(PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005727 else
5728 val = I915_READ(CUR_WM(pipe, level));
5729
5730 skl_wm_level_from_reg_val(val, &wm->wm[level]);
5731 }
5732
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005733 if (plane_id != PLANE_CURSOR)
5734 val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005735 else
5736 val = I915_READ(CUR_WM_TRANS(pipe));
5737
5738 skl_wm_level_from_reg_val(val, &wm->trans_wm);
5739 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005740
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005741 if (!crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00005742 return;
5743
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005744 out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00005745}
5746
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005747void skl_wm_get_hw_state(struct drm_i915_private *dev_priv)
Pradeep Bhat30789992014-11-04 17:06:45 +00005748{
Mahesh Kumar60f8e872018-04-09 09:11:00 +05305749 struct skl_ddb_values *hw = &dev_priv->wm.skl_hw;
Damien Lespiaua269c582014-11-04 17:06:49 +00005750 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005751 struct intel_crtc *crtc;
Maarten Lankhorstec193642019-06-28 10:55:17 +02005752 struct intel_crtc_state *crtc_state;
Pradeep Bhat30789992014-11-04 17:06:45 +00005753
Damien Lespiaua269c582014-11-04 17:06:49 +00005754 skl_ddb_get_hw_state(dev_priv, ddb);
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005755 for_each_intel_crtc(&dev_priv->drm, crtc) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02005756 crtc_state = to_intel_crtc_state(crtc->base.state);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005757
Maarten Lankhorstec193642019-06-28 10:55:17 +02005758 skl_pipe_wm_get_hw_state(crtc, &crtc_state->wm.skl.optimal);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005759
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005760 if (crtc->active)
5761 hw->dirty_pipes |= drm_crtc_mask(&crtc->base);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005762 }
Matt Ropera1de91e2016-05-12 07:05:57 -07005763
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03005764 if (dev_priv->active_pipes) {
Matt Roper279e99d2016-05-12 07:06:02 -07005765 /* Fully recompute DDB on first atomic commit */
5766 dev_priv->wm.distrust_bios_wm = true;
Matt Roper279e99d2016-05-12 07:06:02 -07005767 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005768}
5769
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005770static void ilk_pipe_wm_get_hw_state(struct intel_crtc *crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005771{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005772 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005773 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02005774 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Maarten Lankhorstec193642019-06-28 10:55:17 +02005775 struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
5776 struct intel_pipe_wm *active = &crtc_state->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005777 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005778 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005779 [PIPE_A] = WM0_PIPEA_ILK,
5780 [PIPE_B] = WM0_PIPEB_ILK,
5781 [PIPE_C] = WM0_PIPEC_IVB,
5782 };
5783
5784 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005785 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02005786 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005787
Ville Syrjälä15606532016-05-13 17:55:17 +03005788 memset(active, 0, sizeof(*active));
5789
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005790 active->pipe_enabled = crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02005791
5792 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005793 u32 tmp = hw->wm_pipe[pipe];
5794
5795 /*
5796 * For active pipes LP0 watermark is marked as
5797 * enabled, and LP1+ watermaks as disabled since
5798 * we can't really reverse compute them in case
5799 * multiple pipes are active.
5800 */
5801 active->wm[0].enable = true;
5802 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
5803 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
5804 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
5805 active->linetime = hw->wm_linetime[pipe];
5806 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005807 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005808
5809 /*
5810 * For inactive pipes, all watermark levels
5811 * should be marked as enabled but zeroed,
5812 * which is what we'd compute them to.
5813 */
5814 for (level = 0; level <= max_level; level++)
5815 active->wm[level].enable = true;
5816 }
Matt Roper4e0963c2015-09-24 15:53:15 -07005817
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005818 crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005819}
5820
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005821#define _FW_WM(value, plane) \
5822 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
5823#define _FW_WM_VLV(value, plane) \
5824 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
5825
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005826static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
5827 struct g4x_wm_values *wm)
5828{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005829 u32 tmp;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005830
5831 tmp = I915_READ(DSPFW1);
5832 wm->sr.plane = _FW_WM(tmp, SR);
5833 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5834 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
5835 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
5836
5837 tmp = I915_READ(DSPFW2);
5838 wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
5839 wm->sr.fbc = _FW_WM(tmp, FBC_SR);
5840 wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
5841 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
5842 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5843 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
5844
5845 tmp = I915_READ(DSPFW3);
5846 wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
5847 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5848 wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
5849 wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
5850}
5851
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005852static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
5853 struct vlv_wm_values *wm)
5854{
5855 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005856 u32 tmp;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005857
5858 for_each_pipe(dev_priv, pipe) {
5859 tmp = I915_READ(VLV_DDL(pipe));
5860
Ville Syrjälä1b313892016-11-28 19:37:08 +02005861 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005862 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005863 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005864 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005865 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005866 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005867 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005868 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
5869 }
5870
5871 tmp = I915_READ(DSPFW1);
5872 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005873 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5874 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
5875 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005876
5877 tmp = I915_READ(DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005878 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
5879 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5880 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005881
5882 tmp = I915_READ(DSPFW3);
5883 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5884
5885 if (IS_CHERRYVIEW(dev_priv)) {
5886 tmp = I915_READ(DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005887 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5888 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005889
5890 tmp = I915_READ(DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005891 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
5892 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005893
5894 tmp = I915_READ(DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005895 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
5896 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005897
5898 tmp = I915_READ(DSPHOWM);
5899 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005900 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
5901 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
5902 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
5903 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5904 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5905 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5906 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5907 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5908 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005909 } else {
5910 tmp = I915_READ(DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005911 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5912 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005913
5914 tmp = I915_READ(DSPHOWM);
5915 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005916 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5917 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5918 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5919 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5920 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5921 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005922 }
5923}
5924
5925#undef _FW_WM
5926#undef _FW_WM_VLV
5927
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005928void g4x_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005929{
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005930 struct g4x_wm_values *wm = &dev_priv->wm.g4x;
5931 struct intel_crtc *crtc;
5932
5933 g4x_read_wm_values(dev_priv, wm);
5934
5935 wm->cxsr = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
5936
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005937 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005938 struct intel_crtc_state *crtc_state =
5939 to_intel_crtc_state(crtc->base.state);
5940 struct g4x_wm_state *active = &crtc->wm.active.g4x;
5941 struct g4x_pipe_wm *raw;
5942 enum pipe pipe = crtc->pipe;
5943 enum plane_id plane_id;
5944 int level, max_level;
5945
5946 active->cxsr = wm->cxsr;
5947 active->hpll_en = wm->hpll_en;
5948 active->fbc_en = wm->fbc_en;
5949
5950 active->sr = wm->sr;
5951 active->hpll = wm->hpll;
5952
5953 for_each_plane_id_on_crtc(crtc, plane_id) {
5954 active->wm.plane[plane_id] =
5955 wm->pipe[pipe].plane[plane_id];
5956 }
5957
5958 if (wm->cxsr && wm->hpll_en)
5959 max_level = G4X_WM_LEVEL_HPLL;
5960 else if (wm->cxsr)
5961 max_level = G4X_WM_LEVEL_SR;
5962 else
5963 max_level = G4X_WM_LEVEL_NORMAL;
5964
5965 level = G4X_WM_LEVEL_NORMAL;
5966 raw = &crtc_state->wm.g4x.raw[level];
5967 for_each_plane_id_on_crtc(crtc, plane_id)
5968 raw->plane[plane_id] = active->wm.plane[plane_id];
5969
5970 if (++level > max_level)
5971 goto out;
5972
5973 raw = &crtc_state->wm.g4x.raw[level];
5974 raw->plane[PLANE_PRIMARY] = active->sr.plane;
5975 raw->plane[PLANE_CURSOR] = active->sr.cursor;
5976 raw->plane[PLANE_SPRITE0] = 0;
5977 raw->fbc = active->sr.fbc;
5978
5979 if (++level > max_level)
5980 goto out;
5981
5982 raw = &crtc_state->wm.g4x.raw[level];
5983 raw->plane[PLANE_PRIMARY] = active->hpll.plane;
5984 raw->plane[PLANE_CURSOR] = active->hpll.cursor;
5985 raw->plane[PLANE_SPRITE0] = 0;
5986 raw->fbc = active->hpll.fbc;
5987
5988 out:
5989 for_each_plane_id_on_crtc(crtc, plane_id)
5990 g4x_raw_plane_wm_set(crtc_state, level,
5991 plane_id, USHRT_MAX);
5992 g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
5993
5994 crtc_state->wm.g4x.optimal = *active;
5995 crtc_state->wm.g4x.intermediate = *active;
5996
5997 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
5998 pipe_name(pipe),
5999 wm->pipe[pipe].plane[PLANE_PRIMARY],
6000 wm->pipe[pipe].plane[PLANE_CURSOR],
6001 wm->pipe[pipe].plane[PLANE_SPRITE0]);
6002 }
6003
6004 DRM_DEBUG_KMS("Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
6005 wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
6006 DRM_DEBUG_KMS("Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
6007 wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
6008 DRM_DEBUG_KMS("Initial SR=%s HPLL=%s FBC=%s\n",
6009 yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
6010}
6011
6012void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
6013{
6014 struct intel_plane *plane;
6015 struct intel_crtc *crtc;
6016
6017 mutex_lock(&dev_priv->wm.wm_mutex);
6018
6019 for_each_intel_plane(&dev_priv->drm, plane) {
6020 struct intel_crtc *crtc =
6021 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
6022 struct intel_crtc_state *crtc_state =
6023 to_intel_crtc_state(crtc->base.state);
6024 struct intel_plane_state *plane_state =
6025 to_intel_plane_state(plane->base.state);
6026 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
6027 enum plane_id plane_id = plane->id;
6028 int level;
6029
6030 if (plane_state->base.visible)
6031 continue;
6032
6033 for (level = 0; level < 3; level++) {
6034 struct g4x_pipe_wm *raw =
6035 &crtc_state->wm.g4x.raw[level];
6036
6037 raw->plane[plane_id] = 0;
6038 wm_state->wm.plane[plane_id] = 0;
6039 }
6040
6041 if (plane_id == PLANE_PRIMARY) {
6042 for (level = 0; level < 3; level++) {
6043 struct g4x_pipe_wm *raw =
6044 &crtc_state->wm.g4x.raw[level];
6045 raw->fbc = 0;
6046 }
6047
6048 wm_state->sr.fbc = 0;
6049 wm_state->hpll.fbc = 0;
6050 wm_state->fbc_en = false;
6051 }
6052 }
6053
6054 for_each_intel_crtc(&dev_priv->drm, crtc) {
6055 struct intel_crtc_state *crtc_state =
6056 to_intel_crtc_state(crtc->base.state);
6057
6058 crtc_state->wm.g4x.intermediate =
6059 crtc_state->wm.g4x.optimal;
6060 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
6061 }
6062
6063 g4x_program_watermarks(dev_priv);
6064
6065 mutex_unlock(&dev_priv->wm.wm_mutex);
6066}
6067
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006068void vlv_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006069{
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006070 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02006071 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006072 u32 val;
6073
6074 vlv_read_wm_values(dev_priv, wm);
6075
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006076 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
6077 wm->level = VLV_WM_LEVEL_PM2;
6078
6079 if (IS_CHERRYVIEW(dev_priv)) {
Chris Wilson337fa6e2019-04-26 09:17:20 +01006080 vlv_punit_get(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006081
Ville Syrjäläc11b8132018-11-29 19:55:03 +02006082 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006083 if (val & DSP_MAXFIFO_PM5_ENABLE)
6084 wm->level = VLV_WM_LEVEL_PM5;
6085
Ville Syrjälä58590c12015-09-08 21:05:12 +03006086 /*
6087 * If DDR DVFS is disabled in the BIOS, Punit
6088 * will never ack the request. So if that happens
6089 * assume we don't have to enable/disable DDR DVFS
6090 * dynamically. To test that just set the REQ_ACK
6091 * bit to poke the Punit, but don't change the
6092 * HIGH/LOW bits so that we don't actually change
6093 * the current state.
6094 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006095 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03006096 val |= FORCE_DDR_FREQ_REQ_ACK;
6097 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
6098
6099 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
6100 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
6101 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
6102 "assuming DDR DVFS is disabled\n");
6103 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
6104 } else {
6105 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
6106 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
6107 wm->level = VLV_WM_LEVEL_DDR_DVFS;
6108 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006109
Chris Wilson337fa6e2019-04-26 09:17:20 +01006110 vlv_punit_put(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006111 }
6112
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006113 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02006114 struct intel_crtc_state *crtc_state =
6115 to_intel_crtc_state(crtc->base.state);
6116 struct vlv_wm_state *active = &crtc->wm.active.vlv;
6117 const struct vlv_fifo_state *fifo_state =
6118 &crtc_state->wm.vlv.fifo_state;
6119 enum pipe pipe = crtc->pipe;
6120 enum plane_id plane_id;
6121 int level;
6122
6123 vlv_get_fifo_size(crtc_state);
6124
6125 active->num_levels = wm->level + 1;
6126 active->cxsr = wm->cxsr;
6127
Ville Syrjäläff32c542017-03-02 19:14:57 +02006128 for (level = 0; level < active->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03006129 struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02006130 &crtc_state->wm.vlv.raw[level];
6131
6132 active->sr[level].plane = wm->sr.plane;
6133 active->sr[level].cursor = wm->sr.cursor;
6134
6135 for_each_plane_id_on_crtc(crtc, plane_id) {
6136 active->wm[level].plane[plane_id] =
6137 wm->pipe[pipe].plane[plane_id];
6138
6139 raw->plane[plane_id] =
6140 vlv_invert_wm_value(active->wm[level].plane[plane_id],
6141 fifo_state->plane[plane_id]);
6142 }
6143 }
6144
6145 for_each_plane_id_on_crtc(crtc, plane_id)
6146 vlv_raw_plane_wm_set(crtc_state, level,
6147 plane_id, USHRT_MAX);
6148 vlv_invalidate_wms(crtc, active, level);
6149
6150 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02006151 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02006152
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006153 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 +02006154 pipe_name(pipe),
6155 wm->pipe[pipe].plane[PLANE_PRIMARY],
6156 wm->pipe[pipe].plane[PLANE_CURSOR],
6157 wm->pipe[pipe].plane[PLANE_SPRITE0],
6158 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02006159 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006160
6161 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
6162 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
6163}
6164
Ville Syrjälä602ae832017-03-02 19:15:02 +02006165void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
6166{
6167 struct intel_plane *plane;
6168 struct intel_crtc *crtc;
6169
6170 mutex_lock(&dev_priv->wm.wm_mutex);
6171
6172 for_each_intel_plane(&dev_priv->drm, plane) {
6173 struct intel_crtc *crtc =
6174 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
6175 struct intel_crtc_state *crtc_state =
6176 to_intel_crtc_state(crtc->base.state);
6177 struct intel_plane_state *plane_state =
6178 to_intel_plane_state(plane->base.state);
6179 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
6180 const struct vlv_fifo_state *fifo_state =
6181 &crtc_state->wm.vlv.fifo_state;
6182 enum plane_id plane_id = plane->id;
6183 int level;
6184
6185 if (plane_state->base.visible)
6186 continue;
6187
6188 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03006189 struct g4x_pipe_wm *raw =
Ville Syrjälä602ae832017-03-02 19:15:02 +02006190 &crtc_state->wm.vlv.raw[level];
6191
6192 raw->plane[plane_id] = 0;
6193
6194 wm_state->wm[level].plane[plane_id] =
6195 vlv_invert_wm_value(raw->plane[plane_id],
6196 fifo_state->plane[plane_id]);
6197 }
6198 }
6199
6200 for_each_intel_crtc(&dev_priv->drm, crtc) {
6201 struct intel_crtc_state *crtc_state =
6202 to_intel_crtc_state(crtc->base.state);
6203
6204 crtc_state->wm.vlv.intermediate =
6205 crtc_state->wm.vlv.optimal;
6206 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
6207 }
6208
6209 vlv_program_watermarks(dev_priv);
6210
6211 mutex_unlock(&dev_priv->wm.wm_mutex);
6212}
6213
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02006214/*
6215 * FIXME should probably kill this and improve
6216 * the real watermark readout/sanitation instead
6217 */
6218static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
6219{
6220 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
6221 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
6222 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
6223
6224 /*
6225 * Don't touch WM1S_LP_EN here.
6226 * Doing so could cause underruns.
6227 */
6228}
6229
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006230void ilk_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006231{
Imre Deak820c1982013-12-17 14:46:36 +02006232 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006233 struct intel_crtc *crtc;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006234
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02006235 ilk_init_lp_watermarks(dev_priv);
6236
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006237 for_each_intel_crtc(&dev_priv->drm, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006238 ilk_pipe_wm_get_hw_state(crtc);
6239
6240 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
6241 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
6242 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
6243
6244 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00006245 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjäläcfa76982014-03-07 18:32:08 +02006246 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
6247 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
6248 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006249
Tvrtko Ursulin86527442016-10-13 11:03:00 +01006250 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02006251 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
6252 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01006253 else if (IS_IVYBRIDGE(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02006254 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
6255 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006256
6257 hw->enable_fbc_wm =
6258 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
6259}
6260
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006261/**
6262 * intel_update_watermarks - update FIFO watermark values based on current modes
Chris Wilson31383412018-02-14 14:03:03 +00006263 * @crtc: the #intel_crtc on which to compute the WM
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006264 *
6265 * Calculate watermark values for the various WM regs based on current mode
6266 * and plane configuration.
6267 *
6268 * There are several cases to deal with here:
6269 * - normal (i.e. non-self-refresh)
6270 * - self-refresh (SR) mode
6271 * - lines are large relative to FIFO size (buffer can hold up to 2)
6272 * - lines are small relative to FIFO size (buffer can hold more than 2
6273 * lines), so need to account for TLB latency
6274 *
6275 * The normal calculation is:
6276 * watermark = dotclock * bytes per pixel * latency
6277 * where latency is platform & configuration dependent (we assume pessimal
6278 * values here).
6279 *
6280 * The SR calculation is:
6281 * watermark = (trunc(latency/line time)+1) * surface width *
6282 * bytes per pixel
6283 * where
6284 * line time = htotal / dotclock
6285 * surface width = hdisplay for normal plane and 64 for cursor
6286 * and latency is assumed to be high, as above.
6287 *
6288 * The final value programmed to the register should always be rounded up,
6289 * and include an extra 2 entries to account for clock crossings.
6290 *
6291 * We don't use the sprite, so we can ignore that. And on Crestline we have
6292 * to set the non-SR watermarks to 8.
6293 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02006294void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006295{
Ville Syrjälä432081b2016-10-31 22:37:03 +02006296 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006297
6298 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03006299 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006300}
6301
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306302void intel_enable_ipc(struct drm_i915_private *dev_priv)
6303{
6304 u32 val;
6305
José Roberto de Souzafd847b82018-09-18 13:47:11 -07006306 if (!HAS_IPC(dev_priv))
6307 return;
6308
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306309 val = I915_READ(DISP_ARB_CTL2);
6310
6311 if (dev_priv->ipc_enabled)
6312 val |= DISP_IPC_ENABLE;
6313 else
6314 val &= ~DISP_IPC_ENABLE;
6315
6316 I915_WRITE(DISP_ARB_CTL2, val);
6317}
6318
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03006319static bool intel_can_enable_ipc(struct drm_i915_private *dev_priv)
6320{
6321 /* Display WA #0477 WaDisableIPC: skl */
6322 if (IS_SKYLAKE(dev_priv))
6323 return false;
6324
6325 /* Display WA #1141: SKL:all KBL:all CFL */
6326 if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv))
6327 return dev_priv->dram_info.symmetric_memory;
6328
6329 return true;
6330}
6331
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306332void intel_init_ipc(struct drm_i915_private *dev_priv)
6333{
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306334 if (!HAS_IPC(dev_priv))
6335 return;
6336
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03006337 dev_priv->ipc_enabled = intel_can_enable_ipc(dev_priv);
José Roberto de Souzac9b818d2018-09-18 13:47:13 -07006338
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306339 intel_enable_ipc(dev_priv);
6340}
6341
Jani Nikulae2828912016-01-18 09:19:47 +02006342/*
Daniel Vetter92703882012-08-09 16:46:01 +02006343 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02006344 */
6345DEFINE_SPINLOCK(mchdev_lock);
6346
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006347bool ironlake_set_drps(struct drm_i915_private *i915, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006348{
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006349 struct intel_uncore *uncore = &i915->uncore;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006350 u16 rgvswctl;
6351
Chris Wilson67520412017-03-02 13:28:01 +00006352 lockdep_assert_held(&mchdev_lock);
Daniel Vetter92703882012-08-09 16:46:01 +02006353
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006354 rgvswctl = intel_uncore_read16(uncore, MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006355 if (rgvswctl & MEMCTL_CMD_STS) {
6356 DRM_DEBUG("gpu busy, RCS change rejected\n");
6357 return false; /* still busy with another command */
6358 }
6359
6360 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
6361 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006362 intel_uncore_write16(uncore, MEMSWCTL, rgvswctl);
6363 intel_uncore_posting_read16(uncore, MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006364
6365 rgvswctl |= MEMCTL_CMD_STS;
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006366 intel_uncore_write16(uncore, MEMSWCTL, rgvswctl);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006367
6368 return true;
6369}
6370
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006371static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006372{
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006373 struct intel_uncore *uncore = &dev_priv->uncore;
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00006374 u32 rgvmodectl;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006375 u8 fmax, fmin, fstart, vstart;
6376
Daniel Vetter92703882012-08-09 16:46:01 +02006377 spin_lock_irq(&mchdev_lock);
6378
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006379 rgvmodectl = intel_uncore_read(uncore, MEMMODECTL);
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00006380
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006381 /* Enable temp reporting */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006382 intel_uncore_write16(uncore, PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
6383 intel_uncore_write16(uncore, TSC1, I915_READ(TSC1) | TSE);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006384
6385 /* 100ms RC evaluation intervals */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006386 intel_uncore_write(uncore, RCUPEI, 100000);
6387 intel_uncore_write(uncore, RCDNEI, 100000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006388
6389 /* Set max/min thresholds to 90ms and 80ms respectively */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006390 intel_uncore_write(uncore, RCBMAXAVG, 90000);
6391 intel_uncore_write(uncore, RCBMINAVG, 80000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006392
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006393 intel_uncore_write(uncore, MEMIHYST, 1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006394
6395 /* Set up min, max, and cur for interrupt handling */
6396 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
6397 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
6398 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
6399 MEMMODE_FSTART_SHIFT;
6400
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006401 vstart = (intel_uncore_read(uncore, PXVFREQ(fstart)) &
6402 PXVFREQ_PX_MASK) >> PXVFREQ_PX_SHIFT;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006403
Daniel Vetter20e4d402012-08-08 23:35:39 +02006404 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
6405 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006406
Daniel Vetter20e4d402012-08-08 23:35:39 +02006407 dev_priv->ips.max_delay = fstart;
6408 dev_priv->ips.min_delay = fmin;
6409 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006410
6411 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
6412 fmax, fmin, fstart);
6413
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006414 intel_uncore_write(uncore,
6415 MEMINTREN,
6416 MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006417
6418 /*
6419 * Interrupts will be enabled in ironlake_irq_postinstall
6420 */
6421
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006422 intel_uncore_write(uncore, VIDSTART, vstart);
6423 intel_uncore_posting_read(uncore, VIDSTART);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006424
6425 rgvmodectl |= MEMMODE_SWMODE_EN;
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006426 intel_uncore_write(uncore, MEMMODECTL, rgvmodectl);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006427
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006428 if (wait_for_atomic((intel_uncore_read(uncore, MEMSWCTL) &
6429 MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006430 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02006431 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006432
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006433 ironlake_set_drps(dev_priv, fstart);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006434
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006435 dev_priv->ips.last_count1 =
6436 intel_uncore_read(uncore, DMIEC) +
6437 intel_uncore_read(uncore, DDREC) +
6438 intel_uncore_read(uncore, CSIEC);
Daniel Vetter20e4d402012-08-08 23:35:39 +02006439 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006440 dev_priv->ips.last_count2 = intel_uncore_read(uncore, GFXEC);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00006441 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02006442
6443 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006444}
6445
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006446static void ironlake_disable_drps(struct drm_i915_private *i915)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006447{
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006448 struct intel_uncore *uncore = &i915->uncore;
Daniel Vetter92703882012-08-09 16:46:01 +02006449 u16 rgvswctl;
6450
6451 spin_lock_irq(&mchdev_lock);
6452
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006453 rgvswctl = intel_uncore_read16(uncore, MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006454
6455 /* Ack interrupts, disable EFC interrupt */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006456 intel_uncore_write(uncore,
6457 MEMINTREN,
6458 intel_uncore_read(uncore, MEMINTREN) &
6459 ~MEMINT_EVAL_CHG_EN);
6460 intel_uncore_write(uncore, MEMINTRSTS, MEMINT_EVAL_CHG);
6461 intel_uncore_write(uncore,
6462 DEIER,
6463 intel_uncore_read(uncore, DEIER) & ~DE_PCU_EVENT);
6464 intel_uncore_write(uncore, DEIIR, DE_PCU_EVENT);
6465 intel_uncore_write(uncore,
6466 DEIMR,
6467 intel_uncore_read(uncore, DEIMR) | DE_PCU_EVENT);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006468
6469 /* Go back to the starting frequency */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006470 ironlake_set_drps(i915, i915->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02006471 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006472 rgvswctl |= MEMCTL_CMD_STS;
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006473 intel_uncore_write(uncore, MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02006474 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006475
Daniel Vetter92703882012-08-09 16:46:01 +02006476 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006477}
6478
Daniel Vetteracbe9472012-07-26 11:50:05 +02006479/* There's a funny hw issue where the hw returns all 0 when reading from
6480 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
6481 * ourselves, instead of doing a rmw cycle (which might result in us clearing
6482 * all limits and the gpu stuck at whatever frequency it is at atm).
6483 */
Akash Goel74ef1172015-03-06 11:07:19 +05306484static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006485{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006486 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006487 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006488
Daniel Vetter20b46e52012-07-26 11:16:14 +02006489 /* Only set the down limit when we've reached the lowest level to avoid
6490 * getting more interrupts, otherwise leave this clear. This prevents a
6491 * race in the hw when coming out of rc6: There's a tiny window where
6492 * the hw runs at the minimal clock before selecting the desired
6493 * frequency, if the down threshold expires in that window we will not
6494 * receive a down interrupt. */
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006495 if (INTEL_GEN(dev_priv) >= 9) {
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006496 limits = (rps->max_freq_softlimit) << 23;
6497 if (val <= rps->min_freq_softlimit)
6498 limits |= (rps->min_freq_softlimit) << 14;
Akash Goel74ef1172015-03-06 11:07:19 +05306499 } else {
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006500 limits = rps->max_freq_softlimit << 24;
6501 if (val <= rps->min_freq_softlimit)
6502 limits |= rps->min_freq_softlimit << 16;
Akash Goel74ef1172015-03-06 11:07:19 +05306503 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02006504
6505 return limits;
6506}
6507
Chris Wilson60548c52018-07-31 14:26:29 +01006508static void rps_set_power(struct drm_i915_private *dev_priv, int new_power)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006509{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006510 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Akash Goel8a586432015-03-06 11:07:18 +05306511 u32 threshold_up = 0, threshold_down = 0; /* in % */
6512 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006513
Chris Wilson60548c52018-07-31 14:26:29 +01006514 lockdep_assert_held(&rps->power.mutex);
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006515
Chris Wilson60548c52018-07-31 14:26:29 +01006516 if (new_power == rps->power.mode)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006517 return;
6518
6519 /* Note the units here are not exactly 1us, but 1280ns. */
6520 switch (new_power) {
6521 case LOW_POWER:
6522 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05306523 ei_up = 16000;
6524 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006525
6526 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306527 ei_down = 32000;
6528 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006529 break;
6530
6531 case BETWEEN:
6532 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05306533 ei_up = 13000;
6534 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006535
6536 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306537 ei_down = 32000;
6538 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006539 break;
6540
6541 case HIGH_POWER:
6542 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05306543 ei_up = 10000;
6544 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006545
6546 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306547 ei_down = 32000;
6548 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006549 break;
6550 }
6551
Mika Kuoppala6067a272017-02-15 15:52:59 +02006552 /* When byt can survive without system hang with dynamic
6553 * sw freq adjustments, this restriction can be lifted.
6554 */
6555 if (IS_VALLEYVIEW(dev_priv))
6556 goto skip_hw_write;
6557
Akash Goel8a586432015-03-06 11:07:18 +05306558 I915_WRITE(GEN6_RP_UP_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01006559 GT_INTERVAL_FROM_US(dev_priv, ei_up));
Akash Goel8a586432015-03-06 11:07:18 +05306560 I915_WRITE(GEN6_RP_UP_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01006561 GT_INTERVAL_FROM_US(dev_priv,
6562 ei_up * threshold_up / 100));
Akash Goel8a586432015-03-06 11:07:18 +05306563
6564 I915_WRITE(GEN6_RP_DOWN_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01006565 GT_INTERVAL_FROM_US(dev_priv, ei_down));
Akash Goel8a586432015-03-06 11:07:18 +05306566 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01006567 GT_INTERVAL_FROM_US(dev_priv,
6568 ei_down * threshold_down / 100));
Akash Goel8a586432015-03-06 11:07:18 +05306569
Chris Wilsona72b5622016-07-02 15:35:59 +01006570 I915_WRITE(GEN6_RP_CONTROL,
Mika Kuoppala1071d0f2019-04-10 16:24:36 +03006571 (INTEL_GEN(dev_priv) > 9 ? 0 : GEN6_RP_MEDIA_TURBO) |
Chris Wilsona72b5622016-07-02 15:35:59 +01006572 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6573 GEN6_RP_MEDIA_IS_GFX |
6574 GEN6_RP_ENABLE |
6575 GEN6_RP_UP_BUSY_AVG |
6576 GEN6_RP_DOWN_IDLE_AVG);
Akash Goel8a586432015-03-06 11:07:18 +05306577
Mika Kuoppala6067a272017-02-15 15:52:59 +02006578skip_hw_write:
Chris Wilson60548c52018-07-31 14:26:29 +01006579 rps->power.mode = new_power;
6580 rps->power.up_threshold = threshold_up;
6581 rps->power.down_threshold = threshold_down;
6582}
6583
6584static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
6585{
6586 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6587 int new_power;
6588
6589 new_power = rps->power.mode;
6590 switch (rps->power.mode) {
6591 case LOW_POWER:
6592 if (val > rps->efficient_freq + 1 &&
6593 val > rps->cur_freq)
6594 new_power = BETWEEN;
6595 break;
6596
6597 case BETWEEN:
6598 if (val <= rps->efficient_freq &&
6599 val < rps->cur_freq)
6600 new_power = LOW_POWER;
6601 else if (val >= rps->rp0_freq &&
6602 val > rps->cur_freq)
6603 new_power = HIGH_POWER;
6604 break;
6605
6606 case HIGH_POWER:
6607 if (val < (rps->rp1_freq + rps->rp0_freq) >> 1 &&
6608 val < rps->cur_freq)
6609 new_power = BETWEEN;
6610 break;
6611 }
6612 /* Max/min bins are special */
6613 if (val <= rps->min_freq_softlimit)
6614 new_power = LOW_POWER;
6615 if (val >= rps->max_freq_softlimit)
6616 new_power = HIGH_POWER;
6617
6618 mutex_lock(&rps->power.mutex);
6619 if (rps->power.interactive)
6620 new_power = HIGH_POWER;
6621 rps_set_power(dev_priv, new_power);
6622 mutex_unlock(&rps->power.mutex);
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006623}
6624
Chris Wilson60548c52018-07-31 14:26:29 +01006625void intel_rps_mark_interactive(struct drm_i915_private *i915, bool interactive)
6626{
6627 struct intel_rps *rps = &i915->gt_pm.rps;
6628
6629 if (INTEL_GEN(i915) < 6)
6630 return;
6631
6632 mutex_lock(&rps->power.mutex);
6633 if (interactive) {
6634 if (!rps->power.interactive++ && READ_ONCE(i915->gt.awake))
6635 rps_set_power(i915, HIGH_POWER);
6636 } else {
6637 GEM_BUG_ON(!rps->power.interactive);
6638 rps->power.interactive--;
6639 }
6640 mutex_unlock(&rps->power.mutex);
6641}
6642
Chris Wilson2876ce72014-03-28 08:03:34 +00006643static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
6644{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006645 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Chris Wilson2876ce72014-03-28 08:03:34 +00006646 u32 mask = 0;
6647
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006648 /* We use UP_EI_EXPIRED interupts for both up/down in manual mode */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006649 if (val > rps->min_freq_softlimit)
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006650 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 +01006651 if (val < rps->max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00006652 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00006653
Chris Wilson7b3c29f2014-07-10 20:31:19 +01006654 mask &= dev_priv->pm_rps_events;
6655
Imre Deak59d02a12014-12-19 19:33:26 +02006656 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00006657}
6658
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006659/* gen6_set_rps is called to update the frequency request, but should also be
6660 * called when the range (min_delay and max_delay) is modified so that we can
6661 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006662static int gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02006663{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006664 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6665
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006666 /* min/max delay may still have been modified so be sure to
6667 * write the limits value.
6668 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006669 if (val != rps->cur_freq) {
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006670 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006671
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006672 if (INTEL_GEN(dev_priv) >= 9)
Akash Goel57041952015-03-06 11:07:17 +05306673 I915_WRITE(GEN6_RPNSWREQ,
6674 GEN9_FREQUENCY(val));
Chris Wilsondc979972016-05-10 14:10:04 +01006675 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006676 I915_WRITE(GEN6_RPNSWREQ,
6677 HSW_FREQUENCY(val));
6678 else
6679 I915_WRITE(GEN6_RPNSWREQ,
6680 GEN6_FREQUENCY(val) |
6681 GEN6_OFFSET(0) |
6682 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006683 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006684
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006685 /* Make sure we continue to get interrupts
6686 * until we hit the minimum or maximum frequencies.
6687 */
Akash Goel74ef1172015-03-06 11:07:19 +05306688 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00006689 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006690
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006691 rps->cur_freq = val;
Mika Kuoppala0f945922015-11-17 18:14:26 +02006692 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006693
6694 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006695}
6696
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006697static int valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006698{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006699 int err;
6700
Chris Wilsondc979972016-05-10 14:10:04 +01006701 if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006702 "Odd GPU freq value\n"))
6703 val &= ~1;
6704
Deepak Scd25dd52015-07-10 18:31:40 +05306705 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
6706
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006707 if (val != dev_priv->gt_pm.rps.cur_freq) {
Chris Wilson337fa6e2019-04-26 09:17:20 +01006708 vlv_punit_get(dev_priv);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006709 err = vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
Chris Wilson337fa6e2019-04-26 09:17:20 +01006710 vlv_punit_put(dev_priv);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006711 if (err)
6712 return err;
6713
Chris Wilsondb4c5e02017-02-10 15:03:46 +00006714 gen6_set_rps_thresholds(dev_priv, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01006715 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006716
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006717 dev_priv->gt_pm.rps.cur_freq = val;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006718 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006719
6720 return 0;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006721}
6722
Deepak Sa7f6e232015-05-09 18:04:44 +05306723/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05306724 *
6725 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05306726 * 1. Forcewake Media well.
6727 * 2. Request idle freq.
6728 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05306729*/
6730static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
6731{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006732 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6733 u32 val = rps->idle_freq;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006734 int err;
Deepak S5549d252014-06-28 11:26:11 +05306735
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006736 if (rps->cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05306737 return;
6738
Chris Wilsonc9efef72017-01-02 15:28:45 +00006739 /* The punit delays the write of the frequency and voltage until it
6740 * determines the GPU is awake. During normal usage we don't want to
6741 * waste power changing the frequency if the GPU is sleeping (rc6).
6742 * However, the GPU and driver is now idle and we do not want to delay
6743 * switching to minimum voltage (reducing power whilst idle) as we do
6744 * not expect to be woken in the near future and so must flush the
6745 * change by waking the device.
6746 *
6747 * We choose to take the media powerwell (either would do to trick the
6748 * punit into committing the voltage change) as that takes a lot less
6749 * power than the render powerwell.
6750 */
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07006751 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006752 err = valleyview_set_rps(dev_priv, val);
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07006753 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006754
6755 if (err)
6756 DRM_ERROR("Failed to set RPS for idle\n");
Deepak S76c3552f2014-01-30 23:08:16 +05306757}
6758
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006759void gen6_rps_busy(struct drm_i915_private *dev_priv)
6760{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006761 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6762
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006763 mutex_lock(&rps->lock);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006764 if (rps->enabled) {
Chris Wilsonbd648182017-02-10 15:03:48 +00006765 u8 freq;
6766
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006767 if (dev_priv->pm_rps_events & GEN6_PM_RP_UP_EI_EXPIRED)
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006768 gen6_rps_reset_ei(dev_priv);
6769 I915_WRITE(GEN6_PMINTRMSK,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006770 gen6_rps_pm_mask(dev_priv, rps->cur_freq));
Michał Winiarski2b83c4c2016-06-20 11:58:27 +02006771
Chris Wilsonc33d2472016-07-04 08:08:36 +01006772 gen6_enable_rps_interrupts(dev_priv);
6773
Chris Wilsonbd648182017-02-10 15:03:48 +00006774 /* Use the user's desired frequency as a guide, but for better
6775 * performance, jump directly to RPe as our starting frequency.
6776 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006777 freq = max(rps->cur_freq,
6778 rps->efficient_freq);
Chris Wilsonbd648182017-02-10 15:03:48 +00006779
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006780 if (intel_set_rps(dev_priv,
Chris Wilsonbd648182017-02-10 15:03:48 +00006781 clamp(freq,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006782 rps->min_freq_softlimit,
6783 rps->max_freq_softlimit)))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006784 DRM_DEBUG_DRIVER("Failed to set idle frequency\n");
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006785 }
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006786 mutex_unlock(&rps->lock);
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006787}
6788
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006789void gen6_rps_idle(struct drm_i915_private *dev_priv)
6790{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006791 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6792
Chris Wilsonc33d2472016-07-04 08:08:36 +01006793 /* Flush our bottom-half so that it does not race with us
6794 * setting the idle frequency and so that it is bounded by
6795 * our rpm wakeref. And then disable the interrupts to stop any
6796 * futher RPS reclocking whilst we are asleep.
6797 */
6798 gen6_disable_rps_interrupts(dev_priv);
6799
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006800 mutex_lock(&rps->lock);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006801 if (rps->enabled) {
Chris Wilsondc979972016-05-10 14:10:04 +01006802 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Deepak S76c3552f2014-01-30 23:08:16 +05306803 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02006804 else
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006805 gen6_set_rps(dev_priv, rps->idle_freq);
6806 rps->last_adj = 0;
Ville Syrjälä12c100b2016-05-23 17:42:48 +03006807 I915_WRITE(GEN6_PMINTRMSK,
6808 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Chris Wilsonc0951f02013-10-10 21:58:50 +01006809 }
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006810 mutex_unlock(&rps->lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006811}
6812
Chris Wilson62eb3c22019-02-13 09:25:04 +00006813void gen6_rps_boost(struct i915_request *rq)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006814{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006815 struct intel_rps *rps = &rq->i915->gt_pm.rps;
Chris Wilson74d290f2017-08-17 13:37:06 +01006816 unsigned long flags;
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006817 bool boost;
6818
Chris Wilson8d3afd72015-05-21 21:01:47 +01006819 /* This is intentionally racy! We peek at the state here, then
6820 * validate inside the RPS worker.
6821 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006822 if (!rps->enabled)
Chris Wilson8d3afd72015-05-21 21:01:47 +01006823 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006824
Chris Wilson0e218342019-01-21 22:21:02 +00006825 if (i915_request_signaled(rq))
Chris Wilson253a2812018-02-06 14:31:37 +00006826 return;
6827
Chris Wilsone61e0f52018-02-21 09:56:36 +00006828 /* Serializes with i915_request_retire() */
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006829 boost = false;
Chris Wilson74d290f2017-08-17 13:37:06 +01006830 spin_lock_irqsave(&rq->lock, flags);
Lionel Landwerlin2a98f4e2019-07-09 17:42:27 +01006831 if (!i915_request_has_waitboost(rq) &&
6832 !dma_fence_is_signaled_locked(&rq->fence)) {
Chris Wilson253a2812018-02-06 14:31:37 +00006833 boost = !atomic_fetch_inc(&rps->num_waiters);
Lionel Landwerlin2a98f4e2019-07-09 17:42:27 +01006834 rq->flags |= I915_REQUEST_WAITBOOST;
Chris Wilsonc0951f02013-10-10 21:58:50 +01006835 }
Chris Wilson74d290f2017-08-17 13:37:06 +01006836 spin_unlock_irqrestore(&rq->lock, flags);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006837 if (!boost)
6838 return;
6839
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006840 if (READ_ONCE(rps->cur_freq) < rps->boost_freq)
6841 schedule_work(&rps->work);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006842
Chris Wilson62eb3c22019-02-13 09:25:04 +00006843 atomic_inc(&rps->boosts);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006844}
6845
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006846int intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006847{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006848 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006849 int err;
6850
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006851 lockdep_assert_held(&rps->lock);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006852 GEM_BUG_ON(val > rps->max_freq);
6853 GEM_BUG_ON(val < rps->min_freq);
Chris Wilsoncfd1c482017-02-20 09:47:07 +00006854
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006855 if (!rps->enabled) {
6856 rps->cur_freq = val;
Chris Wilson76e4e4b2017-02-20 09:47:08 +00006857 return 0;
6858 }
6859
Chris Wilsondc979972016-05-10 14:10:04 +01006860 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006861 err = valleyview_set_rps(dev_priv, val);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006862 else
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006863 err = gen6_set_rps(dev_priv, val);
6864
6865 return err;
Jesse Barnes0a073b82013-04-17 15:54:58 -07006866}
6867
Chris Wilsondc979972016-05-10 14:10:04 +01006868static void gen9_disable_rps(struct drm_i915_private *dev_priv)
Akash Goel2030d682016-04-23 00:05:45 +05306869{
Akash Goel2030d682016-04-23 00:05:45 +05306870 I915_WRITE(GEN6_RP_CONTROL, 0);
6871}
6872
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01006873static void gen6_disable_rps(struct drm_i915_private *dev_priv)
6874{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006875 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Akash Goel2030d682016-04-23 00:05:45 +05306876 I915_WRITE(GEN6_RP_CONTROL, 0);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006877}
6878
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01006879static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
6880{
6881 I915_WRITE(GEN6_RP_CONTROL, 0);
6882}
6883
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01006884static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
6885{
6886 I915_WRITE(GEN6_RP_CONTROL, 0);
6887}
6888
Chris Wilsondc979972016-05-10 14:10:04 +01006889static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
Imre Deake6069ca2014-04-18 16:01:02 +03006890{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006891 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6892
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006893 /* All of these values are in units of 50MHz */
Chris Wilson773ea9a2016-07-13 09:10:33 +01006894
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006895 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02006896 if (IS_GEN9_LP(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006897 u32 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006898 rps->rp0_freq = (rp_state_cap >> 16) & 0xff;
6899 rps->rp1_freq = (rp_state_cap >> 8) & 0xff;
6900 rps->min_freq = (rp_state_cap >> 0) & 0xff;
Bob Paauwe35040562015-06-25 14:54:07 -07006901 } else {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006902 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006903 rps->rp0_freq = (rp_state_cap >> 0) & 0xff;
6904 rps->rp1_freq = (rp_state_cap >> 8) & 0xff;
6905 rps->min_freq = (rp_state_cap >> 16) & 0xff;
Bob Paauwe35040562015-06-25 14:54:07 -07006906 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006907 /* hw_max = RP0 until we check for overclocking */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006908 rps->max_freq = rps->rp0_freq;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006909
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006910 rps->efficient_freq = rps->rp1_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01006911 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
Oscar Mateo2b2874e2018-04-05 17:00:52 +03006912 IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006913 u32 ddcc_status = 0;
6914
6915 if (sandybridge_pcode_read(dev_priv,
6916 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
Ville Syrjäläd284d512019-05-21 19:40:24 +03006917 &ddcc_status, NULL) == 0)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006918 rps->efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08006919 clamp_t(u8,
6920 ((ddcc_status >> 8) & 0xff),
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006921 rps->min_freq,
6922 rps->max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006923 }
6924
Oscar Mateo2b2874e2018-04-05 17:00:52 +03006925 if (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
Akash Goelc5e06882015-06-29 14:50:19 +05306926 /* Store the frequency values in 16.66 MHZ units, which is
Chris Wilson773ea9a2016-07-13 09:10:33 +01006927 * the natural hardware unit for SKL
6928 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006929 rps->rp0_freq *= GEN9_FREQ_SCALER;
6930 rps->rp1_freq *= GEN9_FREQ_SCALER;
6931 rps->min_freq *= GEN9_FREQ_SCALER;
6932 rps->max_freq *= GEN9_FREQ_SCALER;
6933 rps->efficient_freq *= GEN9_FREQ_SCALER;
Akash Goelc5e06882015-06-29 14:50:19 +05306934 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006935}
6936
Chris Wilson3a45b052016-07-13 09:10:32 +01006937static void reset_rps(struct drm_i915_private *dev_priv,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006938 int (*set)(struct drm_i915_private *, u8))
Chris Wilson3a45b052016-07-13 09:10:32 +01006939{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006940 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6941 u8 freq = rps->cur_freq;
Chris Wilson3a45b052016-07-13 09:10:32 +01006942
6943 /* force a reset */
Chris Wilson60548c52018-07-31 14:26:29 +01006944 rps->power.mode = -1;
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006945 rps->cur_freq = -1;
Chris Wilson3a45b052016-07-13 09:10:32 +01006946
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006947 if (set(dev_priv, freq))
6948 DRM_ERROR("Failed to reset RPS to initial values\n");
Chris Wilson3a45b052016-07-13 09:10:32 +01006949}
6950
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006951/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Chris Wilsondc979972016-05-10 14:10:04 +01006952static void gen9_enable_rps(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00006953{
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07006954 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006955
David Weinehall36fe7782017-11-17 10:01:46 +02006956 /* Program defaults and thresholds for RPS */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08006957 if (IS_GEN(dev_priv, 9))
David Weinehall36fe7782017-11-17 10:01:46 +02006958 I915_WRITE(GEN6_RC_VIDEO_FREQ,
6959 GEN9_FREQUENCY(dev_priv->gt_pm.rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006960
Akash Goel0beb0592015-03-06 11:07:20 +05306961 /* 1 second timeout*/
6962 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
6963 GT_INTERVAL_FROM_US(dev_priv, 1000000));
6964
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006965 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006966
Akash Goel0beb0592015-03-06 11:07:20 +05306967 /* Leaning on the below call to gen6_set_rps to program/setup the
6968 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
6969 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
Chris Wilson3a45b052016-07-13 09:10:32 +01006970 reset_rps(dev_priv, gen6_set_rps);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006971
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07006972 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006973}
6974
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01006975static void gen8_enable_rps(struct drm_i915_private *dev_priv)
6976{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006977 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6978
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07006979 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01006980
6981 /* 1 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07006982 I915_WRITE(GEN6_RPNSWREQ,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006983 HSW_FREQUENCY(rps->rp1_freq));
Ben Widawskyf9bdc582014-03-31 17:16:41 -07006984 I915_WRITE(GEN6_RC_VIDEO_FREQ,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006985 HSW_FREQUENCY(rps->rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02006986 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
6987 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006988
Daniel Vetter7526ed72014-09-29 15:07:19 +02006989 /* Docs recommend 900MHz, and 300 MHz respectively */
6990 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006991 rps->max_freq_softlimit << 24 |
6992 rps->min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006993
Daniel Vetter7526ed72014-09-29 15:07:19 +02006994 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
6995 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
6996 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
6997 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006998
Daniel Vetter7526ed72014-09-29 15:07:19 +02006999 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007000
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01007001 /* 2: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02007002 I915_WRITE(GEN6_RP_CONTROL,
7003 GEN6_RP_MEDIA_TURBO |
7004 GEN6_RP_MEDIA_HW_NORMAL_MODE |
7005 GEN6_RP_MEDIA_IS_GFX |
7006 GEN6_RP_ENABLE |
7007 GEN6_RP_UP_BUSY_AVG |
7008 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007009
Chris Wilson3a45b052016-07-13 09:10:32 +01007010 reset_rps(dev_priv, gen6_set_rps);
Daniel Vetter7526ed72014-09-29 15:07:19 +02007011
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007012 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007013}
7014
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01007015static void gen6_enable_rps(struct drm_i915_private *dev_priv)
7016{
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01007017 /* Here begins a magic sequence of register writes to enable
7018 * auto-downclocking.
7019 *
7020 * Perhaps there might be some value in exposing these to
7021 * userspace...
7022 */
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007023 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01007024
7025 /* Power down if completely idle for over 50ms */
7026 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
7027 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7028
7029 reset_rps(dev_priv, gen6_set_rps);
7030
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007031 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01007032}
7033
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007034static void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007035{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007036 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Mika Kuoppala66c1f772018-03-20 17:17:33 +02007037 const int min_freq = 15;
7038 const int scaling_factor = 180;
Chris Wilson3ebecd02013-04-12 19:10:13 +01007039 unsigned int gpu_freq;
7040 unsigned int max_ia_freq, min_ring_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05307041 unsigned int max_gpu_freq, min_gpu_freq;
Ben Widawskyeda79642013-10-07 17:15:48 -03007042 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007043
Chris Wilsonebb5eb72019-04-26 09:17:21 +01007044 lockdep_assert_held(&rps->lock);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02007045
Mika Kuoppala66c1f772018-03-20 17:17:33 +02007046 if (rps->max_freq <= rps->min_freq)
7047 return;
7048
Ben Widawskyeda79642013-10-07 17:15:48 -03007049 policy = cpufreq_cpu_get(0);
7050 if (policy) {
7051 max_ia_freq = policy->cpuinfo.max_freq;
7052 cpufreq_cpu_put(policy);
7053 } else {
7054 /*
7055 * Default to measured freq if none found, PCU will ensure we
7056 * don't go over
7057 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007058 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03007059 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007060
7061 /* Convert from kHz to MHz */
7062 max_ia_freq /= 1000;
7063
Ben Widawsky153b4b952013-10-22 22:05:09 -07007064 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07007065 /* convert DDR frequency from units of 266.6MHz to bandwidth */
7066 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01007067
Chris Wilsond586b5f2018-03-08 14:26:48 +00007068 min_gpu_freq = rps->min_freq;
7069 max_gpu_freq = rps->max_freq;
Oscar Mateo2b2874e2018-04-05 17:00:52 +03007070 if (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
Akash Goel4c8c7742015-06-29 14:50:20 +05307071 /* Convert GT frequency to 50 HZ units */
Chris Wilsond586b5f2018-03-08 14:26:48 +00007072 min_gpu_freq /= GEN9_FREQ_SCALER;
7073 max_gpu_freq /= GEN9_FREQ_SCALER;
Akash Goel4c8c7742015-06-29 14:50:20 +05307074 }
7075
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007076 /*
7077 * For each potential GPU frequency, load a ring frequency we'd like
7078 * to use for memory access. We do this by specifying the IA frequency
7079 * the PCU should use as a reference to determine the ring frequency.
7080 */
Akash Goel4c8c7742015-06-29 14:50:20 +05307081 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
Mika Kuoppala66c1f772018-03-20 17:17:33 +02007082 const int diff = max_gpu_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01007083 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007084
Oscar Mateo2b2874e2018-04-05 17:00:52 +03007085 if (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
Akash Goel4c8c7742015-06-29 14:50:20 +05307086 /*
7087 * ring_freq = 2 * GT. ring_freq is in 100MHz units
7088 * No floor required for ring frequency on SKL.
7089 */
7090 ring_freq = gpu_freq;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00007091 } else if (INTEL_GEN(dev_priv) >= 8) {
Ben Widawsky46c764d2013-11-02 21:07:49 -07007092 /* max(2 * GT, DDR). NB: GT is 50MHz units */
7093 ring_freq = max(min_ring_freq, gpu_freq);
Chris Wilsondc979972016-05-10 14:10:04 +01007094 } else if (IS_HASWELL(dev_priv)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07007095 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01007096 ring_freq = max(min_ring_freq, ring_freq);
7097 /* leave ia_freq as the default, chosen by cpufreq */
7098 } else {
7099 /* On older processors, there is no separate ring
7100 * clock domain, so in order to boost the bandwidth
7101 * of the ring, we need to upclock the CPU (ia_freq).
7102 *
7103 * For GPU frequencies less than 750MHz,
7104 * just use the lowest ring freq.
7105 */
7106 if (gpu_freq < min_freq)
7107 ia_freq = 800;
7108 else
7109 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
7110 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
7111 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007112
Ben Widawsky42c05262012-09-26 10:34:00 -07007113 sandybridge_pcode_write(dev_priv,
7114 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01007115 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
7116 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
7117 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007118 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007119}
7120
Ville Syrjälä03af2042014-06-28 02:03:53 +03007121static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05307122{
7123 u32 val, rp0;
7124
Jani Nikula5b5929c2015-10-07 11:17:46 +03007125 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05307126
Jani Nikula02584042018-12-31 16:56:41 +02007127 switch (RUNTIME_INFO(dev_priv)->sseu.eu_total) {
Jani Nikula5b5929c2015-10-07 11:17:46 +03007128 case 8:
7129 /* (2 * 4) config */
7130 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
7131 break;
7132 case 12:
7133 /* (2 * 6) config */
7134 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
7135 break;
7136 case 16:
7137 /* (2 * 8) config */
7138 default:
7139 /* Setting (2 * 8) Min RP0 for any other combination */
7140 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
7141 break;
Deepak S095acd52015-01-17 11:05:59 +05307142 }
Jani Nikula5b5929c2015-10-07 11:17:46 +03007143
7144 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
7145
Deepak S2b6b3a02014-05-27 15:59:30 +05307146 return rp0;
7147}
7148
7149static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
7150{
7151 u32 val, rpe;
7152
7153 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
7154 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
7155
7156 return rpe;
7157}
7158
Deepak S7707df42014-07-12 18:46:14 +05307159static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
7160{
7161 u32 val, rp1;
7162
Jani Nikula5b5929c2015-10-07 11:17:46 +03007163 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
7164 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
7165
Deepak S7707df42014-07-12 18:46:14 +05307166 return rp1;
7167}
7168
Deepak S96676fe2016-08-12 18:46:41 +05307169static u32 cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
7170{
7171 u32 val, rpn;
7172
7173 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
7174 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
7175 FB_GFX_FREQ_FUSE_MASK);
7176
7177 return rpn;
7178}
7179
Deepak Sf8f2b002014-07-10 13:16:21 +05307180static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
7181{
7182 u32 val, rp1;
7183
7184 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
7185
7186 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
7187
7188 return rp1;
7189}
7190
Ville Syrjälä03af2042014-06-28 02:03:53 +03007191static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07007192{
7193 u32 val, rp0;
7194
Jani Nikula64936252013-05-22 15:36:20 +03007195 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007196
7197 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
7198 /* Clamp to max */
7199 rp0 = min_t(u32, rp0, 0xea);
7200
7201 return rp0;
7202}
7203
7204static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
7205{
7206 u32 val, rpe;
7207
Jani Nikula64936252013-05-22 15:36:20 +03007208 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007209 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03007210 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007211 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
7212
7213 return rpe;
7214}
7215
Ville Syrjälä03af2042014-06-28 02:03:53 +03007216static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07007217{
Imre Deak36146032014-12-04 18:39:35 +02007218 u32 val;
7219
7220 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
7221 /*
7222 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
7223 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
7224 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
7225 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
7226 * to make sure it matches what Punit accepts.
7227 */
7228 return max_t(u32, val, 0xc0);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007229}
7230
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007231static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
7232{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007233 dev_priv->gt_pm.rps.gpll_ref_freq =
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007234 vlv_get_cck_clock(dev_priv, "GPLL ref",
7235 CCK_GPLL_CLOCK_CONTROL,
7236 dev_priv->czclk_freq);
7237
7238 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007239 dev_priv->gt_pm.rps.gpll_ref_freq);
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007240}
7241
Chris Wilsondc979972016-05-10 14:10:04 +01007242static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03007243{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007244 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007245 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03007246
Chris Wilson337fa6e2019-04-26 09:17:20 +01007247 vlv_iosf_sb_get(dev_priv,
7248 BIT(VLV_IOSF_SB_PUNIT) |
7249 BIT(VLV_IOSF_SB_NC) |
7250 BIT(VLV_IOSF_SB_CCK));
7251
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007252 vlv_init_gpll_ref_freq(dev_priv);
7253
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007254 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
7255 switch ((val >> 6) & 3) {
7256 case 0:
7257 case 1:
7258 dev_priv->mem_freq = 800;
7259 break;
7260 case 2:
7261 dev_priv->mem_freq = 1066;
7262 break;
7263 case 3:
7264 dev_priv->mem_freq = 1333;
7265 break;
7266 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02007267 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007268
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007269 rps->max_freq = valleyview_rps_max_freq(dev_priv);
7270 rps->rp0_freq = rps->max_freq;
Imre Deak4e805192014-04-14 20:24:41 +03007271 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007272 intel_gpu_freq(dev_priv, rps->max_freq),
7273 rps->max_freq);
Imre Deak4e805192014-04-14 20:24:41 +03007274
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007275 rps->efficient_freq = valleyview_rps_rpe_freq(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007276 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007277 intel_gpu_freq(dev_priv, rps->efficient_freq),
7278 rps->efficient_freq);
Imre Deak4e805192014-04-14 20:24:41 +03007279
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007280 rps->rp1_freq = valleyview_rps_guar_freq(dev_priv);
Deepak Sf8f2b002014-07-10 13:16:21 +05307281 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007282 intel_gpu_freq(dev_priv, rps->rp1_freq),
7283 rps->rp1_freq);
Deepak Sf8f2b002014-07-10 13:16:21 +05307284
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007285 rps->min_freq = valleyview_rps_min_freq(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007286 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007287 intel_gpu_freq(dev_priv, rps->min_freq),
7288 rps->min_freq);
Chris Wilson337fa6e2019-04-26 09:17:20 +01007289
7290 vlv_iosf_sb_put(dev_priv,
7291 BIT(VLV_IOSF_SB_PUNIT) |
7292 BIT(VLV_IOSF_SB_NC) |
7293 BIT(VLV_IOSF_SB_CCK));
Imre Deak4e805192014-04-14 20:24:41 +03007294}
7295
Chris Wilsondc979972016-05-10 14:10:04 +01007296static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05307297{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007298 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007299 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05307300
Chris Wilson337fa6e2019-04-26 09:17:20 +01007301 vlv_iosf_sb_get(dev_priv,
7302 BIT(VLV_IOSF_SB_PUNIT) |
7303 BIT(VLV_IOSF_SB_NC) |
7304 BIT(VLV_IOSF_SB_CCK));
7305
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007306 vlv_init_gpll_ref_freq(dev_priv);
7307
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02007308 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02007309
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007310 switch ((val >> 2) & 0x7) {
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007311 case 3:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007312 dev_priv->mem_freq = 2000;
7313 break;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03007314 default:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007315 dev_priv->mem_freq = 1600;
7316 break;
7317 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02007318 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007319
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007320 rps->max_freq = cherryview_rps_max_freq(dev_priv);
7321 rps->rp0_freq = rps->max_freq;
Deepak S2b6b3a02014-05-27 15:59:30 +05307322 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007323 intel_gpu_freq(dev_priv, rps->max_freq),
7324 rps->max_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05307325
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007326 rps->efficient_freq = cherryview_rps_rpe_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307327 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007328 intel_gpu_freq(dev_priv, rps->efficient_freq),
7329 rps->efficient_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05307330
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007331 rps->rp1_freq = cherryview_rps_guar_freq(dev_priv);
Deepak S7707df42014-07-12 18:46:14 +05307332 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007333 intel_gpu_freq(dev_priv, rps->rp1_freq),
7334 rps->rp1_freq);
Deepak S7707df42014-07-12 18:46:14 +05307335
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007336 rps->min_freq = cherryview_rps_min_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307337 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007338 intel_gpu_freq(dev_priv, rps->min_freq),
7339 rps->min_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05307340
Chris Wilson337fa6e2019-04-26 09:17:20 +01007341 vlv_iosf_sb_put(dev_priv,
7342 BIT(VLV_IOSF_SB_PUNIT) |
7343 BIT(VLV_IOSF_SB_NC) |
7344 BIT(VLV_IOSF_SB_CCK));
7345
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007346 WARN_ONCE((rps->max_freq | rps->efficient_freq | rps->rp1_freq |
7347 rps->min_freq) & 1,
Ville Syrjälä1c147622014-08-18 14:42:43 +03007348 "Odd GPU freq values\n");
Deepak S38807742014-05-23 21:00:15 +05307349}
7350
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007351static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
7352{
7353 u32 val;
7354
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007355 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007356
7357 /* 1: Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02007358 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05307359 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
7360 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
7361 I915_WRITE(GEN6_RP_UP_EI, 66000);
7362 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
7363
7364 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7365
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007366 /* 2: Enable RPS */
Deepak S2b6b3a02014-05-27 15:59:30 +05307367 I915_WRITE(GEN6_RP_CONTROL,
7368 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02007369 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05307370 GEN6_RP_ENABLE |
7371 GEN6_RP_UP_BUSY_AVG |
7372 GEN6_RP_DOWN_IDLE_AVG);
7373
Deepak S3ef62342015-04-29 08:36:24 +05307374 /* Setting Fixed Bias */
Chris Wilson337fa6e2019-04-26 09:17:20 +01007375 vlv_punit_get(dev_priv);
7376
7377 val = VLV_OVERRIDE_EN | VLV_SOC_TDP_EN | CHV_BIAS_CPU_50_SOC_50;
Deepak S3ef62342015-04-29 08:36:24 +05307378 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
7379
Deepak S2b6b3a02014-05-27 15:59:30 +05307380 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
7381
Chris Wilson337fa6e2019-04-26 09:17:20 +01007382 vlv_punit_put(dev_priv);
7383
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02007384 /* RPS code assumes GPLL is used */
7385 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
7386
Jani Nikula742f4912015-09-03 11:16:09 +03007387 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05307388 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7389
Chris Wilson3a45b052016-07-13 09:10:32 +01007390 reset_rps(dev_priv, valleyview_set_rps);
Deepak S2b6b3a02014-05-27 15:59:30 +05307391
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007392 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05307393}
7394
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01007395static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
7396{
7397 u32 val;
7398
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007399 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01007400
Ville Syrjäläcad725f2015-01-19 13:50:48 +02007401 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007402 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
7403 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
7404 I915_WRITE(GEN6_RP_UP_EI, 66000);
7405 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
7406
7407 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7408
7409 I915_WRITE(GEN6_RP_CONTROL,
7410 GEN6_RP_MEDIA_TURBO |
7411 GEN6_RP_MEDIA_HW_NORMAL_MODE |
7412 GEN6_RP_MEDIA_IS_GFX |
7413 GEN6_RP_ENABLE |
7414 GEN6_RP_UP_BUSY_AVG |
7415 GEN6_RP_DOWN_IDLE_CONT);
7416
Chris Wilson337fa6e2019-04-26 09:17:20 +01007417 vlv_punit_get(dev_priv);
7418
Deepak S3ef62342015-04-29 08:36:24 +05307419 /* Setting Fixed Bias */
Chris Wilson337fa6e2019-04-26 09:17:20 +01007420 val = VLV_OVERRIDE_EN | VLV_SOC_TDP_EN | VLV_BIAS_CPU_125_SOC_875;
Deepak S3ef62342015-04-29 08:36:24 +05307421 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
7422
Jani Nikula64936252013-05-22 15:36:20 +03007423 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007424
Chris Wilson337fa6e2019-04-26 09:17:20 +01007425 vlv_punit_put(dev_priv);
7426
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02007427 /* RPS code assumes GPLL is used */
7428 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
7429
Jani Nikula742f4912015-09-03 11:16:09 +03007430 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07007431 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7432
Chris Wilson3a45b052016-07-13 09:10:32 +01007433 reset_rps(dev_priv, valleyview_set_rps);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007434
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007435 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007436}
7437
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007438static unsigned long intel_pxfreq(u32 vidfreq)
7439{
7440 unsigned long freq;
7441 int div = (vidfreq & 0x3f0000) >> 16;
7442 int post = (vidfreq & 0x3000) >> 12;
7443 int pre = (vidfreq & 0x7);
7444
7445 if (!pre)
7446 return 0;
7447
7448 freq = ((div * 133333) / ((1<<post) * pre));
7449
7450 return freq;
7451}
7452
Daniel Vettereb48eb02012-04-26 23:28:12 +02007453static const struct cparams {
7454 u16 i;
7455 u16 t;
7456 u16 m;
7457 u16 c;
7458} cparams[] = {
7459 { 1, 1333, 301, 28664 },
7460 { 1, 1066, 294, 24460 },
7461 { 1, 800, 294, 25192 },
7462 { 0, 1333, 276, 27605 },
7463 { 0, 1066, 276, 27605 },
7464 { 0, 800, 231, 23784 },
7465};
7466
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007467static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007468{
7469 u64 total_count, diff, ret;
7470 u32 count1, count2, count3, m = 0, c = 0;
7471 unsigned long now = jiffies_to_msecs(jiffies), diff1;
7472 int i;
7473
Chris Wilson67520412017-03-02 13:28:01 +00007474 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007475
Daniel Vetter20e4d402012-08-08 23:35:39 +02007476 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007477
7478 /* Prevent division-by-zero if we are asking too fast.
7479 * Also, we don't get interesting results if we are polling
7480 * faster than once in 10ms, so just return the saved value
7481 * in such cases.
7482 */
7483 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02007484 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007485
7486 count1 = I915_READ(DMIEC);
7487 count2 = I915_READ(DDREC);
7488 count3 = I915_READ(CSIEC);
7489
7490 total_count = count1 + count2 + count3;
7491
7492 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02007493 if (total_count < dev_priv->ips.last_count1) {
7494 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007495 diff += total_count;
7496 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007497 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007498 }
7499
7500 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007501 if (cparams[i].i == dev_priv->ips.c_m &&
7502 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02007503 m = cparams[i].m;
7504 c = cparams[i].c;
7505 break;
7506 }
7507 }
7508
7509 diff = div_u64(diff, diff1);
7510 ret = ((m * diff) + c);
7511 ret = div_u64(ret, 10);
7512
Daniel Vetter20e4d402012-08-08 23:35:39 +02007513 dev_priv->ips.last_count1 = total_count;
7514 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007515
Daniel Vetter20e4d402012-08-08 23:35:39 +02007516 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007517
7518 return ret;
7519}
7520
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007521unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
7522{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007523 intel_wakeref_t wakeref;
7524 unsigned long val = 0;
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007525
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007526 if (!IS_GEN(dev_priv, 5))
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007527 return 0;
7528
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -07007529 with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref) {
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007530 spin_lock_irq(&mchdev_lock);
7531 val = __i915_chipset_val(dev_priv);
7532 spin_unlock_irq(&mchdev_lock);
7533 }
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007534
7535 return val;
7536}
7537
Tvrtko Ursulinc54f0ba2019-06-11 11:45:43 +01007538unsigned long i915_mch_val(struct drm_i915_private *i915)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007539{
7540 unsigned long m, x, b;
7541 u32 tsfs;
7542
Tvrtko Ursulinc54f0ba2019-06-11 11:45:43 +01007543 tsfs = intel_uncore_read(&i915->uncore, TSFS);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007544
7545 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
Tvrtko Ursulinc54f0ba2019-06-11 11:45:43 +01007546 x = intel_uncore_read8(&i915->uncore, TR1);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007547
7548 b = tsfs & TSFS_INTR_MASK;
7549
7550 return ((m * x) / 127) - b;
7551}
7552
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007553static int _pxvid_to_vd(u8 pxvid)
7554{
7555 if (pxvid == 0)
7556 return 0;
7557
7558 if (pxvid >= 8 && pxvid < 31)
7559 pxvid = 31;
7560
7561 return (pxvid + 2) * 125;
7562}
7563
7564static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007565{
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007566 const int vd = _pxvid_to_vd(pxvid);
7567 const int vm = vd - 1125;
7568
Chris Wilsondc979972016-05-10 14:10:04 +01007569 if (INTEL_INFO(dev_priv)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007570 return vm > 0 ? vm : 0;
7571
7572 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007573}
7574
Daniel Vetter02d71952012-08-09 16:44:54 +02007575static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007576{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00007577 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007578 u32 count;
7579
Chris Wilson67520412017-03-02 13:28:01 +00007580 lockdep_assert_held(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007581
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00007582 now = ktime_get_raw_ns();
7583 diffms = now - dev_priv->ips.last_time2;
7584 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007585
7586 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02007587 if (!diffms)
7588 return;
7589
7590 count = I915_READ(GFXEC);
7591
Daniel Vetter20e4d402012-08-08 23:35:39 +02007592 if (count < dev_priv->ips.last_count2) {
7593 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007594 diff += count;
7595 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007596 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007597 }
7598
Daniel Vetter20e4d402012-08-08 23:35:39 +02007599 dev_priv->ips.last_count2 = count;
7600 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007601
7602 /* More magic constants... */
7603 diff = diff * 1181;
7604 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02007605 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007606}
7607
Daniel Vetter02d71952012-08-09 16:44:54 +02007608void i915_update_gfx_val(struct drm_i915_private *dev_priv)
7609{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007610 intel_wakeref_t wakeref;
7611
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007612 if (!IS_GEN(dev_priv, 5))
Daniel Vetter02d71952012-08-09 16:44:54 +02007613 return;
7614
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -07007615 with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref) {
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007616 spin_lock_irq(&mchdev_lock);
7617 __i915_update_gfx_val(dev_priv);
7618 spin_unlock_irq(&mchdev_lock);
7619 }
Daniel Vetter02d71952012-08-09 16:44:54 +02007620}
7621
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007622static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007623{
7624 unsigned long t, corr, state1, corr2, state2;
7625 u32 pxvid, ext_v;
7626
Chris Wilson67520412017-03-02 13:28:01 +00007627 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007628
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007629 pxvid = I915_READ(PXVFREQ(dev_priv->gt_pm.rps.cur_freq));
Daniel Vettereb48eb02012-04-26 23:28:12 +02007630 pxvid = (pxvid >> 24) & 0x7f;
7631 ext_v = pvid_to_extvid(dev_priv, pxvid);
7632
7633 state1 = ext_v;
7634
7635 t = i915_mch_val(dev_priv);
7636
7637 /* Revel in the empirically derived constants */
7638
7639 /* Correction factor in 1/100000 units */
7640 if (t > 80)
7641 corr = ((t * 2349) + 135940);
7642 else if (t >= 50)
7643 corr = ((t * 964) + 29317);
7644 else /* < 50 */
7645 corr = ((t * 301) + 1004);
7646
7647 corr = corr * ((150142 * state1) / 10000 - 78642);
7648 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02007649 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007650
7651 state2 = (corr2 * state1) / 10000;
7652 state2 /= 100; /* convert to mW */
7653
Daniel Vetter02d71952012-08-09 16:44:54 +02007654 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007655
Daniel Vetter20e4d402012-08-08 23:35:39 +02007656 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007657}
7658
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007659unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
7660{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007661 intel_wakeref_t wakeref;
7662 unsigned long val = 0;
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007663
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007664 if (!IS_GEN(dev_priv, 5))
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007665 return 0;
7666
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -07007667 with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref) {
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007668 spin_lock_irq(&mchdev_lock);
7669 val = __i915_gfx_val(dev_priv);
7670 spin_unlock_irq(&mchdev_lock);
7671 }
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007672
7673 return val;
7674}
7675
Chris Wilsonadc674c2019-04-12 09:53:22 +01007676static struct drm_i915_private __rcu *i915_mch_dev;
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007677
7678static struct drm_i915_private *mchdev_get(void)
7679{
7680 struct drm_i915_private *i915;
7681
7682 rcu_read_lock();
Chris Wilsonadc674c2019-04-12 09:53:22 +01007683 i915 = rcu_dereference(i915_mch_dev);
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007684 if (!kref_get_unless_zero(&i915->drm.ref))
7685 i915 = NULL;
7686 rcu_read_unlock();
7687
7688 return i915;
7689}
7690
Daniel Vettereb48eb02012-04-26 23:28:12 +02007691/**
7692 * i915_read_mch_val - return value for IPS use
7693 *
7694 * Calculate and return a value for the IPS driver to use when deciding whether
7695 * we have thermal and power headroom to increase CPU or GPU power budget.
7696 */
7697unsigned long i915_read_mch_val(void)
7698{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007699 struct drm_i915_private *i915;
7700 unsigned long chipset_val = 0;
7701 unsigned long graphics_val = 0;
7702 intel_wakeref_t wakeref;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007703
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007704 i915 = mchdev_get();
7705 if (!i915)
7706 return 0;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007707
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -07007708 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007709 spin_lock_irq(&mchdev_lock);
7710 chipset_val = __i915_chipset_val(i915);
7711 graphics_val = __i915_gfx_val(i915);
7712 spin_unlock_irq(&mchdev_lock);
7713 }
Daniel Vettereb48eb02012-04-26 23:28:12 +02007714
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007715 drm_dev_put(&i915->drm);
7716 return chipset_val + graphics_val;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007717}
7718EXPORT_SYMBOL_GPL(i915_read_mch_val);
7719
7720/**
7721 * i915_gpu_raise - raise GPU frequency limit
7722 *
7723 * Raise the limit; IPS indicates we have thermal headroom.
7724 */
7725bool i915_gpu_raise(void)
7726{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007727 struct drm_i915_private *i915;
7728
7729 i915 = mchdev_get();
7730 if (!i915)
7731 return false;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007732
Daniel Vetter92703882012-08-09 16:46:01 +02007733 spin_lock_irq(&mchdev_lock);
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007734 if (i915->ips.max_delay > i915->ips.fmax)
7735 i915->ips.max_delay--;
Daniel Vetter92703882012-08-09 16:46:01 +02007736 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007737
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007738 drm_dev_put(&i915->drm);
7739 return true;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007740}
7741EXPORT_SYMBOL_GPL(i915_gpu_raise);
7742
7743/**
7744 * i915_gpu_lower - lower GPU frequency limit
7745 *
7746 * IPS indicates we're close to a thermal limit, so throttle back the GPU
7747 * frequency maximum.
7748 */
7749bool i915_gpu_lower(void)
7750{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007751 struct drm_i915_private *i915;
7752
7753 i915 = mchdev_get();
7754 if (!i915)
7755 return false;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007756
Daniel Vetter92703882012-08-09 16:46:01 +02007757 spin_lock_irq(&mchdev_lock);
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007758 if (i915->ips.max_delay < i915->ips.min_delay)
7759 i915->ips.max_delay++;
Daniel Vetter92703882012-08-09 16:46:01 +02007760 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007761
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007762 drm_dev_put(&i915->drm);
7763 return true;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007764}
7765EXPORT_SYMBOL_GPL(i915_gpu_lower);
7766
7767/**
7768 * i915_gpu_busy - indicate GPU business to IPS
7769 *
7770 * Tell the IPS driver whether or not the GPU is busy.
7771 */
7772bool i915_gpu_busy(void)
7773{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007774 struct drm_i915_private *i915;
7775 bool ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007776
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007777 i915 = mchdev_get();
7778 if (!i915)
7779 return false;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007780
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007781 ret = i915->gt.awake;
7782
7783 drm_dev_put(&i915->drm);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007784 return ret;
7785}
7786EXPORT_SYMBOL_GPL(i915_gpu_busy);
7787
7788/**
7789 * i915_gpu_turbo_disable - disable graphics turbo
7790 *
7791 * Disable graphics turbo by resetting the max frequency and setting the
7792 * current frequency to the default.
7793 */
7794bool i915_gpu_turbo_disable(void)
7795{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007796 struct drm_i915_private *i915;
7797 bool ret;
7798
7799 i915 = mchdev_get();
7800 if (!i915)
7801 return false;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007802
Daniel Vetter92703882012-08-09 16:46:01 +02007803 spin_lock_irq(&mchdev_lock);
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007804 i915->ips.max_delay = i915->ips.fstart;
7805 ret = ironlake_set_drps(i915, i915->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02007806 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007807
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007808 drm_dev_put(&i915->drm);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007809 return ret;
7810}
7811EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
7812
7813/**
7814 * Tells the intel_ips driver that the i915 driver is now loaded, if
7815 * IPS got loaded first.
7816 *
7817 * This awkward dance is so that neither module has to depend on the
7818 * other in order for IPS to do the appropriate communication of
7819 * GPU turbo limits to i915.
7820 */
7821static void
7822ips_ping_for_i915_load(void)
7823{
7824 void (*link)(void);
7825
7826 link = symbol_get(ips_link_to_i915_driver);
7827 if (link) {
7828 link();
7829 symbol_put(ips_link_to_i915_driver);
7830 }
7831}
7832
7833void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
7834{
Daniel Vetter02d71952012-08-09 16:44:54 +02007835 /* We only register the i915 ips part with intel-ips once everything is
7836 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007837 rcu_assign_pointer(i915_mch_dev, dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007838
7839 ips_ping_for_i915_load();
7840}
7841
7842void intel_gpu_ips_teardown(void)
7843{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007844 rcu_assign_pointer(i915_mch_dev, NULL);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007845}
Deepak S76c3552f2014-01-30 23:08:16 +05307846
Chris Wilsondc979972016-05-10 14:10:04 +01007847static void intel_init_emon(struct drm_i915_private *dev_priv)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007848{
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007849 u32 lcfuse;
7850 u8 pxw[16];
7851 int i;
7852
7853 /* Disable to program */
7854 I915_WRITE(ECR, 0);
7855 POSTING_READ(ECR);
7856
7857 /* Program energy weights for various events */
7858 I915_WRITE(SDEW, 0x15040d00);
7859 I915_WRITE(CSIEW0, 0x007f0000);
7860 I915_WRITE(CSIEW1, 0x1e220004);
7861 I915_WRITE(CSIEW2, 0x04000004);
7862
7863 for (i = 0; i < 5; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007864 I915_WRITE(PEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007865 for (i = 0; i < 3; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007866 I915_WRITE(DEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007867
7868 /* Program P-state weights to account for frequency power adjustment */
7869 for (i = 0; i < 16; i++) {
Ville Syrjälä616847e2015-09-18 20:03:19 +03007870 u32 pxvidfreq = I915_READ(PXVFREQ(i));
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007871 unsigned long freq = intel_pxfreq(pxvidfreq);
7872 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
7873 PXVFREQ_PX_SHIFT;
7874 unsigned long val;
7875
7876 val = vid * vid;
7877 val *= (freq / 1000);
7878 val *= 255;
7879 val /= (127*127*900);
7880 if (val > 0xff)
7881 DRM_ERROR("bad pxval: %ld\n", val);
7882 pxw[i] = val;
7883 }
7884 /* Render standby states get 0 weight */
7885 pxw[14] = 0;
7886 pxw[15] = 0;
7887
7888 for (i = 0; i < 4; i++) {
7889 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
7890 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
Ville Syrjälä616847e2015-09-18 20:03:19 +03007891 I915_WRITE(PXW(i), val);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007892 }
7893
7894 /* Adjust magic regs to magic values (more experimental results) */
7895 I915_WRITE(OGW0, 0);
7896 I915_WRITE(OGW1, 0);
7897 I915_WRITE(EG0, 0x00007f00);
7898 I915_WRITE(EG1, 0x0000000e);
7899 I915_WRITE(EG2, 0x000e0000);
7900 I915_WRITE(EG3, 0x68000300);
7901 I915_WRITE(EG4, 0x42000000);
7902 I915_WRITE(EG5, 0x00140031);
7903 I915_WRITE(EG6, 0);
7904 I915_WRITE(EG7, 0);
7905
7906 for (i = 0; i < 8; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007907 I915_WRITE(PXWL(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007908
7909 /* Enable PMON + select events */
7910 I915_WRITE(ECR, 0x80000019);
7911
7912 lcfuse = I915_READ(LCFUSE02);
7913
Daniel Vetter20e4d402012-08-08 23:35:39 +02007914 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007915}
7916
Chris Wilsondc979972016-05-10 14:10:04 +01007917void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007918{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007919 struct intel_rps *rps = &dev_priv->gt_pm.rps;
7920
Andi Shytic1132362019-09-27 12:08:49 +01007921 /* Powersaving is controlled by the host when inside a VM */
7922 if (intel_vgpu_active(dev_priv))
7923 mkwrite_device_info(dev_priv)->has_rps = false;
Imre Deake6069ca2014-04-18 16:01:02 +03007924
Chris Wilson773ea9a2016-07-13 09:10:33 +01007925 /* Initialize RPS limits (for userspace) */
Chris Wilsondc979972016-05-10 14:10:04 +01007926 if (IS_CHERRYVIEW(dev_priv))
7927 cherryview_init_gt_powersave(dev_priv);
7928 else if (IS_VALLEYVIEW(dev_priv))
7929 valleyview_init_gt_powersave(dev_priv);
Chris Wilson2a13ae72016-08-02 11:15:27 +01007930 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson773ea9a2016-07-13 09:10:33 +01007931 gen6_init_rps_frequencies(dev_priv);
7932
7933 /* Derive initial user preferences/limits from the hardware limits */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007934 rps->max_freq_softlimit = rps->max_freq;
7935 rps->min_freq_softlimit = rps->min_freq;
Chris Wilson773ea9a2016-07-13 09:10:33 +01007936
Chris Wilson99ac9612016-07-13 09:10:34 +01007937 /* After setting max-softlimit, find the overclock max freq */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007938 if (IS_GEN(dev_priv, 6) ||
Chris Wilson99ac9612016-07-13 09:10:34 +01007939 IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
7940 u32 params = 0;
7941
Ville Syrjäläd284d512019-05-21 19:40:24 +03007942 sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS,
7943 &params, NULL);
Chris Wilson99ac9612016-07-13 09:10:34 +01007944 if (params & BIT(31)) { /* OC supported */
7945 DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007946 (rps->max_freq & 0xff) * 50,
Chris Wilson99ac9612016-07-13 09:10:34 +01007947 (params & 0xff) * 50);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007948 rps->max_freq = params & 0xff;
Chris Wilson99ac9612016-07-13 09:10:34 +01007949 }
7950 }
7951
Chris Wilson29ecd78d2016-07-13 09:10:35 +01007952 /* Finally allow us to boost to max by default */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007953 rps->boost_freq = rps->max_freq;
Chris Wilson844e3312019-04-18 21:53:58 +01007954 rps->idle_freq = rps->min_freq;
7955 rps->cur_freq = rps->idle_freq;
Imre Deakae484342014-03-31 15:10:44 +03007956}
7957
Chris Wilsonb7137e02016-07-13 09:10:37 +01007958void intel_sanitize_gt_powersave(struct drm_i915_private *dev_priv)
7959{
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01007960 dev_priv->gt_pm.rps.enabled = true; /* force RPS disabling */
Chris Wilsonb7137e02016-07-13 09:10:37 +01007961 intel_disable_gt_powersave(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007962
Oscar Mateod02b98b2018-04-05 17:00:50 +03007963 if (INTEL_GEN(dev_priv) >= 11)
7964 gen11_reset_rps_interrupts(dev_priv);
Chris Wilson61e1e372018-08-12 23:36:30 +01007965 else if (INTEL_GEN(dev_priv) >= 6)
Oscar Mateod02b98b2018-04-05 17:00:50 +03007966 gen6_reset_rps_interrupts(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07007967}
7968
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01007969static inline void intel_disable_llc_pstate(struct drm_i915_private *i915)
7970{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01007971 lockdep_assert_held(&i915->gt_pm.rps.lock);
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01007972
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01007973 if (!i915->gt_pm.llc_pstate.enabled)
7974 return;
7975
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01007976 /* Currently there is no HW configuration to be done to disable. */
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01007977
7978 i915->gt_pm.llc_pstate.enabled = false;
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01007979}
7980
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01007981static void intel_disable_rps(struct drm_i915_private *dev_priv)
7982{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01007983 lockdep_assert_held(&dev_priv->gt_pm.rps.lock);
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01007984
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01007985 if (!dev_priv->gt_pm.rps.enabled)
7986 return;
7987
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01007988 if (INTEL_GEN(dev_priv) >= 9)
7989 gen9_disable_rps(dev_priv);
7990 else if (IS_CHERRYVIEW(dev_priv))
7991 cherryview_disable_rps(dev_priv);
7992 else if (IS_VALLEYVIEW(dev_priv))
7993 valleyview_disable_rps(dev_priv);
7994 else if (INTEL_GEN(dev_priv) >= 6)
7995 gen6_disable_rps(dev_priv);
7996 else if (IS_IRONLAKE_M(dev_priv))
7997 ironlake_disable_drps(dev_priv);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01007998
7999 dev_priv->gt_pm.rps.enabled = false;
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008000}
8001
Chris Wilsondc979972016-05-10 14:10:04 +01008002void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
Daniel Vetter8090c6b2012-06-24 16:42:32 +02008003{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008004 mutex_lock(&dev_priv->gt_pm.rps.lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07008005
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008006 intel_disable_rps(dev_priv);
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008007 if (HAS_LLC(dev_priv))
8008 intel_disable_llc_pstate(dev_priv);
8009
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008010 mutex_unlock(&dev_priv->gt_pm.rps.lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01008011}
8012
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008013static inline void intel_enable_llc_pstate(struct drm_i915_private *i915)
8014{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008015 lockdep_assert_held(&i915->gt_pm.rps.lock);
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008016
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008017 if (i915->gt_pm.llc_pstate.enabled)
8018 return;
8019
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008020 gen6_update_ring_freq(i915);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008021
8022 i915->gt_pm.llc_pstate.enabled = true;
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008023}
8024
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008025static void intel_enable_rps(struct drm_i915_private *dev_priv)
8026{
8027 struct intel_rps *rps = &dev_priv->gt_pm.rps;
8028
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008029 lockdep_assert_held(&rps->lock);
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008030
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008031 if (rps->enabled)
8032 return;
8033
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008034 if (IS_CHERRYVIEW(dev_priv)) {
8035 cherryview_enable_rps(dev_priv);
8036 } else if (IS_VALLEYVIEW(dev_priv)) {
8037 valleyview_enable_rps(dev_priv);
8038 } else if (INTEL_GEN(dev_priv) >= 9) {
8039 gen9_enable_rps(dev_priv);
8040 } else if (IS_BROADWELL(dev_priv)) {
8041 gen8_enable_rps(dev_priv);
8042 } else if (INTEL_GEN(dev_priv) >= 6) {
8043 gen6_enable_rps(dev_priv);
8044 } else if (IS_IRONLAKE_M(dev_priv)) {
8045 ironlake_enable_drps(dev_priv);
8046 intel_init_emon(dev_priv);
8047 }
8048
8049 WARN_ON(rps->max_freq < rps->min_freq);
8050 WARN_ON(rps->idle_freq > rps->max_freq);
8051
8052 WARN_ON(rps->efficient_freq < rps->min_freq);
8053 WARN_ON(rps->efficient_freq > rps->max_freq);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008054
8055 rps->enabled = true;
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008056}
8057
Chris Wilsonb7137e02016-07-13 09:10:37 +01008058void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
8059{
Chris Wilsonb7137e02016-07-13 09:10:37 +01008060 /* Powersaving is controlled by the host when inside a VM */
8061 if (intel_vgpu_active(dev_priv))
8062 return;
8063
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008064 mutex_lock(&dev_priv->gt_pm.rps.lock);
Imre Deak3cc134e2014-11-19 15:30:03 +02008065
Chris Wilson91cbdb82019-04-19 14:48:36 +01008066 if (HAS_RPS(dev_priv))
8067 intel_enable_rps(dev_priv);
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008068 if (HAS_LLC(dev_priv))
8069 intel_enable_llc_pstate(dev_priv);
8070
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008071 mutex_unlock(&dev_priv->gt_pm.rps.lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01008072}
Imre Deakc6df39b2014-04-14 20:24:29 +03008073
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008074static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01008075{
Daniel Vetter3107bd42012-10-31 22:52:31 +01008076 /*
8077 * On Ibex Peak and Cougar Point, we need to disable clock
8078 * gating for the panel power sequencer or it will fail to
8079 * start up when no ports are active.
8080 */
8081 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
8082}
8083
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008084static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008085{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03008086 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008087
Damien Lespiau055e3932014-08-18 13:49:10 +01008088 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008089 I915_WRITE(DSPCNTR(pipe),
8090 I915_READ(DSPCNTR(pipe)) |
8091 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03008092
8093 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
8094 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008095 }
8096}
8097
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008098static void ilk_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008099{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008100 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008101
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01008102 /*
8103 * Required for FBC
8104 * WaFbcDisableDpfcClockGating:ilk
8105 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008106 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
8107 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
8108 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008109
8110 I915_WRITE(PCH_3DCGDIS0,
8111 MARIUNIT_CLOCK_GATE_DISABLE |
8112 SVSMUNIT_CLOCK_GATE_DISABLE);
8113 I915_WRITE(PCH_3DCGDIS1,
8114 VFMUNIT_CLOCK_GATE_DISABLE);
8115
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008116 /*
8117 * According to the spec the following bits should be set in
8118 * order to enable memory self-refresh
8119 * The bit 22/21 of 0x42004
8120 * The bit 5 of 0x42020
8121 * The bit 15 of 0x45000
8122 */
8123 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8124 (I915_READ(ILK_DISPLAY_CHICKEN2) |
8125 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008126 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008127 I915_WRITE(DISP_ARB_CTL,
8128 (I915_READ(DISP_ARB_CTL) |
8129 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02008130
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008131 /*
8132 * Based on the document from hardware guys the following bits
8133 * should be set unconditionally in order to enable FBC.
8134 * The bit 22 of 0x42000
8135 * The bit 22 of 0x42004
8136 * The bit 7,8,9 of 0x42020.
8137 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008138 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01008139 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008140 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8141 I915_READ(ILK_DISPLAY_CHICKEN1) |
8142 ILK_FBCQ_DIS);
8143 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8144 I915_READ(ILK_DISPLAY_CHICKEN2) |
8145 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008146 }
8147
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008148 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
8149
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008150 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8151 I915_READ(ILK_DISPLAY_CHICKEN2) |
8152 ILK_ELPIN_409_SELECT);
8153 I915_WRITE(_3D_CHICKEN2,
8154 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
8155 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02008156
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008157 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02008158 I915_WRITE(CACHE_MODE_0,
8159 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01008160
Akash Goel4e046322014-04-04 17:14:38 +05308161 /* WaDisable_RenderCache_OperationalFlush:ilk */
8162 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8163
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008164 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03008165
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008166 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008167}
8168
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008169static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01008170{
Ville Syrjäläd048a262019-08-21 20:30:31 +03008171 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008172 u32 val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01008173
8174 /*
8175 * On Ibex Peak and Cougar Point, we need to disable clock
8176 * gating for the panel power sequencer or it will fail to
8177 * start up when no ports are active.
8178 */
Jesse Barnescd664072013-10-02 10:34:19 -07008179 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
8180 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
8181 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008182 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
8183 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01008184 /* The below fixes the weird display corruption, a few pixels shifted
8185 * downward, on (only) LVDS of some HP laptops with IVY.
8186 */
Damien Lespiau055e3932014-08-18 13:49:10 +01008187 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03008188 val = I915_READ(TRANS_CHICKEN2(pipe));
8189 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
8190 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008191 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008192 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03008193 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
8194 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
8195 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008196 I915_WRITE(TRANS_CHICKEN2(pipe), val);
8197 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01008198 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01008199 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01008200 I915_WRITE(TRANS_CHICKEN1(pipe),
8201 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
8202 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008203}
8204
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008205static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008206{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008207 u32 tmp;
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008208
8209 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02008210 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
8211 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
8212 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008213}
8214
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008215static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008216{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008217 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008218
Damien Lespiau231e54f2012-10-19 17:55:41 +01008219 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008220
8221 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8222 I915_READ(ILK_DISPLAY_CHICKEN2) |
8223 ILK_ELPIN_409_SELECT);
8224
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008225 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01008226 I915_WRITE(_3D_CHICKEN,
8227 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
8228
Akash Goel4e046322014-04-04 17:14:38 +05308229 /* WaDisable_RenderCache_OperationalFlush:snb */
8230 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8231
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008232 /*
8233 * BSpec recoomends 8x4 when MSAA is used,
8234 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008235 *
8236 * Note that PS/WM thread counts depend on the WIZ hashing
8237 * disable bit, which we don't touch here, but it's good
8238 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008239 */
8240 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008241 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008242
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008243 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02008244 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008245
8246 I915_WRITE(GEN6_UCGCTL1,
8247 I915_READ(GEN6_UCGCTL1) |
8248 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
8249 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
8250
8251 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
8252 * gating disable must be set. Failure to set it results in
8253 * flickering pixels due to Z write ordering failures after
8254 * some amount of runtime in the Mesa "fire" demo, and Unigine
8255 * Sanctuary and Tropics, and apparently anything else with
8256 * alpha test or pixel discard.
8257 *
8258 * According to the spec, bit 11 (RCCUNIT) must also be set,
8259 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008260 *
Ville Syrjäläef593182014-01-22 21:32:47 +02008261 * WaDisableRCCUnitClockGating:snb
8262 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008263 */
8264 I915_WRITE(GEN6_UCGCTL2,
8265 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
8266 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
8267
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02008268 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02008269 I915_WRITE(_3D_CHICKEN3,
8270 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008271
8272 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02008273 * Bspec says:
8274 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
8275 * 3DSTATE_SF number of SF output attributes is more than 16."
8276 */
8277 I915_WRITE(_3D_CHICKEN3,
8278 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
8279
8280 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008281 * According to the spec the following bits should be
8282 * set in order to enable memory self-refresh and fbc:
8283 * The bit21 and bit22 of 0x42000
8284 * The bit21 and bit22 of 0x42004
8285 * The bit5 and bit7 of 0x42020
8286 * The bit14 of 0x70180
8287 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01008288 *
8289 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008290 */
8291 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8292 I915_READ(ILK_DISPLAY_CHICKEN1) |
8293 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
8294 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8295 I915_READ(ILK_DISPLAY_CHICKEN2) |
8296 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01008297 I915_WRITE(ILK_DSPCLK_GATE_D,
8298 I915_READ(ILK_DSPCLK_GATE_D) |
8299 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
8300 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008301
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008302 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07008303
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008304 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008305
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008306 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008307}
8308
8309static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
8310{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008311 u32 reg = I915_READ(GEN7_FF_THREAD_MODE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008312
Ville Syrjälä3aad9052014-01-22 21:32:59 +02008313 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02008314 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02008315 *
8316 * This actually overrides the dispatch
8317 * mode for all thread types.
8318 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008319 reg &= ~GEN7_FF_SCHED_MASK;
8320 reg |= GEN7_FF_TS_SCHED_HW;
8321 reg |= GEN7_FF_VS_SCHED_HW;
8322 reg |= GEN7_FF_DS_SCHED_HW;
8323
8324 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
8325}
8326
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008327static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008328{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008329 /*
8330 * TODO: this bit should only be enabled when really needed, then
8331 * disabled when not needed anymore in order to save power.
8332 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008333 if (HAS_PCH_LPT_LP(dev_priv))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008334 I915_WRITE(SOUTH_DSPCLK_GATE_D,
8335 I915_READ(SOUTH_DSPCLK_GATE_D) |
8336 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03008337
8338 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03008339 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
8340 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03008341 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008342}
8343
Ville Syrjälä712bf362016-10-31 22:37:23 +02008344static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03008345{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008346 if (HAS_PCH_LPT_LP(dev_priv)) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008347 u32 val = I915_READ(SOUTH_DSPCLK_GATE_D);
Imre Deak7d708ee2013-04-17 14:04:50 +03008348
8349 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8350 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8351 }
8352}
8353
Imre Deak450174f2016-05-03 15:54:21 +03008354static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
8355 int general_prio_credits,
8356 int high_prio_credits)
8357{
8358 u32 misccpctl;
Oscar Mateo930a7842017-10-17 13:25:45 -07008359 u32 val;
Imre Deak450174f2016-05-03 15:54:21 +03008360
8361 /* WaTempDisableDOPClkGating:bdw */
8362 misccpctl = I915_READ(GEN7_MISCCPCTL);
8363 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
8364
Oscar Mateo930a7842017-10-17 13:25:45 -07008365 val = I915_READ(GEN8_L3SQCREG1);
8366 val &= ~L3_PRIO_CREDITS_MASK;
8367 val |= L3_GENERAL_PRIO_CREDITS(general_prio_credits);
8368 val |= L3_HIGH_PRIO_CREDITS(high_prio_credits);
8369 I915_WRITE(GEN8_L3SQCREG1, val);
Imre Deak450174f2016-05-03 15:54:21 +03008370
8371 /*
8372 * Wait at least 100 clocks before re-enabling clock gating.
8373 * See the definition of L3SQCREG1 in BSpec.
8374 */
8375 POSTING_READ(GEN8_L3SQCREG1);
8376 udelay(1);
8377 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
8378}
8379
Oscar Mateod65dc3e2018-05-08 14:29:24 -07008380static void icl_init_clock_gating(struct drm_i915_private *dev_priv)
8381{
8382 /* This is not an Wa. Enable to reduce Sampler power */
8383 I915_WRITE(GEN10_DFR_RATIO_EN_AND_CHICKEN,
8384 I915_READ(GEN10_DFR_RATIO_EN_AND_CHICKEN) & ~DFR_DISABLE);
Radhakrishna Sripada622b3f62018-10-30 01:45:01 -07008385
8386 /* WaEnable32PlaneMode:icl */
8387 I915_WRITE(GEN9_CSFE_CHICKEN1_RCS,
8388 _MASKED_BIT_ENABLE(GEN11_ENABLE_32_PLANE_MODE));
Oscar Mateod65dc3e2018-05-08 14:29:24 -07008389}
8390
Michel Thierry5d869232019-08-23 01:20:34 -07008391static void tgl_init_clock_gating(struct drm_i915_private *dev_priv)
8392{
8393 u32 vd_pg_enable = 0;
8394 unsigned int i;
8395
8396 /* This is not a WA. Enable VD HCP & MFX_ENC powergate */
8397 for (i = 0; i < I915_MAX_VCS; i++) {
8398 if (HAS_ENGINE(dev_priv, _VCS(i)))
8399 vd_pg_enable |= VDN_HCP_POWERGATE_ENABLE(i) |
8400 VDN_MFX_POWERGATE_ENABLE(i);
8401 }
8402
8403 I915_WRITE(POWERGATE_ENABLE,
8404 I915_READ(POWERGATE_ENABLE) | vd_pg_enable);
8405}
8406
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008407static void cnp_init_clock_gating(struct drm_i915_private *dev_priv)
8408{
8409 if (!HAS_PCH_CNP(dev_priv))
8410 return;
8411
Rodrigo Vivi470e7c62018-03-05 17:28:12 -08008412 /* Display WA #1181 WaSouthDisplayDisablePWMCGEGating: cnp */
Rodrigo Vivi4cc6feb2017-09-08 16:45:33 -07008413 I915_WRITE(SOUTH_DSPCLK_GATE_D, I915_READ(SOUTH_DSPCLK_GATE_D) |
8414 CNP_PWM_CGE_GATING_DISABLE);
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008415}
8416
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008417static void cnl_init_clock_gating(struct drm_i915_private *dev_priv)
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008418{
Rodrigo Vivi8f067832017-09-05 12:30:13 -07008419 u32 val;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008420 cnp_init_clock_gating(dev_priv);
8421
Rodrigo Vivi1a25db62017-08-15 16:16:51 -07008422 /* This is not an Wa. Enable for better image quality */
8423 I915_WRITE(_3D_CHICKEN3,
8424 _MASKED_BIT_ENABLE(_3D_CHICKEN3_AA_LINE_QUALITY_FIX_ENABLE));
8425
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008426 /* WaEnableChickenDCPR:cnl */
8427 I915_WRITE(GEN8_CHICKEN_DCPR_1,
8428 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
8429
8430 /* WaFbcWakeMemOn:cnl */
8431 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
8432 DISP_FBC_MEMORY_WAKE);
8433
Chris Wilson34991bd2017-11-11 10:03:36 +00008434 val = I915_READ(SLICE_UNIT_LEVEL_CLKGATE);
8435 /* ReadHitWriteOnlyDisable:cnl */
8436 val |= RCCUNIT_CLKGATE_DIS;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008437 /* WaSarbUnitClockGatingDisable:cnl (pre-prod) */
8438 if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_B0))
Chris Wilson34991bd2017-11-11 10:03:36 +00008439 val |= SARBUNIT_CLKGATE_DIS;
8440 I915_WRITE(SLICE_UNIT_LEVEL_CLKGATE, val);
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08008441
Rodrigo Vivia4713c52018-03-07 14:09:12 -08008442 /* Wa_2201832410:cnl */
8443 val = I915_READ(SUBSLICE_UNIT_LEVEL_CLKGATE);
8444 val |= GWUNIT_CLKGATE_DIS;
8445 I915_WRITE(SUBSLICE_UNIT_LEVEL_CLKGATE, val);
8446
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08008447 /* WaDisableVFclkgate:cnl */
Rodrigo Vivi14941b62018-03-05 17:20:00 -08008448 /* WaVFUnitClockGatingDisable:cnl */
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08008449 val = I915_READ(UNSLICE_UNIT_LEVEL_CLKGATE);
8450 val |= VFUNIT_CLKGATE_DIS;
8451 I915_WRITE(UNSLICE_UNIT_LEVEL_CLKGATE, val);
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008452}
8453
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008454static void cfl_init_clock_gating(struct drm_i915_private *dev_priv)
8455{
8456 cnp_init_clock_gating(dev_priv);
8457 gen9_init_clock_gating(dev_priv);
8458
8459 /* WaFbcNukeOnHostModify:cfl */
8460 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8461 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
8462}
8463
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008464static void kbl_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008465{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008466 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008467
8468 /* WaDisableSDEUnitClockGating:kbl */
8469 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
8470 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8471 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03008472
8473 /* WaDisableGamClockGating:kbl */
8474 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
8475 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
8476 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008477
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008478 /* WaFbcNukeOnHostModify:kbl */
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008479 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8480 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008481}
8482
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008483static void skl_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008484{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008485 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03008486
8487 /* WAC6entrylatency:skl */
8488 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
8489 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008490
8491 /* WaFbcNukeOnHostModify:skl */
8492 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8493 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008494}
8495
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008496static void bdw_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008497{
Damien Lespiau07d27e22014-03-03 17:31:46 +00008498 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008499
Ben Widawskyab57fff2013-12-12 15:28:04 -08008500 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07008501 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008502
Ben Widawskyab57fff2013-12-12 15:28:04 -08008503 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008504 I915_WRITE(CHICKEN_PAR1_1,
8505 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
8506
Ben Widawskyab57fff2013-12-12 15:28:04 -08008507 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01008508 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00008509 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02008510 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02008511 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008512 }
Ben Widawsky63801f22013-12-12 17:26:03 -08008513
Ben Widawskyab57fff2013-12-12 15:28:04 -08008514 /* WaVSRefCountFullforceMissDisable:bdw */
8515 /* WaDSRefCountFullforceMissDisable:bdw */
8516 I915_WRITE(GEN7_FF_THREAD_MODE,
8517 I915_READ(GEN7_FF_THREAD_MODE) &
8518 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02008519
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02008520 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
8521 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02008522
8523 /* WaDisableSDEUnitClockGating:bdw */
8524 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8525 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00008526
Imre Deak450174f2016-05-03 15:54:21 +03008527 /* WaProgramL3SqcReg1Default:bdw */
8528 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03008529
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03008530 /* WaKVMNotificationOnConfigChange:bdw */
8531 I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
8532 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
8533
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008534 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00008535
8536 /* WaDisableDopClockGating:bdw
8537 *
8538 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
8539 * clock gating.
8540 */
8541 I915_WRITE(GEN6_UCGCTL1,
8542 I915_READ(GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008543}
8544
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008545static void hsw_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008546{
Francisco Jerezf3fc4882013-10-02 15:53:16 -07008547 /* L3 caching of data atomics doesn't work -- disable it. */
8548 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
8549 I915_WRITE(HSW_ROW_CHICKEN3,
8550 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
8551
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008552 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008553 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8554 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8555 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8556
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02008557 /* WaVSRefCountFullforceMissDisable:hsw */
8558 I915_WRITE(GEN7_FF_THREAD_MODE,
8559 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008560
Akash Goel4e046322014-04-04 17:14:38 +05308561 /* WaDisable_RenderCache_OperationalFlush:hsw */
8562 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8563
Chia-I Wufe27c602014-01-28 13:29:33 +08008564 /* enable HiZ Raw Stall Optimization */
8565 I915_WRITE(CACHE_MODE_0_GEN7,
8566 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
8567
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008568 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008569 I915_WRITE(CACHE_MODE_1,
8570 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03008571
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008572 /*
8573 * BSpec recommends 8x4 when MSAA is used,
8574 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008575 *
8576 * Note that PS/WM thread counts depend on the WIZ hashing
8577 * disable bit, which we don't touch here, but it's good
8578 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008579 */
8580 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008581 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008582
Kenneth Graunke94411592014-12-31 16:23:00 -08008583 /* WaSampleCChickenBitEnable:hsw */
8584 I915_WRITE(HALF_SLICE_CHICKEN3,
8585 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
8586
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008587 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07008588 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
8589
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008590 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008591}
8592
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008593static void ivb_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008594{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008595 u32 snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008596
Damien Lespiau231e54f2012-10-19 17:55:41 +01008597 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008598
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008599 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05008600 I915_WRITE(_3D_CHICKEN3,
8601 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
8602
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008603 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008604 I915_WRITE(IVB_CHICKEN3,
8605 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8606 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8607
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008608 /* WaDisablePSDDualDispatchEnable:ivb */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008609 if (IS_IVB_GT1(dev_priv))
Jesse Barnes12f33822012-10-25 12:15:45 -07008610 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
8611 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07008612
Akash Goel4e046322014-04-04 17:14:38 +05308613 /* WaDisable_RenderCache_OperationalFlush:ivb */
8614 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8615
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008616 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008617 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
8618 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
8619
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008620 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008621 I915_WRITE(GEN7_L3CNTLREG1,
8622 GEN7_WA_FOR_GEN7_L3_CONTROL);
8623 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07008624 GEN7_WA_L3_CHICKEN_MODE);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008625 if (IS_IVB_GT1(dev_priv))
Jesse Barnes8ab43972012-10-25 12:15:42 -07008626 I915_WRITE(GEN7_ROW_CHICKEN2,
8627 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02008628 else {
8629 /* must write both registers */
8630 I915_WRITE(GEN7_ROW_CHICKEN2,
8631 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07008632 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
8633 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02008634 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008635
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008636 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05008637 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
8638 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
8639
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02008640 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07008641 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008642 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008643 */
8644 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02008645 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07008646
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008647 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008648 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8649 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8650 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8651
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008652 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008653
8654 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02008655
Chris Wilson22721342014-03-04 09:41:43 +00008656 if (0) { /* causes HiZ corruption on ivb:gt1 */
8657 /* enable HiZ Raw Stall Optimization */
8658 I915_WRITE(CACHE_MODE_0_GEN7,
8659 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
8660 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08008661
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008662 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02008663 I915_WRITE(CACHE_MODE_1,
8664 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07008665
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008666 /*
8667 * BSpec recommends 8x4 when MSAA is used,
8668 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008669 *
8670 * Note that PS/WM thread counts depend on the WIZ hashing
8671 * disable bit, which we don't touch here, but it's good
8672 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008673 */
8674 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008675 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008676
Ben Widawsky20848222012-05-04 18:58:59 -07008677 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
8678 snpcr &= ~GEN6_MBC_SNPCR_MASK;
8679 snpcr |= GEN6_MBC_SNPCR_MED;
8680 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008681
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008682 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008683 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008684
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008685 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008686}
8687
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008688static void vlv_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008689{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008690 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05008691 I915_WRITE(_3D_CHICKEN3,
8692 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
8693
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008694 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008695 I915_WRITE(IVB_CHICKEN3,
8696 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8697 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8698
Ville Syrjäläfad7d362014-01-22 21:32:39 +02008699 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008700 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07008701 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08008702 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
8703 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07008704
Akash Goel4e046322014-04-04 17:14:38 +05308705 /* WaDisable_RenderCache_OperationalFlush:vlv */
8706 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8707
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008708 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05008709 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
8710 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
8711
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008712 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07008713 I915_WRITE(GEN7_ROW_CHICKEN2,
8714 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
8715
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008716 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008717 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8718 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8719 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8720
Ville Syrjälä46680e02014-01-22 21:33:01 +02008721 gen7_setup_fixed_func_scheduler(dev_priv);
8722
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02008723 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07008724 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008725 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008726 */
8727 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02008728 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07008729
Akash Goelc98f5062014-03-24 23:00:07 +05308730 /* WaDisableL3Bank2xClockGate:vlv
8731 * Disabling L3 clock gating- MMIO 940c[25] = 1
8732 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
8733 I915_WRITE(GEN7_UCGCTL4,
8734 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07008735
Ville Syrjäläafd58e72014-01-22 21:33:03 +02008736 /*
8737 * BSpec says this must be set, even though
8738 * WaDisable4x2SubspanOptimization isn't listed for VLV.
8739 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02008740 I915_WRITE(CACHE_MODE_1,
8741 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07008742
8743 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02008744 * BSpec recommends 8x4 when MSAA is used,
8745 * however in practice 16x4 seems fastest.
8746 *
8747 * Note that PS/WM thread counts depend on the WIZ hashing
8748 * disable bit, which we don't touch here, but it's good
8749 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
8750 */
8751 I915_WRITE(GEN7_GT_MODE,
8752 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
8753
8754 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02008755 * WaIncreaseL3CreditsForVLVB0:vlv
8756 * This is the hardware default actually.
8757 */
8758 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
8759
8760 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008761 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07008762 * Disable clock gating on th GCFG unit to prevent a delay
8763 * in the reporting of vblank events.
8764 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02008765 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008766}
8767
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008768static void chv_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03008769{
Ville Syrjälä232ce332014-04-09 13:28:35 +03008770 /* WaVSRefCountFullforceMissDisable:chv */
8771 /* WaDSRefCountFullforceMissDisable:chv */
8772 I915_WRITE(GEN7_FF_THREAD_MODE,
8773 I915_READ(GEN7_FF_THREAD_MODE) &
8774 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03008775
8776 /* WaDisableSemaphoreAndSyncFlipWait:chv */
8777 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
8778 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03008779
8780 /* WaDisableCSUnitClockGating:chv */
8781 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
8782 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03008783
8784 /* WaDisableSDEUnitClockGating:chv */
8785 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8786 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008787
8788 /*
Imre Deak450174f2016-05-03 15:54:21 +03008789 * WaProgramL3SqcReg1Default:chv
8790 * See gfxspecs/Related Documents/Performance Guide/
8791 * LSQC Setting Recommendations.
8792 */
8793 gen8_set_l3sqc_credits(dev_priv, 38, 2);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03008794}
8795
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008796static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008797{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008798 u32 dspclk_gate;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008799
8800 I915_WRITE(RENCLK_GATE_D1, 0);
8801 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
8802 GS_UNIT_CLOCK_GATE_DISABLE |
8803 CL_UNIT_CLOCK_GATE_DISABLE);
8804 I915_WRITE(RAMCLK_GATE_D, 0);
8805 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
8806 OVRUNIT_CLOCK_GATE_DISABLE |
8807 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008808 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008809 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
8810 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02008811
8812 /* WaDisableRenderCachePipelinedFlush */
8813 I915_WRITE(CACHE_MODE_0,
8814 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03008815
Akash Goel4e046322014-04-04 17:14:38 +05308816 /* WaDisable_RenderCache_OperationalFlush:g4x */
8817 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8818
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008819 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008820}
8821
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008822static void i965gm_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008823{
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01008824 struct intel_uncore *uncore = &dev_priv->uncore;
8825
8826 intel_uncore_write(uncore, RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
8827 intel_uncore_write(uncore, RENCLK_GATE_D2, 0);
8828 intel_uncore_write(uncore, DSPCLK_GATE_D, 0);
8829 intel_uncore_write(uncore, RAMCLK_GATE_D, 0);
8830 intel_uncore_write16(uncore, DEUC, 0);
8831 intel_uncore_write(uncore,
8832 MI_ARB_STATE,
8833 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05308834
8835 /* WaDisable_RenderCache_OperationalFlush:gen4 */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01008836 intel_uncore_write(uncore,
8837 CACHE_MODE_0,
8838 _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008839}
8840
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008841static void i965g_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008842{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008843 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
8844 I965_RCC_CLOCK_GATE_DISABLE |
8845 I965_RCPB_CLOCK_GATE_DISABLE |
8846 I965_ISC_CLOCK_GATE_DISABLE |
8847 I965_FBC_CLOCK_GATE_DISABLE);
8848 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03008849 I915_WRITE(MI_ARB_STATE,
8850 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05308851
8852 /* WaDisable_RenderCache_OperationalFlush:gen4 */
8853 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008854}
8855
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008856static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008857{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008858 u32 dstate = I915_READ(D_STATE);
8859
8860 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
8861 DSTATE_DOT_CLOCK_GATING;
8862 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01008863
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008864 if (IS_PINEVIEW(dev_priv))
Chris Wilson13a86b82012-04-24 14:51:43 +01008865 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02008866
8867 /* IIR "flip pending" means done if this bit is set */
8868 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02008869
8870 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02008871 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02008872
8873 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
8874 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03008875
8876 I915_WRITE(MI_ARB_STATE,
8877 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008878}
8879
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008880static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008881{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008882 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02008883
8884 /* interrupts should cause a wake up from C3 */
8885 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
8886 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03008887
8888 I915_WRITE(MEM_MODE,
8889 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008890}
8891
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008892static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008893{
Ville Syrjälä10383922014-08-15 01:21:54 +03008894 I915_WRITE(MEM_MODE,
8895 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
8896 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008897}
8898
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008899void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008900{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008901 dev_priv->display.init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008902}
8903
Ville Syrjälä712bf362016-10-31 22:37:23 +02008904void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03008905{
Ville Syrjälä712bf362016-10-31 22:37:23 +02008906 if (HAS_PCH_LPT(dev_priv))
8907 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03008908}
8909
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008910static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02008911{
8912 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
8913}
8914
8915/**
8916 * intel_init_clock_gating_hooks - setup the clock gating hooks
8917 * @dev_priv: device private
8918 *
8919 * Setup the hooks that configure which clocks of a given platform can be
8920 * gated and also apply various GT and display specific workarounds for these
8921 * platforms. Note that some GT specific workarounds are applied separately
8922 * when GPU contexts or batchbuffers start their execution.
8923 */
8924void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
8925{
Lucas De Marchi13e53c52019-08-17 02:38:42 -07008926 if (IS_GEN(dev_priv, 12))
Michel Thierry5d869232019-08-23 01:20:34 -07008927 dev_priv->display.init_clock_gating = tgl_init_clock_gating;
Lucas De Marchi13e53c52019-08-17 02:38:42 -07008928 else if (IS_GEN(dev_priv, 11))
Oscar Mateod65dc3e2018-05-08 14:29:24 -07008929 dev_priv->display.init_clock_gating = icl_init_clock_gating;
Oscar Mateocc38cae2018-05-08 14:29:23 -07008930 else if (IS_CANNONLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008931 dev_priv->display.init_clock_gating = cnl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008932 else if (IS_COFFEELAKE(dev_priv))
8933 dev_priv->display.init_clock_gating = cfl_init_clock_gating;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008934 else if (IS_SKYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008935 dev_priv->display.init_clock_gating = skl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008936 else if (IS_KABYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008937 dev_priv->display.init_clock_gating = kbl_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02008938 else if (IS_BROXTON(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02008939 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02008940 else if (IS_GEMINILAKE(dev_priv))
8941 dev_priv->display.init_clock_gating = glk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008942 else if (IS_BROADWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008943 dev_priv->display.init_clock_gating = bdw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008944 else if (IS_CHERRYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008945 dev_priv->display.init_clock_gating = chv_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008946 else if (IS_HASWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008947 dev_priv->display.init_clock_gating = hsw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008948 else if (IS_IVYBRIDGE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008949 dev_priv->display.init_clock_gating = ivb_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008950 else if (IS_VALLEYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008951 dev_priv->display.init_clock_gating = vlv_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008952 else if (IS_GEN(dev_priv, 6))
Imre Deakbb400da2016-03-16 13:38:54 +02008953 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008954 else if (IS_GEN(dev_priv, 5))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008955 dev_priv->display.init_clock_gating = ilk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008956 else if (IS_G4X(dev_priv))
8957 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02008958 else if (IS_I965GM(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008959 dev_priv->display.init_clock_gating = i965gm_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02008960 else if (IS_I965G(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008961 dev_priv->display.init_clock_gating = i965g_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008962 else if (IS_GEN(dev_priv, 3))
Imre Deakbb400da2016-03-16 13:38:54 +02008963 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
8964 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
8965 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008966 else if (IS_GEN(dev_priv, 2))
Imre Deakbb400da2016-03-16 13:38:54 +02008967 dev_priv->display.init_clock_gating = i830_init_clock_gating;
8968 else {
8969 MISSING_CASE(INTEL_DEVID(dev_priv));
8970 dev_priv->display.init_clock_gating = nop_init_clock_gating;
8971 }
8972}
8973
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008974/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008975void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008976{
Daniel Vetterc921aba2012-04-26 23:28:17 +02008977 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008978 if (IS_PINEVIEW(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02008979 i915_pineview_get_mem_freq(dev_priv);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008980 else if (IS_GEN(dev_priv, 5))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02008981 i915_ironlake_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02008982
James Ausmusb068a862019-10-09 10:23:14 -07008983 if (intel_has_sagv(dev_priv))
8984 skl_setup_sagv_block_time(dev_priv);
8985
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008986 /* For FIFO watermark updates */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008987 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008988 skl_setup_wm_latency(dev_priv);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01008989 dev_priv->display.initial_watermarks = skl_initial_wm;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01008990 dev_priv->display.atomic_update_watermarks = skl_atomic_update_crtc_wm;
Matt Roper98d39492016-05-12 07:06:03 -07008991 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008992 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008993 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03008994
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008995 if ((IS_GEN(dev_priv, 5) && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008996 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008997 (!IS_GEN(dev_priv, 5) && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008998 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07008999 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08009000 dev_priv->display.compute_intermediate_wm =
9001 ilk_compute_intermediate_wm;
9002 dev_priv->display.initial_watermarks =
9003 ilk_initial_watermarks;
9004 dev_priv->display.optimize_watermarks =
9005 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02009006 } else {
9007 DRM_DEBUG_KMS("Failed to read display plane latency. "
9008 "Disable CxSR\n");
9009 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02009010 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02009011 vlv_setup_wm_latency(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02009012 dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
Ville Syrjälä4841da52017-03-02 19:14:59 +02009013 dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02009014 dev_priv->display.initial_watermarks = vlv_initial_watermarks;
Ville Syrjälä4841da52017-03-02 19:14:59 +02009015 dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
Ville Syrjäläff32c542017-03-02 19:14:57 +02009016 dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03009017 } else if (IS_G4X(dev_priv)) {
9018 g4x_setup_wm_latency(dev_priv);
9019 dev_priv->display.compute_pipe_wm = g4x_compute_pipe_wm;
9020 dev_priv->display.compute_intermediate_wm = g4x_compute_intermediate_wm;
9021 dev_priv->display.initial_watermarks = g4x_initial_watermarks;
9022 dev_priv->display.optimize_watermarks = g4x_optimize_watermarks;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02009023 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +00009024 if (!intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009025 dev_priv->is_ddr3,
9026 dev_priv->fsb_freq,
9027 dev_priv->mem_freq)) {
9028 DRM_INFO("failed to find known CxSR latency "
9029 "(found ddr%s fsb freq %d, mem freq %d), "
9030 "disabling CxSR\n",
9031 (dev_priv->is_ddr3 == 1) ? "3" : "2",
9032 dev_priv->fsb_freq, dev_priv->mem_freq);
9033 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03009034 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009035 dev_priv->display.update_wm = NULL;
9036 } else
9037 dev_priv->display.update_wm = pineview_update_wm;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009038 } else if (IS_GEN(dev_priv, 4)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009039 dev_priv->display.update_wm = i965_update_wm;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009040 } else if (IS_GEN(dev_priv, 3)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009041 dev_priv->display.update_wm = i9xx_update_wm;
9042 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009043 } else if (IS_GEN(dev_priv, 2)) {
Jani Nikula24977872019-09-11 12:26:08 +03009044 if (INTEL_NUM_PIPES(dev_priv) == 1) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009045 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009046 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009047 } else {
9048 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009049 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009050 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009051 } else {
9052 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009053 }
9054}
9055
Ville Syrjälädd06f882014-11-10 22:55:12 +02009056static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
9057{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009058 struct intel_rps *rps = &dev_priv->gt_pm.rps;
9059
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009060 /*
9061 * N = val - 0xb7
9062 * Slow = Fast = GPLL ref * N
9063 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009064 return DIV_ROUND_CLOSEST(rps->gpll_ref_freq * (val - 0xb7), 1000);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009065}
9066
Fengguang Wub55dd642014-07-12 11:21:39 +02009067static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009068{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009069 struct intel_rps *rps = &dev_priv->gt_pm.rps;
9070
9071 return DIV_ROUND_CLOSEST(1000 * val, rps->gpll_ref_freq) + 0xb7;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009072}
9073
Fengguang Wub55dd642014-07-12 11:21:39 +02009074static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05309075{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009076 struct intel_rps *rps = &dev_priv->gt_pm.rps;
9077
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009078 /*
9079 * N = val / 2
9080 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
9081 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009082 return DIV_ROUND_CLOSEST(rps->gpll_ref_freq * val, 2 * 2 * 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05309083}
9084
Fengguang Wub55dd642014-07-12 11:21:39 +02009085static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05309086{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009087 struct intel_rps *rps = &dev_priv->gt_pm.rps;
9088
Ville Syrjälä1c147622014-08-18 14:42:43 +03009089 /* CHV needs even values */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009090 return DIV_ROUND_CLOSEST(2 * 1000 * val, rps->gpll_ref_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05309091}
9092
Ville Syrjälä616bc822015-01-23 21:04:25 +02009093int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
9094{
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07009095 if (INTEL_GEN(dev_priv) >= 9)
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009096 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
9097 GEN9_FREQ_SCALER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009098 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009099 return chv_gpu_freq(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009100 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009101 return byt_gpu_freq(dev_priv, val);
9102 else
9103 return val * GT_FREQUENCY_MULTIPLIER;
9104}
9105
Ville Syrjälä616bc822015-01-23 21:04:25 +02009106int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
9107{
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07009108 if (INTEL_GEN(dev_priv) >= 9)
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009109 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
9110 GT_FREQUENCY_MULTIPLIER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009111 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009112 return chv_freq_opcode(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009113 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009114 return byt_freq_opcode(dev_priv, val);
9115 else
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009116 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
Deepak S22b1b2f2014-07-12 14:54:33 +05309117}
9118
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00009119void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01009120{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01009121 mutex_init(&dev_priv->gt_pm.rps.lock);
Chris Wilson60548c52018-07-31 14:26:29 +01009122 mutex_init(&dev_priv->gt_pm.rps.power.mutex);
Daniel Vetterf742a552013-12-06 10:17:53 +01009123
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009124 atomic_set(&dev_priv->gt_pm.rps.num_waiters, 0);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03009125
Sagar Arun Kamblead1443f2017-10-10 22:30:04 +01009126 dev_priv->runtime_pm.suspended = false;
9127 atomic_set(&dev_priv->runtime_pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01009128}
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009129
Tvrtko Ursulinc84b2702017-11-21 18:18:44 +00009130u32 intel_get_cagf(struct drm_i915_private *dev_priv, u32 rpstat)
9131{
9132 u32 cagf;
9133
9134 if (INTEL_GEN(dev_priv) >= 9)
9135 cagf = (rpstat & GEN9_CAGF_MASK) >> GEN9_CAGF_SHIFT;
9136 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
9137 cagf = (rpstat & HSW_CAGF_MASK) >> HSW_CAGF_SHIFT;
9138 else
9139 cagf = (rpstat & GEN6_CAGF_MASK) >> GEN6_CAGF_SHIFT;
9140
9141 return cagf;
9142}