blob: f00e64f9320cc9a646948dbbf7eaf527e82385ae [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{
3644 if (IS_GEN(dev_priv, 11)) {
3645 dev_priv->sagv_block_time_us = 10;
3646 return;
3647 } else if (IS_GEN(dev_priv, 10)) {
3648 dev_priv->sagv_block_time_us = 20;
3649 return;
3650 } else if (IS_GEN(dev_priv, 9)) {
3651 dev_priv->sagv_block_time_us = 30;
3652 return;
3653 } else {
3654 MISSING_CASE(INTEL_GEN(dev_priv));
3655 }
3656
3657 /* Default to an unusable block time */
3658 dev_priv->sagv_block_time_us = -1;
3659}
3660
Lyude656d1b82016-08-17 15:55:54 -04003661/*
3662 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3663 * depending on power and performance requirements. The display engine access
3664 * to system memory is blocked during the adjustment time. Because of the
3665 * blocking time, having this enabled can cause full system hangs and/or pipe
3666 * underruns if we don't meet all of the following requirements:
3667 *
3668 * - <= 1 pipe enabled
3669 * - All planes can enable watermarks for latencies >= SAGV engine block time
3670 * - We're not using an interlaced display configuration
3671 */
3672int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003673intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003674{
3675 int ret;
3676
Paulo Zanoni56feca92016-09-22 18:00:28 -03003677 if (!intel_has_sagv(dev_priv))
3678 return 0;
3679
3680 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003681 return 0;
3682
Ville Syrjäläff61a972018-12-21 19:14:34 +02003683 DRM_DEBUG_KMS("Enabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003684 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3685 GEN9_SAGV_ENABLE);
3686
Ville Syrjäläff61a972018-12-21 19:14:34 +02003687 /* We don't need to wait for SAGV when enabling */
Lyude656d1b82016-08-17 15:55:54 -04003688
3689 /*
3690 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003691 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003692 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003693 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003694 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003695 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003696 return 0;
3697 } else if (ret < 0) {
Ville Syrjäläff61a972018-12-21 19:14:34 +02003698 DRM_ERROR("Failed to enable SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003699 return ret;
3700 }
3701
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003702 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003703 return 0;
3704}
3705
Lyude656d1b82016-08-17 15:55:54 -04003706int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003707intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003708{
Imre Deakb3b8e992016-12-05 18:27:38 +02003709 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003710
Paulo Zanoni56feca92016-09-22 18:00:28 -03003711 if (!intel_has_sagv(dev_priv))
3712 return 0;
3713
3714 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003715 return 0;
3716
Ville Syrjäläff61a972018-12-21 19:14:34 +02003717 DRM_DEBUG_KMS("Disabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003718 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003719 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3720 GEN9_SAGV_DISABLE,
3721 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3722 1);
Lyude656d1b82016-08-17 15:55:54 -04003723 /*
3724 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003725 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003726 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003727 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003728 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003729 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003730 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003731 } else if (ret < 0) {
Ville Syrjäläff61a972018-12-21 19:14:34 +02003732 DRM_ERROR("Failed to disable SAGV (%d)\n", ret);
Imre Deakb3b8e992016-12-05 18:27:38 +02003733 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003734 }
3735
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003736 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003737 return 0;
3738}
3739
Maarten Lankhorst855e0d62019-06-28 10:55:13 +02003740bool intel_can_enable_sagv(struct intel_atomic_state *state)
Lyude656d1b82016-08-17 15:55:54 -04003741{
Maarten Lankhorst855e0d62019-06-28 10:55:13 +02003742 struct drm_device *dev = state->base.dev;
Lyude656d1b82016-08-17 15:55:54 -04003743 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003744 struct intel_crtc *crtc;
3745 struct intel_plane *plane;
Maarten Lankhorstec193642019-06-28 10:55:17 +02003746 struct intel_crtc_state *crtc_state;
Lyude656d1b82016-08-17 15:55:54 -04003747 enum pipe pipe;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003748 int level, latency;
Lyude656d1b82016-08-17 15:55:54 -04003749
Paulo Zanoni56feca92016-09-22 18:00:28 -03003750 if (!intel_has_sagv(dev_priv))
3751 return false;
3752
Lyude656d1b82016-08-17 15:55:54 -04003753 /*
Lyude656d1b82016-08-17 15:55:54 -04003754 * If there are no active CRTCs, no additional checks need be performed
3755 */
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003756 if (hweight8(state->active_pipes) == 0)
Lyude656d1b82016-08-17 15:55:54 -04003757 return true;
Lucas De Marchida172232019-04-04 16:04:26 -07003758
3759 /*
3760 * SKL+ workaround: bspec recommends we disable SAGV when we have
3761 * more then one pipe enabled
3762 */
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003763 if (hweight8(state->active_pipes) > 1)
Lyude656d1b82016-08-17 15:55:54 -04003764 return false;
3765
3766 /* Since we're now guaranteed to only have one active CRTC... */
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03003767 pipe = ffs(state->active_pipes) - 1;
Ville Syrjälä98187832016-10-31 22:37:10 +02003768 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Maarten Lankhorstec193642019-06-28 10:55:17 +02003769 crtc_state = to_intel_crtc_state(crtc->base.state);
Lyude656d1b82016-08-17 15:55:54 -04003770
Paulo Zanonic89cadd2016-10-10 17:30:59 -03003771 if (crtc->base.state->adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003772 return false;
3773
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003774 for_each_intel_plane_on_crtc(dev, crtc, plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003775 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02003776 &crtc_state->wm.skl.optimal.planes[plane->id];
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003777
Lyude656d1b82016-08-17 15:55:54 -04003778 /* Skip this plane if it's not enabled */
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003779 if (!wm->wm[0].plane_en)
Lyude656d1b82016-08-17 15:55:54 -04003780 continue;
3781
3782 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003783 for (level = ilk_wm_max_level(dev_priv);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003784 !wm->wm[level].plane_en; --level)
Lyude656d1b82016-08-17 15:55:54 -04003785 { }
3786
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003787 latency = dev_priv->wm.skl_latency[level];
3788
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003789 if (skl_needs_memory_bw_wa(dev_priv) &&
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003790 plane->base.state->fb->modifier ==
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003791 I915_FORMAT_MOD_X_TILED)
3792 latency += 15;
3793
Lyude656d1b82016-08-17 15:55:54 -04003794 /*
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003795 * If any of the planes on this pipe don't enable wm levels that
3796 * incur memory latencies higher than sagv_block_time_us we
Ville Syrjäläff61a972018-12-21 19:14:34 +02003797 * can't enable SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003798 */
James Ausmusb068a862019-10-09 10:23:14 -07003799 if (latency < dev_priv->sagv_block_time_us)
Lyude656d1b82016-08-17 15:55:54 -04003800 return false;
3801 }
3802
3803 return true;
3804}
3805
Mahesh Kumaraaa02372018-07-31 19:54:44 +05303806static u16 intel_get_ddb_size(struct drm_i915_private *dev_priv,
Maarten Lankhorstec193642019-06-28 10:55:17 +02003807 const struct intel_crtc_state *crtc_state,
Maarten Lankhorst24719e92018-10-22 12:20:00 +02003808 const u64 total_data_rate,
Mahesh Kumaraaa02372018-07-31 19:54:44 +05303809 const int num_active,
3810 struct skl_ddb_allocation *ddb)
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303811{
3812 const struct drm_display_mode *adjusted_mode;
3813 u64 total_data_bw;
3814 u16 ddb_size = INTEL_INFO(dev_priv)->ddb_size;
3815
3816 WARN_ON(ddb_size == 0);
3817
3818 if (INTEL_GEN(dev_priv) < 11)
3819 return ddb_size - 4; /* 4 blocks for bypass path allocation */
3820
Maarten Lankhorstec193642019-06-28 10:55:17 +02003821 adjusted_mode = &crtc_state->base.adjusted_mode;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02003822 total_data_bw = total_data_rate * drm_mode_vrefresh(adjusted_mode);
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303823
3824 /*
3825 * 12GB/s is maximum BW supported by single DBuf slice.
Ville Syrjäläad3e7b82019-01-30 17:51:10 +02003826 *
3827 * FIXME dbuf slice code is broken:
3828 * - must wait for planes to stop using the slice before powering it off
3829 * - plane straddling both slices is illegal in multi-pipe scenarios
3830 * - should validate we stay within the hw bandwidth limits
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303831 */
Ville Syrjäläad3e7b82019-01-30 17:51:10 +02003832 if (0 && (num_active > 1 || total_data_bw >= GBps(12))) {
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303833 ddb->enabled_slices = 2;
3834 } else {
3835 ddb->enabled_slices = 1;
3836 ddb_size /= 2;
3837 }
3838
3839 return ddb_size;
3840}
3841
Damien Lespiaub9cec072014-11-04 17:06:43 +00003842static void
Maarten Lankhorstb048a002018-10-18 13:51:30 +02003843skl_ddb_get_pipe_allocation_limits(struct drm_i915_private *dev_priv,
Maarten Lankhorstec193642019-06-28 10:55:17 +02003844 const struct intel_crtc_state *crtc_state,
Maarten Lankhorst24719e92018-10-22 12:20:00 +02003845 const u64 total_data_rate,
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303846 struct skl_ddb_allocation *ddb,
Matt Roperc107acf2016-05-12 07:06:01 -07003847 struct skl_ddb_entry *alloc, /* out */
3848 int *num_active /* out */)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003849{
Maarten Lankhorstec193642019-06-28 10:55:17 +02003850 struct drm_atomic_state *state = crtc_state->base.state;
Matt Roperc107acf2016-05-12 07:06:01 -07003851 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorstec193642019-06-28 10:55:17 +02003852 struct drm_crtc *for_crtc = crtc_state->base.crtc;
3853 const struct intel_crtc *crtc;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303854 u32 pipe_width = 0, total_width = 0, width_before_pipe = 0;
3855 enum pipe for_pipe = to_intel_crtc(for_crtc)->pipe;
3856 u16 ddb_size;
3857 u32 i;
Matt Roperc107acf2016-05-12 07:06:01 -07003858
Maarten Lankhorstec193642019-06-28 10:55:17 +02003859 if (WARN_ON(!state) || !crtc_state->base.active) {
Damien Lespiaub9cec072014-11-04 17:06:43 +00003860 alloc->start = 0;
3861 alloc->end = 0;
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003862 *num_active = hweight8(dev_priv->active_pipes);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003863 return;
3864 }
3865
Matt Ropera6d3460e2016-05-12 07:06:04 -07003866 if (intel_state->active_pipe_changes)
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003867 *num_active = hweight8(intel_state->active_pipes);
Matt Ropera6d3460e2016-05-12 07:06:04 -07003868 else
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003869 *num_active = hweight8(dev_priv->active_pipes);
Matt Ropera6d3460e2016-05-12 07:06:04 -07003870
Maarten Lankhorstec193642019-06-28 10:55:17 +02003871 ddb_size = intel_get_ddb_size(dev_priv, crtc_state, total_data_rate,
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303872 *num_active, ddb);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003873
Matt Roperc107acf2016-05-12 07:06:01 -07003874 /*
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303875 * If the state doesn't change the active CRTC's or there is no
3876 * modeset request, then there's no need to recalculate;
3877 * the existing pipe allocation limits should remain unchanged.
3878 * Note that we're safe from racing commits since any racing commit
3879 * that changes the active CRTC list or do modeset would need to
3880 * grab _all_ crtc locks, including the one we currently hold.
Matt Roperc107acf2016-05-12 07:06:01 -07003881 */
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303882 if (!intel_state->active_pipe_changes && !intel_state->modeset) {
Maarten Lankhorst512b5522016-11-08 13:55:34 +01003883 /*
3884 * alloc may be cleared by clear_intel_crtc_state,
3885 * copy from old state to be sure
3886 */
3887 *alloc = to_intel_crtc_state(for_crtc->state)->wm.skl.ddb;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003888 return;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003889 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07003890
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303891 /*
3892 * Watermark/ddb requirement highly depends upon width of the
3893 * framebuffer, So instead of allocating DDB equally among pipes
3894 * distribute DDB based on resolution/width of the display.
3895 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02003896 for_each_new_intel_crtc_in_state(intel_state, crtc, crtc_state, i) {
3897 const struct drm_display_mode *adjusted_mode =
3898 &crtc_state->base.adjusted_mode;
3899 enum pipe pipe = crtc->pipe;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303900 int hdisplay, vdisplay;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303901
Maarten Lankhorstec193642019-06-28 10:55:17 +02003902 if (!crtc_state->base.enable)
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303903 continue;
3904
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303905 drm_mode_get_hv_timing(adjusted_mode, &hdisplay, &vdisplay);
3906 total_width += hdisplay;
3907
3908 if (pipe < for_pipe)
3909 width_before_pipe += hdisplay;
3910 else if (pipe == for_pipe)
3911 pipe_width = hdisplay;
3912 }
3913
3914 alloc->start = ddb_size * width_before_pipe / total_width;
3915 alloc->end = ddb_size * (width_before_pipe + pipe_width) / total_width;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003916}
3917
Ville Syrjälädf331de2019-03-19 18:03:11 +02003918static int skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
3919 int width, const struct drm_format_info *format,
3920 u64 modifier, unsigned int rotation,
3921 u32 plane_pixel_rate, struct skl_wm_params *wp,
3922 int color_plane);
Maarten Lankhorstec193642019-06-28 10:55:17 +02003923static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Ville Syrjälädf331de2019-03-19 18:03:11 +02003924 int level,
3925 const struct skl_wm_params *wp,
3926 const struct skl_wm_level *result_prev,
3927 struct skl_wm_level *result /* out */);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003928
Ville Syrjälädf331de2019-03-19 18:03:11 +02003929static unsigned int
3930skl_cursor_allocation(const struct intel_crtc_state *crtc_state,
3931 int num_active)
3932{
3933 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
3934 int level, max_level = ilk_wm_max_level(dev_priv);
3935 struct skl_wm_level wm = {};
3936 int ret, min_ddb_alloc = 0;
3937 struct skl_wm_params wp;
3938
3939 ret = skl_compute_wm_params(crtc_state, 256,
3940 drm_format_info(DRM_FORMAT_ARGB8888),
3941 DRM_FORMAT_MOD_LINEAR,
3942 DRM_MODE_ROTATE_0,
3943 crtc_state->pixel_rate, &wp, 0);
3944 WARN_ON(ret);
3945
3946 for (level = 0; level <= max_level; level++) {
Ville Syrjälä6086e472019-03-21 19:51:28 +02003947 skl_compute_plane_wm(crtc_state, level, &wp, &wm, &wm);
Ville Syrjälädf331de2019-03-19 18:03:11 +02003948 if (wm.min_ddb_alloc == U16_MAX)
3949 break;
3950
3951 min_ddb_alloc = wm.min_ddb_alloc;
3952 }
3953
3954 return max(num_active == 1 ? 32 : 8, min_ddb_alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003955}
3956
Mahesh Kumar37cde112018-04-26 19:55:17 +05303957static void skl_ddb_entry_init_from_hw(struct drm_i915_private *dev_priv,
3958 struct skl_ddb_entry *entry, u32 reg)
Damien Lespiaua269c582014-11-04 17:06:49 +00003959{
Mahesh Kumar37cde112018-04-26 19:55:17 +05303960
Ville Syrjäläd7e449a2019-02-05 22:50:56 +02003961 entry->start = reg & DDB_ENTRY_MASK;
3962 entry->end = (reg >> DDB_ENTRY_END_SHIFT) & DDB_ENTRY_MASK;
Mahesh Kumar37cde112018-04-26 19:55:17 +05303963
Damien Lespiau16160e32014-11-04 17:06:53 +00003964 if (entry->end)
3965 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00003966}
3967
Mahesh Kumarddf34312018-04-09 09:11:03 +05303968static void
3969skl_ddb_get_hw_plane_state(struct drm_i915_private *dev_priv,
3970 const enum pipe pipe,
3971 const enum plane_id plane_id,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02003972 struct skl_ddb_entry *ddb_y,
3973 struct skl_ddb_entry *ddb_uv)
Mahesh Kumarddf34312018-04-09 09:11:03 +05303974{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02003975 u32 val, val2;
3976 u32 fourcc = 0;
Mahesh Kumarddf34312018-04-09 09:11:03 +05303977
3978 /* Cursor doesn't support NV12/planar, so no extra calculation needed */
3979 if (plane_id == PLANE_CURSOR) {
3980 val = I915_READ(CUR_BUF_CFG(pipe));
Ville Syrjäläff43bc32018-11-27 18:59:00 +02003981 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
Mahesh Kumarddf34312018-04-09 09:11:03 +05303982 return;
3983 }
3984
3985 val = I915_READ(PLANE_CTL(pipe, plane_id));
3986
3987 /* No DDB allocated for disabled planes */
Ville Syrjäläff43bc32018-11-27 18:59:00 +02003988 if (val & PLANE_CTL_ENABLE)
3989 fourcc = skl_format_to_fourcc(val & PLANE_CTL_FORMAT_MASK,
3990 val & PLANE_CTL_ORDER_RGBX,
3991 val & PLANE_CTL_ALPHA_MASK);
Mahesh Kumarddf34312018-04-09 09:11:03 +05303992
Ville Syrjäläff43bc32018-11-27 18:59:00 +02003993 if (INTEL_GEN(dev_priv) >= 11) {
3994 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
3995 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
3996 } else {
3997 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
Paulo Zanoni12a6c932018-07-31 17:46:14 -07003998 val2 = I915_READ(PLANE_NV12_BUF_CFG(pipe, plane_id));
Mahesh Kumarddf34312018-04-09 09:11:03 +05303999
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004000 if (fourcc &&
4001 drm_format_info_is_yuv_semiplanar(drm_format_info(fourcc)))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004002 swap(val, val2);
4003
4004 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4005 skl_ddb_entry_init_from_hw(dev_priv, ddb_uv, val2);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304006 }
4007}
4008
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004009void skl_pipe_ddb_get_hw_state(struct intel_crtc *crtc,
4010 struct skl_ddb_entry *ddb_y,
4011 struct skl_ddb_entry *ddb_uv)
4012{
4013 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4014 enum intel_display_power_domain power_domain;
4015 enum pipe pipe = crtc->pipe;
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004016 intel_wakeref_t wakeref;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004017 enum plane_id plane_id;
4018
4019 power_domain = POWER_DOMAIN_PIPE(pipe);
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004020 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
4021 if (!wakeref)
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004022 return;
4023
4024 for_each_plane_id_on_crtc(crtc, plane_id)
4025 skl_ddb_get_hw_plane_state(dev_priv, pipe,
4026 plane_id,
4027 &ddb_y[plane_id],
4028 &ddb_uv[plane_id]);
4029
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004030 intel_display_power_put(dev_priv, power_domain, wakeref);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004031}
4032
Damien Lespiau08db6652014-11-04 17:06:52 +00004033void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
4034 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00004035{
Mahesh Kumar74bd8002018-04-26 19:55:15 +05304036 ddb->enabled_slices = intel_enabled_dbuf_slices_num(dev_priv);
Damien Lespiaua269c582014-11-04 17:06:49 +00004037}
4038
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004039/*
4040 * Determines the downscale amount of a plane for the purposes of watermark calculations.
4041 * The bspec defines downscale amount as:
4042 *
4043 * """
4044 * Horizontal down scale amount = maximum[1, Horizontal source size /
4045 * Horizontal destination size]
4046 * Vertical down scale amount = maximum[1, Vertical source size /
4047 * Vertical destination size]
4048 * Total down scale amount = Horizontal down scale amount *
4049 * Vertical down scale amount
4050 * """
4051 *
4052 * Return value is provided in 16.16 fixed point form to retain fractional part.
4053 * Caller should take care of dividing & rounding off the value.
4054 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304055static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02004056skl_plane_downscale_amount(const struct intel_crtc_state *crtc_state,
4057 const struct intel_plane_state *plane_state)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004058{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004059 u32 src_w, src_h, dst_w, dst_h;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304060 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
4061 uint_fixed_16_16_t downscale_h, downscale_w;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004062
Maarten Lankhorstec193642019-06-28 10:55:17 +02004063 if (WARN_ON(!intel_wm_plane_visible(crtc_state, plane_state)))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304064 return u32_to_fixed16(0);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004065
Maarten Lankhorst3a612762019-10-04 13:34:54 +02004066 /*
4067 * Src coordinates are already rotated by 270 degrees for
4068 * the 90/270 degree plane rotation cases (to match the
4069 * GTT mapping), hence no need to account for rotation here.
4070 *
4071 * n.b., src is 16.16 fixed point, dst is whole integer.
4072 */
4073 src_w = drm_rect_width(&plane_state->base.src) >> 16;
4074 src_h = drm_rect_height(&plane_state->base.src) >> 16;
4075 dst_w = drm_rect_width(&plane_state->base.dst);
4076 dst_h = drm_rect_height(&plane_state->base.dst);
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004077
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304078 fp_w_ratio = div_fixed16(src_w, dst_w);
4079 fp_h_ratio = div_fixed16(src_h, dst_h);
4080 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
4081 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004082
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304083 return mul_fixed16(downscale_w, downscale_h);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004084}
4085
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304086static uint_fixed_16_16_t
4087skl_pipe_downscale_amount(const struct intel_crtc_state *crtc_state)
4088{
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304089 uint_fixed_16_16_t pipe_downscale = u32_to_fixed16(1);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304090
4091 if (!crtc_state->base.enable)
4092 return pipe_downscale;
4093
4094 if (crtc_state->pch_pfit.enabled) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004095 u32 src_w, src_h, dst_w, dst_h;
4096 u32 pfit_size = crtc_state->pch_pfit.size;
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304097 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
4098 uint_fixed_16_16_t downscale_h, downscale_w;
4099
4100 src_w = crtc_state->pipe_src_w;
4101 src_h = crtc_state->pipe_src_h;
4102 dst_w = pfit_size >> 16;
4103 dst_h = pfit_size & 0xffff;
4104
4105 if (!dst_w || !dst_h)
4106 return pipe_downscale;
4107
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304108 fp_w_ratio = div_fixed16(src_w, dst_w);
4109 fp_h_ratio = div_fixed16(src_h, dst_h);
4110 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
4111 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304112
4113 pipe_downscale = mul_fixed16(downscale_w, downscale_h);
4114 }
4115
4116 return pipe_downscale;
4117}
4118
4119int skl_check_pipe_max_pixel_rate(struct intel_crtc *intel_crtc,
Maarten Lankhorstec193642019-06-28 10:55:17 +02004120 struct intel_crtc_state *crtc_state)
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304121{
Rodrigo Vivi43037c82017-10-03 15:31:42 -07004122 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Maarten Lankhorstec193642019-06-28 10:55:17 +02004123 struct drm_atomic_state *state = crtc_state->base.state;
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004124 const struct intel_plane_state *plane_state;
4125 struct intel_plane *plane;
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02004126 int crtc_clock, dotclk;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004127 u32 pipe_max_pixel_rate;
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304128 uint_fixed_16_16_t pipe_downscale;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304129 uint_fixed_16_16_t max_downscale = u32_to_fixed16(1);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304130
Maarten Lankhorstec193642019-06-28 10:55:17 +02004131 if (!crtc_state->base.enable)
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304132 return 0;
4133
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004134 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304135 uint_fixed_16_16_t plane_downscale;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304136 uint_fixed_16_16_t fp_9_div_8 = div_fixed16(9, 8);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304137 int bpp;
4138
Maarten Lankhorstec193642019-06-28 10:55:17 +02004139 if (!intel_wm_plane_visible(crtc_state, plane_state))
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304140 continue;
4141
Maarten Lankhorstec193642019-06-28 10:55:17 +02004142 if (WARN_ON(!plane_state->base.fb))
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304143 return -EINVAL;
4144
Maarten Lankhorstec193642019-06-28 10:55:17 +02004145 plane_downscale = skl_plane_downscale_amount(crtc_state, plane_state);
4146 bpp = plane_state->base.fb->format->cpp[0] * 8;
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304147 if (bpp == 64)
4148 plane_downscale = mul_fixed16(plane_downscale,
4149 fp_9_div_8);
4150
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304151 max_downscale = max_fixed16(plane_downscale, max_downscale);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304152 }
Maarten Lankhorstec193642019-06-28 10:55:17 +02004153 pipe_downscale = skl_pipe_downscale_amount(crtc_state);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304154
4155 pipe_downscale = mul_fixed16(pipe_downscale, max_downscale);
4156
Maarten Lankhorstec193642019-06-28 10:55:17 +02004157 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02004158 dotclk = to_intel_atomic_state(state)->cdclk.logical.cdclk;
4159
Rodrigo Vivi43037c82017-10-03 15:31:42 -07004160 if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10)
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02004161 dotclk *= 2;
4162
4163 pipe_max_pixel_rate = div_round_up_u32_fixed16(dotclk, pipe_downscale);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304164
4165 if (pipe_max_pixel_rate < crtc_clock) {
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02004166 DRM_DEBUG_KMS("Max supported pixel clock with scaling exceeded\n");
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304167 return -EINVAL;
4168 }
4169
4170 return 0;
4171}
4172
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004173static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004174skl_plane_relative_data_rate(const struct intel_crtc_state *crtc_state,
4175 const struct intel_plane_state *plane_state,
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004176 int color_plane)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004177{
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004178 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
4179 const struct drm_framebuffer *fb = plane_state->base.fb;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004180 u32 data_rate;
4181 u32 width = 0, height = 0;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304182 uint_fixed_16_16_t down_scale_amount;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004183 u64 rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07004184
Maarten Lankhorstec193642019-06-28 10:55:17 +02004185 if (!plane_state->base.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07004186 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02004187
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004188 if (plane->id == PLANE_CURSOR)
Matt Ropera1de91e2016-05-12 07:05:57 -07004189 return 0;
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004190
4191 if (color_plane == 1 &&
4192 !drm_format_info_is_yuv_semiplanar(fb->format))
Matt Ropera1de91e2016-05-12 07:05:57 -07004193 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004194
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004195 /*
4196 * Src coordinates are already rotated by 270 degrees for
4197 * the 90/270 degree plane rotation cases (to match the
4198 * GTT mapping), hence no need to account for rotation here.
4199 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004200 width = drm_rect_width(&plane_state->base.src) >> 16;
4201 height = drm_rect_height(&plane_state->base.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004202
Mahesh Kumarb879d582018-04-09 09:11:01 +05304203 /* UV plane does 1/2 pixel sub-sampling */
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004204 if (color_plane == 1) {
Mahesh Kumarb879d582018-04-09 09:11:01 +05304205 width /= 2;
4206 height /= 2;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004207 }
4208
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004209 data_rate = width * height;
Mahesh Kumarb879d582018-04-09 09:11:01 +05304210
Maarten Lankhorstec193642019-06-28 10:55:17 +02004211 down_scale_amount = skl_plane_downscale_amount(crtc_state, plane_state);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004212
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004213 rate = mul_round_up_u32_fixed16(data_rate, down_scale_amount);
4214
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004215 rate *= fb->format->cpp[color_plane];
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004216 return rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004217}
4218
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004219static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004220skl_get_total_relative_data_rate(struct intel_crtc_state *crtc_state,
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004221 u64 *plane_data_rate,
4222 u64 *uv_plane_data_rate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004223{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004224 struct drm_atomic_state *state = crtc_state->base.state;
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004225 struct intel_plane *plane;
4226 const struct intel_plane_state *plane_state;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004227 u64 total_data_rate = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07004228
4229 if (WARN_ON(!state))
4230 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004231
Matt Ropera1de91e2016-05-12 07:05:57 -07004232 /* Calculate and cache data rate for each plane */
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004233 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
4234 enum plane_id plane_id = plane->id;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004235 u64 rate;
Matt Roper024c9042015-09-24 15:53:11 -07004236
Mahesh Kumarb879d582018-04-09 09:11:01 +05304237 /* packed/y */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004238 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004239 plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004240 total_data_rate += rate;
Matt Roper9c74d822016-05-12 07:05:58 -07004241
Mahesh Kumarb879d582018-04-09 09:11:01 +05304242 /* uv-plane */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004243 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 1);
Mahesh Kumarb879d582018-04-09 09:11:01 +05304244 uv_plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004245 total_data_rate += rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004246 }
4247
4248 return total_data_rate;
4249}
4250
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004251static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004252icl_get_total_relative_data_rate(struct intel_crtc_state *crtc_state,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004253 u64 *plane_data_rate)
4254{
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004255 struct intel_plane *plane;
4256 const struct intel_plane_state *plane_state;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004257 u64 total_data_rate = 0;
4258
Maarten Lankhorstec193642019-06-28 10:55:17 +02004259 if (WARN_ON(!crtc_state->base.state))
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004260 return 0;
4261
4262 /* Calculate and cache data rate for each plane */
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004263 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
4264 enum plane_id plane_id = plane->id;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004265 u64 rate;
4266
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004267 if (!plane_state->planar_linked_plane) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02004268 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004269 plane_data_rate[plane_id] = rate;
4270 total_data_rate += rate;
4271 } else {
4272 enum plane_id y_plane_id;
4273
4274 /*
4275 * The slave plane might not iterate in
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004276 * intel_atomic_crtc_state_for_each_plane_state(),
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004277 * and needs the master plane state which may be
4278 * NULL if we try get_new_plane_state(), so we
4279 * always calculate from the master.
4280 */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004281 if (plane_state->planar_slave)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004282 continue;
4283
4284 /* Y plane rate is calculated on the slave */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004285 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004286 y_plane_id = plane_state->planar_linked_plane->id;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004287 plane_data_rate[y_plane_id] = rate;
4288 total_data_rate += rate;
4289
Maarten Lankhorstec193642019-06-28 10:55:17 +02004290 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004291 plane_data_rate[plane_id] = rate;
4292 total_data_rate += rate;
4293 }
4294 }
4295
4296 return total_data_rate;
4297}
4298
Matt Roperc107acf2016-05-12 07:06:01 -07004299static int
Maarten Lankhorstec193642019-06-28 10:55:17 +02004300skl_allocate_pipe_ddb(struct intel_crtc_state *crtc_state,
Damien Lespiaub9cec072014-11-04 17:06:43 +00004301 struct skl_ddb_allocation *ddb /* out */)
4302{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004303 struct drm_atomic_state *state = crtc_state->base.state;
4304 struct drm_crtc *crtc = crtc_state->base.crtc;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004305 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004306 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstec193642019-06-28 10:55:17 +02004307 struct skl_ddb_entry *alloc = &crtc_state->wm.skl.ddb;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004308 u16 alloc_size, start = 0;
4309 u16 total[I915_MAX_PLANES] = {};
4310 u16 uv_total[I915_MAX_PLANES] = {};
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004311 u64 total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004312 enum plane_id plane_id;
Matt Roperc107acf2016-05-12 07:06:01 -07004313 int num_active;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004314 u64 plane_data_rate[I915_MAX_PLANES] = {};
4315 u64 uv_plane_data_rate[I915_MAX_PLANES] = {};
Ville Syrjälä0aded172019-02-05 17:50:53 +02004316 u32 blocks;
Matt Roperd8e87492018-12-11 09:31:07 -08004317 int level;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004318
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004319 /* Clear the partitioning for disabled planes. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004320 memset(crtc_state->wm.skl.plane_ddb_y, 0, sizeof(crtc_state->wm.skl.plane_ddb_y));
4321 memset(crtc_state->wm.skl.plane_ddb_uv, 0, sizeof(crtc_state->wm.skl.plane_ddb_uv));
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004322
Matt Ropera6d3460e2016-05-12 07:06:04 -07004323 if (WARN_ON(!state))
4324 return 0;
4325
Maarten Lankhorstec193642019-06-28 10:55:17 +02004326 if (!crtc_state->base.active) {
Lyudece0ba282016-09-15 10:46:35 -04004327 alloc->start = alloc->end = 0;
Matt Roperc107acf2016-05-12 07:06:01 -07004328 return 0;
4329 }
4330
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004331 if (INTEL_GEN(dev_priv) >= 11)
4332 total_data_rate =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004333 icl_get_total_relative_data_rate(crtc_state,
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004334 plane_data_rate);
4335 else
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004336 total_data_rate =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004337 skl_get_total_relative_data_rate(crtc_state,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004338 plane_data_rate,
4339 uv_plane_data_rate);
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004340
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004341
Maarten Lankhorstec193642019-06-28 10:55:17 +02004342 skl_ddb_get_pipe_allocation_limits(dev_priv, crtc_state, total_data_rate,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004343 ddb, alloc, &num_active);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00004344 alloc_size = skl_ddb_entry_size(alloc);
Kumar, Mahesh336031e2017-05-17 17:28:25 +05304345 if (alloc_size == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004346 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004347
Matt Roperd8e87492018-12-11 09:31:07 -08004348 /* Allocate fixed number of blocks for cursor. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004349 total[PLANE_CURSOR] = skl_cursor_allocation(crtc_state, num_active);
Matt Roperd8e87492018-12-11 09:31:07 -08004350 alloc_size -= total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02004351 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].start =
Matt Roperd8e87492018-12-11 09:31:07 -08004352 alloc->end - total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02004353 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].end = alloc->end;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004354
Matt Ropera1de91e2016-05-12 07:05:57 -07004355 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004356 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004357
Matt Roperd8e87492018-12-11 09:31:07 -08004358 /*
4359 * Find the highest watermark level for which we can satisfy the block
4360 * requirement of active planes.
4361 */
4362 for (level = ilk_wm_max_level(dev_priv); level >= 0; level--) {
Matt Roper25db2ea2018-12-12 11:17:20 -08004363 blocks = 0;
Matt Roperd8e87492018-12-11 09:31:07 -08004364 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004365 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004366 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä10a7e072019-03-12 22:58:40 +02004367
4368 if (plane_id == PLANE_CURSOR) {
4369 if (WARN_ON(wm->wm[level].min_ddb_alloc >
4370 total[PLANE_CURSOR])) {
4371 blocks = U32_MAX;
4372 break;
4373 }
4374 continue;
4375 }
4376
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004377 blocks += wm->wm[level].min_ddb_alloc;
4378 blocks += wm->uv_wm[level].min_ddb_alloc;
Matt Roperd8e87492018-12-11 09:31:07 -08004379 }
4380
Ville Syrjälä3cf963c2019-03-12 22:58:36 +02004381 if (blocks <= alloc_size) {
Matt Roperd8e87492018-12-11 09:31:07 -08004382 alloc_size -= blocks;
4383 break;
4384 }
4385 }
4386
4387 if (level < 0) {
4388 DRM_DEBUG_KMS("Requested display configuration exceeds system DDB limitations");
4389 DRM_DEBUG_KMS("minimum required %d/%d\n", blocks,
4390 alloc_size);
4391 return -EINVAL;
4392 }
4393
4394 /*
4395 * Grant each plane the blocks it requires at the highest achievable
4396 * watermark level, plus an extra share of the leftover blocks
4397 * proportional to its relative data rate.
4398 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004399 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004400 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004401 &crtc_state->wm.skl.optimal.planes[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08004402 u64 rate;
4403 u16 extra;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004404
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004405 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004406 continue;
4407
Damien Lespiaub9cec072014-11-04 17:06:43 +00004408 /*
Matt Roperd8e87492018-12-11 09:31:07 -08004409 * We've accounted for all active planes; remaining planes are
4410 * all disabled.
Damien Lespiaub9cec072014-11-04 17:06:43 +00004411 */
Matt Roperd8e87492018-12-11 09:31:07 -08004412 if (total_data_rate == 0)
4413 break;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004414
Matt Roperd8e87492018-12-11 09:31:07 -08004415 rate = plane_data_rate[plane_id];
4416 extra = min_t(u16, alloc_size,
4417 DIV64_U64_ROUND_UP(alloc_size * rate,
4418 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004419 total[plane_id] = wm->wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08004420 alloc_size -= extra;
4421 total_data_rate -= rate;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004422
Matt Roperd8e87492018-12-11 09:31:07 -08004423 if (total_data_rate == 0)
4424 break;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004425
Matt Roperd8e87492018-12-11 09:31:07 -08004426 rate = uv_plane_data_rate[plane_id];
4427 extra = min_t(u16, alloc_size,
4428 DIV64_U64_ROUND_UP(alloc_size * rate,
4429 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004430 uv_total[plane_id] = wm->uv_wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08004431 alloc_size -= extra;
4432 total_data_rate -= rate;
4433 }
4434 WARN_ON(alloc_size != 0 || total_data_rate != 0);
4435
4436 /* Set the actual DDB start/end points for each plane */
4437 start = alloc->start;
4438 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004439 struct skl_ddb_entry *plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004440 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004441 struct skl_ddb_entry *uv_plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004442 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08004443
4444 if (plane_id == PLANE_CURSOR)
4445 continue;
4446
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004447 /* Gen11+ uses a separate plane for UV watermarks */
Matt Roperd8e87492018-12-11 09:31:07 -08004448 WARN_ON(INTEL_GEN(dev_priv) >= 11 && uv_total[plane_id]);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004449
Matt Roperd8e87492018-12-11 09:31:07 -08004450 /* Leave disabled planes at (0,0) */
4451 if (total[plane_id]) {
4452 plane_alloc->start = start;
4453 start += total[plane_id];
4454 plane_alloc->end = start;
Matt Roperc107acf2016-05-12 07:06:01 -07004455 }
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004456
Matt Roperd8e87492018-12-11 09:31:07 -08004457 if (uv_total[plane_id]) {
4458 uv_plane_alloc->start = start;
4459 start += uv_total[plane_id];
4460 uv_plane_alloc->end = start;
4461 }
4462 }
4463
4464 /*
4465 * When we calculated watermark values we didn't know how high
4466 * of a level we'd actually be able to hit, so we just marked
4467 * all levels as "enabled." Go back now and disable the ones
4468 * that aren't actually possible.
4469 */
4470 for (level++; level <= ilk_wm_max_level(dev_priv); level++) {
4471 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004472 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004473 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjäläa301cb02019-03-12 22:58:41 +02004474
4475 /*
4476 * We only disable the watermarks for each plane if
4477 * they exceed the ddb allocation of said plane. This
4478 * is done so that we don't end up touching cursor
4479 * watermarks needlessly when some other plane reduces
4480 * our max possible watermark level.
4481 *
4482 * Bspec has this to say about the PLANE_WM enable bit:
4483 * "All the watermarks at this level for all enabled
4484 * planes must be enabled before the level will be used."
4485 * So this is actually safe to do.
4486 */
4487 if (wm->wm[level].min_ddb_alloc > total[plane_id] ||
4488 wm->uv_wm[level].min_ddb_alloc > uv_total[plane_id])
4489 memset(&wm->wm[level], 0, sizeof(wm->wm[level]));
Ville Syrjälä290248c2019-02-13 18:54:24 +02004490
Ville Syrjäläc384afe2019-02-28 19:36:39 +02004491 /*
Bob Paauwe39564ae2019-04-12 11:09:20 -07004492 * Wa_1408961008:icl, ehl
Ville Syrjäläc384afe2019-02-28 19:36:39 +02004493 * Underruns with WM1+ disabled
4494 */
Bob Paauwe39564ae2019-04-12 11:09:20 -07004495 if (IS_GEN(dev_priv, 11) &&
Ville Syrjälä290248c2019-02-13 18:54:24 +02004496 level == 1 && wm->wm[0].plane_en) {
4497 wm->wm[level].plane_res_b = wm->wm[0].plane_res_b;
Ville Syrjäläc384afe2019-02-28 19:36:39 +02004498 wm->wm[level].plane_res_l = wm->wm[0].plane_res_l;
4499 wm->wm[level].ignore_lines = wm->wm[0].ignore_lines;
Ville Syrjälä290248c2019-02-13 18:54:24 +02004500 }
Matt Roperd8e87492018-12-11 09:31:07 -08004501 }
4502 }
4503
4504 /*
4505 * Go back and disable the transition watermark if it turns out we
4506 * don't have enough DDB blocks for it.
4507 */
4508 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004509 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004510 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004511
Ville Syrjäläb19c9bc2018-12-21 19:14:31 +02004512 if (wm->trans_wm.plane_res_b >= total[plane_id])
Matt Roperd8e87492018-12-11 09:31:07 -08004513 memset(&wm->trans_wm, 0, sizeof(wm->trans_wm));
Damien Lespiaub9cec072014-11-04 17:06:43 +00004514 }
4515
Matt Roperc107acf2016-05-12 07:06:01 -07004516 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004517}
4518
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004519/*
4520 * 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 +02004521 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004522 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
4523 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
4524*/
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07004525static uint_fixed_16_16_t
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004526skl_wm_method1(const struct drm_i915_private *dev_priv, u32 pixel_rate,
4527 u8 cpp, u32 latency, u32 dbuf_block_size)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004528{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004529 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304530 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004531
4532 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304533 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004534
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304535 wm_intermediate_val = latency * pixel_rate * cpp;
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004536 ret = div_fixed16(wm_intermediate_val, 1000 * dbuf_block_size);
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07004537
4538 if (INTEL_GEN(dev_priv) >= 10)
4539 ret = add_fixed16_u32(ret, 1);
4540
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004541 return ret;
4542}
4543
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004544static uint_fixed_16_16_t
4545skl_wm_method2(u32 pixel_rate, u32 pipe_htotal, u32 latency,
4546 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004547{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004548 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304549 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004550
4551 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304552 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004553
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004554 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304555 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
4556 pipe_htotal * 1000);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304557 ret = mul_u32_fixed16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004558 return ret;
4559}
4560
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304561static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02004562intel_get_linetime_us(const struct intel_crtc_state *crtc_state)
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304563{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004564 u32 pixel_rate;
4565 u32 crtc_htotal;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304566 uint_fixed_16_16_t linetime_us;
4567
Maarten Lankhorstec193642019-06-28 10:55:17 +02004568 if (!crtc_state->base.active)
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304569 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304570
Maarten Lankhorstec193642019-06-28 10:55:17 +02004571 pixel_rate = crtc_state->pixel_rate;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304572
4573 if (WARN_ON(pixel_rate == 0))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304574 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304575
Maarten Lankhorstec193642019-06-28 10:55:17 +02004576 crtc_htotal = crtc_state->base.adjusted_mode.crtc_htotal;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304577 linetime_us = div_fixed16(crtc_htotal * 1000, pixel_rate);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304578
4579 return linetime_us;
4580}
4581
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004582static u32
Maarten Lankhorstec193642019-06-28 10:55:17 +02004583skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *crtc_state,
4584 const struct intel_plane_state *plane_state)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004585{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004586 u64 adjusted_pixel_rate;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304587 uint_fixed_16_16_t downscale_amount;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004588
4589 /* Shouldn't reach here on disabled planes... */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004590 if (WARN_ON(!intel_wm_plane_visible(crtc_state, plane_state)))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004591 return 0;
4592
4593 /*
4594 * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
4595 * with additional adjustments for plane-specific scaling.
4596 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004597 adjusted_pixel_rate = crtc_state->pixel_rate;
4598 downscale_amount = skl_plane_downscale_amount(crtc_state, plane_state);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004599
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304600 return mul_round_up_u32_fixed16(adjusted_pixel_rate,
4601 downscale_amount);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004602}
4603
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304604static int
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004605skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
4606 int width, const struct drm_format_info *format,
4607 u64 modifier, unsigned int rotation,
4608 u32 plane_pixel_rate, struct skl_wm_params *wp,
4609 int color_plane)
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304610{
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004611 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
4612 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004613 u32 interm_pbpl;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304614
Juha-Pekka Heikkiladf7d4152019-03-04 17:26:31 +05304615 /* only planar format has two planes */
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004616 if (color_plane == 1 && !drm_format_info_is_yuv_semiplanar(format)) {
Juha-Pekka Heikkiladf7d4152019-03-04 17:26:31 +05304617 DRM_DEBUG_KMS("Non planar format have single plane\n");
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05304618 return -EINVAL;
4619 }
4620
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004621 wp->y_tiled = modifier == I915_FORMAT_MOD_Y_TILED ||
4622 modifier == I915_FORMAT_MOD_Yf_TILED ||
4623 modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
4624 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
4625 wp->x_tiled = modifier == I915_FORMAT_MOD_X_TILED;
4626 wp->rc_surface = modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
4627 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004628 wp->is_planar = drm_format_info_is_yuv_semiplanar(format);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304629
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004630 wp->width = width;
Ville Syrjälä45bee432018-11-14 23:07:28 +02004631 if (color_plane == 1 && wp->is_planar)
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05304632 wp->width /= 2;
4633
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004634 wp->cpp = format->cpp[color_plane];
4635 wp->plane_pixel_rate = plane_pixel_rate;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304636
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004637 if (INTEL_GEN(dev_priv) >= 11 &&
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004638 modifier == I915_FORMAT_MOD_Yf_TILED && wp->cpp == 1)
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004639 wp->dbuf_block_size = 256;
4640 else
4641 wp->dbuf_block_size = 512;
4642
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004643 if (drm_rotation_90_or_270(rotation)) {
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304644 switch (wp->cpp) {
4645 case 1:
4646 wp->y_min_scanlines = 16;
4647 break;
4648 case 2:
4649 wp->y_min_scanlines = 8;
4650 break;
4651 case 4:
4652 wp->y_min_scanlines = 4;
4653 break;
4654 default:
4655 MISSING_CASE(wp->cpp);
4656 return -EINVAL;
4657 }
4658 } else {
4659 wp->y_min_scanlines = 4;
4660 }
4661
Ville Syrjälä60e983f2018-12-21 19:14:33 +02004662 if (skl_needs_memory_bw_wa(dev_priv))
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304663 wp->y_min_scanlines *= 2;
4664
4665 wp->plane_bytes_per_line = wp->width * wp->cpp;
4666 if (wp->y_tiled) {
4667 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line *
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004668 wp->y_min_scanlines,
4669 wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304670
4671 if (INTEL_GEN(dev_priv) >= 10)
4672 interm_pbpl++;
4673
4674 wp->plane_blocks_per_line = div_fixed16(interm_pbpl,
4675 wp->y_min_scanlines);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004676 } else if (wp->x_tiled && IS_GEN(dev_priv, 9)) {
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004677 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
4678 wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304679 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
4680 } else {
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004681 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
4682 wp->dbuf_block_size) + 1;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304683 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
4684 }
4685
4686 wp->y_tile_minimum = mul_u32_fixed16(wp->y_min_scanlines,
4687 wp->plane_blocks_per_line);
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004688
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304689 wp->linetime_us = fixed16_to_u32_round_up(
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004690 intel_get_linetime_us(crtc_state));
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304691
4692 return 0;
4693}
4694
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004695static int
4696skl_compute_plane_wm_params(const struct intel_crtc_state *crtc_state,
4697 const struct intel_plane_state *plane_state,
4698 struct skl_wm_params *wp, int color_plane)
4699{
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004700 const struct drm_framebuffer *fb = plane_state->base.fb;
4701 int width;
4702
Maarten Lankhorst3a612762019-10-04 13:34:54 +02004703 /*
4704 * Src coordinates are already rotated by 270 degrees for
4705 * the 90/270 degree plane rotation cases (to match the
4706 * GTT mapping), hence no need to account for rotation here.
4707 */
4708 width = drm_rect_width(&plane_state->base.src) >> 16;
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004709
4710 return skl_compute_wm_params(crtc_state, width,
4711 fb->format, fb->modifier,
4712 plane_state->base.rotation,
4713 skl_adjusted_plane_pixel_rate(crtc_state, plane_state),
4714 wp, color_plane);
4715}
4716
Ville Syrjäläb52c2732018-12-21 19:14:28 +02004717static bool skl_wm_has_lines(struct drm_i915_private *dev_priv, int level)
4718{
4719 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
4720 return true;
4721
4722 /* The number of lines are ignored for the level 0 watermark. */
4723 return level > 0;
4724}
4725
Maarten Lankhorstec193642019-06-28 10:55:17 +02004726static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Matt Roperd8e87492018-12-11 09:31:07 -08004727 int level,
4728 const struct skl_wm_params *wp,
4729 const struct skl_wm_level *result_prev,
4730 struct skl_wm_level *result /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004731{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004732 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004733 u32 latency = dev_priv->wm.skl_latency[level];
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304734 uint_fixed_16_16_t method1, method2;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304735 uint_fixed_16_16_t selected_result;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004736 u32 res_blocks, res_lines, min_ddb_alloc = 0;
Ville Syrjäläce110ec2018-11-14 23:07:21 +02004737
Ville Syrjälä0aded172019-02-05 17:50:53 +02004738 if (latency == 0) {
4739 /* reject it */
4740 result->min_ddb_alloc = U16_MAX;
Ville Syrjälä692927f2018-12-21 19:14:29 +02004741 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02004742 }
Ville Syrjälä692927f2018-12-21 19:14:29 +02004743
Ville Syrjälä25312ef2019-05-03 20:38:05 +03004744 /*
4745 * WaIncreaseLatencyIPCEnabled: kbl,cfl
4746 * Display WA #1141: kbl,cfl
4747 */
Ville Syrjälä5a7d2022019-05-03 20:38:06 +03004748 if ((IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv)) ||
Rodrigo Vivi82525c12017-06-08 08:50:00 -07004749 dev_priv->ipc_enabled)
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05304750 latency += 4;
4751
Ville Syrjälä60e983f2018-12-21 19:14:33 +02004752 if (skl_needs_memory_bw_wa(dev_priv) && wp->x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03004753 latency += 15;
4754
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304755 method1 = skl_wm_method1(dev_priv, wp->plane_pixel_rate,
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004756 wp->cpp, latency, wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304757 method2 = skl_wm_method2(wp->plane_pixel_rate,
Maarten Lankhorstec193642019-06-28 10:55:17 +02004758 crtc_state->base.adjusted_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004759 latency,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304760 wp->plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004761
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304762 if (wp->y_tiled) {
4763 selected_result = max_fixed16(method2, wp->y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004764 } else {
Maarten Lankhorstec193642019-06-28 10:55:17 +02004765 if ((wp->cpp * crtc_state->base.adjusted_mode.crtc_htotal /
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004766 wp->dbuf_block_size < 1) &&
Paulo Zanoni077b5822018-10-04 16:15:57 -07004767 (wp->plane_bytes_per_line / wp->dbuf_block_size < 1)) {
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03004768 selected_result = method2;
Paulo Zanoni077b5822018-10-04 16:15:57 -07004769 } else if (latency >= wp->linetime_us) {
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004770 if (IS_GEN(dev_priv, 9) &&
Paulo Zanoni077b5822018-10-04 16:15:57 -07004771 !IS_GEMINILAKE(dev_priv))
4772 selected_result = min_fixed16(method1, method2);
4773 else
4774 selected_result = method2;
4775 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004776 selected_result = method1;
Paulo Zanoni077b5822018-10-04 16:15:57 -07004777 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004778 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004779
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304780 res_blocks = fixed16_to_u32_round_up(selected_result) + 1;
Kumar, Maheshd273ecc2017-05-17 17:28:22 +05304781 res_lines = div_round_up_fixed16(selected_result,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304782 wp->plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00004783
Paulo Zanonia5b79d32018-11-13 17:24:32 -08004784 if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv)) {
4785 /* Display WA #1125: skl,bxt,kbl */
4786 if (level == 0 && wp->rc_surface)
4787 res_blocks +=
4788 fixed16_to_u32_round_up(wp->y_tile_minimum);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07004789
Paulo Zanonia5b79d32018-11-13 17:24:32 -08004790 /* Display WA #1126: skl,bxt,kbl */
4791 if (level >= 1 && level <= 7) {
4792 if (wp->y_tiled) {
4793 res_blocks +=
4794 fixed16_to_u32_round_up(wp->y_tile_minimum);
4795 res_lines += wp->y_min_scanlines;
4796 } else {
4797 res_blocks++;
4798 }
4799
4800 /*
4801 * Make sure result blocks for higher latency levels are
4802 * atleast as high as level below the current level.
4803 * Assumption in DDB algorithm optimization for special
4804 * cases. Also covers Display WA #1125 for RC.
4805 */
4806 if (result_prev->plane_res_b > res_blocks)
4807 res_blocks = result_prev->plane_res_b;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004808 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004809 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004810
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004811 if (INTEL_GEN(dev_priv) >= 11) {
4812 if (wp->y_tiled) {
4813 int extra_lines;
4814
4815 if (res_lines % wp->y_min_scanlines == 0)
4816 extra_lines = wp->y_min_scanlines;
4817 else
4818 extra_lines = wp->y_min_scanlines * 2 -
4819 res_lines % wp->y_min_scanlines;
4820
4821 min_ddb_alloc = mul_round_up_u32_fixed16(res_lines + extra_lines,
4822 wp->plane_blocks_per_line);
4823 } else {
4824 min_ddb_alloc = res_blocks +
4825 DIV_ROUND_UP(res_blocks, 10);
4826 }
4827 }
4828
Ville Syrjäläb52c2732018-12-21 19:14:28 +02004829 if (!skl_wm_has_lines(dev_priv, level))
4830 res_lines = 0;
4831
Ville Syrjälä0aded172019-02-05 17:50:53 +02004832 if (res_lines > 31) {
4833 /* reject it */
4834 result->min_ddb_alloc = U16_MAX;
Matt Roperd8e87492018-12-11 09:31:07 -08004835 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02004836 }
Matt Roperd8e87492018-12-11 09:31:07 -08004837
4838 /*
4839 * If res_lines is valid, assume we can use this watermark level
4840 * for now. We'll come back and disable it after we calculate the
4841 * DDB allocation if it turns out we don't actually have enough
4842 * blocks to satisfy it.
4843 */
Mahesh Kumar62027b72018-04-09 09:11:05 +05304844 result->plane_res_b = res_blocks;
4845 result->plane_res_l = res_lines;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004846 /* Bspec says: value >= plane ddb allocation -> invalid, hence the +1 here */
4847 result->min_ddb_alloc = max(min_ddb_alloc, res_blocks) + 1;
Mahesh Kumar62027b72018-04-09 09:11:05 +05304848 result->plane_en = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004849}
4850
Matt Roperd8e87492018-12-11 09:31:07 -08004851static void
Maarten Lankhorstec193642019-06-28 10:55:17 +02004852skl_compute_wm_levels(const struct intel_crtc_state *crtc_state,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304853 const struct skl_wm_params *wm_params,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004854 struct skl_wm_level *levels)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004855{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004856 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304857 int level, max_level = ilk_wm_max_level(dev_priv);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004858 struct skl_wm_level *result_prev = &levels[0];
Lyudea62163e2016-10-04 14:28:20 -04004859
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304860 for (level = 0; level <= max_level; level++) {
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004861 struct skl_wm_level *result = &levels[level];
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304862
Maarten Lankhorstec193642019-06-28 10:55:17 +02004863 skl_compute_plane_wm(crtc_state, level, wm_params,
Matt Roperd8e87492018-12-11 09:31:07 -08004864 result_prev, result);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004865
4866 result_prev = result;
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304867 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004868}
4869
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004870static u32
Maarten Lankhorstec193642019-06-28 10:55:17 +02004871skl_compute_linetime_wm(const struct intel_crtc_state *crtc_state)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004872{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004873 struct drm_atomic_state *state = crtc_state->base.state;
Mahesh Kumara3a89862016-12-01 21:19:34 +05304874 struct drm_i915_private *dev_priv = to_i915(state->dev);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304875 uint_fixed_16_16_t linetime_us;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004876 u32 linetime_wm;
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03004877
Maarten Lankhorstec193642019-06-28 10:55:17 +02004878 linetime_us = intel_get_linetime_us(crtc_state);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304879 linetime_wm = fixed16_to_u32_round_up(mul_u32_fixed16(8, linetime_us));
Mahesh Kumara3a89862016-12-01 21:19:34 +05304880
Ville Syrjälä717671c2018-12-21 19:14:36 +02004881 /* Display WA #1135: BXT:ALL GLK:ALL */
4882 if (IS_GEN9_LP(dev_priv) && dev_priv->ipc_enabled)
Kumar, Mahesh446e8502017-08-17 19:15:25 +05304883 linetime_wm /= 2;
Mahesh Kumara3a89862016-12-01 21:19:34 +05304884
4885 return linetime_wm;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004886}
4887
Maarten Lankhorstec193642019-06-28 10:55:17 +02004888static void skl_compute_transition_wm(const struct intel_crtc_state *crtc_state,
Ville Syrjälä6a3c910b2018-11-14 23:07:23 +02004889 const struct skl_wm_params *wp,
Matt Roperd8e87492018-12-11 09:31:07 -08004890 struct skl_plane_wm *wm)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004891{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004892 struct drm_device *dev = crtc_state->base.crtc->dev;
Kumar, Maheshca476672017-08-17 19:15:24 +05304893 const struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004894 u16 trans_min, trans_y_tile_min;
4895 const u16 trans_amount = 10; /* This is configurable amount */
4896 u16 wm0_sel_res_b, trans_offset_b, res_blocks;
Damien Lespiau9414f562014-11-04 17:06:58 +00004897
Kumar, Maheshca476672017-08-17 19:15:24 +05304898 /* Transition WM are not recommended by HW team for GEN9 */
4899 if (INTEL_GEN(dev_priv) <= 9)
Ville Syrjälä14a43062018-11-14 23:07:22 +02004900 return;
Kumar, Maheshca476672017-08-17 19:15:24 +05304901
4902 /* Transition WM don't make any sense if ipc is disabled */
4903 if (!dev_priv->ipc_enabled)
Ville Syrjälä14a43062018-11-14 23:07:22 +02004904 return;
Kumar, Maheshca476672017-08-17 19:15:24 +05304905
Paulo Zanoni91961a82018-10-04 16:15:56 -07004906 trans_min = 14;
4907 if (INTEL_GEN(dev_priv) >= 11)
Kumar, Maheshca476672017-08-17 19:15:24 +05304908 trans_min = 4;
4909
4910 trans_offset_b = trans_min + trans_amount;
4911
Paulo Zanonicbacc792018-10-04 16:15:58 -07004912 /*
4913 * The spec asks for Selected Result Blocks for wm0 (the real value),
4914 * not Result Blocks (the integer value). Pay attention to the capital
4915 * letters. The value wm_l0->plane_res_b is actually Result Blocks, but
4916 * since Result Blocks is the ceiling of Selected Result Blocks plus 1,
4917 * and since we later will have to get the ceiling of the sum in the
4918 * transition watermarks calculation, we can just pretend Selected
4919 * Result Blocks is Result Blocks minus 1 and it should work for the
4920 * current platforms.
4921 */
Ville Syrjälä6a3c910b2018-11-14 23:07:23 +02004922 wm0_sel_res_b = wm->wm[0].plane_res_b - 1;
Paulo Zanonicbacc792018-10-04 16:15:58 -07004923
Kumar, Maheshca476672017-08-17 19:15:24 +05304924 if (wp->y_tiled) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004925 trans_y_tile_min =
4926 (u16)mul_round_up_u32_fixed16(2, wp->y_tile_minimum);
Paulo Zanonicbacc792018-10-04 16:15:58 -07004927 res_blocks = max(wm0_sel_res_b, trans_y_tile_min) +
Kumar, Maheshca476672017-08-17 19:15:24 +05304928 trans_offset_b;
4929 } else {
Paulo Zanonicbacc792018-10-04 16:15:58 -07004930 res_blocks = wm0_sel_res_b + trans_offset_b;
Kumar, Maheshca476672017-08-17 19:15:24 +05304931
4932 /* WA BUG:1938466 add one block for non y-tile planes */
4933 if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_A0))
4934 res_blocks += 1;
4935
4936 }
4937
Matt Roperd8e87492018-12-11 09:31:07 -08004938 /*
4939 * Just assume we can enable the transition watermark. After
4940 * computing the DDB we'll come back and disable it if that
4941 * assumption turns out to be false.
4942 */
4943 wm->trans_wm.plane_res_b = res_blocks + 1;
4944 wm->trans_wm.plane_en = true;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004945}
4946
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004947static int skl_build_plane_wm_single(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004948 const struct intel_plane_state *plane_state,
4949 enum plane_id plane_id, int color_plane)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004950{
Ville Syrjälä83158472018-11-27 18:57:26 +02004951 struct skl_plane_wm *wm = &crtc_state->wm.skl.optimal.planes[plane_id];
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004952 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004953 int ret;
4954
Ville Syrjälä51de9c62018-11-14 23:07:25 +02004955 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004956 &wm_params, color_plane);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004957 if (ret)
4958 return ret;
4959
Ville Syrjälä67155a62019-03-12 22:58:37 +02004960 skl_compute_wm_levels(crtc_state, &wm_params, wm->wm);
Matt Roperd8e87492018-12-11 09:31:07 -08004961 skl_compute_transition_wm(crtc_state, &wm_params, wm);
Ville Syrjälä83158472018-11-27 18:57:26 +02004962
4963 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004964}
4965
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004966static int skl_build_plane_wm_uv(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004967 const struct intel_plane_state *plane_state,
4968 enum plane_id plane_id)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004969{
Ville Syrjälä83158472018-11-27 18:57:26 +02004970 struct skl_plane_wm *wm = &crtc_state->wm.skl.optimal.planes[plane_id];
4971 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004972 int ret;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004973
Ville Syrjälä83158472018-11-27 18:57:26 +02004974 wm->is_planar = true;
4975
4976 /* uv plane watermarks must also be validated for NV12/Planar */
Ville Syrjälä51de9c62018-11-14 23:07:25 +02004977 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004978 &wm_params, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004979 if (ret)
4980 return ret;
4981
Ville Syrjälä67155a62019-03-12 22:58:37 +02004982 skl_compute_wm_levels(crtc_state, &wm_params, wm->uv_wm);
Ville Syrjälä83158472018-11-27 18:57:26 +02004983
4984 return 0;
4985}
4986
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02004987static int skl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004988 const struct intel_plane_state *plane_state)
4989{
4990 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
4991 const struct drm_framebuffer *fb = plane_state->base.fb;
4992 enum plane_id plane_id = plane->id;
4993 int ret;
4994
4995 if (!intel_wm_plane_visible(crtc_state, plane_state))
4996 return 0;
4997
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004998 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004999 plane_id, 0);
5000 if (ret)
5001 return ret;
5002
5003 if (fb->format->is_yuv && fb->format->num_planes > 1) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005004 ret = skl_build_plane_wm_uv(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005005 plane_id);
5006 if (ret)
5007 return ret;
5008 }
5009
5010 return 0;
5011}
5012
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005013static int icl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005014 const struct intel_plane_state *plane_state)
5015{
5016 enum plane_id plane_id = to_intel_plane(plane_state->base.plane)->id;
5017 int ret;
5018
5019 /* Watermarks calculated in master */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005020 if (plane_state->planar_slave)
Ville Syrjälä83158472018-11-27 18:57:26 +02005021 return 0;
5022
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005023 if (plane_state->planar_linked_plane) {
Ville Syrjälä83158472018-11-27 18:57:26 +02005024 const struct drm_framebuffer *fb = plane_state->base.fb;
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005025 enum plane_id y_plane_id = plane_state->planar_linked_plane->id;
Ville Syrjälä83158472018-11-27 18:57:26 +02005026
5027 WARN_ON(!intel_wm_plane_visible(crtc_state, plane_state));
5028 WARN_ON(!fb->format->is_yuv ||
5029 fb->format->num_planes == 1);
5030
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005031 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005032 y_plane_id, 0);
5033 if (ret)
5034 return ret;
5035
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005036 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005037 plane_id, 1);
5038 if (ret)
5039 return ret;
5040 } else if (intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005041 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005042 plane_id, 0);
5043 if (ret)
5044 return ret;
5045 }
5046
5047 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005048}
5049
Maarten Lankhorstec193642019-06-28 10:55:17 +02005050static int skl_build_pipe_wm(struct intel_crtc_state *crtc_state)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005051{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005052 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
5053 struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02005054 struct intel_plane *plane;
5055 const struct intel_plane_state *plane_state;
Matt Roper55994c22016-05-12 07:06:08 -07005056 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005057
Lyudea62163e2016-10-04 14:28:20 -04005058 /*
5059 * We'll only calculate watermarks for planes that are actually
5060 * enabled, so make sure all other planes are set as disabled.
5061 */
5062 memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
5063
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02005064 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state,
5065 crtc_state) {
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05305066
Ville Syrjälä83158472018-11-27 18:57:26 +02005067 if (INTEL_GEN(dev_priv) >= 11)
Maarten Lankhorstec193642019-06-28 10:55:17 +02005068 ret = icl_build_plane_wm(crtc_state, plane_state);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005069 else
Maarten Lankhorstec193642019-06-28 10:55:17 +02005070 ret = skl_build_plane_wm(crtc_state, plane_state);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305071 if (ret)
5072 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005073 }
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305074
Maarten Lankhorstec193642019-06-28 10:55:17 +02005075 pipe_wm->linetime = skl_compute_linetime_wm(crtc_state);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005076
Matt Roper55994c22016-05-12 07:06:08 -07005077 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005078}
5079
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005080static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
5081 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00005082 const struct skl_ddb_entry *entry)
5083{
5084 if (entry->end)
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005085 I915_WRITE_FW(reg, (entry->end - 1) << 16 | entry->start);
Damien Lespiau16160e32014-11-04 17:06:53 +00005086 else
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005087 I915_WRITE_FW(reg, 0);
Damien Lespiau16160e32014-11-04 17:06:53 +00005088}
5089
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005090static void skl_write_wm_level(struct drm_i915_private *dev_priv,
5091 i915_reg_t reg,
5092 const struct skl_wm_level *level)
5093{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005094 u32 val = 0;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005095
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005096 if (level->plane_en)
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005097 val |= PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005098 if (level->ignore_lines)
5099 val |= PLANE_WM_IGNORE_LINES;
5100 val |= level->plane_res_b;
5101 val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005102
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005103 I915_WRITE_FW(reg, val);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005104}
5105
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005106void skl_write_plane_wm(struct intel_plane *plane,
5107 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005108{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005109 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005110 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005111 enum plane_id plane_id = plane->id;
5112 enum pipe pipe = plane->pipe;
5113 const struct skl_plane_wm *wm =
5114 &crtc_state->wm.skl.optimal.planes[plane_id];
5115 const struct skl_ddb_entry *ddb_y =
5116 &crtc_state->wm.skl.plane_ddb_y[plane_id];
5117 const struct skl_ddb_entry *ddb_uv =
5118 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005119
5120 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005121 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005122 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04005123 }
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005124 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005125 &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02005126
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005127 if (INTEL_GEN(dev_priv) >= 11) {
Mahesh Kumar234059d2018-01-30 11:49:13 -02005128 skl_ddb_entry_write(dev_priv,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005129 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5130 return;
Mahesh Kumarb879d582018-04-09 09:11:01 +05305131 }
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005132
5133 if (wm->is_planar)
5134 swap(ddb_y, ddb_uv);
5135
5136 skl_ddb_entry_write(dev_priv,
5137 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5138 skl_ddb_entry_write(dev_priv,
5139 PLANE_NV12_BUF_CFG(pipe, plane_id), ddb_uv);
Lyude62e0fb82016-08-22 12:50:08 -04005140}
5141
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005142void skl_write_cursor_wm(struct intel_plane *plane,
5143 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005144{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005145 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005146 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005147 enum plane_id plane_id = plane->id;
5148 enum pipe pipe = plane->pipe;
5149 const struct skl_plane_wm *wm =
5150 &crtc_state->wm.skl.optimal.planes[plane_id];
5151 const struct skl_ddb_entry *ddb =
5152 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005153
5154 for (level = 0; level <= max_level; level++) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005155 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
5156 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04005157 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005158 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02005159
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005160 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe), ddb);
Lyude62e0fb82016-08-22 12:50:08 -04005161}
5162
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005163bool skl_wm_level_equals(const struct skl_wm_level *l1,
5164 const struct skl_wm_level *l2)
5165{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005166 return l1->plane_en == l2->plane_en &&
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005167 l1->ignore_lines == l2->ignore_lines &&
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005168 l1->plane_res_l == l2->plane_res_l &&
5169 l1->plane_res_b == l2->plane_res_b;
5170}
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005171
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005172static bool skl_plane_wm_equals(struct drm_i915_private *dev_priv,
5173 const struct skl_plane_wm *wm1,
5174 const struct skl_plane_wm *wm2)
5175{
5176 int level, max_level = ilk_wm_max_level(dev_priv);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005177
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005178 for (level = 0; level <= max_level; level++) {
5179 if (!skl_wm_level_equals(&wm1->wm[level], &wm2->wm[level]) ||
5180 !skl_wm_level_equals(&wm1->uv_wm[level], &wm2->uv_wm[level]))
5181 return false;
5182 }
5183
5184 return skl_wm_level_equals(&wm1->trans_wm, &wm2->trans_wm);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005185}
5186
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005187static bool skl_pipe_wm_equals(struct intel_crtc *crtc,
5188 const struct skl_pipe_wm *wm1,
5189 const struct skl_pipe_wm *wm2)
5190{
5191 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5192 enum plane_id plane_id;
5193
5194 for_each_plane_id_on_crtc(crtc, plane_id) {
5195 if (!skl_plane_wm_equals(dev_priv,
5196 &wm1->planes[plane_id],
5197 &wm2->planes[plane_id]))
5198 return false;
5199 }
5200
5201 return wm1->linetime == wm2->linetime;
5202}
5203
Lyude27082492016-08-24 07:48:10 +02005204static inline bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
5205 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005206{
Lyude27082492016-08-24 07:48:10 +02005207 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005208}
5209
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005210bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry *ddb,
Jani Nikula696173b2019-04-05 14:00:15 +03005211 const struct skl_ddb_entry *entries,
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005212 int num_entries, int ignore_idx)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005213{
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005214 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005215
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005216 for (i = 0; i < num_entries; i++) {
5217 if (i != ignore_idx &&
5218 skl_ddb_entries_overlap(ddb, &entries[i]))
Lyude27082492016-08-24 07:48:10 +02005219 return true;
Mika Kahola2b685042017-10-10 13:17:03 +03005220 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005221
Lyude27082492016-08-24 07:48:10 +02005222 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005223}
5224
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005225static u32
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005226pipes_modified(struct intel_atomic_state *state)
Matt Roper9b613022016-06-27 16:42:44 -07005227{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005228 struct intel_crtc *crtc;
Maarten Lankhorstec193642019-06-28 10:55:17 +02005229 struct intel_crtc_state *crtc_state;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005230 u32 i, ret = 0;
Matt Roper9b613022016-06-27 16:42:44 -07005231
Maarten Lankhorstec193642019-06-28 10:55:17 +02005232 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i)
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005233 ret |= drm_crtc_mask(&crtc->base);
Matt Roper9b613022016-06-27 16:42:44 -07005234
5235 return ret;
5236}
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
Matt Roper98d39492016-05-12 07:06:03 -07005413static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005414skl_ddb_add_affected_pipes(struct intel_atomic_state *state, bool *changed)
Matt Roper98d39492016-05-12 07:06:03 -07005415{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005416 struct drm_device *dev = state->base.dev;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305417 const struct drm_i915_private *dev_priv = to_i915(dev);
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005418 struct intel_crtc *crtc;
5419 struct intel_crtc_state *crtc_state;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005420 u32 realloc_pipes = pipes_modified(state);
Matt Roper734fa012016-05-12 15:11:40 -07005421 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07005422
5423 /*
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02005424 * When we distrust bios wm we always need to recompute to set the
5425 * expected DDB allocations for each CRTC.
5426 */
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305427 if (dev_priv->wm.distrust_bios_wm)
5428 (*changed) = true;
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02005429
5430 /*
Matt Roper98d39492016-05-12 07:06:03 -07005431 * If this transaction isn't actually touching any CRTC's, don't
5432 * bother with watermark calculation. Note that if we pass this
5433 * test, we're guaranteed to hold at least one CRTC state mutex,
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03005434 * which means we can safely use values like dev_priv->active_pipes
Matt Roper98d39492016-05-12 07:06:03 -07005435 * since any racing commits that want to update them would need to
5436 * hold _all_ CRTC state mutexes.
5437 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005438 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i)
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305439 (*changed) = true;
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02005440
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305441 if (!*changed)
Matt Roper98d39492016-05-12 07:06:03 -07005442 return 0;
5443
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305444 /*
5445 * If this is our first atomic update following hardware readout,
5446 * we can't trust the DDB that the BIOS programmed for us. Let's
5447 * pretend that all pipes switched active status so that we'll
5448 * ensure a full DDB recompute.
5449 */
5450 if (dev_priv->wm.distrust_bios_wm) {
5451 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005452 state->base.acquire_ctx);
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305453 if (ret)
5454 return ret;
5455
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005456 state->active_pipe_changes = ~0;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305457
5458 /*
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03005459 * We usually only initialize state->active_pipes if we
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305460 * we're doing a modeset; make sure this field is always
5461 * initialized during the sanitization process that happens
5462 * on the first commit too.
5463 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005464 if (!state->modeset)
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03005465 state->active_pipes = dev_priv->active_pipes;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305466 }
5467
5468 /*
5469 * If the modeset changes which CRTC's are active, we need to
5470 * recompute the DDB allocation for *all* active pipes, even
5471 * those that weren't otherwise being modified in any way by this
5472 * atomic commit. Due to the shrinking of the per-pipe allocations
5473 * when new active CRTC's are added, it's possible for a pipe that
5474 * we were already using and aren't changing at all here to suddenly
5475 * become invalid if its DDB needs exceeds its new allocation.
5476 *
5477 * Note that if we wind up doing a full DDB recompute, we can't let
5478 * any other display updates race with this transaction, so we need
5479 * to grab the lock on *all* CRTC's.
5480 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005481 if (state->active_pipe_changes || state->modeset) {
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305482 realloc_pipes = ~0;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005483 state->wm_results.dirty_pipes = ~0;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305484 }
5485
5486 /*
5487 * We're not recomputing for the pipes not included in the commit, so
5488 * make sure we start with the current state.
5489 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005490 for_each_intel_crtc_mask(dev, crtc, realloc_pipes) {
5491 crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
5492 if (IS_ERR(crtc_state))
5493 return PTR_ERR(crtc_state);
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305494 }
5495
5496 return 0;
5497}
5498
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005499/*
5500 * To make sure the cursor watermark registers are always consistent
5501 * with our computed state the following scenario needs special
5502 * treatment:
5503 *
5504 * 1. enable cursor
5505 * 2. move cursor entirely offscreen
5506 * 3. disable cursor
5507 *
5508 * Step 2. does call .disable_plane() but does not zero the watermarks
5509 * (since we consider an offscreen cursor still active for the purposes
5510 * of watermarks). Step 3. would not normally call .disable_plane()
5511 * because the actual plane visibility isn't changing, and we don't
5512 * deallocate the cursor ddb until the pipe gets disabled. So we must
5513 * force step 3. to call .disable_plane() to update the watermark
5514 * registers properly.
5515 *
5516 * Other planes do not suffer from this issues as their watermarks are
5517 * calculated based on the actual plane visibility. The only time this
5518 * can trigger for the other planes is during the initial readout as the
5519 * default value of the watermarks registers is not zero.
5520 */
5521static int skl_wm_add_affected_planes(struct intel_atomic_state *state,
5522 struct intel_crtc *crtc)
5523{
5524 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5525 const struct intel_crtc_state *old_crtc_state =
5526 intel_atomic_get_old_crtc_state(state, crtc);
5527 struct intel_crtc_state *new_crtc_state =
5528 intel_atomic_get_new_crtc_state(state, crtc);
5529 struct intel_plane *plane;
5530
5531 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5532 struct intel_plane_state *plane_state;
5533 enum plane_id plane_id = plane->id;
5534
5535 /*
5536 * Force a full wm update for every plane on modeset.
5537 * Required because the reset value of the wm registers
5538 * is non-zero, whereas we want all disabled planes to
5539 * have zero watermarks. So if we turn off the relevant
5540 * power well the hardware state will go out of sync
5541 * with the software state.
5542 */
5543 if (!drm_atomic_crtc_needs_modeset(&new_crtc_state->base) &&
5544 skl_plane_wm_equals(dev_priv,
5545 &old_crtc_state->wm.skl.optimal.planes[plane_id],
5546 &new_crtc_state->wm.skl.optimal.planes[plane_id]))
5547 continue;
5548
5549 plane_state = intel_atomic_get_plane_state(state, plane);
5550 if (IS_ERR(plane_state))
5551 return PTR_ERR(plane_state);
5552
5553 new_crtc_state->update_planes |= BIT(plane_id);
5554 }
5555
5556 return 0;
5557}
5558
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305559static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005560skl_compute_wm(struct intel_atomic_state *state)
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305561{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005562 struct intel_crtc *crtc;
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02005563 struct intel_crtc_state *new_crtc_state;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005564 struct intel_crtc_state *old_crtc_state;
5565 struct skl_ddb_values *results = &state->wm_results;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305566 bool changed = false;
5567 int ret, i;
5568
Matt Roper734fa012016-05-12 15:11:40 -07005569 /* Clear all dirty flags */
5570 results->dirty_pipes = 0;
5571
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305572 ret = skl_ddb_add_affected_pipes(state, &changed);
5573 if (ret || !changed)
5574 return ret;
5575
Matt Roper734fa012016-05-12 15:11:40 -07005576 /*
5577 * Calculate WM's for all pipes that are part of this transaction.
Matt Roperd8e87492018-12-11 09:31:07 -08005578 * Note that skl_ddb_add_affected_pipes may have added more CRTC's that
Matt Roper734fa012016-05-12 15:11:40 -07005579 * weren't otherwise being modified (and set bits in dirty_pipes) if
5580 * pipe allocations had to change.
Matt Roper734fa012016-05-12 15:11:40 -07005581 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005582 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02005583 new_crtc_state, i) {
5584 ret = skl_build_pipe_wm(new_crtc_state);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005585 if (ret)
5586 return ret;
5587
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005588 ret = skl_wm_add_affected_planes(state, crtc);
Matt Roper734fa012016-05-12 15:11:40 -07005589 if (ret)
5590 return ret;
5591
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02005592 if (!skl_pipe_wm_equals(crtc,
5593 &old_crtc_state->wm.skl.optimal,
5594 &new_crtc_state->wm.skl.optimal))
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005595 results->dirty_pipes |= drm_crtc_mask(&crtc->base);
Matt Roper734fa012016-05-12 15:11:40 -07005596 }
5597
Matt Roperd8e87492018-12-11 09:31:07 -08005598 ret = skl_compute_ddb(state);
5599 if (ret)
5600 return ret;
5601
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005602 skl_print_wm_changes(state);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005603
Matt Roper98d39492016-05-12 07:06:03 -07005604 return 0;
5605}
5606
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005607static void skl_atomic_update_crtc_wm(struct intel_atomic_state *state,
Maarten Lankhorstec193642019-06-28 10:55:17 +02005608 struct intel_crtc_state *crtc_state)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005609{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005610 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005611 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Maarten Lankhorstec193642019-06-28 10:55:17 +02005612 struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005613 enum pipe pipe = crtc->pipe;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005614
5615 if (!(state->wm_results.dirty_pipes & drm_crtc_mask(&crtc->base)))
5616 return;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005617
5618 I915_WRITE(PIPE_WM_LINETIME(pipe), pipe_wm->linetime);
5619}
5620
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005621static void skl_initial_wm(struct intel_atomic_state *state,
Maarten Lankhorstec193642019-06-28 10:55:17 +02005622 struct intel_crtc_state *crtc_state)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005623{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005624 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02005625 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005626 struct drm_i915_private *dev_priv = to_i915(dev);
Mahesh Kumar60f8e872018-04-09 09:11:00 +05305627 struct skl_ddb_values *results = &state->wm_results;
Bob Paauweadda50b2015-07-21 10:42:53 -07005628
Ville Syrjälä432081b2016-10-31 22:37:03 +02005629 if ((results->dirty_pipes & drm_crtc_mask(&intel_crtc->base)) == 0)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005630 return;
5631
Matt Roper734fa012016-05-12 15:11:40 -07005632 mutex_lock(&dev_priv->wm.wm_mutex);
5633
Maarten Lankhorstec193642019-06-28 10:55:17 +02005634 if (crtc_state->base.active_changed)
5635 skl_atomic_update_crtc_wm(state, crtc_state);
Lyude27082492016-08-24 07:48:10 +02005636
Matt Roper734fa012016-05-12 15:11:40 -07005637 mutex_unlock(&dev_priv->wm.wm_mutex);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005638}
5639
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005640static void ilk_compute_wm_config(struct drm_i915_private *dev_priv,
Ville Syrjäläd8905652016-01-14 14:53:35 +02005641 struct intel_wm_config *config)
5642{
5643 struct intel_crtc *crtc;
5644
5645 /* Compute the currently _active_ config */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005646 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläd8905652016-01-14 14:53:35 +02005647 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
5648
5649 if (!wm->pipe_enabled)
5650 continue;
5651
5652 config->sprites_enabled |= wm->sprites_enabled;
5653 config->sprites_scaled |= wm->sprites_scaled;
5654 config->num_pipes_active++;
5655 }
5656}
5657
Matt Ropered4a6a72016-02-23 17:20:13 -08005658static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03005659{
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005660 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02005661 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02005662 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02005663 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005664 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07005665
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005666 ilk_compute_wm_config(dev_priv, &config);
Ville Syrjäläd8905652016-01-14 14:53:35 +02005667
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005668 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_1_2, &max);
5669 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03005670
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005671 /* 5/6 split only in single pipe config on IVB+ */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00005672 if (INTEL_GEN(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02005673 config.num_pipes_active == 1 && config.sprites_enabled) {
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005674 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_5_6, &max);
5675 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005676
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005677 best_lp_wm = ilk_find_best_result(dev_priv, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03005678 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005679 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005680 }
5681
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005682 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005683 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005684
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005685 ilk_compute_wm_results(dev_priv, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03005686
Imre Deak820c1982013-12-17 14:46:36 +02005687 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03005688}
5689
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005690static void ilk_initial_watermarks(struct intel_atomic_state *state,
Maarten Lankhorstec193642019-06-28 10:55:17 +02005691 struct intel_crtc_state *crtc_state)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005692{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005693 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä88016a92019-07-01 19:05:45 +03005694 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005695
Matt Ropered4a6a72016-02-23 17:20:13 -08005696 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03005697 crtc->wm.active.ilk = crtc_state->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08005698 ilk_program_watermarks(dev_priv);
5699 mutex_unlock(&dev_priv->wm.wm_mutex);
5700}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005701
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005702static void ilk_optimize_watermarks(struct intel_atomic_state *state,
Maarten Lankhorstec193642019-06-28 10:55:17 +02005703 struct intel_crtc_state *crtc_state)
Matt Ropered4a6a72016-02-23 17:20:13 -08005704{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005705 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä88016a92019-07-01 19:05:45 +03005706 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
5707
5708 if (!crtc_state->wm.need_postvbl_update)
5709 return;
Matt Ropered4a6a72016-02-23 17:20:13 -08005710
5711 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03005712 crtc->wm.active.ilk = crtc_state->wm.ilk.optimal;
5713 ilk_program_watermarks(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08005714 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005715}
5716
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005717static inline void skl_wm_level_from_reg_val(u32 val,
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005718 struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00005719{
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005720 level->plane_en = val & PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005721 level->ignore_lines = val & PLANE_WM_IGNORE_LINES;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005722 level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
5723 level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
5724 PLANE_WM_LINES_MASK;
Pradeep Bhat30789992014-11-04 17:06:45 +00005725}
5726
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005727void skl_pipe_wm_get_hw_state(struct intel_crtc *crtc,
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005728 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00005729{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005730 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5731 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005732 int level, max_level;
5733 enum plane_id plane_id;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005734 u32 val;
Pradeep Bhat30789992014-11-04 17:06:45 +00005735
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005736 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00005737
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005738 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005739 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00005740
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005741 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005742 if (plane_id != PLANE_CURSOR)
5743 val = I915_READ(PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005744 else
5745 val = I915_READ(CUR_WM(pipe, level));
5746
5747 skl_wm_level_from_reg_val(val, &wm->wm[level]);
5748 }
5749
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005750 if (plane_id != PLANE_CURSOR)
5751 val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005752 else
5753 val = I915_READ(CUR_WM_TRANS(pipe));
5754
5755 skl_wm_level_from_reg_val(val, &wm->trans_wm);
5756 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005757
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005758 if (!crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00005759 return;
5760
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005761 out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00005762}
5763
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005764void skl_wm_get_hw_state(struct drm_i915_private *dev_priv)
Pradeep Bhat30789992014-11-04 17:06:45 +00005765{
Mahesh Kumar60f8e872018-04-09 09:11:00 +05305766 struct skl_ddb_values *hw = &dev_priv->wm.skl_hw;
Damien Lespiaua269c582014-11-04 17:06:49 +00005767 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005768 struct intel_crtc *crtc;
Maarten Lankhorstec193642019-06-28 10:55:17 +02005769 struct intel_crtc_state *crtc_state;
Pradeep Bhat30789992014-11-04 17:06:45 +00005770
Damien Lespiaua269c582014-11-04 17:06:49 +00005771 skl_ddb_get_hw_state(dev_priv, ddb);
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005772 for_each_intel_crtc(&dev_priv->drm, crtc) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02005773 crtc_state = to_intel_crtc_state(crtc->base.state);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005774
Maarten Lankhorstec193642019-06-28 10:55:17 +02005775 skl_pipe_wm_get_hw_state(crtc, &crtc_state->wm.skl.optimal);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005776
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005777 if (crtc->active)
5778 hw->dirty_pipes |= drm_crtc_mask(&crtc->base);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005779 }
Matt Ropera1de91e2016-05-12 07:05:57 -07005780
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03005781 if (dev_priv->active_pipes) {
Matt Roper279e99d2016-05-12 07:06:02 -07005782 /* Fully recompute DDB on first atomic commit */
5783 dev_priv->wm.distrust_bios_wm = true;
Matt Roper279e99d2016-05-12 07:06:02 -07005784 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005785}
5786
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005787static void ilk_pipe_wm_get_hw_state(struct intel_crtc *crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005788{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005789 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005790 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02005791 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Maarten Lankhorstec193642019-06-28 10:55:17 +02005792 struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
5793 struct intel_pipe_wm *active = &crtc_state->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005794 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005795 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005796 [PIPE_A] = WM0_PIPEA_ILK,
5797 [PIPE_B] = WM0_PIPEB_ILK,
5798 [PIPE_C] = WM0_PIPEC_IVB,
5799 };
5800
5801 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005802 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02005803 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005804
Ville Syrjälä15606532016-05-13 17:55:17 +03005805 memset(active, 0, sizeof(*active));
5806
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005807 active->pipe_enabled = crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02005808
5809 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005810 u32 tmp = hw->wm_pipe[pipe];
5811
5812 /*
5813 * For active pipes LP0 watermark is marked as
5814 * enabled, and LP1+ watermaks as disabled since
5815 * we can't really reverse compute them in case
5816 * multiple pipes are active.
5817 */
5818 active->wm[0].enable = true;
5819 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
5820 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
5821 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
5822 active->linetime = hw->wm_linetime[pipe];
5823 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005824 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005825
5826 /*
5827 * For inactive pipes, all watermark levels
5828 * should be marked as enabled but zeroed,
5829 * which is what we'd compute them to.
5830 */
5831 for (level = 0; level <= max_level; level++)
5832 active->wm[level].enable = true;
5833 }
Matt Roper4e0963c2015-09-24 15:53:15 -07005834
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005835 crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005836}
5837
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005838#define _FW_WM(value, plane) \
5839 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
5840#define _FW_WM_VLV(value, plane) \
5841 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
5842
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005843static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
5844 struct g4x_wm_values *wm)
5845{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005846 u32 tmp;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005847
5848 tmp = I915_READ(DSPFW1);
5849 wm->sr.plane = _FW_WM(tmp, SR);
5850 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5851 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
5852 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
5853
5854 tmp = I915_READ(DSPFW2);
5855 wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
5856 wm->sr.fbc = _FW_WM(tmp, FBC_SR);
5857 wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
5858 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
5859 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5860 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
5861
5862 tmp = I915_READ(DSPFW3);
5863 wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
5864 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5865 wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
5866 wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
5867}
5868
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005869static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
5870 struct vlv_wm_values *wm)
5871{
5872 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005873 u32 tmp;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005874
5875 for_each_pipe(dev_priv, pipe) {
5876 tmp = I915_READ(VLV_DDL(pipe));
5877
Ville Syrjälä1b313892016-11-28 19:37:08 +02005878 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005879 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005880 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005881 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005882 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005883 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005884 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005885 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
5886 }
5887
5888 tmp = I915_READ(DSPFW1);
5889 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005890 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5891 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
5892 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005893
5894 tmp = I915_READ(DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005895 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
5896 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5897 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005898
5899 tmp = I915_READ(DSPFW3);
5900 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5901
5902 if (IS_CHERRYVIEW(dev_priv)) {
5903 tmp = I915_READ(DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005904 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5905 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005906
5907 tmp = I915_READ(DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005908 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
5909 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005910
5911 tmp = I915_READ(DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005912 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
5913 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005914
5915 tmp = I915_READ(DSPHOWM);
5916 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005917 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
5918 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
5919 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
5920 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5921 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5922 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5923 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5924 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5925 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005926 } else {
5927 tmp = I915_READ(DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005928 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5929 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005930
5931 tmp = I915_READ(DSPHOWM);
5932 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005933 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5934 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5935 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5936 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5937 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5938 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005939 }
5940}
5941
5942#undef _FW_WM
5943#undef _FW_WM_VLV
5944
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005945void g4x_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005946{
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005947 struct g4x_wm_values *wm = &dev_priv->wm.g4x;
5948 struct intel_crtc *crtc;
5949
5950 g4x_read_wm_values(dev_priv, wm);
5951
5952 wm->cxsr = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
5953
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005954 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005955 struct intel_crtc_state *crtc_state =
5956 to_intel_crtc_state(crtc->base.state);
5957 struct g4x_wm_state *active = &crtc->wm.active.g4x;
5958 struct g4x_pipe_wm *raw;
5959 enum pipe pipe = crtc->pipe;
5960 enum plane_id plane_id;
5961 int level, max_level;
5962
5963 active->cxsr = wm->cxsr;
5964 active->hpll_en = wm->hpll_en;
5965 active->fbc_en = wm->fbc_en;
5966
5967 active->sr = wm->sr;
5968 active->hpll = wm->hpll;
5969
5970 for_each_plane_id_on_crtc(crtc, plane_id) {
5971 active->wm.plane[plane_id] =
5972 wm->pipe[pipe].plane[plane_id];
5973 }
5974
5975 if (wm->cxsr && wm->hpll_en)
5976 max_level = G4X_WM_LEVEL_HPLL;
5977 else if (wm->cxsr)
5978 max_level = G4X_WM_LEVEL_SR;
5979 else
5980 max_level = G4X_WM_LEVEL_NORMAL;
5981
5982 level = G4X_WM_LEVEL_NORMAL;
5983 raw = &crtc_state->wm.g4x.raw[level];
5984 for_each_plane_id_on_crtc(crtc, plane_id)
5985 raw->plane[plane_id] = active->wm.plane[plane_id];
5986
5987 if (++level > max_level)
5988 goto out;
5989
5990 raw = &crtc_state->wm.g4x.raw[level];
5991 raw->plane[PLANE_PRIMARY] = active->sr.plane;
5992 raw->plane[PLANE_CURSOR] = active->sr.cursor;
5993 raw->plane[PLANE_SPRITE0] = 0;
5994 raw->fbc = active->sr.fbc;
5995
5996 if (++level > max_level)
5997 goto out;
5998
5999 raw = &crtc_state->wm.g4x.raw[level];
6000 raw->plane[PLANE_PRIMARY] = active->hpll.plane;
6001 raw->plane[PLANE_CURSOR] = active->hpll.cursor;
6002 raw->plane[PLANE_SPRITE0] = 0;
6003 raw->fbc = active->hpll.fbc;
6004
6005 out:
6006 for_each_plane_id_on_crtc(crtc, plane_id)
6007 g4x_raw_plane_wm_set(crtc_state, level,
6008 plane_id, USHRT_MAX);
6009 g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
6010
6011 crtc_state->wm.g4x.optimal = *active;
6012 crtc_state->wm.g4x.intermediate = *active;
6013
6014 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
6015 pipe_name(pipe),
6016 wm->pipe[pipe].plane[PLANE_PRIMARY],
6017 wm->pipe[pipe].plane[PLANE_CURSOR],
6018 wm->pipe[pipe].plane[PLANE_SPRITE0]);
6019 }
6020
6021 DRM_DEBUG_KMS("Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
6022 wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
6023 DRM_DEBUG_KMS("Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
6024 wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
6025 DRM_DEBUG_KMS("Initial SR=%s HPLL=%s FBC=%s\n",
6026 yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
6027}
6028
6029void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
6030{
6031 struct intel_plane *plane;
6032 struct intel_crtc *crtc;
6033
6034 mutex_lock(&dev_priv->wm.wm_mutex);
6035
6036 for_each_intel_plane(&dev_priv->drm, plane) {
6037 struct intel_crtc *crtc =
6038 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
6039 struct intel_crtc_state *crtc_state =
6040 to_intel_crtc_state(crtc->base.state);
6041 struct intel_plane_state *plane_state =
6042 to_intel_plane_state(plane->base.state);
6043 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
6044 enum plane_id plane_id = plane->id;
6045 int level;
6046
6047 if (plane_state->base.visible)
6048 continue;
6049
6050 for (level = 0; level < 3; level++) {
6051 struct g4x_pipe_wm *raw =
6052 &crtc_state->wm.g4x.raw[level];
6053
6054 raw->plane[plane_id] = 0;
6055 wm_state->wm.plane[plane_id] = 0;
6056 }
6057
6058 if (plane_id == PLANE_PRIMARY) {
6059 for (level = 0; level < 3; level++) {
6060 struct g4x_pipe_wm *raw =
6061 &crtc_state->wm.g4x.raw[level];
6062 raw->fbc = 0;
6063 }
6064
6065 wm_state->sr.fbc = 0;
6066 wm_state->hpll.fbc = 0;
6067 wm_state->fbc_en = false;
6068 }
6069 }
6070
6071 for_each_intel_crtc(&dev_priv->drm, crtc) {
6072 struct intel_crtc_state *crtc_state =
6073 to_intel_crtc_state(crtc->base.state);
6074
6075 crtc_state->wm.g4x.intermediate =
6076 crtc_state->wm.g4x.optimal;
6077 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
6078 }
6079
6080 g4x_program_watermarks(dev_priv);
6081
6082 mutex_unlock(&dev_priv->wm.wm_mutex);
6083}
6084
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006085void vlv_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006086{
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006087 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02006088 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006089 u32 val;
6090
6091 vlv_read_wm_values(dev_priv, wm);
6092
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006093 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
6094 wm->level = VLV_WM_LEVEL_PM2;
6095
6096 if (IS_CHERRYVIEW(dev_priv)) {
Chris Wilson337fa6e2019-04-26 09:17:20 +01006097 vlv_punit_get(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006098
Ville Syrjäläc11b8132018-11-29 19:55:03 +02006099 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006100 if (val & DSP_MAXFIFO_PM5_ENABLE)
6101 wm->level = VLV_WM_LEVEL_PM5;
6102
Ville Syrjälä58590c12015-09-08 21:05:12 +03006103 /*
6104 * If DDR DVFS is disabled in the BIOS, Punit
6105 * will never ack the request. So if that happens
6106 * assume we don't have to enable/disable DDR DVFS
6107 * dynamically. To test that just set the REQ_ACK
6108 * bit to poke the Punit, but don't change the
6109 * HIGH/LOW bits so that we don't actually change
6110 * the current state.
6111 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006112 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03006113 val |= FORCE_DDR_FREQ_REQ_ACK;
6114 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
6115
6116 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
6117 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
6118 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
6119 "assuming DDR DVFS is disabled\n");
6120 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
6121 } else {
6122 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
6123 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
6124 wm->level = VLV_WM_LEVEL_DDR_DVFS;
6125 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006126
Chris Wilson337fa6e2019-04-26 09:17:20 +01006127 vlv_punit_put(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006128 }
6129
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006130 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02006131 struct intel_crtc_state *crtc_state =
6132 to_intel_crtc_state(crtc->base.state);
6133 struct vlv_wm_state *active = &crtc->wm.active.vlv;
6134 const struct vlv_fifo_state *fifo_state =
6135 &crtc_state->wm.vlv.fifo_state;
6136 enum pipe pipe = crtc->pipe;
6137 enum plane_id plane_id;
6138 int level;
6139
6140 vlv_get_fifo_size(crtc_state);
6141
6142 active->num_levels = wm->level + 1;
6143 active->cxsr = wm->cxsr;
6144
Ville Syrjäläff32c542017-03-02 19:14:57 +02006145 for (level = 0; level < active->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03006146 struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02006147 &crtc_state->wm.vlv.raw[level];
6148
6149 active->sr[level].plane = wm->sr.plane;
6150 active->sr[level].cursor = wm->sr.cursor;
6151
6152 for_each_plane_id_on_crtc(crtc, plane_id) {
6153 active->wm[level].plane[plane_id] =
6154 wm->pipe[pipe].plane[plane_id];
6155
6156 raw->plane[plane_id] =
6157 vlv_invert_wm_value(active->wm[level].plane[plane_id],
6158 fifo_state->plane[plane_id]);
6159 }
6160 }
6161
6162 for_each_plane_id_on_crtc(crtc, plane_id)
6163 vlv_raw_plane_wm_set(crtc_state, level,
6164 plane_id, USHRT_MAX);
6165 vlv_invalidate_wms(crtc, active, level);
6166
6167 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02006168 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02006169
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006170 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 +02006171 pipe_name(pipe),
6172 wm->pipe[pipe].plane[PLANE_PRIMARY],
6173 wm->pipe[pipe].plane[PLANE_CURSOR],
6174 wm->pipe[pipe].plane[PLANE_SPRITE0],
6175 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02006176 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006177
6178 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
6179 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
6180}
6181
Ville Syrjälä602ae832017-03-02 19:15:02 +02006182void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
6183{
6184 struct intel_plane *plane;
6185 struct intel_crtc *crtc;
6186
6187 mutex_lock(&dev_priv->wm.wm_mutex);
6188
6189 for_each_intel_plane(&dev_priv->drm, plane) {
6190 struct intel_crtc *crtc =
6191 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
6192 struct intel_crtc_state *crtc_state =
6193 to_intel_crtc_state(crtc->base.state);
6194 struct intel_plane_state *plane_state =
6195 to_intel_plane_state(plane->base.state);
6196 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
6197 const struct vlv_fifo_state *fifo_state =
6198 &crtc_state->wm.vlv.fifo_state;
6199 enum plane_id plane_id = plane->id;
6200 int level;
6201
6202 if (plane_state->base.visible)
6203 continue;
6204
6205 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03006206 struct g4x_pipe_wm *raw =
Ville Syrjälä602ae832017-03-02 19:15:02 +02006207 &crtc_state->wm.vlv.raw[level];
6208
6209 raw->plane[plane_id] = 0;
6210
6211 wm_state->wm[level].plane[plane_id] =
6212 vlv_invert_wm_value(raw->plane[plane_id],
6213 fifo_state->plane[plane_id]);
6214 }
6215 }
6216
6217 for_each_intel_crtc(&dev_priv->drm, crtc) {
6218 struct intel_crtc_state *crtc_state =
6219 to_intel_crtc_state(crtc->base.state);
6220
6221 crtc_state->wm.vlv.intermediate =
6222 crtc_state->wm.vlv.optimal;
6223 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
6224 }
6225
6226 vlv_program_watermarks(dev_priv);
6227
6228 mutex_unlock(&dev_priv->wm.wm_mutex);
6229}
6230
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02006231/*
6232 * FIXME should probably kill this and improve
6233 * the real watermark readout/sanitation instead
6234 */
6235static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
6236{
6237 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
6238 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
6239 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
6240
6241 /*
6242 * Don't touch WM1S_LP_EN here.
6243 * Doing so could cause underruns.
6244 */
6245}
6246
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006247void ilk_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006248{
Imre Deak820c1982013-12-17 14:46:36 +02006249 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006250 struct intel_crtc *crtc;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006251
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02006252 ilk_init_lp_watermarks(dev_priv);
6253
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006254 for_each_intel_crtc(&dev_priv->drm, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006255 ilk_pipe_wm_get_hw_state(crtc);
6256
6257 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
6258 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
6259 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
6260
6261 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00006262 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjäläcfa76982014-03-07 18:32:08 +02006263 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
6264 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
6265 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006266
Tvrtko Ursulin86527442016-10-13 11:03:00 +01006267 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02006268 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
6269 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01006270 else if (IS_IVYBRIDGE(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02006271 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
6272 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006273
6274 hw->enable_fbc_wm =
6275 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
6276}
6277
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006278/**
6279 * intel_update_watermarks - update FIFO watermark values based on current modes
Chris Wilson31383412018-02-14 14:03:03 +00006280 * @crtc: the #intel_crtc on which to compute the WM
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006281 *
6282 * Calculate watermark values for the various WM regs based on current mode
6283 * and plane configuration.
6284 *
6285 * There are several cases to deal with here:
6286 * - normal (i.e. non-self-refresh)
6287 * - self-refresh (SR) mode
6288 * - lines are large relative to FIFO size (buffer can hold up to 2)
6289 * - lines are small relative to FIFO size (buffer can hold more than 2
6290 * lines), so need to account for TLB latency
6291 *
6292 * The normal calculation is:
6293 * watermark = dotclock * bytes per pixel * latency
6294 * where latency is platform & configuration dependent (we assume pessimal
6295 * values here).
6296 *
6297 * The SR calculation is:
6298 * watermark = (trunc(latency/line time)+1) * surface width *
6299 * bytes per pixel
6300 * where
6301 * line time = htotal / dotclock
6302 * surface width = hdisplay for normal plane and 64 for cursor
6303 * and latency is assumed to be high, as above.
6304 *
6305 * The final value programmed to the register should always be rounded up,
6306 * and include an extra 2 entries to account for clock crossings.
6307 *
6308 * We don't use the sprite, so we can ignore that. And on Crestline we have
6309 * to set the non-SR watermarks to 8.
6310 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02006311void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006312{
Ville Syrjälä432081b2016-10-31 22:37:03 +02006313 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006314
6315 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03006316 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006317}
6318
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306319void intel_enable_ipc(struct drm_i915_private *dev_priv)
6320{
6321 u32 val;
6322
José Roberto de Souzafd847b82018-09-18 13:47:11 -07006323 if (!HAS_IPC(dev_priv))
6324 return;
6325
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306326 val = I915_READ(DISP_ARB_CTL2);
6327
6328 if (dev_priv->ipc_enabled)
6329 val |= DISP_IPC_ENABLE;
6330 else
6331 val &= ~DISP_IPC_ENABLE;
6332
6333 I915_WRITE(DISP_ARB_CTL2, val);
6334}
6335
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03006336static bool intel_can_enable_ipc(struct drm_i915_private *dev_priv)
6337{
6338 /* Display WA #0477 WaDisableIPC: skl */
6339 if (IS_SKYLAKE(dev_priv))
6340 return false;
6341
6342 /* Display WA #1141: SKL:all KBL:all CFL */
6343 if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv))
6344 return dev_priv->dram_info.symmetric_memory;
6345
6346 return true;
6347}
6348
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306349void intel_init_ipc(struct drm_i915_private *dev_priv)
6350{
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306351 if (!HAS_IPC(dev_priv))
6352 return;
6353
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03006354 dev_priv->ipc_enabled = intel_can_enable_ipc(dev_priv);
José Roberto de Souzac9b818d2018-09-18 13:47:13 -07006355
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306356 intel_enable_ipc(dev_priv);
6357}
6358
Jani Nikulae2828912016-01-18 09:19:47 +02006359/*
Daniel Vetter92703882012-08-09 16:46:01 +02006360 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02006361 */
6362DEFINE_SPINLOCK(mchdev_lock);
6363
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006364bool ironlake_set_drps(struct drm_i915_private *i915, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006365{
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006366 struct intel_uncore *uncore = &i915->uncore;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006367 u16 rgvswctl;
6368
Chris Wilson67520412017-03-02 13:28:01 +00006369 lockdep_assert_held(&mchdev_lock);
Daniel Vetter92703882012-08-09 16:46:01 +02006370
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006371 rgvswctl = intel_uncore_read16(uncore, MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006372 if (rgvswctl & MEMCTL_CMD_STS) {
6373 DRM_DEBUG("gpu busy, RCS change rejected\n");
6374 return false; /* still busy with another command */
6375 }
6376
6377 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
6378 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006379 intel_uncore_write16(uncore, MEMSWCTL, rgvswctl);
6380 intel_uncore_posting_read16(uncore, MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006381
6382 rgvswctl |= MEMCTL_CMD_STS;
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006383 intel_uncore_write16(uncore, MEMSWCTL, rgvswctl);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006384
6385 return true;
6386}
6387
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006388static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006389{
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006390 struct intel_uncore *uncore = &dev_priv->uncore;
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00006391 u32 rgvmodectl;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006392 u8 fmax, fmin, fstart, vstart;
6393
Daniel Vetter92703882012-08-09 16:46:01 +02006394 spin_lock_irq(&mchdev_lock);
6395
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006396 rgvmodectl = intel_uncore_read(uncore, MEMMODECTL);
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00006397
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006398 /* Enable temp reporting */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006399 intel_uncore_write16(uncore, PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
6400 intel_uncore_write16(uncore, TSC1, I915_READ(TSC1) | TSE);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006401
6402 /* 100ms RC evaluation intervals */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006403 intel_uncore_write(uncore, RCUPEI, 100000);
6404 intel_uncore_write(uncore, RCDNEI, 100000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006405
6406 /* Set max/min thresholds to 90ms and 80ms respectively */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006407 intel_uncore_write(uncore, RCBMAXAVG, 90000);
6408 intel_uncore_write(uncore, RCBMINAVG, 80000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006409
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006410 intel_uncore_write(uncore, MEMIHYST, 1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006411
6412 /* Set up min, max, and cur for interrupt handling */
6413 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
6414 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
6415 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
6416 MEMMODE_FSTART_SHIFT;
6417
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006418 vstart = (intel_uncore_read(uncore, PXVFREQ(fstart)) &
6419 PXVFREQ_PX_MASK) >> PXVFREQ_PX_SHIFT;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006420
Daniel Vetter20e4d402012-08-08 23:35:39 +02006421 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
6422 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006423
Daniel Vetter20e4d402012-08-08 23:35:39 +02006424 dev_priv->ips.max_delay = fstart;
6425 dev_priv->ips.min_delay = fmin;
6426 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006427
6428 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
6429 fmax, fmin, fstart);
6430
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006431 intel_uncore_write(uncore,
6432 MEMINTREN,
6433 MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006434
6435 /*
6436 * Interrupts will be enabled in ironlake_irq_postinstall
6437 */
6438
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006439 intel_uncore_write(uncore, VIDSTART, vstart);
6440 intel_uncore_posting_read(uncore, VIDSTART);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006441
6442 rgvmodectl |= MEMMODE_SWMODE_EN;
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006443 intel_uncore_write(uncore, MEMMODECTL, rgvmodectl);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006444
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006445 if (wait_for_atomic((intel_uncore_read(uncore, MEMSWCTL) &
6446 MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006447 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02006448 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006449
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006450 ironlake_set_drps(dev_priv, fstart);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006451
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006452 dev_priv->ips.last_count1 =
6453 intel_uncore_read(uncore, DMIEC) +
6454 intel_uncore_read(uncore, DDREC) +
6455 intel_uncore_read(uncore, CSIEC);
Daniel Vetter20e4d402012-08-08 23:35:39 +02006456 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006457 dev_priv->ips.last_count2 = intel_uncore_read(uncore, GFXEC);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00006458 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02006459
6460 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006461}
6462
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006463static void ironlake_disable_drps(struct drm_i915_private *i915)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006464{
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006465 struct intel_uncore *uncore = &i915->uncore;
Daniel Vetter92703882012-08-09 16:46:01 +02006466 u16 rgvswctl;
6467
6468 spin_lock_irq(&mchdev_lock);
6469
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006470 rgvswctl = intel_uncore_read16(uncore, MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006471
6472 /* Ack interrupts, disable EFC interrupt */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006473 intel_uncore_write(uncore,
6474 MEMINTREN,
6475 intel_uncore_read(uncore, MEMINTREN) &
6476 ~MEMINT_EVAL_CHG_EN);
6477 intel_uncore_write(uncore, MEMINTRSTS, MEMINT_EVAL_CHG);
6478 intel_uncore_write(uncore,
6479 DEIER,
6480 intel_uncore_read(uncore, DEIER) & ~DE_PCU_EVENT);
6481 intel_uncore_write(uncore, DEIIR, DE_PCU_EVENT);
6482 intel_uncore_write(uncore,
6483 DEIMR,
6484 intel_uncore_read(uncore, DEIMR) | DE_PCU_EVENT);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006485
6486 /* Go back to the starting frequency */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006487 ironlake_set_drps(i915, i915->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02006488 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006489 rgvswctl |= MEMCTL_CMD_STS;
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006490 intel_uncore_write(uncore, MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02006491 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006492
Daniel Vetter92703882012-08-09 16:46:01 +02006493 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006494}
6495
Daniel Vetteracbe9472012-07-26 11:50:05 +02006496/* There's a funny hw issue where the hw returns all 0 when reading from
6497 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
6498 * ourselves, instead of doing a rmw cycle (which might result in us clearing
6499 * all limits and the gpu stuck at whatever frequency it is at atm).
6500 */
Akash Goel74ef1172015-03-06 11:07:19 +05306501static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006502{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006503 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006504 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006505
Daniel Vetter20b46e52012-07-26 11:16:14 +02006506 /* Only set the down limit when we've reached the lowest level to avoid
6507 * getting more interrupts, otherwise leave this clear. This prevents a
6508 * race in the hw when coming out of rc6: There's a tiny window where
6509 * the hw runs at the minimal clock before selecting the desired
6510 * frequency, if the down threshold expires in that window we will not
6511 * receive a down interrupt. */
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006512 if (INTEL_GEN(dev_priv) >= 9) {
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006513 limits = (rps->max_freq_softlimit) << 23;
6514 if (val <= rps->min_freq_softlimit)
6515 limits |= (rps->min_freq_softlimit) << 14;
Akash Goel74ef1172015-03-06 11:07:19 +05306516 } else {
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006517 limits = rps->max_freq_softlimit << 24;
6518 if (val <= rps->min_freq_softlimit)
6519 limits |= rps->min_freq_softlimit << 16;
Akash Goel74ef1172015-03-06 11:07:19 +05306520 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02006521
6522 return limits;
6523}
6524
Chris Wilson60548c52018-07-31 14:26:29 +01006525static void rps_set_power(struct drm_i915_private *dev_priv, int new_power)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006526{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006527 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Akash Goel8a586432015-03-06 11:07:18 +05306528 u32 threshold_up = 0, threshold_down = 0; /* in % */
6529 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006530
Chris Wilson60548c52018-07-31 14:26:29 +01006531 lockdep_assert_held(&rps->power.mutex);
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006532
Chris Wilson60548c52018-07-31 14:26:29 +01006533 if (new_power == rps->power.mode)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006534 return;
6535
6536 /* Note the units here are not exactly 1us, but 1280ns. */
6537 switch (new_power) {
6538 case LOW_POWER:
6539 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05306540 ei_up = 16000;
6541 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006542
6543 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306544 ei_down = 32000;
6545 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006546 break;
6547
6548 case BETWEEN:
6549 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05306550 ei_up = 13000;
6551 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006552
6553 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306554 ei_down = 32000;
6555 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006556 break;
6557
6558 case HIGH_POWER:
6559 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05306560 ei_up = 10000;
6561 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006562
6563 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306564 ei_down = 32000;
6565 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006566 break;
6567 }
6568
Mika Kuoppala6067a272017-02-15 15:52:59 +02006569 /* When byt can survive without system hang with dynamic
6570 * sw freq adjustments, this restriction can be lifted.
6571 */
6572 if (IS_VALLEYVIEW(dev_priv))
6573 goto skip_hw_write;
6574
Akash Goel8a586432015-03-06 11:07:18 +05306575 I915_WRITE(GEN6_RP_UP_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01006576 GT_INTERVAL_FROM_US(dev_priv, ei_up));
Akash Goel8a586432015-03-06 11:07:18 +05306577 I915_WRITE(GEN6_RP_UP_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01006578 GT_INTERVAL_FROM_US(dev_priv,
6579 ei_up * threshold_up / 100));
Akash Goel8a586432015-03-06 11:07:18 +05306580
6581 I915_WRITE(GEN6_RP_DOWN_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01006582 GT_INTERVAL_FROM_US(dev_priv, ei_down));
Akash Goel8a586432015-03-06 11:07:18 +05306583 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01006584 GT_INTERVAL_FROM_US(dev_priv,
6585 ei_down * threshold_down / 100));
Akash Goel8a586432015-03-06 11:07:18 +05306586
Chris Wilsona72b5622016-07-02 15:35:59 +01006587 I915_WRITE(GEN6_RP_CONTROL,
Mika Kuoppala1071d0f2019-04-10 16:24:36 +03006588 (INTEL_GEN(dev_priv) > 9 ? 0 : GEN6_RP_MEDIA_TURBO) |
Chris Wilsona72b5622016-07-02 15:35:59 +01006589 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6590 GEN6_RP_MEDIA_IS_GFX |
6591 GEN6_RP_ENABLE |
6592 GEN6_RP_UP_BUSY_AVG |
6593 GEN6_RP_DOWN_IDLE_AVG);
Akash Goel8a586432015-03-06 11:07:18 +05306594
Mika Kuoppala6067a272017-02-15 15:52:59 +02006595skip_hw_write:
Chris Wilson60548c52018-07-31 14:26:29 +01006596 rps->power.mode = new_power;
6597 rps->power.up_threshold = threshold_up;
6598 rps->power.down_threshold = threshold_down;
6599}
6600
6601static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
6602{
6603 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6604 int new_power;
6605
6606 new_power = rps->power.mode;
6607 switch (rps->power.mode) {
6608 case LOW_POWER:
6609 if (val > rps->efficient_freq + 1 &&
6610 val > rps->cur_freq)
6611 new_power = BETWEEN;
6612 break;
6613
6614 case BETWEEN:
6615 if (val <= rps->efficient_freq &&
6616 val < rps->cur_freq)
6617 new_power = LOW_POWER;
6618 else if (val >= rps->rp0_freq &&
6619 val > rps->cur_freq)
6620 new_power = HIGH_POWER;
6621 break;
6622
6623 case HIGH_POWER:
6624 if (val < (rps->rp1_freq + rps->rp0_freq) >> 1 &&
6625 val < rps->cur_freq)
6626 new_power = BETWEEN;
6627 break;
6628 }
6629 /* Max/min bins are special */
6630 if (val <= rps->min_freq_softlimit)
6631 new_power = LOW_POWER;
6632 if (val >= rps->max_freq_softlimit)
6633 new_power = HIGH_POWER;
6634
6635 mutex_lock(&rps->power.mutex);
6636 if (rps->power.interactive)
6637 new_power = HIGH_POWER;
6638 rps_set_power(dev_priv, new_power);
6639 mutex_unlock(&rps->power.mutex);
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006640}
6641
Chris Wilson60548c52018-07-31 14:26:29 +01006642void intel_rps_mark_interactive(struct drm_i915_private *i915, bool interactive)
6643{
6644 struct intel_rps *rps = &i915->gt_pm.rps;
6645
6646 if (INTEL_GEN(i915) < 6)
6647 return;
6648
6649 mutex_lock(&rps->power.mutex);
6650 if (interactive) {
6651 if (!rps->power.interactive++ && READ_ONCE(i915->gt.awake))
6652 rps_set_power(i915, HIGH_POWER);
6653 } else {
6654 GEM_BUG_ON(!rps->power.interactive);
6655 rps->power.interactive--;
6656 }
6657 mutex_unlock(&rps->power.mutex);
6658}
6659
Chris Wilson2876ce72014-03-28 08:03:34 +00006660static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
6661{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006662 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Chris Wilson2876ce72014-03-28 08:03:34 +00006663 u32 mask = 0;
6664
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006665 /* We use UP_EI_EXPIRED interupts for both up/down in manual mode */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006666 if (val > rps->min_freq_softlimit)
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006667 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 +01006668 if (val < rps->max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00006669 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00006670
Chris Wilson7b3c29f2014-07-10 20:31:19 +01006671 mask &= dev_priv->pm_rps_events;
6672
Imre Deak59d02a12014-12-19 19:33:26 +02006673 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00006674}
6675
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006676/* gen6_set_rps is called to update the frequency request, but should also be
6677 * called when the range (min_delay and max_delay) is modified so that we can
6678 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006679static int gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02006680{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006681 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6682
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006683 /* min/max delay may still have been modified so be sure to
6684 * write the limits value.
6685 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006686 if (val != rps->cur_freq) {
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006687 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006688
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006689 if (INTEL_GEN(dev_priv) >= 9)
Akash Goel57041952015-03-06 11:07:17 +05306690 I915_WRITE(GEN6_RPNSWREQ,
6691 GEN9_FREQUENCY(val));
Chris Wilsondc979972016-05-10 14:10:04 +01006692 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006693 I915_WRITE(GEN6_RPNSWREQ,
6694 HSW_FREQUENCY(val));
6695 else
6696 I915_WRITE(GEN6_RPNSWREQ,
6697 GEN6_FREQUENCY(val) |
6698 GEN6_OFFSET(0) |
6699 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006700 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006701
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006702 /* Make sure we continue to get interrupts
6703 * until we hit the minimum or maximum frequencies.
6704 */
Akash Goel74ef1172015-03-06 11:07:19 +05306705 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00006706 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006707
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006708 rps->cur_freq = val;
Mika Kuoppala0f945922015-11-17 18:14:26 +02006709 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006710
6711 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006712}
6713
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006714static int valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006715{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006716 int err;
6717
Chris Wilsondc979972016-05-10 14:10:04 +01006718 if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006719 "Odd GPU freq value\n"))
6720 val &= ~1;
6721
Deepak Scd25dd52015-07-10 18:31:40 +05306722 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
6723
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006724 if (val != dev_priv->gt_pm.rps.cur_freq) {
Chris Wilson337fa6e2019-04-26 09:17:20 +01006725 vlv_punit_get(dev_priv);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006726 err = vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
Chris Wilson337fa6e2019-04-26 09:17:20 +01006727 vlv_punit_put(dev_priv);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006728 if (err)
6729 return err;
6730
Chris Wilsondb4c5e02017-02-10 15:03:46 +00006731 gen6_set_rps_thresholds(dev_priv, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01006732 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006733
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006734 dev_priv->gt_pm.rps.cur_freq = val;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006735 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006736
6737 return 0;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006738}
6739
Deepak Sa7f6e232015-05-09 18:04:44 +05306740/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05306741 *
6742 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05306743 * 1. Forcewake Media well.
6744 * 2. Request idle freq.
6745 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05306746*/
6747static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
6748{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006749 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6750 u32 val = rps->idle_freq;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006751 int err;
Deepak S5549d252014-06-28 11:26:11 +05306752
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006753 if (rps->cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05306754 return;
6755
Chris Wilsonc9efef72017-01-02 15:28:45 +00006756 /* The punit delays the write of the frequency and voltage until it
6757 * determines the GPU is awake. During normal usage we don't want to
6758 * waste power changing the frequency if the GPU is sleeping (rc6).
6759 * However, the GPU and driver is now idle and we do not want to delay
6760 * switching to minimum voltage (reducing power whilst idle) as we do
6761 * not expect to be woken in the near future and so must flush the
6762 * change by waking the device.
6763 *
6764 * We choose to take the media powerwell (either would do to trick the
6765 * punit into committing the voltage change) as that takes a lot less
6766 * power than the render powerwell.
6767 */
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07006768 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006769 err = valleyview_set_rps(dev_priv, val);
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07006770 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006771
6772 if (err)
6773 DRM_ERROR("Failed to set RPS for idle\n");
Deepak S76c3552f2014-01-30 23:08:16 +05306774}
6775
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006776void gen6_rps_busy(struct drm_i915_private *dev_priv)
6777{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006778 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6779
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006780 mutex_lock(&rps->lock);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006781 if (rps->enabled) {
Chris Wilsonbd648182017-02-10 15:03:48 +00006782 u8 freq;
6783
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006784 if (dev_priv->pm_rps_events & GEN6_PM_RP_UP_EI_EXPIRED)
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006785 gen6_rps_reset_ei(dev_priv);
6786 I915_WRITE(GEN6_PMINTRMSK,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006787 gen6_rps_pm_mask(dev_priv, rps->cur_freq));
Michał Winiarski2b83c4c2016-06-20 11:58:27 +02006788
Chris Wilsonc33d2472016-07-04 08:08:36 +01006789 gen6_enable_rps_interrupts(dev_priv);
6790
Chris Wilsonbd648182017-02-10 15:03:48 +00006791 /* Use the user's desired frequency as a guide, but for better
6792 * performance, jump directly to RPe as our starting frequency.
6793 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006794 freq = max(rps->cur_freq,
6795 rps->efficient_freq);
Chris Wilsonbd648182017-02-10 15:03:48 +00006796
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006797 if (intel_set_rps(dev_priv,
Chris Wilsonbd648182017-02-10 15:03:48 +00006798 clamp(freq,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006799 rps->min_freq_softlimit,
6800 rps->max_freq_softlimit)))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006801 DRM_DEBUG_DRIVER("Failed to set idle frequency\n");
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006802 }
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006803 mutex_unlock(&rps->lock);
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006804}
6805
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006806void gen6_rps_idle(struct drm_i915_private *dev_priv)
6807{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006808 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6809
Chris Wilsonc33d2472016-07-04 08:08:36 +01006810 /* Flush our bottom-half so that it does not race with us
6811 * setting the idle frequency and so that it is bounded by
6812 * our rpm wakeref. And then disable the interrupts to stop any
6813 * futher RPS reclocking whilst we are asleep.
6814 */
6815 gen6_disable_rps_interrupts(dev_priv);
6816
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006817 mutex_lock(&rps->lock);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006818 if (rps->enabled) {
Chris Wilsondc979972016-05-10 14:10:04 +01006819 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Deepak S76c3552f2014-01-30 23:08:16 +05306820 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02006821 else
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006822 gen6_set_rps(dev_priv, rps->idle_freq);
6823 rps->last_adj = 0;
Ville Syrjälä12c100b2016-05-23 17:42:48 +03006824 I915_WRITE(GEN6_PMINTRMSK,
6825 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Chris Wilsonc0951f02013-10-10 21:58:50 +01006826 }
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006827 mutex_unlock(&rps->lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006828}
6829
Chris Wilson62eb3c22019-02-13 09:25:04 +00006830void gen6_rps_boost(struct i915_request *rq)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006831{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006832 struct intel_rps *rps = &rq->i915->gt_pm.rps;
Chris Wilson74d290f2017-08-17 13:37:06 +01006833 unsigned long flags;
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006834 bool boost;
6835
Chris Wilson8d3afd72015-05-21 21:01:47 +01006836 /* This is intentionally racy! We peek at the state here, then
6837 * validate inside the RPS worker.
6838 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006839 if (!rps->enabled)
Chris Wilson8d3afd72015-05-21 21:01:47 +01006840 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006841
Chris Wilson0e218342019-01-21 22:21:02 +00006842 if (i915_request_signaled(rq))
Chris Wilson253a2812018-02-06 14:31:37 +00006843 return;
6844
Chris Wilsone61e0f52018-02-21 09:56:36 +00006845 /* Serializes with i915_request_retire() */
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006846 boost = false;
Chris Wilson74d290f2017-08-17 13:37:06 +01006847 spin_lock_irqsave(&rq->lock, flags);
Lionel Landwerlin2a98f4e2019-07-09 17:42:27 +01006848 if (!i915_request_has_waitboost(rq) &&
6849 !dma_fence_is_signaled_locked(&rq->fence)) {
Chris Wilson253a2812018-02-06 14:31:37 +00006850 boost = !atomic_fetch_inc(&rps->num_waiters);
Lionel Landwerlin2a98f4e2019-07-09 17:42:27 +01006851 rq->flags |= I915_REQUEST_WAITBOOST;
Chris Wilsonc0951f02013-10-10 21:58:50 +01006852 }
Chris Wilson74d290f2017-08-17 13:37:06 +01006853 spin_unlock_irqrestore(&rq->lock, flags);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006854 if (!boost)
6855 return;
6856
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006857 if (READ_ONCE(rps->cur_freq) < rps->boost_freq)
6858 schedule_work(&rps->work);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006859
Chris Wilson62eb3c22019-02-13 09:25:04 +00006860 atomic_inc(&rps->boosts);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006861}
6862
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006863int intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006864{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006865 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006866 int err;
6867
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006868 lockdep_assert_held(&rps->lock);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006869 GEM_BUG_ON(val > rps->max_freq);
6870 GEM_BUG_ON(val < rps->min_freq);
Chris Wilsoncfd1c482017-02-20 09:47:07 +00006871
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006872 if (!rps->enabled) {
6873 rps->cur_freq = val;
Chris Wilson76e4e4b2017-02-20 09:47:08 +00006874 return 0;
6875 }
6876
Chris Wilsondc979972016-05-10 14:10:04 +01006877 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006878 err = valleyview_set_rps(dev_priv, val);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006879 else
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006880 err = gen6_set_rps(dev_priv, val);
6881
6882 return err;
Jesse Barnes0a073b82013-04-17 15:54:58 -07006883}
6884
Chris Wilsondc979972016-05-10 14:10:04 +01006885static void gen9_disable_rps(struct drm_i915_private *dev_priv)
Akash Goel2030d682016-04-23 00:05:45 +05306886{
Akash Goel2030d682016-04-23 00:05:45 +05306887 I915_WRITE(GEN6_RP_CONTROL, 0);
6888}
6889
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01006890static void gen6_disable_rps(struct drm_i915_private *dev_priv)
6891{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006892 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Akash Goel2030d682016-04-23 00:05:45 +05306893 I915_WRITE(GEN6_RP_CONTROL, 0);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006894}
6895
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01006896static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
6897{
6898 I915_WRITE(GEN6_RP_CONTROL, 0);
6899}
6900
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01006901static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
6902{
6903 I915_WRITE(GEN6_RP_CONTROL, 0);
6904}
6905
Chris Wilsondc979972016-05-10 14:10:04 +01006906static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
Imre Deake6069ca2014-04-18 16:01:02 +03006907{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006908 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6909
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006910 /* All of these values are in units of 50MHz */
Chris Wilson773ea9a2016-07-13 09:10:33 +01006911
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006912 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02006913 if (IS_GEN9_LP(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006914 u32 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006915 rps->rp0_freq = (rp_state_cap >> 16) & 0xff;
6916 rps->rp1_freq = (rp_state_cap >> 8) & 0xff;
6917 rps->min_freq = (rp_state_cap >> 0) & 0xff;
Bob Paauwe35040562015-06-25 14:54:07 -07006918 } else {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006919 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006920 rps->rp0_freq = (rp_state_cap >> 0) & 0xff;
6921 rps->rp1_freq = (rp_state_cap >> 8) & 0xff;
6922 rps->min_freq = (rp_state_cap >> 16) & 0xff;
Bob Paauwe35040562015-06-25 14:54:07 -07006923 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006924 /* hw_max = RP0 until we check for overclocking */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006925 rps->max_freq = rps->rp0_freq;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006926
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006927 rps->efficient_freq = rps->rp1_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01006928 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
Oscar Mateo2b2874e2018-04-05 17:00:52 +03006929 IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006930 u32 ddcc_status = 0;
6931
6932 if (sandybridge_pcode_read(dev_priv,
6933 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
Ville Syrjäläd284d512019-05-21 19:40:24 +03006934 &ddcc_status, NULL) == 0)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006935 rps->efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08006936 clamp_t(u8,
6937 ((ddcc_status >> 8) & 0xff),
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006938 rps->min_freq,
6939 rps->max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006940 }
6941
Oscar Mateo2b2874e2018-04-05 17:00:52 +03006942 if (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
Akash Goelc5e06882015-06-29 14:50:19 +05306943 /* Store the frequency values in 16.66 MHZ units, which is
Chris Wilson773ea9a2016-07-13 09:10:33 +01006944 * the natural hardware unit for SKL
6945 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006946 rps->rp0_freq *= GEN9_FREQ_SCALER;
6947 rps->rp1_freq *= GEN9_FREQ_SCALER;
6948 rps->min_freq *= GEN9_FREQ_SCALER;
6949 rps->max_freq *= GEN9_FREQ_SCALER;
6950 rps->efficient_freq *= GEN9_FREQ_SCALER;
Akash Goelc5e06882015-06-29 14:50:19 +05306951 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006952}
6953
Chris Wilson3a45b052016-07-13 09:10:32 +01006954static void reset_rps(struct drm_i915_private *dev_priv,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006955 int (*set)(struct drm_i915_private *, u8))
Chris Wilson3a45b052016-07-13 09:10:32 +01006956{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006957 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6958 u8 freq = rps->cur_freq;
Chris Wilson3a45b052016-07-13 09:10:32 +01006959
6960 /* force a reset */
Chris Wilson60548c52018-07-31 14:26:29 +01006961 rps->power.mode = -1;
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006962 rps->cur_freq = -1;
Chris Wilson3a45b052016-07-13 09:10:32 +01006963
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006964 if (set(dev_priv, freq))
6965 DRM_ERROR("Failed to reset RPS to initial values\n");
Chris Wilson3a45b052016-07-13 09:10:32 +01006966}
6967
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006968/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Chris Wilsondc979972016-05-10 14:10:04 +01006969static void gen9_enable_rps(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00006970{
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07006971 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006972
David Weinehall36fe7782017-11-17 10:01:46 +02006973 /* Program defaults and thresholds for RPS */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08006974 if (IS_GEN(dev_priv, 9))
David Weinehall36fe7782017-11-17 10:01:46 +02006975 I915_WRITE(GEN6_RC_VIDEO_FREQ,
6976 GEN9_FREQUENCY(dev_priv->gt_pm.rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006977
Akash Goel0beb0592015-03-06 11:07:20 +05306978 /* 1 second timeout*/
6979 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
6980 GT_INTERVAL_FROM_US(dev_priv, 1000000));
6981
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006982 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006983
Akash Goel0beb0592015-03-06 11:07:20 +05306984 /* Leaning on the below call to gen6_set_rps to program/setup the
6985 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
6986 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
Chris Wilson3a45b052016-07-13 09:10:32 +01006987 reset_rps(dev_priv, gen6_set_rps);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006988
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07006989 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006990}
6991
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01006992static void gen8_enable_rps(struct drm_i915_private *dev_priv)
6993{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006994 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6995
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07006996 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01006997
6998 /* 1 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07006999 I915_WRITE(GEN6_RPNSWREQ,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007000 HSW_FREQUENCY(rps->rp1_freq));
Ben Widawskyf9bdc582014-03-31 17:16:41 -07007001 I915_WRITE(GEN6_RC_VIDEO_FREQ,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007002 HSW_FREQUENCY(rps->rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02007003 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
7004 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007005
Daniel Vetter7526ed72014-09-29 15:07:19 +02007006 /* Docs recommend 900MHz, and 300 MHz respectively */
7007 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007008 rps->max_freq_softlimit << 24 |
7009 rps->min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007010
Daniel Vetter7526ed72014-09-29 15:07:19 +02007011 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
7012 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
7013 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
7014 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007015
Daniel Vetter7526ed72014-09-29 15:07:19 +02007016 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007017
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01007018 /* 2: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02007019 I915_WRITE(GEN6_RP_CONTROL,
7020 GEN6_RP_MEDIA_TURBO |
7021 GEN6_RP_MEDIA_HW_NORMAL_MODE |
7022 GEN6_RP_MEDIA_IS_GFX |
7023 GEN6_RP_ENABLE |
7024 GEN6_RP_UP_BUSY_AVG |
7025 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007026
Chris Wilson3a45b052016-07-13 09:10:32 +01007027 reset_rps(dev_priv, gen6_set_rps);
Daniel Vetter7526ed72014-09-29 15:07:19 +02007028
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007029 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007030}
7031
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01007032static void gen6_enable_rps(struct drm_i915_private *dev_priv)
7033{
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01007034 /* Here begins a magic sequence of register writes to enable
7035 * auto-downclocking.
7036 *
7037 * Perhaps there might be some value in exposing these to
7038 * userspace...
7039 */
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007040 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01007041
7042 /* Power down if completely idle for over 50ms */
7043 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
7044 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7045
7046 reset_rps(dev_priv, gen6_set_rps);
7047
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007048 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01007049}
7050
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007051static void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007052{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007053 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Mika Kuoppala66c1f772018-03-20 17:17:33 +02007054 const int min_freq = 15;
7055 const int scaling_factor = 180;
Chris Wilson3ebecd02013-04-12 19:10:13 +01007056 unsigned int gpu_freq;
7057 unsigned int max_ia_freq, min_ring_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05307058 unsigned int max_gpu_freq, min_gpu_freq;
Ben Widawskyeda79642013-10-07 17:15:48 -03007059 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007060
Chris Wilsonebb5eb72019-04-26 09:17:21 +01007061 lockdep_assert_held(&rps->lock);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02007062
Mika Kuoppala66c1f772018-03-20 17:17:33 +02007063 if (rps->max_freq <= rps->min_freq)
7064 return;
7065
Ben Widawskyeda79642013-10-07 17:15:48 -03007066 policy = cpufreq_cpu_get(0);
7067 if (policy) {
7068 max_ia_freq = policy->cpuinfo.max_freq;
7069 cpufreq_cpu_put(policy);
7070 } else {
7071 /*
7072 * Default to measured freq if none found, PCU will ensure we
7073 * don't go over
7074 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007075 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03007076 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007077
7078 /* Convert from kHz to MHz */
7079 max_ia_freq /= 1000;
7080
Ben Widawsky153b4b952013-10-22 22:05:09 -07007081 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07007082 /* convert DDR frequency from units of 266.6MHz to bandwidth */
7083 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01007084
Chris Wilsond586b5f2018-03-08 14:26:48 +00007085 min_gpu_freq = rps->min_freq;
7086 max_gpu_freq = rps->max_freq;
Oscar Mateo2b2874e2018-04-05 17:00:52 +03007087 if (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
Akash Goel4c8c7742015-06-29 14:50:20 +05307088 /* Convert GT frequency to 50 HZ units */
Chris Wilsond586b5f2018-03-08 14:26:48 +00007089 min_gpu_freq /= GEN9_FREQ_SCALER;
7090 max_gpu_freq /= GEN9_FREQ_SCALER;
Akash Goel4c8c7742015-06-29 14:50:20 +05307091 }
7092
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007093 /*
7094 * For each potential GPU frequency, load a ring frequency we'd like
7095 * to use for memory access. We do this by specifying the IA frequency
7096 * the PCU should use as a reference to determine the ring frequency.
7097 */
Akash Goel4c8c7742015-06-29 14:50:20 +05307098 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
Mika Kuoppala66c1f772018-03-20 17:17:33 +02007099 const int diff = max_gpu_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01007100 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007101
Oscar Mateo2b2874e2018-04-05 17:00:52 +03007102 if (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
Akash Goel4c8c7742015-06-29 14:50:20 +05307103 /*
7104 * ring_freq = 2 * GT. ring_freq is in 100MHz units
7105 * No floor required for ring frequency on SKL.
7106 */
7107 ring_freq = gpu_freq;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00007108 } else if (INTEL_GEN(dev_priv) >= 8) {
Ben Widawsky46c764d2013-11-02 21:07:49 -07007109 /* max(2 * GT, DDR). NB: GT is 50MHz units */
7110 ring_freq = max(min_ring_freq, gpu_freq);
Chris Wilsondc979972016-05-10 14:10:04 +01007111 } else if (IS_HASWELL(dev_priv)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07007112 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01007113 ring_freq = max(min_ring_freq, ring_freq);
7114 /* leave ia_freq as the default, chosen by cpufreq */
7115 } else {
7116 /* On older processors, there is no separate ring
7117 * clock domain, so in order to boost the bandwidth
7118 * of the ring, we need to upclock the CPU (ia_freq).
7119 *
7120 * For GPU frequencies less than 750MHz,
7121 * just use the lowest ring freq.
7122 */
7123 if (gpu_freq < min_freq)
7124 ia_freq = 800;
7125 else
7126 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
7127 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
7128 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007129
Ben Widawsky42c05262012-09-26 10:34:00 -07007130 sandybridge_pcode_write(dev_priv,
7131 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01007132 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
7133 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
7134 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007135 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03007136}
7137
Ville Syrjälä03af2042014-06-28 02:03:53 +03007138static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05307139{
7140 u32 val, rp0;
7141
Jani Nikula5b5929c2015-10-07 11:17:46 +03007142 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05307143
Jani Nikula02584042018-12-31 16:56:41 +02007144 switch (RUNTIME_INFO(dev_priv)->sseu.eu_total) {
Jani Nikula5b5929c2015-10-07 11:17:46 +03007145 case 8:
7146 /* (2 * 4) config */
7147 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
7148 break;
7149 case 12:
7150 /* (2 * 6) config */
7151 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
7152 break;
7153 case 16:
7154 /* (2 * 8) config */
7155 default:
7156 /* Setting (2 * 8) Min RP0 for any other combination */
7157 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
7158 break;
Deepak S095acd52015-01-17 11:05:59 +05307159 }
Jani Nikula5b5929c2015-10-07 11:17:46 +03007160
7161 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
7162
Deepak S2b6b3a02014-05-27 15:59:30 +05307163 return rp0;
7164}
7165
7166static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
7167{
7168 u32 val, rpe;
7169
7170 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
7171 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
7172
7173 return rpe;
7174}
7175
Deepak S7707df42014-07-12 18:46:14 +05307176static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
7177{
7178 u32 val, rp1;
7179
Jani Nikula5b5929c2015-10-07 11:17:46 +03007180 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
7181 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
7182
Deepak S7707df42014-07-12 18:46:14 +05307183 return rp1;
7184}
7185
Deepak S96676fe2016-08-12 18:46:41 +05307186static u32 cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
7187{
7188 u32 val, rpn;
7189
7190 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
7191 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
7192 FB_GFX_FREQ_FUSE_MASK);
7193
7194 return rpn;
7195}
7196
Deepak Sf8f2b002014-07-10 13:16:21 +05307197static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
7198{
7199 u32 val, rp1;
7200
7201 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
7202
7203 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
7204
7205 return rp1;
7206}
7207
Ville Syrjälä03af2042014-06-28 02:03:53 +03007208static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07007209{
7210 u32 val, rp0;
7211
Jani Nikula64936252013-05-22 15:36:20 +03007212 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007213
7214 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
7215 /* Clamp to max */
7216 rp0 = min_t(u32, rp0, 0xea);
7217
7218 return rp0;
7219}
7220
7221static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
7222{
7223 u32 val, rpe;
7224
Jani Nikula64936252013-05-22 15:36:20 +03007225 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007226 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03007227 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007228 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
7229
7230 return rpe;
7231}
7232
Ville Syrjälä03af2042014-06-28 02:03:53 +03007233static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07007234{
Imre Deak36146032014-12-04 18:39:35 +02007235 u32 val;
7236
7237 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
7238 /*
7239 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
7240 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
7241 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
7242 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
7243 * to make sure it matches what Punit accepts.
7244 */
7245 return max_t(u32, val, 0xc0);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007246}
7247
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007248static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
7249{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007250 dev_priv->gt_pm.rps.gpll_ref_freq =
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007251 vlv_get_cck_clock(dev_priv, "GPLL ref",
7252 CCK_GPLL_CLOCK_CONTROL,
7253 dev_priv->czclk_freq);
7254
7255 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007256 dev_priv->gt_pm.rps.gpll_ref_freq);
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007257}
7258
Chris Wilsondc979972016-05-10 14:10:04 +01007259static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03007260{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007261 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007262 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03007263
Chris Wilson337fa6e2019-04-26 09:17:20 +01007264 vlv_iosf_sb_get(dev_priv,
7265 BIT(VLV_IOSF_SB_PUNIT) |
7266 BIT(VLV_IOSF_SB_NC) |
7267 BIT(VLV_IOSF_SB_CCK));
7268
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007269 vlv_init_gpll_ref_freq(dev_priv);
7270
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007271 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
7272 switch ((val >> 6) & 3) {
7273 case 0:
7274 case 1:
7275 dev_priv->mem_freq = 800;
7276 break;
7277 case 2:
7278 dev_priv->mem_freq = 1066;
7279 break;
7280 case 3:
7281 dev_priv->mem_freq = 1333;
7282 break;
7283 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02007284 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007285
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007286 rps->max_freq = valleyview_rps_max_freq(dev_priv);
7287 rps->rp0_freq = rps->max_freq;
Imre Deak4e805192014-04-14 20:24:41 +03007288 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007289 intel_gpu_freq(dev_priv, rps->max_freq),
7290 rps->max_freq);
Imre Deak4e805192014-04-14 20:24:41 +03007291
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007292 rps->efficient_freq = valleyview_rps_rpe_freq(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007293 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007294 intel_gpu_freq(dev_priv, rps->efficient_freq),
7295 rps->efficient_freq);
Imre Deak4e805192014-04-14 20:24:41 +03007296
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007297 rps->rp1_freq = valleyview_rps_guar_freq(dev_priv);
Deepak Sf8f2b002014-07-10 13:16:21 +05307298 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007299 intel_gpu_freq(dev_priv, rps->rp1_freq),
7300 rps->rp1_freq);
Deepak Sf8f2b002014-07-10 13:16:21 +05307301
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007302 rps->min_freq = valleyview_rps_min_freq(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007303 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007304 intel_gpu_freq(dev_priv, rps->min_freq),
7305 rps->min_freq);
Chris Wilson337fa6e2019-04-26 09:17:20 +01007306
7307 vlv_iosf_sb_put(dev_priv,
7308 BIT(VLV_IOSF_SB_PUNIT) |
7309 BIT(VLV_IOSF_SB_NC) |
7310 BIT(VLV_IOSF_SB_CCK));
Imre Deak4e805192014-04-14 20:24:41 +03007311}
7312
Chris Wilsondc979972016-05-10 14:10:04 +01007313static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05307314{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007315 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007316 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05307317
Chris Wilson337fa6e2019-04-26 09:17:20 +01007318 vlv_iosf_sb_get(dev_priv,
7319 BIT(VLV_IOSF_SB_PUNIT) |
7320 BIT(VLV_IOSF_SB_NC) |
7321 BIT(VLV_IOSF_SB_CCK));
7322
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007323 vlv_init_gpll_ref_freq(dev_priv);
7324
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02007325 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02007326
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007327 switch ((val >> 2) & 0x7) {
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007328 case 3:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007329 dev_priv->mem_freq = 2000;
7330 break;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03007331 default:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007332 dev_priv->mem_freq = 1600;
7333 break;
7334 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02007335 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007336
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007337 rps->max_freq = cherryview_rps_max_freq(dev_priv);
7338 rps->rp0_freq = rps->max_freq;
Deepak S2b6b3a02014-05-27 15:59:30 +05307339 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007340 intel_gpu_freq(dev_priv, rps->max_freq),
7341 rps->max_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05307342
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007343 rps->efficient_freq = cherryview_rps_rpe_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307344 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007345 intel_gpu_freq(dev_priv, rps->efficient_freq),
7346 rps->efficient_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05307347
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007348 rps->rp1_freq = cherryview_rps_guar_freq(dev_priv);
Deepak S7707df42014-07-12 18:46:14 +05307349 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007350 intel_gpu_freq(dev_priv, rps->rp1_freq),
7351 rps->rp1_freq);
Deepak S7707df42014-07-12 18:46:14 +05307352
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007353 rps->min_freq = cherryview_rps_min_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307354 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007355 intel_gpu_freq(dev_priv, rps->min_freq),
7356 rps->min_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05307357
Chris Wilson337fa6e2019-04-26 09:17:20 +01007358 vlv_iosf_sb_put(dev_priv,
7359 BIT(VLV_IOSF_SB_PUNIT) |
7360 BIT(VLV_IOSF_SB_NC) |
7361 BIT(VLV_IOSF_SB_CCK));
7362
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007363 WARN_ONCE((rps->max_freq | rps->efficient_freq | rps->rp1_freq |
7364 rps->min_freq) & 1,
Ville Syrjälä1c147622014-08-18 14:42:43 +03007365 "Odd GPU freq values\n");
Deepak S38807742014-05-23 21:00:15 +05307366}
7367
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007368static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
7369{
7370 u32 val;
7371
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007372 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007373
7374 /* 1: Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02007375 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05307376 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
7377 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
7378 I915_WRITE(GEN6_RP_UP_EI, 66000);
7379 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
7380
7381 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7382
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007383 /* 2: Enable RPS */
Deepak S2b6b3a02014-05-27 15:59:30 +05307384 I915_WRITE(GEN6_RP_CONTROL,
7385 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02007386 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05307387 GEN6_RP_ENABLE |
7388 GEN6_RP_UP_BUSY_AVG |
7389 GEN6_RP_DOWN_IDLE_AVG);
7390
Deepak S3ef62342015-04-29 08:36:24 +05307391 /* Setting Fixed Bias */
Chris Wilson337fa6e2019-04-26 09:17:20 +01007392 vlv_punit_get(dev_priv);
7393
7394 val = VLV_OVERRIDE_EN | VLV_SOC_TDP_EN | CHV_BIAS_CPU_50_SOC_50;
Deepak S3ef62342015-04-29 08:36:24 +05307395 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
7396
Deepak S2b6b3a02014-05-27 15:59:30 +05307397 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
7398
Chris Wilson337fa6e2019-04-26 09:17:20 +01007399 vlv_punit_put(dev_priv);
7400
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02007401 /* RPS code assumes GPLL is used */
7402 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
7403
Jani Nikula742f4912015-09-03 11:16:09 +03007404 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05307405 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7406
Chris Wilson3a45b052016-07-13 09:10:32 +01007407 reset_rps(dev_priv, valleyview_set_rps);
Deepak S2b6b3a02014-05-27 15:59:30 +05307408
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007409 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05307410}
7411
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01007412static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
7413{
7414 u32 val;
7415
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007416 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01007417
Ville Syrjäläcad725f2015-01-19 13:50:48 +02007418 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007419 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
7420 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
7421 I915_WRITE(GEN6_RP_UP_EI, 66000);
7422 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
7423
7424 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7425
7426 I915_WRITE(GEN6_RP_CONTROL,
7427 GEN6_RP_MEDIA_TURBO |
7428 GEN6_RP_MEDIA_HW_NORMAL_MODE |
7429 GEN6_RP_MEDIA_IS_GFX |
7430 GEN6_RP_ENABLE |
7431 GEN6_RP_UP_BUSY_AVG |
7432 GEN6_RP_DOWN_IDLE_CONT);
7433
Chris Wilson337fa6e2019-04-26 09:17:20 +01007434 vlv_punit_get(dev_priv);
7435
Deepak S3ef62342015-04-29 08:36:24 +05307436 /* Setting Fixed Bias */
Chris Wilson337fa6e2019-04-26 09:17:20 +01007437 val = VLV_OVERRIDE_EN | VLV_SOC_TDP_EN | VLV_BIAS_CPU_125_SOC_875;
Deepak S3ef62342015-04-29 08:36:24 +05307438 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
7439
Jani Nikula64936252013-05-22 15:36:20 +03007440 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007441
Chris Wilson337fa6e2019-04-26 09:17:20 +01007442 vlv_punit_put(dev_priv);
7443
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02007444 /* RPS code assumes GPLL is used */
7445 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
7446
Jani Nikula742f4912015-09-03 11:16:09 +03007447 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07007448 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7449
Chris Wilson3a45b052016-07-13 09:10:32 +01007450 reset_rps(dev_priv, valleyview_set_rps);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007451
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007452 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007453}
7454
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007455static unsigned long intel_pxfreq(u32 vidfreq)
7456{
7457 unsigned long freq;
7458 int div = (vidfreq & 0x3f0000) >> 16;
7459 int post = (vidfreq & 0x3000) >> 12;
7460 int pre = (vidfreq & 0x7);
7461
7462 if (!pre)
7463 return 0;
7464
7465 freq = ((div * 133333) / ((1<<post) * pre));
7466
7467 return freq;
7468}
7469
Daniel Vettereb48eb02012-04-26 23:28:12 +02007470static const struct cparams {
7471 u16 i;
7472 u16 t;
7473 u16 m;
7474 u16 c;
7475} cparams[] = {
7476 { 1, 1333, 301, 28664 },
7477 { 1, 1066, 294, 24460 },
7478 { 1, 800, 294, 25192 },
7479 { 0, 1333, 276, 27605 },
7480 { 0, 1066, 276, 27605 },
7481 { 0, 800, 231, 23784 },
7482};
7483
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007484static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007485{
7486 u64 total_count, diff, ret;
7487 u32 count1, count2, count3, m = 0, c = 0;
7488 unsigned long now = jiffies_to_msecs(jiffies), diff1;
7489 int i;
7490
Chris Wilson67520412017-03-02 13:28:01 +00007491 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007492
Daniel Vetter20e4d402012-08-08 23:35:39 +02007493 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007494
7495 /* Prevent division-by-zero if we are asking too fast.
7496 * Also, we don't get interesting results if we are polling
7497 * faster than once in 10ms, so just return the saved value
7498 * in such cases.
7499 */
7500 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02007501 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007502
7503 count1 = I915_READ(DMIEC);
7504 count2 = I915_READ(DDREC);
7505 count3 = I915_READ(CSIEC);
7506
7507 total_count = count1 + count2 + count3;
7508
7509 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02007510 if (total_count < dev_priv->ips.last_count1) {
7511 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007512 diff += total_count;
7513 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007514 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007515 }
7516
7517 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007518 if (cparams[i].i == dev_priv->ips.c_m &&
7519 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02007520 m = cparams[i].m;
7521 c = cparams[i].c;
7522 break;
7523 }
7524 }
7525
7526 diff = div_u64(diff, diff1);
7527 ret = ((m * diff) + c);
7528 ret = div_u64(ret, 10);
7529
Daniel Vetter20e4d402012-08-08 23:35:39 +02007530 dev_priv->ips.last_count1 = total_count;
7531 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007532
Daniel Vetter20e4d402012-08-08 23:35:39 +02007533 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007534
7535 return ret;
7536}
7537
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007538unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
7539{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007540 intel_wakeref_t wakeref;
7541 unsigned long val = 0;
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007542
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007543 if (!IS_GEN(dev_priv, 5))
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007544 return 0;
7545
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -07007546 with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref) {
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007547 spin_lock_irq(&mchdev_lock);
7548 val = __i915_chipset_val(dev_priv);
7549 spin_unlock_irq(&mchdev_lock);
7550 }
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007551
7552 return val;
7553}
7554
Tvrtko Ursulinc54f0ba2019-06-11 11:45:43 +01007555unsigned long i915_mch_val(struct drm_i915_private *i915)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007556{
7557 unsigned long m, x, b;
7558 u32 tsfs;
7559
Tvrtko Ursulinc54f0ba2019-06-11 11:45:43 +01007560 tsfs = intel_uncore_read(&i915->uncore, TSFS);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007561
7562 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
Tvrtko Ursulinc54f0ba2019-06-11 11:45:43 +01007563 x = intel_uncore_read8(&i915->uncore, TR1);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007564
7565 b = tsfs & TSFS_INTR_MASK;
7566
7567 return ((m * x) / 127) - b;
7568}
7569
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007570static int _pxvid_to_vd(u8 pxvid)
7571{
7572 if (pxvid == 0)
7573 return 0;
7574
7575 if (pxvid >= 8 && pxvid < 31)
7576 pxvid = 31;
7577
7578 return (pxvid + 2) * 125;
7579}
7580
7581static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007582{
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007583 const int vd = _pxvid_to_vd(pxvid);
7584 const int vm = vd - 1125;
7585
Chris Wilsondc979972016-05-10 14:10:04 +01007586 if (INTEL_INFO(dev_priv)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007587 return vm > 0 ? vm : 0;
7588
7589 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007590}
7591
Daniel Vetter02d71952012-08-09 16:44:54 +02007592static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007593{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00007594 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007595 u32 count;
7596
Chris Wilson67520412017-03-02 13:28:01 +00007597 lockdep_assert_held(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007598
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00007599 now = ktime_get_raw_ns();
7600 diffms = now - dev_priv->ips.last_time2;
7601 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007602
7603 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02007604 if (!diffms)
7605 return;
7606
7607 count = I915_READ(GFXEC);
7608
Daniel Vetter20e4d402012-08-08 23:35:39 +02007609 if (count < dev_priv->ips.last_count2) {
7610 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007611 diff += count;
7612 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007613 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007614 }
7615
Daniel Vetter20e4d402012-08-08 23:35:39 +02007616 dev_priv->ips.last_count2 = count;
7617 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007618
7619 /* More magic constants... */
7620 diff = diff * 1181;
7621 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02007622 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007623}
7624
Daniel Vetter02d71952012-08-09 16:44:54 +02007625void i915_update_gfx_val(struct drm_i915_private *dev_priv)
7626{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007627 intel_wakeref_t wakeref;
7628
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007629 if (!IS_GEN(dev_priv, 5))
Daniel Vetter02d71952012-08-09 16:44:54 +02007630 return;
7631
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -07007632 with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref) {
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007633 spin_lock_irq(&mchdev_lock);
7634 __i915_update_gfx_val(dev_priv);
7635 spin_unlock_irq(&mchdev_lock);
7636 }
Daniel Vetter02d71952012-08-09 16:44:54 +02007637}
7638
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007639static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007640{
7641 unsigned long t, corr, state1, corr2, state2;
7642 u32 pxvid, ext_v;
7643
Chris Wilson67520412017-03-02 13:28:01 +00007644 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007645
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007646 pxvid = I915_READ(PXVFREQ(dev_priv->gt_pm.rps.cur_freq));
Daniel Vettereb48eb02012-04-26 23:28:12 +02007647 pxvid = (pxvid >> 24) & 0x7f;
7648 ext_v = pvid_to_extvid(dev_priv, pxvid);
7649
7650 state1 = ext_v;
7651
7652 t = i915_mch_val(dev_priv);
7653
7654 /* Revel in the empirically derived constants */
7655
7656 /* Correction factor in 1/100000 units */
7657 if (t > 80)
7658 corr = ((t * 2349) + 135940);
7659 else if (t >= 50)
7660 corr = ((t * 964) + 29317);
7661 else /* < 50 */
7662 corr = ((t * 301) + 1004);
7663
7664 corr = corr * ((150142 * state1) / 10000 - 78642);
7665 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02007666 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007667
7668 state2 = (corr2 * state1) / 10000;
7669 state2 /= 100; /* convert to mW */
7670
Daniel Vetter02d71952012-08-09 16:44:54 +02007671 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007672
Daniel Vetter20e4d402012-08-08 23:35:39 +02007673 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007674}
7675
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007676unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
7677{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007678 intel_wakeref_t wakeref;
7679 unsigned long val = 0;
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007680
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007681 if (!IS_GEN(dev_priv, 5))
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007682 return 0;
7683
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -07007684 with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref) {
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007685 spin_lock_irq(&mchdev_lock);
7686 val = __i915_gfx_val(dev_priv);
7687 spin_unlock_irq(&mchdev_lock);
7688 }
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007689
7690 return val;
7691}
7692
Chris Wilsonadc674c2019-04-12 09:53:22 +01007693static struct drm_i915_private __rcu *i915_mch_dev;
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007694
7695static struct drm_i915_private *mchdev_get(void)
7696{
7697 struct drm_i915_private *i915;
7698
7699 rcu_read_lock();
Chris Wilsonadc674c2019-04-12 09:53:22 +01007700 i915 = rcu_dereference(i915_mch_dev);
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007701 if (!kref_get_unless_zero(&i915->drm.ref))
7702 i915 = NULL;
7703 rcu_read_unlock();
7704
7705 return i915;
7706}
7707
Daniel Vettereb48eb02012-04-26 23:28:12 +02007708/**
7709 * i915_read_mch_val - return value for IPS use
7710 *
7711 * Calculate and return a value for the IPS driver to use when deciding whether
7712 * we have thermal and power headroom to increase CPU or GPU power budget.
7713 */
7714unsigned long i915_read_mch_val(void)
7715{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007716 struct drm_i915_private *i915;
7717 unsigned long chipset_val = 0;
7718 unsigned long graphics_val = 0;
7719 intel_wakeref_t wakeref;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007720
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007721 i915 = mchdev_get();
7722 if (!i915)
7723 return 0;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007724
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -07007725 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007726 spin_lock_irq(&mchdev_lock);
7727 chipset_val = __i915_chipset_val(i915);
7728 graphics_val = __i915_gfx_val(i915);
7729 spin_unlock_irq(&mchdev_lock);
7730 }
Daniel Vettereb48eb02012-04-26 23:28:12 +02007731
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007732 drm_dev_put(&i915->drm);
7733 return chipset_val + graphics_val;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007734}
7735EXPORT_SYMBOL_GPL(i915_read_mch_val);
7736
7737/**
7738 * i915_gpu_raise - raise GPU frequency limit
7739 *
7740 * Raise the limit; IPS indicates we have thermal headroom.
7741 */
7742bool i915_gpu_raise(void)
7743{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007744 struct drm_i915_private *i915;
7745
7746 i915 = mchdev_get();
7747 if (!i915)
7748 return false;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007749
Daniel Vetter92703882012-08-09 16:46:01 +02007750 spin_lock_irq(&mchdev_lock);
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007751 if (i915->ips.max_delay > i915->ips.fmax)
7752 i915->ips.max_delay--;
Daniel Vetter92703882012-08-09 16:46:01 +02007753 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007754
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007755 drm_dev_put(&i915->drm);
7756 return true;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007757}
7758EXPORT_SYMBOL_GPL(i915_gpu_raise);
7759
7760/**
7761 * i915_gpu_lower - lower GPU frequency limit
7762 *
7763 * IPS indicates we're close to a thermal limit, so throttle back the GPU
7764 * frequency maximum.
7765 */
7766bool i915_gpu_lower(void)
7767{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007768 struct drm_i915_private *i915;
7769
7770 i915 = mchdev_get();
7771 if (!i915)
7772 return false;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007773
Daniel Vetter92703882012-08-09 16:46:01 +02007774 spin_lock_irq(&mchdev_lock);
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007775 if (i915->ips.max_delay < i915->ips.min_delay)
7776 i915->ips.max_delay++;
Daniel Vetter92703882012-08-09 16:46:01 +02007777 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007778
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007779 drm_dev_put(&i915->drm);
7780 return true;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007781}
7782EXPORT_SYMBOL_GPL(i915_gpu_lower);
7783
7784/**
7785 * i915_gpu_busy - indicate GPU business to IPS
7786 *
7787 * Tell the IPS driver whether or not the GPU is busy.
7788 */
7789bool i915_gpu_busy(void)
7790{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007791 struct drm_i915_private *i915;
7792 bool ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007793
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007794 i915 = mchdev_get();
7795 if (!i915)
7796 return false;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007797
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007798 ret = i915->gt.awake;
7799
7800 drm_dev_put(&i915->drm);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007801 return ret;
7802}
7803EXPORT_SYMBOL_GPL(i915_gpu_busy);
7804
7805/**
7806 * i915_gpu_turbo_disable - disable graphics turbo
7807 *
7808 * Disable graphics turbo by resetting the max frequency and setting the
7809 * current frequency to the default.
7810 */
7811bool i915_gpu_turbo_disable(void)
7812{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007813 struct drm_i915_private *i915;
7814 bool ret;
7815
7816 i915 = mchdev_get();
7817 if (!i915)
7818 return false;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007819
Daniel Vetter92703882012-08-09 16:46:01 +02007820 spin_lock_irq(&mchdev_lock);
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007821 i915->ips.max_delay = i915->ips.fstart;
7822 ret = ironlake_set_drps(i915, i915->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02007823 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007824
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007825 drm_dev_put(&i915->drm);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007826 return ret;
7827}
7828EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
7829
7830/**
7831 * Tells the intel_ips driver that the i915 driver is now loaded, if
7832 * IPS got loaded first.
7833 *
7834 * This awkward dance is so that neither module has to depend on the
7835 * other in order for IPS to do the appropriate communication of
7836 * GPU turbo limits to i915.
7837 */
7838static void
7839ips_ping_for_i915_load(void)
7840{
7841 void (*link)(void);
7842
7843 link = symbol_get(ips_link_to_i915_driver);
7844 if (link) {
7845 link();
7846 symbol_put(ips_link_to_i915_driver);
7847 }
7848}
7849
7850void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
7851{
Daniel Vetter02d71952012-08-09 16:44:54 +02007852 /* We only register the i915 ips part with intel-ips once everything is
7853 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007854 rcu_assign_pointer(i915_mch_dev, dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007855
7856 ips_ping_for_i915_load();
7857}
7858
7859void intel_gpu_ips_teardown(void)
7860{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007861 rcu_assign_pointer(i915_mch_dev, NULL);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007862}
Deepak S76c3552f2014-01-30 23:08:16 +05307863
Chris Wilsondc979972016-05-10 14:10:04 +01007864static void intel_init_emon(struct drm_i915_private *dev_priv)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007865{
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007866 u32 lcfuse;
7867 u8 pxw[16];
7868 int i;
7869
7870 /* Disable to program */
7871 I915_WRITE(ECR, 0);
7872 POSTING_READ(ECR);
7873
7874 /* Program energy weights for various events */
7875 I915_WRITE(SDEW, 0x15040d00);
7876 I915_WRITE(CSIEW0, 0x007f0000);
7877 I915_WRITE(CSIEW1, 0x1e220004);
7878 I915_WRITE(CSIEW2, 0x04000004);
7879
7880 for (i = 0; i < 5; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007881 I915_WRITE(PEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007882 for (i = 0; i < 3; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007883 I915_WRITE(DEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007884
7885 /* Program P-state weights to account for frequency power adjustment */
7886 for (i = 0; i < 16; i++) {
Ville Syrjälä616847e2015-09-18 20:03:19 +03007887 u32 pxvidfreq = I915_READ(PXVFREQ(i));
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007888 unsigned long freq = intel_pxfreq(pxvidfreq);
7889 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
7890 PXVFREQ_PX_SHIFT;
7891 unsigned long val;
7892
7893 val = vid * vid;
7894 val *= (freq / 1000);
7895 val *= 255;
7896 val /= (127*127*900);
7897 if (val > 0xff)
7898 DRM_ERROR("bad pxval: %ld\n", val);
7899 pxw[i] = val;
7900 }
7901 /* Render standby states get 0 weight */
7902 pxw[14] = 0;
7903 pxw[15] = 0;
7904
7905 for (i = 0; i < 4; i++) {
7906 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
7907 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
Ville Syrjälä616847e2015-09-18 20:03:19 +03007908 I915_WRITE(PXW(i), val);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007909 }
7910
7911 /* Adjust magic regs to magic values (more experimental results) */
7912 I915_WRITE(OGW0, 0);
7913 I915_WRITE(OGW1, 0);
7914 I915_WRITE(EG0, 0x00007f00);
7915 I915_WRITE(EG1, 0x0000000e);
7916 I915_WRITE(EG2, 0x000e0000);
7917 I915_WRITE(EG3, 0x68000300);
7918 I915_WRITE(EG4, 0x42000000);
7919 I915_WRITE(EG5, 0x00140031);
7920 I915_WRITE(EG6, 0);
7921 I915_WRITE(EG7, 0);
7922
7923 for (i = 0; i < 8; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007924 I915_WRITE(PXWL(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007925
7926 /* Enable PMON + select events */
7927 I915_WRITE(ECR, 0x80000019);
7928
7929 lcfuse = I915_READ(LCFUSE02);
7930
Daniel Vetter20e4d402012-08-08 23:35:39 +02007931 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007932}
7933
Chris Wilsondc979972016-05-10 14:10:04 +01007934void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007935{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007936 struct intel_rps *rps = &dev_priv->gt_pm.rps;
7937
Andi Shytic1132362019-09-27 12:08:49 +01007938 /* Powersaving is controlled by the host when inside a VM */
7939 if (intel_vgpu_active(dev_priv))
7940 mkwrite_device_info(dev_priv)->has_rps = false;
Imre Deake6069ca2014-04-18 16:01:02 +03007941
Chris Wilson773ea9a2016-07-13 09:10:33 +01007942 /* Initialize RPS limits (for userspace) */
Chris Wilsondc979972016-05-10 14:10:04 +01007943 if (IS_CHERRYVIEW(dev_priv))
7944 cherryview_init_gt_powersave(dev_priv);
7945 else if (IS_VALLEYVIEW(dev_priv))
7946 valleyview_init_gt_powersave(dev_priv);
Chris Wilson2a13ae72016-08-02 11:15:27 +01007947 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson773ea9a2016-07-13 09:10:33 +01007948 gen6_init_rps_frequencies(dev_priv);
7949
7950 /* Derive initial user preferences/limits from the hardware limits */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007951 rps->max_freq_softlimit = rps->max_freq;
7952 rps->min_freq_softlimit = rps->min_freq;
Chris Wilson773ea9a2016-07-13 09:10:33 +01007953
Chris Wilson99ac9612016-07-13 09:10:34 +01007954 /* After setting max-softlimit, find the overclock max freq */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007955 if (IS_GEN(dev_priv, 6) ||
Chris Wilson99ac9612016-07-13 09:10:34 +01007956 IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
7957 u32 params = 0;
7958
Ville Syrjäläd284d512019-05-21 19:40:24 +03007959 sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS,
7960 &params, NULL);
Chris Wilson99ac9612016-07-13 09:10:34 +01007961 if (params & BIT(31)) { /* OC supported */
7962 DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007963 (rps->max_freq & 0xff) * 50,
Chris Wilson99ac9612016-07-13 09:10:34 +01007964 (params & 0xff) * 50);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007965 rps->max_freq = params & 0xff;
Chris Wilson99ac9612016-07-13 09:10:34 +01007966 }
7967 }
7968
Chris Wilson29ecd78d2016-07-13 09:10:35 +01007969 /* Finally allow us to boost to max by default */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007970 rps->boost_freq = rps->max_freq;
Chris Wilson844e3312019-04-18 21:53:58 +01007971 rps->idle_freq = rps->min_freq;
7972 rps->cur_freq = rps->idle_freq;
Imre Deakae484342014-03-31 15:10:44 +03007973}
7974
Chris Wilsonb7137e02016-07-13 09:10:37 +01007975void intel_sanitize_gt_powersave(struct drm_i915_private *dev_priv)
7976{
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01007977 dev_priv->gt_pm.rps.enabled = true; /* force RPS disabling */
Chris Wilsonb7137e02016-07-13 09:10:37 +01007978 intel_disable_gt_powersave(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007979
Oscar Mateod02b98b2018-04-05 17:00:50 +03007980 if (INTEL_GEN(dev_priv) >= 11)
7981 gen11_reset_rps_interrupts(dev_priv);
Chris Wilson61e1e372018-08-12 23:36:30 +01007982 else if (INTEL_GEN(dev_priv) >= 6)
Oscar Mateod02b98b2018-04-05 17:00:50 +03007983 gen6_reset_rps_interrupts(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07007984}
7985
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01007986static inline void intel_disable_llc_pstate(struct drm_i915_private *i915)
7987{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01007988 lockdep_assert_held(&i915->gt_pm.rps.lock);
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01007989
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01007990 if (!i915->gt_pm.llc_pstate.enabled)
7991 return;
7992
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01007993 /* Currently there is no HW configuration to be done to disable. */
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01007994
7995 i915->gt_pm.llc_pstate.enabled = false;
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01007996}
7997
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01007998static void intel_disable_rps(struct drm_i915_private *dev_priv)
7999{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008000 lockdep_assert_held(&dev_priv->gt_pm.rps.lock);
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008001
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008002 if (!dev_priv->gt_pm.rps.enabled)
8003 return;
8004
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008005 if (INTEL_GEN(dev_priv) >= 9)
8006 gen9_disable_rps(dev_priv);
8007 else if (IS_CHERRYVIEW(dev_priv))
8008 cherryview_disable_rps(dev_priv);
8009 else if (IS_VALLEYVIEW(dev_priv))
8010 valleyview_disable_rps(dev_priv);
8011 else if (INTEL_GEN(dev_priv) >= 6)
8012 gen6_disable_rps(dev_priv);
8013 else if (IS_IRONLAKE_M(dev_priv))
8014 ironlake_disable_drps(dev_priv);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008015
8016 dev_priv->gt_pm.rps.enabled = false;
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008017}
8018
Chris Wilsondc979972016-05-10 14:10:04 +01008019void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
Daniel Vetter8090c6b2012-06-24 16:42:32 +02008020{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008021 mutex_lock(&dev_priv->gt_pm.rps.lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07008022
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008023 intel_disable_rps(dev_priv);
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008024 if (HAS_LLC(dev_priv))
8025 intel_disable_llc_pstate(dev_priv);
8026
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008027 mutex_unlock(&dev_priv->gt_pm.rps.lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01008028}
8029
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008030static inline void intel_enable_llc_pstate(struct drm_i915_private *i915)
8031{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008032 lockdep_assert_held(&i915->gt_pm.rps.lock);
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008033
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008034 if (i915->gt_pm.llc_pstate.enabled)
8035 return;
8036
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008037 gen6_update_ring_freq(i915);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008038
8039 i915->gt_pm.llc_pstate.enabled = true;
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008040}
8041
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008042static void intel_enable_rps(struct drm_i915_private *dev_priv)
8043{
8044 struct intel_rps *rps = &dev_priv->gt_pm.rps;
8045
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008046 lockdep_assert_held(&rps->lock);
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008047
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008048 if (rps->enabled)
8049 return;
8050
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008051 if (IS_CHERRYVIEW(dev_priv)) {
8052 cherryview_enable_rps(dev_priv);
8053 } else if (IS_VALLEYVIEW(dev_priv)) {
8054 valleyview_enable_rps(dev_priv);
8055 } else if (INTEL_GEN(dev_priv) >= 9) {
8056 gen9_enable_rps(dev_priv);
8057 } else if (IS_BROADWELL(dev_priv)) {
8058 gen8_enable_rps(dev_priv);
8059 } else if (INTEL_GEN(dev_priv) >= 6) {
8060 gen6_enable_rps(dev_priv);
8061 } else if (IS_IRONLAKE_M(dev_priv)) {
8062 ironlake_enable_drps(dev_priv);
8063 intel_init_emon(dev_priv);
8064 }
8065
8066 WARN_ON(rps->max_freq < rps->min_freq);
8067 WARN_ON(rps->idle_freq > rps->max_freq);
8068
8069 WARN_ON(rps->efficient_freq < rps->min_freq);
8070 WARN_ON(rps->efficient_freq > rps->max_freq);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008071
8072 rps->enabled = true;
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008073}
8074
Chris Wilsonb7137e02016-07-13 09:10:37 +01008075void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
8076{
Chris Wilsonb7137e02016-07-13 09:10:37 +01008077 /* Powersaving is controlled by the host when inside a VM */
8078 if (intel_vgpu_active(dev_priv))
8079 return;
8080
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008081 mutex_lock(&dev_priv->gt_pm.rps.lock);
Imre Deak3cc134e2014-11-19 15:30:03 +02008082
Chris Wilson91cbdb82019-04-19 14:48:36 +01008083 if (HAS_RPS(dev_priv))
8084 intel_enable_rps(dev_priv);
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008085 if (HAS_LLC(dev_priv))
8086 intel_enable_llc_pstate(dev_priv);
8087
Chris Wilsonebb5eb72019-04-26 09:17:21 +01008088 mutex_unlock(&dev_priv->gt_pm.rps.lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01008089}
Imre Deakc6df39b2014-04-14 20:24:29 +03008090
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008091static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01008092{
Daniel Vetter3107bd42012-10-31 22:52:31 +01008093 /*
8094 * On Ibex Peak and Cougar Point, we need to disable clock
8095 * gating for the panel power sequencer or it will fail to
8096 * start up when no ports are active.
8097 */
8098 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
8099}
8100
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008101static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008102{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03008103 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008104
Damien Lespiau055e3932014-08-18 13:49:10 +01008105 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008106 I915_WRITE(DSPCNTR(pipe),
8107 I915_READ(DSPCNTR(pipe)) |
8108 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03008109
8110 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
8111 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008112 }
8113}
8114
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008115static void ilk_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008116{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008117 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008118
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01008119 /*
8120 * Required for FBC
8121 * WaFbcDisableDpfcClockGating:ilk
8122 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008123 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
8124 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
8125 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008126
8127 I915_WRITE(PCH_3DCGDIS0,
8128 MARIUNIT_CLOCK_GATE_DISABLE |
8129 SVSMUNIT_CLOCK_GATE_DISABLE);
8130 I915_WRITE(PCH_3DCGDIS1,
8131 VFMUNIT_CLOCK_GATE_DISABLE);
8132
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008133 /*
8134 * According to the spec the following bits should be set in
8135 * order to enable memory self-refresh
8136 * The bit 22/21 of 0x42004
8137 * The bit 5 of 0x42020
8138 * The bit 15 of 0x45000
8139 */
8140 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8141 (I915_READ(ILK_DISPLAY_CHICKEN2) |
8142 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008143 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008144 I915_WRITE(DISP_ARB_CTL,
8145 (I915_READ(DISP_ARB_CTL) |
8146 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02008147
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008148 /*
8149 * Based on the document from hardware guys the following bits
8150 * should be set unconditionally in order to enable FBC.
8151 * The bit 22 of 0x42000
8152 * The bit 22 of 0x42004
8153 * The bit 7,8,9 of 0x42020.
8154 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008155 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01008156 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008157 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8158 I915_READ(ILK_DISPLAY_CHICKEN1) |
8159 ILK_FBCQ_DIS);
8160 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8161 I915_READ(ILK_DISPLAY_CHICKEN2) |
8162 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008163 }
8164
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008165 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
8166
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008167 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8168 I915_READ(ILK_DISPLAY_CHICKEN2) |
8169 ILK_ELPIN_409_SELECT);
8170 I915_WRITE(_3D_CHICKEN2,
8171 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
8172 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02008173
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008174 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02008175 I915_WRITE(CACHE_MODE_0,
8176 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01008177
Akash Goel4e046322014-04-04 17:14:38 +05308178 /* WaDisable_RenderCache_OperationalFlush:ilk */
8179 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8180
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008181 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03008182
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008183 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008184}
8185
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008186static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01008187{
Ville Syrjäläd048a262019-08-21 20:30:31 +03008188 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008189 u32 val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01008190
8191 /*
8192 * On Ibex Peak and Cougar Point, we need to disable clock
8193 * gating for the panel power sequencer or it will fail to
8194 * start up when no ports are active.
8195 */
Jesse Barnescd664072013-10-02 10:34:19 -07008196 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
8197 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
8198 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008199 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
8200 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01008201 /* The below fixes the weird display corruption, a few pixels shifted
8202 * downward, on (only) LVDS of some HP laptops with IVY.
8203 */
Damien Lespiau055e3932014-08-18 13:49:10 +01008204 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03008205 val = I915_READ(TRANS_CHICKEN2(pipe));
8206 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
8207 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008208 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008209 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03008210 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
8211 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
8212 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008213 I915_WRITE(TRANS_CHICKEN2(pipe), val);
8214 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01008215 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01008216 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01008217 I915_WRITE(TRANS_CHICKEN1(pipe),
8218 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
8219 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008220}
8221
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008222static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008223{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008224 u32 tmp;
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008225
8226 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02008227 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
8228 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
8229 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008230}
8231
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008232static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008233{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008234 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008235
Damien Lespiau231e54f2012-10-19 17:55:41 +01008236 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008237
8238 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8239 I915_READ(ILK_DISPLAY_CHICKEN2) |
8240 ILK_ELPIN_409_SELECT);
8241
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008242 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01008243 I915_WRITE(_3D_CHICKEN,
8244 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
8245
Akash Goel4e046322014-04-04 17:14:38 +05308246 /* WaDisable_RenderCache_OperationalFlush:snb */
8247 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8248
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008249 /*
8250 * BSpec recoomends 8x4 when MSAA is used,
8251 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008252 *
8253 * Note that PS/WM thread counts depend on the WIZ hashing
8254 * disable bit, which we don't touch here, but it's good
8255 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008256 */
8257 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008258 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008259
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008260 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02008261 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008262
8263 I915_WRITE(GEN6_UCGCTL1,
8264 I915_READ(GEN6_UCGCTL1) |
8265 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
8266 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
8267
8268 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
8269 * gating disable must be set. Failure to set it results in
8270 * flickering pixels due to Z write ordering failures after
8271 * some amount of runtime in the Mesa "fire" demo, and Unigine
8272 * Sanctuary and Tropics, and apparently anything else with
8273 * alpha test or pixel discard.
8274 *
8275 * According to the spec, bit 11 (RCCUNIT) must also be set,
8276 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008277 *
Ville Syrjäläef593182014-01-22 21:32:47 +02008278 * WaDisableRCCUnitClockGating:snb
8279 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008280 */
8281 I915_WRITE(GEN6_UCGCTL2,
8282 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
8283 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
8284
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02008285 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02008286 I915_WRITE(_3D_CHICKEN3,
8287 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008288
8289 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02008290 * Bspec says:
8291 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
8292 * 3DSTATE_SF number of SF output attributes is more than 16."
8293 */
8294 I915_WRITE(_3D_CHICKEN3,
8295 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
8296
8297 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008298 * According to the spec the following bits should be
8299 * set in order to enable memory self-refresh and fbc:
8300 * The bit21 and bit22 of 0x42000
8301 * The bit21 and bit22 of 0x42004
8302 * The bit5 and bit7 of 0x42020
8303 * The bit14 of 0x70180
8304 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01008305 *
8306 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008307 */
8308 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8309 I915_READ(ILK_DISPLAY_CHICKEN1) |
8310 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
8311 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8312 I915_READ(ILK_DISPLAY_CHICKEN2) |
8313 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01008314 I915_WRITE(ILK_DSPCLK_GATE_D,
8315 I915_READ(ILK_DSPCLK_GATE_D) |
8316 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
8317 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008318
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008319 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07008320
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008321 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008322
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008323 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008324}
8325
8326static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
8327{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008328 u32 reg = I915_READ(GEN7_FF_THREAD_MODE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008329
Ville Syrjälä3aad9052014-01-22 21:32:59 +02008330 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02008331 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02008332 *
8333 * This actually overrides the dispatch
8334 * mode for all thread types.
8335 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008336 reg &= ~GEN7_FF_SCHED_MASK;
8337 reg |= GEN7_FF_TS_SCHED_HW;
8338 reg |= GEN7_FF_VS_SCHED_HW;
8339 reg |= GEN7_FF_DS_SCHED_HW;
8340
8341 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
8342}
8343
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008344static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008345{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008346 /*
8347 * TODO: this bit should only be enabled when really needed, then
8348 * disabled when not needed anymore in order to save power.
8349 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008350 if (HAS_PCH_LPT_LP(dev_priv))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008351 I915_WRITE(SOUTH_DSPCLK_GATE_D,
8352 I915_READ(SOUTH_DSPCLK_GATE_D) |
8353 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03008354
8355 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03008356 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
8357 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03008358 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008359}
8360
Ville Syrjälä712bf362016-10-31 22:37:23 +02008361static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03008362{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008363 if (HAS_PCH_LPT_LP(dev_priv)) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008364 u32 val = I915_READ(SOUTH_DSPCLK_GATE_D);
Imre Deak7d708ee2013-04-17 14:04:50 +03008365
8366 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8367 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8368 }
8369}
8370
Imre Deak450174f2016-05-03 15:54:21 +03008371static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
8372 int general_prio_credits,
8373 int high_prio_credits)
8374{
8375 u32 misccpctl;
Oscar Mateo930a7842017-10-17 13:25:45 -07008376 u32 val;
Imre Deak450174f2016-05-03 15:54:21 +03008377
8378 /* WaTempDisableDOPClkGating:bdw */
8379 misccpctl = I915_READ(GEN7_MISCCPCTL);
8380 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
8381
Oscar Mateo930a7842017-10-17 13:25:45 -07008382 val = I915_READ(GEN8_L3SQCREG1);
8383 val &= ~L3_PRIO_CREDITS_MASK;
8384 val |= L3_GENERAL_PRIO_CREDITS(general_prio_credits);
8385 val |= L3_HIGH_PRIO_CREDITS(high_prio_credits);
8386 I915_WRITE(GEN8_L3SQCREG1, val);
Imre Deak450174f2016-05-03 15:54:21 +03008387
8388 /*
8389 * Wait at least 100 clocks before re-enabling clock gating.
8390 * See the definition of L3SQCREG1 in BSpec.
8391 */
8392 POSTING_READ(GEN8_L3SQCREG1);
8393 udelay(1);
8394 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
8395}
8396
Oscar Mateod65dc3e2018-05-08 14:29:24 -07008397static void icl_init_clock_gating(struct drm_i915_private *dev_priv)
8398{
8399 /* This is not an Wa. Enable to reduce Sampler power */
8400 I915_WRITE(GEN10_DFR_RATIO_EN_AND_CHICKEN,
8401 I915_READ(GEN10_DFR_RATIO_EN_AND_CHICKEN) & ~DFR_DISABLE);
Radhakrishna Sripada622b3f62018-10-30 01:45:01 -07008402
8403 /* WaEnable32PlaneMode:icl */
8404 I915_WRITE(GEN9_CSFE_CHICKEN1_RCS,
8405 _MASKED_BIT_ENABLE(GEN11_ENABLE_32_PLANE_MODE));
Oscar Mateod65dc3e2018-05-08 14:29:24 -07008406}
8407
Michel Thierry5d869232019-08-23 01:20:34 -07008408static void tgl_init_clock_gating(struct drm_i915_private *dev_priv)
8409{
8410 u32 vd_pg_enable = 0;
8411 unsigned int i;
8412
8413 /* This is not a WA. Enable VD HCP & MFX_ENC powergate */
8414 for (i = 0; i < I915_MAX_VCS; i++) {
8415 if (HAS_ENGINE(dev_priv, _VCS(i)))
8416 vd_pg_enable |= VDN_HCP_POWERGATE_ENABLE(i) |
8417 VDN_MFX_POWERGATE_ENABLE(i);
8418 }
8419
8420 I915_WRITE(POWERGATE_ENABLE,
8421 I915_READ(POWERGATE_ENABLE) | vd_pg_enable);
8422}
8423
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008424static void cnp_init_clock_gating(struct drm_i915_private *dev_priv)
8425{
8426 if (!HAS_PCH_CNP(dev_priv))
8427 return;
8428
Rodrigo Vivi470e7c62018-03-05 17:28:12 -08008429 /* Display WA #1181 WaSouthDisplayDisablePWMCGEGating: cnp */
Rodrigo Vivi4cc6feb2017-09-08 16:45:33 -07008430 I915_WRITE(SOUTH_DSPCLK_GATE_D, I915_READ(SOUTH_DSPCLK_GATE_D) |
8431 CNP_PWM_CGE_GATING_DISABLE);
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008432}
8433
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008434static void cnl_init_clock_gating(struct drm_i915_private *dev_priv)
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008435{
Rodrigo Vivi8f067832017-09-05 12:30:13 -07008436 u32 val;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008437 cnp_init_clock_gating(dev_priv);
8438
Rodrigo Vivi1a25db62017-08-15 16:16:51 -07008439 /* This is not an Wa. Enable for better image quality */
8440 I915_WRITE(_3D_CHICKEN3,
8441 _MASKED_BIT_ENABLE(_3D_CHICKEN3_AA_LINE_QUALITY_FIX_ENABLE));
8442
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008443 /* WaEnableChickenDCPR:cnl */
8444 I915_WRITE(GEN8_CHICKEN_DCPR_1,
8445 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
8446
8447 /* WaFbcWakeMemOn:cnl */
8448 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
8449 DISP_FBC_MEMORY_WAKE);
8450
Chris Wilson34991bd2017-11-11 10:03:36 +00008451 val = I915_READ(SLICE_UNIT_LEVEL_CLKGATE);
8452 /* ReadHitWriteOnlyDisable:cnl */
8453 val |= RCCUNIT_CLKGATE_DIS;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008454 /* WaSarbUnitClockGatingDisable:cnl (pre-prod) */
8455 if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_B0))
Chris Wilson34991bd2017-11-11 10:03:36 +00008456 val |= SARBUNIT_CLKGATE_DIS;
8457 I915_WRITE(SLICE_UNIT_LEVEL_CLKGATE, val);
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08008458
Rodrigo Vivia4713c52018-03-07 14:09:12 -08008459 /* Wa_2201832410:cnl */
8460 val = I915_READ(SUBSLICE_UNIT_LEVEL_CLKGATE);
8461 val |= GWUNIT_CLKGATE_DIS;
8462 I915_WRITE(SUBSLICE_UNIT_LEVEL_CLKGATE, val);
8463
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08008464 /* WaDisableVFclkgate:cnl */
Rodrigo Vivi14941b62018-03-05 17:20:00 -08008465 /* WaVFUnitClockGatingDisable:cnl */
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08008466 val = I915_READ(UNSLICE_UNIT_LEVEL_CLKGATE);
8467 val |= VFUNIT_CLKGATE_DIS;
8468 I915_WRITE(UNSLICE_UNIT_LEVEL_CLKGATE, val);
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008469}
8470
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008471static void cfl_init_clock_gating(struct drm_i915_private *dev_priv)
8472{
8473 cnp_init_clock_gating(dev_priv);
8474 gen9_init_clock_gating(dev_priv);
8475
8476 /* WaFbcNukeOnHostModify:cfl */
8477 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8478 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
8479}
8480
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008481static void kbl_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008482{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008483 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008484
8485 /* WaDisableSDEUnitClockGating:kbl */
8486 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
8487 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8488 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03008489
8490 /* WaDisableGamClockGating:kbl */
8491 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
8492 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
8493 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008494
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008495 /* WaFbcNukeOnHostModify:kbl */
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008496 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8497 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008498}
8499
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008500static void skl_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008501{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008502 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03008503
8504 /* WAC6entrylatency:skl */
8505 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
8506 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008507
8508 /* WaFbcNukeOnHostModify:skl */
8509 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8510 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008511}
8512
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008513static void bdw_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008514{
Damien Lespiau07d27e22014-03-03 17:31:46 +00008515 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008516
Ben Widawskyab57fff2013-12-12 15:28:04 -08008517 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07008518 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008519
Ben Widawskyab57fff2013-12-12 15:28:04 -08008520 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008521 I915_WRITE(CHICKEN_PAR1_1,
8522 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
8523
Ben Widawskyab57fff2013-12-12 15:28:04 -08008524 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01008525 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00008526 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02008527 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02008528 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008529 }
Ben Widawsky63801f22013-12-12 17:26:03 -08008530
Ben Widawskyab57fff2013-12-12 15:28:04 -08008531 /* WaVSRefCountFullforceMissDisable:bdw */
8532 /* WaDSRefCountFullforceMissDisable:bdw */
8533 I915_WRITE(GEN7_FF_THREAD_MODE,
8534 I915_READ(GEN7_FF_THREAD_MODE) &
8535 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02008536
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02008537 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
8538 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02008539
8540 /* WaDisableSDEUnitClockGating:bdw */
8541 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8542 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00008543
Imre Deak450174f2016-05-03 15:54:21 +03008544 /* WaProgramL3SqcReg1Default:bdw */
8545 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03008546
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03008547 /* WaKVMNotificationOnConfigChange:bdw */
8548 I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
8549 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
8550
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008551 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00008552
8553 /* WaDisableDopClockGating:bdw
8554 *
8555 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
8556 * clock gating.
8557 */
8558 I915_WRITE(GEN6_UCGCTL1,
8559 I915_READ(GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008560}
8561
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008562static void hsw_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008563{
Francisco Jerezf3fc4882013-10-02 15:53:16 -07008564 /* L3 caching of data atomics doesn't work -- disable it. */
8565 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
8566 I915_WRITE(HSW_ROW_CHICKEN3,
8567 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
8568
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008569 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008570 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8571 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8572 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8573
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02008574 /* WaVSRefCountFullforceMissDisable:hsw */
8575 I915_WRITE(GEN7_FF_THREAD_MODE,
8576 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008577
Akash Goel4e046322014-04-04 17:14:38 +05308578 /* WaDisable_RenderCache_OperationalFlush:hsw */
8579 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8580
Chia-I Wufe27c602014-01-28 13:29:33 +08008581 /* enable HiZ Raw Stall Optimization */
8582 I915_WRITE(CACHE_MODE_0_GEN7,
8583 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
8584
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008585 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008586 I915_WRITE(CACHE_MODE_1,
8587 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03008588
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008589 /*
8590 * BSpec recommends 8x4 when MSAA is used,
8591 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008592 *
8593 * Note that PS/WM thread counts depend on the WIZ hashing
8594 * disable bit, which we don't touch here, but it's good
8595 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008596 */
8597 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008598 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008599
Kenneth Graunke94411592014-12-31 16:23:00 -08008600 /* WaSampleCChickenBitEnable:hsw */
8601 I915_WRITE(HALF_SLICE_CHICKEN3,
8602 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
8603
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008604 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07008605 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
8606
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008607 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008608}
8609
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008610static void ivb_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008611{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008612 u32 snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008613
Damien Lespiau231e54f2012-10-19 17:55:41 +01008614 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008615
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008616 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05008617 I915_WRITE(_3D_CHICKEN3,
8618 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
8619
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008620 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008621 I915_WRITE(IVB_CHICKEN3,
8622 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8623 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8624
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008625 /* WaDisablePSDDualDispatchEnable:ivb */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008626 if (IS_IVB_GT1(dev_priv))
Jesse Barnes12f33822012-10-25 12:15:45 -07008627 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
8628 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07008629
Akash Goel4e046322014-04-04 17:14:38 +05308630 /* WaDisable_RenderCache_OperationalFlush:ivb */
8631 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8632
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008633 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008634 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
8635 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
8636
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008637 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008638 I915_WRITE(GEN7_L3CNTLREG1,
8639 GEN7_WA_FOR_GEN7_L3_CONTROL);
8640 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07008641 GEN7_WA_L3_CHICKEN_MODE);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008642 if (IS_IVB_GT1(dev_priv))
Jesse Barnes8ab43972012-10-25 12:15:42 -07008643 I915_WRITE(GEN7_ROW_CHICKEN2,
8644 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02008645 else {
8646 /* must write both registers */
8647 I915_WRITE(GEN7_ROW_CHICKEN2,
8648 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07008649 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
8650 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02008651 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008652
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008653 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05008654 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
8655 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
8656
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02008657 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07008658 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008659 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008660 */
8661 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02008662 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07008663
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008664 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008665 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8666 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8667 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8668
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008669 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008670
8671 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02008672
Chris Wilson22721342014-03-04 09:41:43 +00008673 if (0) { /* causes HiZ corruption on ivb:gt1 */
8674 /* enable HiZ Raw Stall Optimization */
8675 I915_WRITE(CACHE_MODE_0_GEN7,
8676 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
8677 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08008678
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008679 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02008680 I915_WRITE(CACHE_MODE_1,
8681 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07008682
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008683 /*
8684 * BSpec recommends 8x4 when MSAA is used,
8685 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008686 *
8687 * Note that PS/WM thread counts depend on the WIZ hashing
8688 * disable bit, which we don't touch here, but it's good
8689 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008690 */
8691 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008692 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008693
Ben Widawsky20848222012-05-04 18:58:59 -07008694 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
8695 snpcr &= ~GEN6_MBC_SNPCR_MASK;
8696 snpcr |= GEN6_MBC_SNPCR_MED;
8697 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008698
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008699 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008700 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008701
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008702 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008703}
8704
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008705static void vlv_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008706{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008707 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05008708 I915_WRITE(_3D_CHICKEN3,
8709 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
8710
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008711 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008712 I915_WRITE(IVB_CHICKEN3,
8713 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8714 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8715
Ville Syrjäläfad7d362014-01-22 21:32:39 +02008716 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008717 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07008718 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08008719 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
8720 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07008721
Akash Goel4e046322014-04-04 17:14:38 +05308722 /* WaDisable_RenderCache_OperationalFlush:vlv */
8723 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8724
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008725 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05008726 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
8727 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
8728
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008729 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07008730 I915_WRITE(GEN7_ROW_CHICKEN2,
8731 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
8732
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008733 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008734 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8735 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8736 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8737
Ville Syrjälä46680e02014-01-22 21:33:01 +02008738 gen7_setup_fixed_func_scheduler(dev_priv);
8739
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02008740 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07008741 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008742 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008743 */
8744 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02008745 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07008746
Akash Goelc98f5062014-03-24 23:00:07 +05308747 /* WaDisableL3Bank2xClockGate:vlv
8748 * Disabling L3 clock gating- MMIO 940c[25] = 1
8749 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
8750 I915_WRITE(GEN7_UCGCTL4,
8751 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07008752
Ville Syrjäläafd58e72014-01-22 21:33:03 +02008753 /*
8754 * BSpec says this must be set, even though
8755 * WaDisable4x2SubspanOptimization isn't listed for VLV.
8756 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02008757 I915_WRITE(CACHE_MODE_1,
8758 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07008759
8760 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02008761 * BSpec recommends 8x4 when MSAA is used,
8762 * however in practice 16x4 seems fastest.
8763 *
8764 * Note that PS/WM thread counts depend on the WIZ hashing
8765 * disable bit, which we don't touch here, but it's good
8766 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
8767 */
8768 I915_WRITE(GEN7_GT_MODE,
8769 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
8770
8771 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02008772 * WaIncreaseL3CreditsForVLVB0:vlv
8773 * This is the hardware default actually.
8774 */
8775 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
8776
8777 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008778 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07008779 * Disable clock gating on th GCFG unit to prevent a delay
8780 * in the reporting of vblank events.
8781 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02008782 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008783}
8784
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008785static void chv_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03008786{
Ville Syrjälä232ce332014-04-09 13:28:35 +03008787 /* WaVSRefCountFullforceMissDisable:chv */
8788 /* WaDSRefCountFullforceMissDisable:chv */
8789 I915_WRITE(GEN7_FF_THREAD_MODE,
8790 I915_READ(GEN7_FF_THREAD_MODE) &
8791 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03008792
8793 /* WaDisableSemaphoreAndSyncFlipWait:chv */
8794 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
8795 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03008796
8797 /* WaDisableCSUnitClockGating:chv */
8798 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
8799 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03008800
8801 /* WaDisableSDEUnitClockGating:chv */
8802 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8803 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008804
8805 /*
Imre Deak450174f2016-05-03 15:54:21 +03008806 * WaProgramL3SqcReg1Default:chv
8807 * See gfxspecs/Related Documents/Performance Guide/
8808 * LSQC Setting Recommendations.
8809 */
8810 gen8_set_l3sqc_credits(dev_priv, 38, 2);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03008811}
8812
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008813static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008814{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008815 u32 dspclk_gate;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008816
8817 I915_WRITE(RENCLK_GATE_D1, 0);
8818 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
8819 GS_UNIT_CLOCK_GATE_DISABLE |
8820 CL_UNIT_CLOCK_GATE_DISABLE);
8821 I915_WRITE(RAMCLK_GATE_D, 0);
8822 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
8823 OVRUNIT_CLOCK_GATE_DISABLE |
8824 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008825 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008826 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
8827 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02008828
8829 /* WaDisableRenderCachePipelinedFlush */
8830 I915_WRITE(CACHE_MODE_0,
8831 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03008832
Akash Goel4e046322014-04-04 17:14:38 +05308833 /* WaDisable_RenderCache_OperationalFlush:g4x */
8834 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8835
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008836 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008837}
8838
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008839static void i965gm_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008840{
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01008841 struct intel_uncore *uncore = &dev_priv->uncore;
8842
8843 intel_uncore_write(uncore, RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
8844 intel_uncore_write(uncore, RENCLK_GATE_D2, 0);
8845 intel_uncore_write(uncore, DSPCLK_GATE_D, 0);
8846 intel_uncore_write(uncore, RAMCLK_GATE_D, 0);
8847 intel_uncore_write16(uncore, DEUC, 0);
8848 intel_uncore_write(uncore,
8849 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 */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01008853 intel_uncore_write(uncore,
8854 CACHE_MODE_0,
8855 _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008856}
8857
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008858static void i965g_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008859{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008860 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
8861 I965_RCC_CLOCK_GATE_DISABLE |
8862 I965_RCPB_CLOCK_GATE_DISABLE |
8863 I965_ISC_CLOCK_GATE_DISABLE |
8864 I965_FBC_CLOCK_GATE_DISABLE);
8865 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03008866 I915_WRITE(MI_ARB_STATE,
8867 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05308868
8869 /* WaDisable_RenderCache_OperationalFlush:gen4 */
8870 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008871}
8872
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008873static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008874{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008875 u32 dstate = I915_READ(D_STATE);
8876
8877 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
8878 DSTATE_DOT_CLOCK_GATING;
8879 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01008880
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008881 if (IS_PINEVIEW(dev_priv))
Chris Wilson13a86b82012-04-24 14:51:43 +01008882 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02008883
8884 /* IIR "flip pending" means done if this bit is set */
8885 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02008886
8887 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02008888 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02008889
8890 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
8891 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03008892
8893 I915_WRITE(MI_ARB_STATE,
8894 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008895}
8896
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008897static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008898{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008899 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02008900
8901 /* interrupts should cause a wake up from C3 */
8902 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
8903 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03008904
8905 I915_WRITE(MEM_MODE,
8906 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008907}
8908
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008909static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008910{
Ville Syrjälä10383922014-08-15 01:21:54 +03008911 I915_WRITE(MEM_MODE,
8912 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
8913 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008914}
8915
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008916void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008917{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008918 dev_priv->display.init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008919}
8920
Ville Syrjälä712bf362016-10-31 22:37:23 +02008921void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03008922{
Ville Syrjälä712bf362016-10-31 22:37:23 +02008923 if (HAS_PCH_LPT(dev_priv))
8924 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03008925}
8926
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008927static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02008928{
8929 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
8930}
8931
8932/**
8933 * intel_init_clock_gating_hooks - setup the clock gating hooks
8934 * @dev_priv: device private
8935 *
8936 * Setup the hooks that configure which clocks of a given platform can be
8937 * gated and also apply various GT and display specific workarounds for these
8938 * platforms. Note that some GT specific workarounds are applied separately
8939 * when GPU contexts or batchbuffers start their execution.
8940 */
8941void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
8942{
Lucas De Marchi13e53c52019-08-17 02:38:42 -07008943 if (IS_GEN(dev_priv, 12))
Michel Thierry5d869232019-08-23 01:20:34 -07008944 dev_priv->display.init_clock_gating = tgl_init_clock_gating;
Lucas De Marchi13e53c52019-08-17 02:38:42 -07008945 else if (IS_GEN(dev_priv, 11))
Oscar Mateod65dc3e2018-05-08 14:29:24 -07008946 dev_priv->display.init_clock_gating = icl_init_clock_gating;
Oscar Mateocc38cae2018-05-08 14:29:23 -07008947 else if (IS_CANNONLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008948 dev_priv->display.init_clock_gating = cnl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008949 else if (IS_COFFEELAKE(dev_priv))
8950 dev_priv->display.init_clock_gating = cfl_init_clock_gating;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008951 else if (IS_SKYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008952 dev_priv->display.init_clock_gating = skl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008953 else if (IS_KABYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008954 dev_priv->display.init_clock_gating = kbl_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02008955 else if (IS_BROXTON(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02008956 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02008957 else if (IS_GEMINILAKE(dev_priv))
8958 dev_priv->display.init_clock_gating = glk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008959 else if (IS_BROADWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008960 dev_priv->display.init_clock_gating = bdw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008961 else if (IS_CHERRYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008962 dev_priv->display.init_clock_gating = chv_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008963 else if (IS_HASWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008964 dev_priv->display.init_clock_gating = hsw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008965 else if (IS_IVYBRIDGE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008966 dev_priv->display.init_clock_gating = ivb_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008967 else if (IS_VALLEYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008968 dev_priv->display.init_clock_gating = vlv_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008969 else if (IS_GEN(dev_priv, 6))
Imre Deakbb400da2016-03-16 13:38:54 +02008970 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008971 else if (IS_GEN(dev_priv, 5))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008972 dev_priv->display.init_clock_gating = ilk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008973 else if (IS_G4X(dev_priv))
8974 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02008975 else if (IS_I965GM(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008976 dev_priv->display.init_clock_gating = i965gm_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02008977 else if (IS_I965G(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008978 dev_priv->display.init_clock_gating = i965g_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008979 else if (IS_GEN(dev_priv, 3))
Imre Deakbb400da2016-03-16 13:38:54 +02008980 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
8981 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
8982 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008983 else if (IS_GEN(dev_priv, 2))
Imre Deakbb400da2016-03-16 13:38:54 +02008984 dev_priv->display.init_clock_gating = i830_init_clock_gating;
8985 else {
8986 MISSING_CASE(INTEL_DEVID(dev_priv));
8987 dev_priv->display.init_clock_gating = nop_init_clock_gating;
8988 }
8989}
8990
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008991/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008992void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008993{
Daniel Vetterc921aba2012-04-26 23:28:17 +02008994 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008995 if (IS_PINEVIEW(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02008996 i915_pineview_get_mem_freq(dev_priv);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008997 else if (IS_GEN(dev_priv, 5))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02008998 i915_ironlake_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02008999
James Ausmusb068a862019-10-09 10:23:14 -07009000 if (intel_has_sagv(dev_priv))
9001 skl_setup_sagv_block_time(dev_priv);
9002
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009003 /* For FIFO watermark updates */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02009004 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02009005 skl_setup_wm_latency(dev_priv);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01009006 dev_priv->display.initial_watermarks = skl_initial_wm;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01009007 dev_priv->display.atomic_update_watermarks = skl_atomic_update_crtc_wm;
Matt Roper98d39492016-05-12 07:06:03 -07009008 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01009009 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02009010 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03009011
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009012 if ((IS_GEN(dev_priv, 5) && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02009013 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009014 (!IS_GEN(dev_priv, 5) && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02009015 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07009016 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08009017 dev_priv->display.compute_intermediate_wm =
9018 ilk_compute_intermediate_wm;
9019 dev_priv->display.initial_watermarks =
9020 ilk_initial_watermarks;
9021 dev_priv->display.optimize_watermarks =
9022 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02009023 } else {
9024 DRM_DEBUG_KMS("Failed to read display plane latency. "
9025 "Disable CxSR\n");
9026 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02009027 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02009028 vlv_setup_wm_latency(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02009029 dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
Ville Syrjälä4841da52017-03-02 19:14:59 +02009030 dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02009031 dev_priv->display.initial_watermarks = vlv_initial_watermarks;
Ville Syrjälä4841da52017-03-02 19:14:59 +02009032 dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
Ville Syrjäläff32c542017-03-02 19:14:57 +02009033 dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03009034 } else if (IS_G4X(dev_priv)) {
9035 g4x_setup_wm_latency(dev_priv);
9036 dev_priv->display.compute_pipe_wm = g4x_compute_pipe_wm;
9037 dev_priv->display.compute_intermediate_wm = g4x_compute_intermediate_wm;
9038 dev_priv->display.initial_watermarks = g4x_initial_watermarks;
9039 dev_priv->display.optimize_watermarks = g4x_optimize_watermarks;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02009040 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +00009041 if (!intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009042 dev_priv->is_ddr3,
9043 dev_priv->fsb_freq,
9044 dev_priv->mem_freq)) {
9045 DRM_INFO("failed to find known CxSR latency "
9046 "(found ddr%s fsb freq %d, mem freq %d), "
9047 "disabling CxSR\n",
9048 (dev_priv->is_ddr3 == 1) ? "3" : "2",
9049 dev_priv->fsb_freq, dev_priv->mem_freq);
9050 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03009051 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009052 dev_priv->display.update_wm = NULL;
9053 } else
9054 dev_priv->display.update_wm = pineview_update_wm;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009055 } else if (IS_GEN(dev_priv, 4)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009056 dev_priv->display.update_wm = i965_update_wm;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009057 } else if (IS_GEN(dev_priv, 3)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009058 dev_priv->display.update_wm = i9xx_update_wm;
9059 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08009060 } else if (IS_GEN(dev_priv, 2)) {
Jani Nikula24977872019-09-11 12:26:08 +03009061 if (INTEL_NUM_PIPES(dev_priv) == 1) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009062 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009063 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009064 } else {
9065 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009066 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009067 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009068 } else {
9069 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009070 }
9071}
9072
Ville Syrjälädd06f882014-11-10 22:55:12 +02009073static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
9074{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009075 struct intel_rps *rps = &dev_priv->gt_pm.rps;
9076
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009077 /*
9078 * N = val - 0xb7
9079 * Slow = Fast = GPLL ref * N
9080 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009081 return DIV_ROUND_CLOSEST(rps->gpll_ref_freq * (val - 0xb7), 1000);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009082}
9083
Fengguang Wub55dd642014-07-12 11:21:39 +02009084static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009085{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009086 struct intel_rps *rps = &dev_priv->gt_pm.rps;
9087
9088 return DIV_ROUND_CLOSEST(1000 * val, rps->gpll_ref_freq) + 0xb7;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009089}
9090
Fengguang Wub55dd642014-07-12 11:21:39 +02009091static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05309092{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009093 struct intel_rps *rps = &dev_priv->gt_pm.rps;
9094
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009095 /*
9096 * N = val / 2
9097 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
9098 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009099 return DIV_ROUND_CLOSEST(rps->gpll_ref_freq * val, 2 * 2 * 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05309100}
9101
Fengguang Wub55dd642014-07-12 11:21:39 +02009102static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05309103{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009104 struct intel_rps *rps = &dev_priv->gt_pm.rps;
9105
Ville Syrjälä1c147622014-08-18 14:42:43 +03009106 /* CHV needs even values */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009107 return DIV_ROUND_CLOSEST(2 * 1000 * val, rps->gpll_ref_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05309108}
9109
Ville Syrjälä616bc822015-01-23 21:04:25 +02009110int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
9111{
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07009112 if (INTEL_GEN(dev_priv) >= 9)
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009113 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
9114 GEN9_FREQ_SCALER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009115 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009116 return chv_gpu_freq(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009117 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009118 return byt_gpu_freq(dev_priv, val);
9119 else
9120 return val * GT_FREQUENCY_MULTIPLIER;
9121}
9122
Ville Syrjälä616bc822015-01-23 21:04:25 +02009123int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
9124{
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07009125 if (INTEL_GEN(dev_priv) >= 9)
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009126 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
9127 GT_FREQUENCY_MULTIPLIER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009128 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009129 return chv_freq_opcode(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009130 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009131 return byt_freq_opcode(dev_priv, val);
9132 else
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009133 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
Deepak S22b1b2f2014-07-12 14:54:33 +05309134}
9135
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00009136void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01009137{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01009138 mutex_init(&dev_priv->gt_pm.rps.lock);
Chris Wilson60548c52018-07-31 14:26:29 +01009139 mutex_init(&dev_priv->gt_pm.rps.power.mutex);
Daniel Vetterf742a552013-12-06 10:17:53 +01009140
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009141 atomic_set(&dev_priv->gt_pm.rps.num_waiters, 0);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03009142
Sagar Arun Kamblead1443f2017-10-10 22:30:04 +01009143 dev_priv->runtime_pm.suspended = false;
9144 atomic_set(&dev_priv->runtime_pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01009145}
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009146
Tvrtko Ursulinc84b2702017-11-21 18:18:44 +00009147u32 intel_get_cagf(struct drm_i915_private *dev_priv, u32 rpstat)
9148{
9149 u32 cagf;
9150
9151 if (INTEL_GEN(dev_priv) >= 9)
9152 cagf = (rpstat & GEN9_CAGF_MASK) >> GEN9_CAGF_SHIFT;
9153 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
9154 cagf = (rpstat & HSW_CAGF_MASK) >> HSW_CAGF_SHIFT;
9155 else
9156 cagf = (rpstat & GEN6_CAGF_MASK) >> GEN6_CAGF_SHIFT;
9157
9158 return cagf;
9159}