blob: 3622344490874f6135c5ffc5aeee43d4eaed8cde [file] [log] [blame]
Eugeni Dodonov85208be2012-04-16 22:20:34 -03001/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eugeni Dodonov <eugeni.dodonov@intel.com>
25 *
26 */
27
Sam Ravnborgd0e93592019-01-26 13:25:24 +010028#include <linux/module.h>
Chris Wilson08ea70a2018-08-12 23:36:31 +010029#include <linux/pm_runtime.h>
Sam Ravnborgd0e93592019-01-26 13:25:24 +010030
31#include <drm/drm_atomic_helper.h>
32#include <drm/drm_fourcc.h>
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -070033#include <drm/drm_plane_helper.h>
Sam Ravnborgd0e93592019-01-26 13:25:24 +010034
Jani Nikuladf0566a2019-06-13 11:44:16 +030035#include "display/intel_atomic.h"
Jani Nikula1d455f82019-08-06 14:39:33 +030036#include "display/intel_display_types.h"
Jani Nikuladf0566a2019-06-13 11:44:16 +030037#include "display/intel_fbc.h"
38#include "display/intel_sprite.h"
39
Andi Shyti0dc3c562019-10-20 19:41:39 +010040#include "gt/intel_llc.h"
41
Eugeni Dodonov85208be2012-04-16 22:20:34 -030042#include "i915_drv.h"
Jani Nikula440e2b32019-04-29 15:29:27 +030043#include "i915_irq.h"
Jani Nikulaa09d9a82019-08-06 13:07:28 +030044#include "i915_trace.h"
Jani Nikula696173b2019-04-05 14:00:15 +030045#include "intel_pm.h"
Chris Wilson56c50982019-04-26 09:17:22 +010046#include "intel_sideband.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020047#include "../../../platform/x86/intel_ips.h"
Eugeni Dodonov85208be2012-04-16 22:20:34 -030048
Ville Syrjälä46f16e62016-10-31 22:37:22 +020049static void gen9_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppalab033bb62016-06-07 17:19:04 +030050{
Ville Syrjälä93564042017-08-24 22:10:51 +030051 if (HAS_LLC(dev_priv)) {
52 /*
53 * WaCompressedResourceDisplayNewHashMode:skl,kbl
Lucas De Marchie0403cb2017-12-05 11:01:17 -080054 * Display WA #0390: skl,kbl
Ville Syrjälä93564042017-08-24 22:10:51 +030055 *
56 * Must match Sampler, Pixel Back End, and Media. See
57 * WaCompressedResourceSamplerPbeMediaNewHashMode.
58 */
59 I915_WRITE(CHICKEN_PAR1_1,
60 I915_READ(CHICKEN_PAR1_1) |
61 SKL_DE_COMPRESSED_HASH_MODE);
62 }
63
Rodrigo Vivi82525c12017-06-08 08:50:00 -070064 /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl,cfl */
Mika Kuoppalab033bb62016-06-07 17:19:04 +030065 I915_WRITE(CHICKEN_PAR1_1,
66 I915_READ(CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
67
Rodrigo Vivi82525c12017-06-08 08:50:00 -070068 /* WaEnableChickenDCPR:skl,bxt,kbl,glk,cfl */
Mika Kuoppala590e8ff2016-06-07 17:19:13 +030069 I915_WRITE(GEN8_CHICKEN_DCPR_1,
70 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
Mika Kuoppala0f78dee2016-06-07 17:19:16 +030071
Rodrigo Vivi82525c12017-06-08 08:50:00 -070072 /* WaFbcTurnOffFbcWatermark:skl,bxt,kbl,cfl */
73 /* WaFbcWakeMemOn:skl,bxt,kbl,glk,cfl */
Mika Kuoppala303d4ea2016-06-07 17:19:17 +030074 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
75 DISP_FBC_WM_DIS |
76 DISP_FBC_MEMORY_WAKE);
Mika Kuoppalad1b4eef2016-06-07 17:19:19 +030077
Rodrigo Vivi82525c12017-06-08 08:50:00 -070078 /* WaFbcHighMemBwCorruptionAvoidance:skl,bxt,kbl,cfl */
Mika Kuoppalad1b4eef2016-06-07 17:19:19 +030079 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
80 ILK_DPFC_DISABLE_DUMMY0);
Praveen Paneri32087d12017-08-03 23:02:10 +053081
82 if (IS_SKYLAKE(dev_priv)) {
83 /* WaDisableDopClockGating */
84 I915_WRITE(GEN7_MISCCPCTL, I915_READ(GEN7_MISCCPCTL)
85 & ~GEN7_DOP_CLOCK_GATE_ENABLE);
86 }
Mika Kuoppalab033bb62016-06-07 17:19:04 +030087}
88
Ville Syrjälä46f16e62016-10-31 22:37:22 +020089static void bxt_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deaka82abe42015-03-27 14:00:04 +020090{
Ville Syrjälä46f16e62016-10-31 22:37:22 +020091 gen9_init_clock_gating(dev_priv);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +020092
Nick Hoatha7546152015-06-29 14:07:32 +010093 /* WaDisableSDEUnitClockGating:bxt */
94 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
95 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
96
Imre Deak32608ca2015-03-11 11:10:27 +020097 /*
98 * FIXME:
Ben Widawsky868434c2015-03-11 10:49:32 +020099 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
Imre Deak32608ca2015-03-11 11:10:27 +0200100 */
Imre Deak32608ca2015-03-11 11:10:27 +0200101 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
Ben Widawsky868434c2015-03-11 10:49:32 +0200102 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
Imre Deakd965e7ac2015-12-01 10:23:52 +0200103
104 /*
105 * Wa: Backlight PWM may stop in the asserted state, causing backlight
106 * to stay fully on.
107 */
Jani Nikula8aeaf642017-02-15 17:21:37 +0200108 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
109 PWM1_GATING_DIS | PWM2_GATING_DIS);
Imre Deaka82abe42015-03-27 14:00:04 +0200110}
111
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200112static void glk_init_clock_gating(struct drm_i915_private *dev_priv)
113{
114 gen9_init_clock_gating(dev_priv);
115
116 /*
117 * WaDisablePWMClockGating:glk
118 * Backlight PWM may stop in the asserted state, causing backlight
119 * to stay fully on.
120 */
121 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
122 PWM1_GATING_DIS | PWM2_GATING_DIS);
Ander Conselvan de Oliveiraf4f4b592017-02-22 08:34:29 +0200123
124 /* WaDDIIOTimeout:glk */
125 if (IS_GLK_REVID(dev_priv, 0, GLK_REVID_A1)) {
126 u32 val = I915_READ(CHICKEN_MISC_2);
127 val &= ~(GLK_CL0_PWR_DOWN |
128 GLK_CL1_PWR_DOWN |
129 GLK_CL2_PWR_DOWN);
130 I915_WRITE(CHICKEN_MISC_2, val);
131 }
132
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200133}
134
Ville Syrjälä148ac1f2016-10-31 22:37:16 +0200135static void i915_pineview_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200136{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200137 u32 tmp;
138
139 tmp = I915_READ(CLKCFG);
140
141 switch (tmp & CLKCFG_FSB_MASK) {
142 case CLKCFG_FSB_533:
143 dev_priv->fsb_freq = 533; /* 133*4 */
144 break;
145 case CLKCFG_FSB_800:
146 dev_priv->fsb_freq = 800; /* 200*4 */
147 break;
148 case CLKCFG_FSB_667:
149 dev_priv->fsb_freq = 667; /* 167*4 */
150 break;
151 case CLKCFG_FSB_400:
152 dev_priv->fsb_freq = 400; /* 100*4 */
153 break;
154 }
155
156 switch (tmp & CLKCFG_MEM_MASK) {
157 case CLKCFG_MEM_533:
158 dev_priv->mem_freq = 533;
159 break;
160 case CLKCFG_MEM_667:
161 dev_priv->mem_freq = 667;
162 break;
163 case CLKCFG_MEM_800:
164 dev_priv->mem_freq = 800;
165 break;
166 }
167
168 /* detect pineview DDR3 setting */
169 tmp = I915_READ(CSHRDDR3CTL);
170 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
171}
172
Ville Syrjälä148ac1f2016-10-31 22:37:16 +0200173static void i915_ironlake_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200174{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200175 u16 ddrpll, csipll;
176
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +0100177 ddrpll = intel_uncore_read16(&dev_priv->uncore, DDRMPLL1);
178 csipll = intel_uncore_read16(&dev_priv->uncore, CSIPLL0);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200179
180 switch (ddrpll & 0xff) {
181 case 0xc:
182 dev_priv->mem_freq = 800;
183 break;
184 case 0x10:
185 dev_priv->mem_freq = 1066;
186 break;
187 case 0x14:
188 dev_priv->mem_freq = 1333;
189 break;
190 case 0x18:
191 dev_priv->mem_freq = 1600;
192 break;
193 default:
194 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
195 ddrpll & 0xff);
196 dev_priv->mem_freq = 0;
197 break;
198 }
199
Daniel Vetter20e4d402012-08-08 23:35:39 +0200200 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200201
202 switch (csipll & 0x3ff) {
203 case 0x00c:
204 dev_priv->fsb_freq = 3200;
205 break;
206 case 0x00e:
207 dev_priv->fsb_freq = 3733;
208 break;
209 case 0x010:
210 dev_priv->fsb_freq = 4266;
211 break;
212 case 0x012:
213 dev_priv->fsb_freq = 4800;
214 break;
215 case 0x014:
216 dev_priv->fsb_freq = 5333;
217 break;
218 case 0x016:
219 dev_priv->fsb_freq = 5866;
220 break;
221 case 0x018:
222 dev_priv->fsb_freq = 6400;
223 break;
224 default:
225 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
226 csipll & 0x3ff);
227 dev_priv->fsb_freq = 0;
228 break;
229 }
230
231 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200232 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200233 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200234 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200235 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200236 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200237 }
238}
239
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300240static const struct cxsr_latency cxsr_latency_table[] = {
241 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
242 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
243 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
244 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
245 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
246
247 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
248 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
249 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
250 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
251 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
252
253 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
254 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
255 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
256 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
257 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
258
259 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
260 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
261 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
262 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
263 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
264
265 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
266 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
267 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
268 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
269 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
270
271 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
272 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
273 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
274 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
275 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
276};
277
Tvrtko Ursulin44a655c2016-10-13 11:09:23 +0100278static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
279 bool is_ddr3,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300280 int fsb,
281 int mem)
282{
283 const struct cxsr_latency *latency;
284 int i;
285
286 if (fsb == 0 || mem == 0)
287 return NULL;
288
289 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
290 latency = &cxsr_latency_table[i];
291 if (is_desktop == latency->is_desktop &&
292 is_ddr3 == latency->is_ddr3 &&
293 fsb == latency->fsb_freq && mem == latency->mem_freq)
294 return latency;
295 }
296
297 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
298
299 return NULL;
300}
301
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200302static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
303{
304 u32 val;
305
Chris Wilson337fa6e2019-04-26 09:17:20 +0100306 vlv_punit_get(dev_priv);
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200307
308 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
309 if (enable)
310 val &= ~FORCE_DDR_HIGH_FREQ;
311 else
312 val |= FORCE_DDR_HIGH_FREQ;
313 val &= ~FORCE_DDR_LOW_FREQ;
314 val |= FORCE_DDR_FREQ_REQ_ACK;
315 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
316
317 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
318 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
319 DRM_ERROR("timed out waiting for Punit DDR DVFS request\n");
320
Chris Wilson337fa6e2019-04-26 09:17:20 +0100321 vlv_punit_put(dev_priv);
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200322}
323
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200324static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
325{
326 u32 val;
327
Chris Wilson337fa6e2019-04-26 09:17:20 +0100328 vlv_punit_get(dev_priv);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200329
Ville Syrjäläc11b8132018-11-29 19:55:03 +0200330 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200331 if (enable)
332 val |= DSP_MAXFIFO_PM5_ENABLE;
333 else
334 val &= ~DSP_MAXFIFO_PM5_ENABLE;
Ville Syrjäläc11b8132018-11-29 19:55:03 +0200335 vlv_punit_write(dev_priv, PUNIT_REG_DSPSSPM, val);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200336
Chris Wilson337fa6e2019-04-26 09:17:20 +0100337 vlv_punit_put(dev_priv);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200338}
339
Ville Syrjäläf4998962015-03-10 17:02:21 +0200340#define FW_WM(value, plane) \
341 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
342
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200343static bool _intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300344{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200345 bool was_enabled;
Imre Deak5209b1f2014-07-01 12:36:17 +0300346 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300347
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100348 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200349 was_enabled = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300350 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300351 POSTING_READ(FW_BLC_SELF_VLV);
Jani Nikulac0f86832016-12-07 12:13:04 +0200352 } else if (IS_G4X(dev_priv) || IS_I965GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200353 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300354 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300355 POSTING_READ(FW_BLC_SELF);
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +0200356 } else if (IS_PINEVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200357 val = I915_READ(DSPFW3);
358 was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
359 if (enable)
360 val |= PINEVIEW_SELF_REFRESH_EN;
361 else
362 val &= ~PINEVIEW_SELF_REFRESH_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300363 I915_WRITE(DSPFW3, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300364 POSTING_READ(DSPFW3);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100365 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200366 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300367 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
368 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
369 I915_WRITE(FW_BLC_SELF, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300370 POSTING_READ(FW_BLC_SELF);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100371 } else if (IS_I915GM(dev_priv)) {
Ville Syrjäläacb91352016-07-29 17:57:02 +0300372 /*
373 * FIXME can't find a bit like this for 915G, and
374 * and yet it does have the related watermark in
375 * FW_BLC_SELF. What's going on?
376 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200377 was_enabled = I915_READ(INSTPM) & INSTPM_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300378 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
379 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
380 I915_WRITE(INSTPM, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300381 POSTING_READ(INSTPM);
Imre Deak5209b1f2014-07-01 12:36:17 +0300382 } else {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200383 return false;
Imre Deak5209b1f2014-07-01 12:36:17 +0300384 }
385
Ville Syrjälä1489bba2017-03-02 19:15:07 +0200386 trace_intel_memory_cxsr(dev_priv, was_enabled, enable);
387
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200388 DRM_DEBUG_KMS("memory self-refresh is %s (was %s)\n",
389 enableddisabled(enable),
390 enableddisabled(was_enabled));
391
392 return was_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300393}
394
Ville Syrjälä62571fc2017-04-21 21:14:23 +0300395/**
396 * intel_set_memory_cxsr - Configure CxSR state
397 * @dev_priv: i915 device
398 * @enable: Allow vs. disallow CxSR
399 *
400 * Allow or disallow the system to enter a special CxSR
401 * (C-state self refresh) state. What typically happens in CxSR mode
402 * is that several display FIFOs may get combined into a single larger
403 * FIFO for a particular plane (so called max FIFO mode) to allow the
404 * system to defer memory fetches longer, and the memory will enter
405 * self refresh.
406 *
407 * Note that enabling CxSR does not guarantee that the system enter
408 * this special mode, nor does it guarantee that the system stays
409 * in that mode once entered. So this just allows/disallows the system
410 * to autonomously utilize the CxSR mode. Other factors such as core
411 * C-states will affect when/if the system actually enters/exits the
412 * CxSR mode.
413 *
414 * Note that on VLV/CHV this actually only controls the max FIFO mode,
415 * and the system is free to enter/exit memory self refresh at any time
416 * even when the use of CxSR has been disallowed.
417 *
418 * While the system is actually in the CxSR/max FIFO mode, some plane
419 * control registers will not get latched on vblank. Thus in order to
420 * guarantee the system will respond to changes in the plane registers
421 * we must always disallow CxSR prior to making changes to those registers.
422 * Unfortunately the system will re-evaluate the CxSR conditions at
423 * frame start which happens after vblank start (which is when the plane
424 * registers would get latched), so we can't proceed with the plane update
425 * during the same frame where we disallowed CxSR.
426 *
427 * Certain platforms also have a deeper HPLL SR mode. Fortunately the
428 * HPLL SR mode depends on CxSR itself, so we don't have to hand hold
429 * the hardware w.r.t. HPLL SR when writing to plane registers.
430 * Disallowing just CxSR is sufficient.
431 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200432bool intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200433{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200434 bool ret;
435
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200436 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200437 ret = _intel_set_memory_cxsr(dev_priv, enable);
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300438 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
439 dev_priv->wm.vlv.cxsr = enable;
440 else if (IS_G4X(dev_priv))
441 dev_priv->wm.g4x.cxsr = enable;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200442 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200443
444 return ret;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200445}
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200446
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300447/*
448 * Latency for FIFO fetches is dependent on several factors:
449 * - memory configuration (speed, channels)
450 * - chipset
451 * - current MCH state
452 * It can be fairly high in some situations, so here we assume a fairly
453 * pessimal value. It's a tradeoff between extra memory fetches (if we
454 * set this value too high, the FIFO will fetch frequently to stay full)
455 * and power consumption (set it too low to save power and we might see
456 * FIFO underruns and display "flicker").
457 *
458 * A value of 5us seems to be a good balance; safe for very low end
459 * platforms but not overly aggressive on lower latency configs.
460 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100461static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300462
Ville Syrjäläb5004722015-03-05 21:19:47 +0200463#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
464 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
465
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200466static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
Ville Syrjäläb5004722015-03-05 21:19:47 +0200467{
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200468 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200469 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200470 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200471 enum pipe pipe = crtc->pipe;
472 int sprite0_start, sprite1_start;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200473
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200474 switch (pipe) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200475 u32 dsparb, dsparb2, dsparb3;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200476 case PIPE_A:
477 dsparb = I915_READ(DSPARB);
478 dsparb2 = I915_READ(DSPARB2);
479 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
480 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
481 break;
482 case PIPE_B:
483 dsparb = I915_READ(DSPARB);
484 dsparb2 = I915_READ(DSPARB2);
485 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
486 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
487 break;
488 case PIPE_C:
489 dsparb2 = I915_READ(DSPARB2);
490 dsparb3 = I915_READ(DSPARB3);
491 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
492 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
493 break;
494 default:
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200495 MISSING_CASE(pipe);
496 return;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200497 }
498
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200499 fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
500 fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
501 fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
502 fifo_state->plane[PLANE_CURSOR] = 63;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200503}
504
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200505static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv,
506 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300507{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200508 u32 dsparb = I915_READ(DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300509 int size;
510
511 size = dsparb & 0x7f;
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200512 if (i9xx_plane == PLANE_B)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300513 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
514
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200515 DRM_DEBUG_KMS("FIFO size - (0x%08x) %c: %d\n",
516 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300517
518 return size;
519}
520
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200521static int i830_get_fifo_size(struct drm_i915_private *dev_priv,
522 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300523{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200524 u32 dsparb = I915_READ(DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300525 int size;
526
527 size = dsparb & 0x1ff;
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200528 if (i9xx_plane == PLANE_B)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300529 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
530 size >>= 1; /* Convert to cachelines */
531
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200532 DRM_DEBUG_KMS("FIFO size - (0x%08x) %c: %d\n",
533 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300534
535 return size;
536}
537
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200538static int i845_get_fifo_size(struct drm_i915_private *dev_priv,
539 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300540{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200541 u32 dsparb = I915_READ(DSPARB);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300542 int size;
543
544 size = dsparb & 0x7f;
545 size >>= 2; /* Convert to cachelines */
546
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200547 DRM_DEBUG_KMS("FIFO size - (0x%08x) %c: %d\n",
548 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300549
550 return size;
551}
552
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300553/* Pineview has different values for various configs */
554static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300555 .fifo_size = PINEVIEW_DISPLAY_FIFO,
556 .max_wm = PINEVIEW_MAX_WM,
557 .default_wm = PINEVIEW_DFT_WM,
558 .guard_size = PINEVIEW_GUARD_WM,
559 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300560};
561static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300562 .fifo_size = PINEVIEW_DISPLAY_FIFO,
563 .max_wm = PINEVIEW_MAX_WM,
564 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
565 .guard_size = PINEVIEW_GUARD_WM,
566 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300567};
568static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300569 .fifo_size = PINEVIEW_CURSOR_FIFO,
570 .max_wm = PINEVIEW_CURSOR_MAX_WM,
571 .default_wm = PINEVIEW_CURSOR_DFT_WM,
572 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
573 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300574};
575static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300576 .fifo_size = PINEVIEW_CURSOR_FIFO,
577 .max_wm = PINEVIEW_CURSOR_MAX_WM,
578 .default_wm = PINEVIEW_CURSOR_DFT_WM,
579 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
580 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300581};
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300582static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300583 .fifo_size = I965_CURSOR_FIFO,
584 .max_wm = I965_CURSOR_MAX_WM,
585 .default_wm = I965_CURSOR_DFT_WM,
586 .guard_size = 2,
587 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300588};
589static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300590 .fifo_size = I945_FIFO_SIZE,
591 .max_wm = I915_MAX_WM,
592 .default_wm = 1,
593 .guard_size = 2,
594 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300595};
596static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300597 .fifo_size = I915_FIFO_SIZE,
598 .max_wm = I915_MAX_WM,
599 .default_wm = 1,
600 .guard_size = 2,
601 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300602};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300603static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300604 .fifo_size = I855GM_FIFO_SIZE,
605 .max_wm = I915_MAX_WM,
606 .default_wm = 1,
607 .guard_size = 2,
608 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300609};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300610static const struct intel_watermark_params i830_bc_wm_info = {
611 .fifo_size = I855GM_FIFO_SIZE,
612 .max_wm = I915_MAX_WM/2,
613 .default_wm = 1,
614 .guard_size = 2,
615 .cacheline_size = I830_FIFO_LINE_SIZE,
616};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200617static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300618 .fifo_size = I830_FIFO_SIZE,
619 .max_wm = I915_MAX_WM,
620 .default_wm = 1,
621 .guard_size = 2,
622 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300623};
624
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300625/**
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300626 * intel_wm_method1 - Method 1 / "small buffer" watermark formula
627 * @pixel_rate: Pipe pixel rate in kHz
628 * @cpp: Plane bytes per pixel
629 * @latency: Memory wakeup latency in 0.1us units
630 *
631 * Compute the watermark using the method 1 or "small buffer"
632 * formula. The caller may additonally add extra cachelines
633 * to account for TLB misses and clock crossings.
634 *
635 * This method is concerned with the short term drain rate
636 * of the FIFO, ie. it does not account for blanking periods
637 * which would effectively reduce the average drain rate across
638 * a longer period. The name "small" refers to the fact the
639 * FIFO is relatively small compared to the amount of data
640 * fetched.
641 *
642 * The FIFO level vs. time graph might look something like:
643 *
644 * |\ |\
645 * | \ | \
646 * __---__---__ (- plane active, _ blanking)
647 * -> time
648 *
649 * or perhaps like this:
650 *
651 * |\|\ |\|\
652 * __----__----__ (- plane active, _ blanking)
653 * -> time
654 *
655 * Returns:
656 * The watermark in bytes
657 */
658static unsigned int intel_wm_method1(unsigned int pixel_rate,
659 unsigned int cpp,
660 unsigned int latency)
661{
Jani Nikula5ce9a6492019-01-18 14:01:20 +0200662 u64 ret;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300663
Ville Syrjäläd492a292019-04-08 18:27:01 +0300664 ret = mul_u32_u32(pixel_rate, cpp * latency);
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300665 ret = DIV_ROUND_UP_ULL(ret, 10000);
666
667 return ret;
668}
669
670/**
671 * intel_wm_method2 - Method 2 / "large buffer" watermark formula
672 * @pixel_rate: Pipe pixel rate in kHz
673 * @htotal: Pipe horizontal total
674 * @width: Plane width in pixels
675 * @cpp: Plane bytes per pixel
676 * @latency: Memory wakeup latency in 0.1us units
677 *
678 * Compute the watermark using the method 2 or "large buffer"
679 * formula. The caller may additonally add extra cachelines
680 * to account for TLB misses and clock crossings.
681 *
682 * This method is concerned with the long term drain rate
683 * of the FIFO, ie. it does account for blanking periods
684 * which effectively reduce the average drain rate across
685 * a longer period. The name "large" refers to the fact the
686 * FIFO is relatively large compared to the amount of data
687 * fetched.
688 *
689 * The FIFO level vs. time graph might look something like:
690 *
691 * |\___ |\___
692 * | \___ | \___
693 * | \ | \
694 * __ --__--__--__--__--__--__ (- plane active, _ blanking)
695 * -> time
696 *
697 * Returns:
698 * The watermark in bytes
699 */
700static unsigned int intel_wm_method2(unsigned int pixel_rate,
701 unsigned int htotal,
702 unsigned int width,
703 unsigned int cpp,
704 unsigned int latency)
705{
706 unsigned int ret;
707
708 /*
709 * FIXME remove once all users are computing
710 * watermarks in the correct place.
711 */
712 if (WARN_ON_ONCE(htotal == 0))
713 htotal = 1;
714
715 ret = (latency * pixel_rate) / (htotal * 10000);
716 ret = (ret + 1) * width * cpp;
717
718 return ret;
719}
720
721/**
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300722 * intel_calculate_wm - calculate watermark level
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300723 * @pixel_rate: pixel clock
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300724 * @wm: chip FIFO params
Chris Wilson31383412018-02-14 14:03:03 +0000725 * @fifo_size: size of the FIFO buffer
Ville Syrjäläac484962016-01-20 21:05:26 +0200726 * @cpp: bytes per pixel
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300727 * @latency_ns: memory latency for the platform
728 *
729 * Calculate the watermark level (the level at which the display plane will
730 * start fetching from memory again). Each chip has a different display
731 * FIFO size and allocation, so the caller needs to figure that out and pass
732 * in the correct intel_watermark_params structure.
733 *
734 * As the pixel clock runs, the FIFO will be drained at a rate that depends
735 * on the pixel size. When it reaches the watermark level, it'll start
736 * fetching FIFO line sized based chunks from memory until the FIFO fills
737 * past the watermark point. If the FIFO drains completely, a FIFO underrun
738 * will occur, and a display engine hang could result.
739 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300740static unsigned int intel_calculate_wm(int pixel_rate,
741 const struct intel_watermark_params *wm,
742 int fifo_size, int cpp,
743 unsigned int latency_ns)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300744{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300745 int entries, wm_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300746
747 /*
748 * Note: we need to make sure we don't overflow for various clock &
749 * latency values.
750 * clocks go from a few thousand to several hundred thousand.
751 * latency is usually a few thousand
752 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300753 entries = intel_wm_method1(pixel_rate, cpp,
754 latency_ns / 100);
755 entries = DIV_ROUND_UP(entries, wm->cacheline_size) +
756 wm->guard_size;
757 DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300758
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300759 wm_size = fifo_size - entries;
760 DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300761
762 /* Don't promote wm_size to unsigned... */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300763 if (wm_size > wm->max_wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300764 wm_size = wm->max_wm;
765 if (wm_size <= 0)
766 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300767
768 /*
769 * Bspec seems to indicate that the value shouldn't be lower than
770 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
771 * Lets go for 8 which is the burst size since certain platforms
772 * already use a hardcoded 8 (which is what the spec says should be
773 * done).
774 */
775 if (wm_size <= 8)
776 wm_size = 8;
777
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300778 return wm_size;
779}
780
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300781static bool is_disabling(int old, int new, int threshold)
782{
783 return old >= threshold && new < threshold;
784}
785
786static bool is_enabling(int old, int new, int threshold)
787{
788 return old < threshold && new >= threshold;
789}
790
Ville Syrjälä6d5019b2017-04-21 21:14:20 +0300791static int intel_wm_num_levels(struct drm_i915_private *dev_priv)
792{
793 return dev_priv->wm.max_level + 1;
794}
795
Ville Syrjälä24304d812017-03-14 17:10:49 +0200796static bool intel_wm_plane_visible(const struct intel_crtc_state *crtc_state,
797 const struct intel_plane_state *plane_state)
798{
799 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
800
801 /* FIXME check the 'enable' instead */
802 if (!crtc_state->base.active)
803 return false;
804
805 /*
806 * Treat cursor with fb as always visible since cursor updates
807 * can happen faster than the vrefresh rate, and the current
808 * watermark code doesn't handle that correctly. Cursor updates
809 * which set/clear the fb or change the cursor size are going
810 * to get throttled by intel_legacy_cursor_update() to work
811 * around this problem with the watermark code.
812 */
813 if (plane->id == PLANE_CURSOR)
814 return plane_state->base.fb != NULL;
815 else
816 return plane_state->base.visible;
817}
818
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200819static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300820{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200821 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300822
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200823 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200824 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300825 if (enabled)
826 return NULL;
827 enabled = crtc;
828 }
829 }
830
831 return enabled;
832}
833
Ville Syrjälä432081b2016-10-31 22:37:03 +0200834static void pineview_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300835{
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200836 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200837 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300838 const struct cxsr_latency *latency;
839 u32 reg;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300840 unsigned int wm;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300841
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +0000842 latency = intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100843 dev_priv->is_ddr3,
844 dev_priv->fsb_freq,
845 dev_priv->mem_freq);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300846 if (!latency) {
847 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300848 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300849 return;
850 }
851
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200852 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300853 if (crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200854 const struct drm_display_mode *adjusted_mode =
855 &crtc->config->base.adjusted_mode;
856 const struct drm_framebuffer *fb =
857 crtc->base.primary->state->fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200858 int cpp = fb->format->cpp[0];
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300859 int clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300860
861 /* Display SR */
862 wm = intel_calculate_wm(clock, &pineview_display_wm,
863 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200864 cpp, latency->display_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300865 reg = I915_READ(DSPFW1);
866 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200867 reg |= FW_WM(wm, SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300868 I915_WRITE(DSPFW1, reg);
869 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
870
871 /* cursor SR */
872 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
873 pineview_display_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300874 4, latency->cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300875 reg = I915_READ(DSPFW3);
876 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200877 reg |= FW_WM(wm, CURSOR_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300878 I915_WRITE(DSPFW3, reg);
879
880 /* Display HPLL off SR */
881 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
882 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200883 cpp, latency->display_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300884 reg = I915_READ(DSPFW3);
885 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200886 reg |= FW_WM(wm, HPLL_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300887 I915_WRITE(DSPFW3, reg);
888
889 /* cursor HPLL off SR */
890 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
891 pineview_display_hplloff_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300892 4, latency->cursor_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300893 reg = I915_READ(DSPFW3);
894 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200895 reg |= FW_WM(wm, HPLL_CURSOR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300896 I915_WRITE(DSPFW3, reg);
897 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
898
Imre Deak5209b1f2014-07-01 12:36:17 +0300899 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300900 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300901 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300902 }
903}
904
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300905/*
906 * Documentation says:
907 * "If the line size is small, the TLB fetches can get in the way of the
908 * data fetches, causing some lag in the pixel data return which is not
909 * accounted for in the above formulas. The following adjustment only
910 * needs to be applied if eight whole lines fit in the buffer at once.
911 * The WM is adjusted upwards by the difference between the FIFO size
912 * and the size of 8 whole lines. This adjustment is always performed
913 * in the actual pixel depth regardless of whether FBC is enabled or not."
914 */
Chris Wilson1a1f1282017-11-07 14:03:38 +0000915static unsigned int g4x_tlb_miss_wa(int fifo_size, int width, int cpp)
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300916{
917 int tlb_miss = fifo_size * 64 - width * cpp * 8;
918
919 return max(0, tlb_miss);
920}
921
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300922static void g4x_write_wm_values(struct drm_i915_private *dev_priv,
923 const struct g4x_wm_values *wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300924{
Ville Syrjäläe93329a2017-04-21 21:14:31 +0300925 enum pipe pipe;
926
927 for_each_pipe(dev_priv, pipe)
928 trace_g4x_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
929
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300930 I915_WRITE(DSPFW1,
931 FW_WM(wm->sr.plane, SR) |
932 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
933 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
934 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
935 I915_WRITE(DSPFW2,
936 (wm->fbc_en ? DSPFW_FBC_SR_EN : 0) |
937 FW_WM(wm->sr.fbc, FBC_SR) |
938 FW_WM(wm->hpll.fbc, FBC_HPLL_SR) |
939 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEB) |
940 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
941 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
942 I915_WRITE(DSPFW3,
943 (wm->hpll_en ? DSPFW_HPLL_SR_EN : 0) |
944 FW_WM(wm->sr.cursor, CURSOR_SR) |
945 FW_WM(wm->hpll.cursor, HPLL_CURSOR) |
946 FW_WM(wm->hpll.plane, HPLL_SR));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300947
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300948 POSTING_READ(DSPFW1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300949}
950
Ville Syrjälä15665972015-03-10 16:16:28 +0200951#define FW_WM_VLV(value, plane) \
952 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
953
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200954static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200955 const struct vlv_wm_values *wm)
956{
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200957 enum pipe pipe;
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200958
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200959 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläc137d662017-03-02 19:15:06 +0200960 trace_vlv_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
961
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200962 I915_WRITE(VLV_DDL(pipe),
963 (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
964 (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
965 (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
966 (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
967 }
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200968
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +0200969 /*
970 * Zero the (unused) WM1 watermarks, and also clear all the
971 * high order bits so that there are no out of bounds values
972 * present in the registers during the reprogramming.
973 */
974 I915_WRITE(DSPHOWM, 0);
975 I915_WRITE(DSPHOWM1, 0);
976 I915_WRITE(DSPFW4, 0);
977 I915_WRITE(DSPFW5, 0);
978 I915_WRITE(DSPFW6, 0);
979
Ville Syrjäläae801522015-03-05 21:19:49 +0200980 I915_WRITE(DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +0200981 FW_WM(wm->sr.plane, SR) |
Ville Syrjälä1b313892016-11-28 19:37:08 +0200982 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
983 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
984 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200985 I915_WRITE(DSPFW2,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200986 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
987 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
988 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200989 I915_WRITE(DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +0200990 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +0200991
992 if (IS_CHERRYVIEW(dev_priv)) {
993 I915_WRITE(DSPFW7_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200994 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
995 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200996 I915_WRITE(DSPFW8_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200997 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
998 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
Ville Syrjäläae801522015-03-05 21:19:49 +0200999 I915_WRITE(DSPFW9_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001000 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
1001 FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001002 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001003 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001004 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
1005 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
1006 FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
1007 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1008 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1009 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1010 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1011 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1012 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001013 } else {
1014 I915_WRITE(DSPFW7,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001015 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1016 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001017 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001018 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001019 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1020 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1021 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1022 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1023 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1024 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001025 }
1026
1027 POSTING_READ(DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001028}
1029
Ville Syrjälä15665972015-03-10 16:16:28 +02001030#undef FW_WM_VLV
1031
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001032static void g4x_setup_wm_latency(struct drm_i915_private *dev_priv)
1033{
1034 /* all latencies in usec */
1035 dev_priv->wm.pri_latency[G4X_WM_LEVEL_NORMAL] = 5;
1036 dev_priv->wm.pri_latency[G4X_WM_LEVEL_SR] = 12;
Ville Syrjälä79d94302017-04-21 21:14:30 +03001037 dev_priv->wm.pri_latency[G4X_WM_LEVEL_HPLL] = 35;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001038
Ville Syrjälä79d94302017-04-21 21:14:30 +03001039 dev_priv->wm.max_level = G4X_WM_LEVEL_HPLL;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001040}
1041
1042static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
1043{
1044 /*
1045 * DSPCNTR[13] supposedly controls whether the
1046 * primary plane can use the FIFO space otherwise
1047 * reserved for the sprite plane. It's not 100% clear
1048 * what the actual FIFO size is, but it looks like we
1049 * can happily set both primary and sprite watermarks
1050 * up to 127 cachelines. So that would seem to mean
1051 * that either DSPCNTR[13] doesn't do anything, or that
1052 * the total FIFO is >= 256 cachelines in size. Either
1053 * way, we don't seem to have to worry about this
1054 * repartitioning as the maximum watermark value the
1055 * register can hold for each plane is lower than the
1056 * minimum FIFO size.
1057 */
1058 switch (plane_id) {
1059 case PLANE_CURSOR:
1060 return 63;
1061 case PLANE_PRIMARY:
1062 return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
1063 case PLANE_SPRITE0:
1064 return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
1065 default:
1066 MISSING_CASE(plane_id);
1067 return 0;
1068 }
1069}
1070
1071static int g4x_fbc_fifo_size(int level)
1072{
1073 switch (level) {
1074 case G4X_WM_LEVEL_SR:
1075 return 7;
1076 case G4X_WM_LEVEL_HPLL:
1077 return 15;
1078 default:
1079 MISSING_CASE(level);
1080 return 0;
1081 }
1082}
1083
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001084static u16 g4x_compute_wm(const struct intel_crtc_state *crtc_state,
1085 const struct intel_plane_state *plane_state,
1086 int level)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001087{
1088 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1089 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1090 const struct drm_display_mode *adjusted_mode =
1091 &crtc_state->base.adjusted_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001092 unsigned int latency = dev_priv->wm.pri_latency[level] * 10;
1093 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001094
1095 if (latency == 0)
1096 return USHRT_MAX;
1097
1098 if (!intel_wm_plane_visible(crtc_state, plane_state))
1099 return 0;
1100
Ville Syrjäläd56e8232019-07-03 23:08:22 +03001101 cpp = plane_state->base.fb->format->cpp[0];
1102
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001103 /*
1104 * Not 100% sure which way ELK should go here as the
1105 * spec only says CL/CTG should assume 32bpp and BW
1106 * doesn't need to. But as these things followed the
1107 * mobile vs. desktop lines on gen3 as well, let's
1108 * assume ELK doesn't need this.
1109 *
1110 * The spec also fails to list such a restriction for
1111 * the HPLL watermark, which seems a little strange.
1112 * Let's use 32bpp for the HPLL watermark as well.
1113 */
1114 if (IS_GM45(dev_priv) && plane->id == PLANE_PRIMARY &&
1115 level != G4X_WM_LEVEL_NORMAL)
Ville Syrjäläd56e8232019-07-03 23:08:22 +03001116 cpp = max(cpp, 4u);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001117
1118 clock = adjusted_mode->crtc_clock;
1119 htotal = adjusted_mode->crtc_htotal;
1120
Maarten Lankhorst3a612762019-10-04 13:34:54 +02001121 width = drm_rect_width(&plane_state->base.dst);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001122
1123 if (plane->id == PLANE_CURSOR) {
1124 wm = intel_wm_method2(clock, htotal, width, cpp, latency);
1125 } else if (plane->id == PLANE_PRIMARY &&
1126 level == G4X_WM_LEVEL_NORMAL) {
1127 wm = intel_wm_method1(clock, cpp, latency);
1128 } else {
Chris Wilson1a1f1282017-11-07 14:03:38 +00001129 unsigned int small, large;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001130
1131 small = intel_wm_method1(clock, cpp, latency);
1132 large = intel_wm_method2(clock, htotal, width, cpp, latency);
1133
1134 wm = min(small, large);
1135 }
1136
1137 wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
1138 width, cpp);
1139
1140 wm = DIV_ROUND_UP(wm, 64) + 2;
1141
Chris Wilson1a1f1282017-11-07 14:03:38 +00001142 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001143}
1144
1145static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1146 int level, enum plane_id plane_id, u16 value)
1147{
1148 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1149 bool dirty = false;
1150
1151 for (; level < intel_wm_num_levels(dev_priv); level++) {
1152 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1153
1154 dirty |= raw->plane[plane_id] != value;
1155 raw->plane[plane_id] = value;
1156 }
1157
1158 return dirty;
1159}
1160
1161static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
1162 int level, u16 value)
1163{
1164 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1165 bool dirty = false;
1166
1167 /* NORMAL level doesn't have an FBC watermark */
1168 level = max(level, G4X_WM_LEVEL_SR);
1169
1170 for (; level < intel_wm_num_levels(dev_priv); level++) {
1171 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1172
1173 dirty |= raw->fbc != value;
1174 raw->fbc = value;
1175 }
1176
1177 return dirty;
1178}
1179
Maarten Lankhorstec193642019-06-28 10:55:17 +02001180static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
1181 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001182 u32 pri_val);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001183
1184static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1185 const struct intel_plane_state *plane_state)
1186{
1187 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1188 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
1189 enum plane_id plane_id = plane->id;
1190 bool dirty = false;
1191 int level;
1192
1193 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1194 dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1195 if (plane_id == PLANE_PRIMARY)
1196 dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
1197 goto out;
1198 }
1199
1200 for (level = 0; level < num_levels; level++) {
1201 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1202 int wm, max_wm;
1203
1204 wm = g4x_compute_wm(crtc_state, plane_state, level);
1205 max_wm = g4x_plane_fifo_size(plane_id, level);
1206
1207 if (wm > max_wm)
1208 break;
1209
1210 dirty |= raw->plane[plane_id] != wm;
1211 raw->plane[plane_id] = wm;
1212
1213 if (plane_id != PLANE_PRIMARY ||
1214 level == G4X_WM_LEVEL_NORMAL)
1215 continue;
1216
1217 wm = ilk_compute_fbc_wm(crtc_state, plane_state,
1218 raw->plane[plane_id]);
1219 max_wm = g4x_fbc_fifo_size(level);
1220
1221 /*
1222 * FBC wm is not mandatory as we
1223 * can always just disable its use.
1224 */
1225 if (wm > max_wm)
1226 wm = USHRT_MAX;
1227
1228 dirty |= raw->fbc != wm;
1229 raw->fbc = wm;
1230 }
1231
1232 /* mark watermarks as invalid */
1233 dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1234
1235 if (plane_id == PLANE_PRIMARY)
1236 dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
1237
1238 out:
1239 if (dirty) {
1240 DRM_DEBUG_KMS("%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
1241 plane->base.name,
1242 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
1243 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
1244 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
1245
1246 if (plane_id == PLANE_PRIMARY)
1247 DRM_DEBUG_KMS("FBC watermarks: SR=%d, HPLL=%d\n",
1248 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
1249 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
1250 }
1251
1252 return dirty;
1253}
1254
1255static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1256 enum plane_id plane_id, int level)
1257{
1258 const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1259
1260 return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
1261}
1262
1263static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
1264 int level)
1265{
1266 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1267
1268 if (level > dev_priv->wm.max_level)
1269 return false;
1270
1271 return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1272 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1273 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1274}
1275
1276/* mark all levels starting from 'level' as invalid */
1277static void g4x_invalidate_wms(struct intel_crtc *crtc,
1278 struct g4x_wm_state *wm_state, int level)
1279{
1280 if (level <= G4X_WM_LEVEL_NORMAL) {
1281 enum plane_id plane_id;
1282
1283 for_each_plane_id_on_crtc(crtc, plane_id)
1284 wm_state->wm.plane[plane_id] = USHRT_MAX;
1285 }
1286
1287 if (level <= G4X_WM_LEVEL_SR) {
1288 wm_state->cxsr = false;
1289 wm_state->sr.cursor = USHRT_MAX;
1290 wm_state->sr.plane = USHRT_MAX;
1291 wm_state->sr.fbc = USHRT_MAX;
1292 }
1293
1294 if (level <= G4X_WM_LEVEL_HPLL) {
1295 wm_state->hpll_en = false;
1296 wm_state->hpll.cursor = USHRT_MAX;
1297 wm_state->hpll.plane = USHRT_MAX;
1298 wm_state->hpll.fbc = USHRT_MAX;
1299 }
1300}
1301
1302static int g4x_compute_pipe_wm(struct intel_crtc_state *crtc_state)
1303{
1304 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1305 struct intel_atomic_state *state =
1306 to_intel_atomic_state(crtc_state->base.state);
1307 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001308 int num_active_planes = hweight8(crtc_state->active_planes &
1309 ~BIT(PLANE_CURSOR));
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001310 const struct g4x_pipe_wm *raw;
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001311 const struct intel_plane_state *old_plane_state;
1312 const struct intel_plane_state *new_plane_state;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001313 struct intel_plane *plane;
1314 enum plane_id plane_id;
1315 int i, level;
1316 unsigned int dirty = 0;
1317
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001318 for_each_oldnew_intel_plane_in_state(state, plane,
1319 old_plane_state,
1320 new_plane_state, i) {
1321 if (new_plane_state->base.crtc != &crtc->base &&
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001322 old_plane_state->base.crtc != &crtc->base)
1323 continue;
1324
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001325 if (g4x_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001326 dirty |= BIT(plane->id);
1327 }
1328
1329 if (!dirty)
1330 return 0;
1331
1332 level = G4X_WM_LEVEL_NORMAL;
1333 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1334 goto out;
1335
1336 raw = &crtc_state->wm.g4x.raw[level];
1337 for_each_plane_id_on_crtc(crtc, plane_id)
1338 wm_state->wm.plane[plane_id] = raw->plane[plane_id];
1339
1340 level = G4X_WM_LEVEL_SR;
1341
1342 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1343 goto out;
1344
1345 raw = &crtc_state->wm.g4x.raw[level];
1346 wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
1347 wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
1348 wm_state->sr.fbc = raw->fbc;
1349
1350 wm_state->cxsr = num_active_planes == BIT(PLANE_PRIMARY);
1351
1352 level = G4X_WM_LEVEL_HPLL;
1353
1354 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1355 goto out;
1356
1357 raw = &crtc_state->wm.g4x.raw[level];
1358 wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
1359 wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
1360 wm_state->hpll.fbc = raw->fbc;
1361
1362 wm_state->hpll_en = wm_state->cxsr;
1363
1364 level++;
1365
1366 out:
1367 if (level == G4X_WM_LEVEL_NORMAL)
1368 return -EINVAL;
1369
1370 /* invalidate the higher levels */
1371 g4x_invalidate_wms(crtc, wm_state, level);
1372
1373 /*
1374 * Determine if the FBC watermark(s) can be used. IF
1375 * this isn't the case we prefer to disable the FBC
1376 ( watermark(s) rather than disable the SR/HPLL
1377 * level(s) entirely.
1378 */
1379 wm_state->fbc_en = level > G4X_WM_LEVEL_NORMAL;
1380
1381 if (level >= G4X_WM_LEVEL_SR &&
1382 wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
1383 wm_state->fbc_en = false;
1384 else if (level >= G4X_WM_LEVEL_HPLL &&
1385 wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
1386 wm_state->fbc_en = false;
1387
1388 return 0;
1389}
1390
Matt Ropercd1d3ee2018-12-10 13:54:14 -08001391static int g4x_compute_intermediate_wm(struct intel_crtc_state *new_crtc_state)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001392{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08001393 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001394 struct g4x_wm_state *intermediate = &new_crtc_state->wm.g4x.intermediate;
1395 const struct g4x_wm_state *optimal = &new_crtc_state->wm.g4x.optimal;
1396 struct intel_atomic_state *intel_state =
1397 to_intel_atomic_state(new_crtc_state->base.state);
1398 const struct intel_crtc_state *old_crtc_state =
1399 intel_atomic_get_old_crtc_state(intel_state, crtc);
1400 const struct g4x_wm_state *active = &old_crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001401 enum plane_id plane_id;
1402
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001403 if (!new_crtc_state->base.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->base)) {
1404 *intermediate = *optimal;
1405
1406 intermediate->cxsr = false;
1407 intermediate->hpll_en = false;
1408 goto out;
1409 }
1410
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001411 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001412 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001413 intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001414 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001415 intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
1416
1417 for_each_plane_id_on_crtc(crtc, plane_id) {
1418 intermediate->wm.plane[plane_id] =
1419 max(optimal->wm.plane[plane_id],
1420 active->wm.plane[plane_id]);
1421
1422 WARN_ON(intermediate->wm.plane[plane_id] >
1423 g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
1424 }
1425
1426 intermediate->sr.plane = max(optimal->sr.plane,
1427 active->sr.plane);
1428 intermediate->sr.cursor = max(optimal->sr.cursor,
1429 active->sr.cursor);
1430 intermediate->sr.fbc = max(optimal->sr.fbc,
1431 active->sr.fbc);
1432
1433 intermediate->hpll.plane = max(optimal->hpll.plane,
1434 active->hpll.plane);
1435 intermediate->hpll.cursor = max(optimal->hpll.cursor,
1436 active->hpll.cursor);
1437 intermediate->hpll.fbc = max(optimal->hpll.fbc,
1438 active->hpll.fbc);
1439
1440 WARN_ON((intermediate->sr.plane >
1441 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
1442 intermediate->sr.cursor >
1443 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
1444 intermediate->cxsr);
1445 WARN_ON((intermediate->sr.plane >
1446 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
1447 intermediate->sr.cursor >
1448 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
1449 intermediate->hpll_en);
1450
1451 WARN_ON(intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
1452 intermediate->fbc_en && intermediate->cxsr);
1453 WARN_ON(intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
1454 intermediate->fbc_en && intermediate->hpll_en);
1455
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001456out:
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001457 /*
1458 * If our intermediate WM are identical to the final WM, then we can
1459 * omit the post-vblank programming; only update if it's different.
1460 */
1461 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001462 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001463
1464 return 0;
1465}
1466
1467static void g4x_merge_wm(struct drm_i915_private *dev_priv,
1468 struct g4x_wm_values *wm)
1469{
1470 struct intel_crtc *crtc;
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001471 int num_active_pipes = 0;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001472
1473 wm->cxsr = true;
1474 wm->hpll_en = true;
1475 wm->fbc_en = true;
1476
1477 for_each_intel_crtc(&dev_priv->drm, crtc) {
1478 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1479
1480 if (!crtc->active)
1481 continue;
1482
1483 if (!wm_state->cxsr)
1484 wm->cxsr = false;
1485 if (!wm_state->hpll_en)
1486 wm->hpll_en = false;
1487 if (!wm_state->fbc_en)
1488 wm->fbc_en = false;
1489
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001490 num_active_pipes++;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001491 }
1492
Ville Syrjäläc08e9132019-08-21 20:30:32 +03001493 if (num_active_pipes != 1) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001494 wm->cxsr = false;
1495 wm->hpll_en = false;
1496 wm->fbc_en = false;
1497 }
1498
1499 for_each_intel_crtc(&dev_priv->drm, crtc) {
1500 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1501 enum pipe pipe = crtc->pipe;
1502
1503 wm->pipe[pipe] = wm_state->wm;
1504 if (crtc->active && wm->cxsr)
1505 wm->sr = wm_state->sr;
1506 if (crtc->active && wm->hpll_en)
1507 wm->hpll = wm_state->hpll;
1508 }
1509}
1510
1511static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
1512{
1513 struct g4x_wm_values *old_wm = &dev_priv->wm.g4x;
1514 struct g4x_wm_values new_wm = {};
1515
1516 g4x_merge_wm(dev_priv, &new_wm);
1517
1518 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
1519 return;
1520
1521 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
1522 _intel_set_memory_cxsr(dev_priv, false);
1523
1524 g4x_write_wm_values(dev_priv, &new_wm);
1525
1526 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
1527 _intel_set_memory_cxsr(dev_priv, true);
1528
1529 *old_wm = new_wm;
1530}
1531
1532static void g4x_initial_watermarks(struct intel_atomic_state *state,
1533 struct intel_crtc_state *crtc_state)
1534{
1535 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1536 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1537
1538 mutex_lock(&dev_priv->wm.wm_mutex);
1539 crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
1540 g4x_program_watermarks(dev_priv);
1541 mutex_unlock(&dev_priv->wm.wm_mutex);
1542}
1543
1544static void g4x_optimize_watermarks(struct intel_atomic_state *state,
1545 struct intel_crtc_state *crtc_state)
1546{
1547 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä88016a92019-07-01 19:05:45 +03001548 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001549
1550 if (!crtc_state->wm.need_postvbl_update)
1551 return;
1552
1553 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03001554 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001555 g4x_program_watermarks(dev_priv);
1556 mutex_unlock(&dev_priv->wm.wm_mutex);
1557}
1558
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001559/* latency must be in 0.1us units. */
1560static unsigned int vlv_wm_method2(unsigned int pixel_rate,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001561 unsigned int htotal,
1562 unsigned int width,
Ville Syrjäläac484962016-01-20 21:05:26 +02001563 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001564 unsigned int latency)
1565{
1566 unsigned int ret;
1567
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001568 ret = intel_wm_method2(pixel_rate, htotal,
1569 width, cpp, latency);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001570 ret = DIV_ROUND_UP(ret, 64);
1571
1572 return ret;
1573}
1574
Ville Syrjäläbb726512016-10-31 22:37:24 +02001575static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001576{
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001577 /* all latencies in usec */
1578 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1579
Ville Syrjälä58590c12015-09-08 21:05:12 +03001580 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
1581
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001582 if (IS_CHERRYVIEW(dev_priv)) {
1583 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1584 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001585
1586 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001587 }
1588}
1589
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001590static u16 vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1591 const struct intel_plane_state *plane_state,
1592 int level)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001593{
Ville Syrjäläe339d672016-11-28 19:37:17 +02001594 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001595 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläe339d672016-11-28 19:37:17 +02001596 const struct drm_display_mode *adjusted_mode =
1597 &crtc_state->base.adjusted_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001598 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001599
1600 if (dev_priv->wm.pri_latency[level] == 0)
1601 return USHRT_MAX;
1602
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001603 if (!intel_wm_plane_visible(crtc_state, plane_state))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001604 return 0;
1605
Daniel Vetteref426c12017-01-04 11:41:10 +01001606 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjäläe339d672016-11-28 19:37:17 +02001607 clock = adjusted_mode->crtc_clock;
1608 htotal = adjusted_mode->crtc_htotal;
1609 width = crtc_state->pipe_src_w;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001610
Ville Syrjälä709f3fc2017-03-03 17:19:26 +02001611 if (plane->id == PLANE_CURSOR) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001612 /*
1613 * FIXME the formula gives values that are
1614 * too big for the cursor FIFO, and hence we
1615 * would never be able to use cursors. For
1616 * now just hardcode the watermark.
1617 */
1618 wm = 63;
1619 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +02001620 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001621 dev_priv->wm.pri_latency[level] * 10);
1622 }
1623
Chris Wilson1a1f1282017-11-07 14:03:38 +00001624 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001625}
1626
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001627static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1628{
1629 return (active_planes & (BIT(PLANE_SPRITE0) |
1630 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1631}
1632
Ville Syrjälä5012e602017-03-02 19:14:56 +02001633static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001634{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001635 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001636 const struct g4x_pipe_wm *raw =
Ville Syrjälä5012e602017-03-02 19:14:56 +02001637 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001638 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001639 unsigned int active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001640 int num_active_planes = hweight8(active_planes);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001641 const int fifo_size = 511;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001642 int fifo_extra, fifo_left = fifo_size;
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001643 int sprite0_fifo_extra = 0;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001644 unsigned int total_rate;
1645 enum plane_id plane_id;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001646
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001647 /*
1648 * When enabling sprite0 after sprite1 has already been enabled
1649 * we tend to get an underrun unless sprite0 already has some
1650 * FIFO space allcoated. Hence we always allocate at least one
1651 * cacheline for sprite0 whenever sprite1 is enabled.
1652 *
1653 * All other plane enable sequences appear immune to this problem.
1654 */
1655 if (vlv_need_sprite0_fifo_workaround(active_planes))
1656 sprite0_fifo_extra = 1;
1657
Ville Syrjälä5012e602017-03-02 19:14:56 +02001658 total_rate = raw->plane[PLANE_PRIMARY] +
1659 raw->plane[PLANE_SPRITE0] +
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001660 raw->plane[PLANE_SPRITE1] +
1661 sprite0_fifo_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001662
Ville Syrjälä5012e602017-03-02 19:14:56 +02001663 if (total_rate > fifo_size)
1664 return -EINVAL;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001665
Ville Syrjälä5012e602017-03-02 19:14:56 +02001666 if (total_rate == 0)
1667 total_rate = 1;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001668
Ville Syrjälä5012e602017-03-02 19:14:56 +02001669 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001670 unsigned int rate;
1671
Ville Syrjälä5012e602017-03-02 19:14:56 +02001672 if ((active_planes & BIT(plane_id)) == 0) {
1673 fifo_state->plane[plane_id] = 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001674 continue;
1675 }
1676
Ville Syrjälä5012e602017-03-02 19:14:56 +02001677 rate = raw->plane[plane_id];
1678 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1679 fifo_left -= fifo_state->plane[plane_id];
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001680 }
1681
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001682 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1683 fifo_left -= sprite0_fifo_extra;
1684
Ville Syrjälä5012e602017-03-02 19:14:56 +02001685 fifo_state->plane[PLANE_CURSOR] = 63;
1686
1687 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001688
1689 /* spread the remainder evenly */
Ville Syrjälä5012e602017-03-02 19:14:56 +02001690 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001691 int plane_extra;
1692
1693 if (fifo_left == 0)
1694 break;
1695
Ville Syrjälä5012e602017-03-02 19:14:56 +02001696 if ((active_planes & BIT(plane_id)) == 0)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001697 continue;
1698
1699 plane_extra = min(fifo_extra, fifo_left);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001700 fifo_state->plane[plane_id] += plane_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001701 fifo_left -= plane_extra;
1702 }
1703
Ville Syrjälä5012e602017-03-02 19:14:56 +02001704 WARN_ON(active_planes != 0 && fifo_left != 0);
1705
1706 /* give it all to the first plane if none are active */
1707 if (active_planes == 0) {
1708 WARN_ON(fifo_left != fifo_size);
1709 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1710 }
1711
1712 return 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001713}
1714
Ville Syrjäläff32c542017-03-02 19:14:57 +02001715/* mark all levels starting from 'level' as invalid */
1716static void vlv_invalidate_wms(struct intel_crtc *crtc,
1717 struct vlv_wm_state *wm_state, int level)
1718{
1719 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1720
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001721 for (; level < intel_wm_num_levels(dev_priv); level++) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02001722 enum plane_id plane_id;
1723
1724 for_each_plane_id_on_crtc(crtc, plane_id)
1725 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1726
1727 wm_state->sr[level].cursor = USHRT_MAX;
1728 wm_state->sr[level].plane = USHRT_MAX;
1729 }
1730}
1731
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001732static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1733{
1734 if (wm > fifo_size)
1735 return USHRT_MAX;
1736 else
1737 return fifo_size - wm;
1738}
1739
Ville Syrjäläff32c542017-03-02 19:14:57 +02001740/*
1741 * Starting from 'level' set all higher
1742 * levels to 'value' in the "raw" watermarks.
1743 */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001744static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001745 int level, enum plane_id plane_id, u16 value)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001746{
Ville Syrjäläff32c542017-03-02 19:14:57 +02001747 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001748 int num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001749 bool dirty = false;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001750
Ville Syrjäläff32c542017-03-02 19:14:57 +02001751 for (; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001752 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001753
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001754 dirty |= raw->plane[plane_id] != value;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001755 raw->plane[plane_id] = value;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001756 }
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001757
1758 return dirty;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001759}
1760
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001761static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1762 const struct intel_plane_state *plane_state)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001763{
1764 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1765 enum plane_id plane_id = plane->id;
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001766 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
Ville Syrjäläff32c542017-03-02 19:14:57 +02001767 int level;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001768 bool dirty = false;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001769
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001770 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001771 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1772 goto out;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001773 }
1774
1775 for (level = 0; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001776 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001777 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1778 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1779
Ville Syrjäläff32c542017-03-02 19:14:57 +02001780 if (wm > max_wm)
1781 break;
1782
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001783 dirty |= raw->plane[plane_id] != wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001784 raw->plane[plane_id] = wm;
1785 }
1786
1787 /* mark all higher levels as invalid */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001788 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001789
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001790out:
1791 if (dirty)
Ville Syrjälä57a65282017-04-21 21:14:22 +03001792 DRM_DEBUG_KMS("%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001793 plane->base.name,
1794 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1795 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1796 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
1797
1798 return dirty;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001799}
1800
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001801static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1802 enum plane_id plane_id, int level)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001803{
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001804 const struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02001805 &crtc_state->wm.vlv.raw[level];
1806 const struct vlv_fifo_state *fifo_state =
1807 &crtc_state->wm.vlv.fifo_state;
1808
1809 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1810}
1811
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001812static 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 +02001813{
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001814 return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1815 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1816 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1817 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001818}
1819
1820static int vlv_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001821{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001822 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001823 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001824 struct intel_atomic_state *state =
1825 to_intel_atomic_state(crtc_state->base.state);
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001826 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001827 const struct vlv_fifo_state *fifo_state =
1828 &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä0b14d962019-08-21 20:30:33 +03001829 int num_active_planes = hweight8(crtc_state->active_planes &
1830 ~BIT(PLANE_CURSOR));
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001831 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->base);
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001832 const struct intel_plane_state *old_plane_state;
1833 const struct intel_plane_state *new_plane_state;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001834 struct intel_plane *plane;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001835 enum plane_id plane_id;
1836 int level, ret, i;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001837 unsigned int dirty = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001838
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001839 for_each_oldnew_intel_plane_in_state(state, plane,
1840 old_plane_state,
1841 new_plane_state, i) {
1842 if (new_plane_state->base.crtc != &crtc->base &&
Ville Syrjäläff32c542017-03-02 19:14:57 +02001843 old_plane_state->base.crtc != &crtc->base)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001844 continue;
1845
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001846 if (vlv_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001847 dirty |= BIT(plane->id);
1848 }
1849
1850 /*
1851 * DSPARB registers may have been reset due to the
1852 * power well being turned off. Make sure we restore
1853 * them to a consistent state even if no primary/sprite
1854 * planes are initially active.
1855 */
1856 if (needs_modeset)
1857 crtc_state->fifo_changed = true;
1858
1859 if (!dirty)
1860 return 0;
1861
1862 /* cursor changes don't warrant a FIFO recompute */
1863 if (dirty & ~BIT(PLANE_CURSOR)) {
1864 const struct intel_crtc_state *old_crtc_state =
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001865 intel_atomic_get_old_crtc_state(state, crtc);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001866 const struct vlv_fifo_state *old_fifo_state =
1867 &old_crtc_state->wm.vlv.fifo_state;
1868
1869 ret = vlv_compute_fifo(crtc_state);
1870 if (ret)
1871 return ret;
1872
1873 if (needs_modeset ||
1874 memcmp(old_fifo_state, fifo_state,
1875 sizeof(*fifo_state)) != 0)
1876 crtc_state->fifo_changed = true;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001877 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001878
Ville Syrjäläff32c542017-03-02 19:14:57 +02001879 /* initially allow all levels */
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001880 wm_state->num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001881 /*
1882 * Note that enabling cxsr with no primary/sprite planes
1883 * enabled can wedge the pipe. Hence we only allow cxsr
1884 * with exactly one enabled primary/sprite plane.
1885 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001886 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001887
Ville Syrjälä5012e602017-03-02 19:14:56 +02001888 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001889 const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Jani Nikula24977872019-09-11 12:26:08 +03001890 const int sr_fifo_size = INTEL_NUM_PIPES(dev_priv) * 512 - 1;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001891
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001892 if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
Ville Syrjäläff32c542017-03-02 19:14:57 +02001893 break;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001894
Ville Syrjäläff32c542017-03-02 19:14:57 +02001895 for_each_plane_id_on_crtc(crtc, plane_id) {
1896 wm_state->wm[level].plane[plane_id] =
1897 vlv_invert_wm_value(raw->plane[plane_id],
1898 fifo_state->plane[plane_id]);
1899 }
1900
1901 wm_state->sr[level].plane =
1902 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
Ville Syrjälä5012e602017-03-02 19:14:56 +02001903 raw->plane[PLANE_SPRITE0],
Ville Syrjäläff32c542017-03-02 19:14:57 +02001904 raw->plane[PLANE_SPRITE1]),
1905 sr_fifo_size);
1906
1907 wm_state->sr[level].cursor =
1908 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1909 63);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001910 }
1911
Ville Syrjäläff32c542017-03-02 19:14:57 +02001912 if (level == 0)
1913 return -EINVAL;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001914
Ville Syrjäläff32c542017-03-02 19:14:57 +02001915 /* limit to only levels we can actually handle */
1916 wm_state->num_levels = level;
1917
1918 /* invalidate the higher levels */
1919 vlv_invalidate_wms(crtc, wm_state, level);
1920
1921 return 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001922}
1923
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001924#define VLV_FIFO(plane, value) \
1925 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1926
Ville Syrjäläff32c542017-03-02 19:14:57 +02001927static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
1928 struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001929{
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001930 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001931 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001932 struct intel_uncore *uncore = &dev_priv->uncore;
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001933 const struct vlv_fifo_state *fifo_state =
1934 &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001935 int sprite0_start, sprite1_start, fifo_size;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001936
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001937 if (!crtc_state->fifo_changed)
1938 return;
1939
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001940 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
1941 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
1942 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001943
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001944 WARN_ON(fifo_state->plane[PLANE_CURSOR] != 63);
1945 WARN_ON(fifo_size != 511);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001946
Ville Syrjäläc137d662017-03-02 19:15:06 +02001947 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
1948
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001949 /*
1950 * uncore.lock serves a double purpose here. It allows us to
1951 * use the less expensive I915_{READ,WRITE}_FW() functions, and
1952 * it protects the DSPARB registers from getting clobbered by
1953 * parallel updates from multiple pipes.
1954 *
1955 * intel_pipe_update_start() has already disabled interrupts
1956 * for us, so a plain spin_lock() is sufficient here.
1957 */
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001958 spin_lock(&uncore->lock);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001959
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001960 switch (crtc->pipe) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02001961 u32 dsparb, dsparb2, dsparb3;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001962 case PIPE_A:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001963 dsparb = intel_uncore_read_fw(uncore, DSPARB);
1964 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001965
1966 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1967 VLV_FIFO(SPRITEB, 0xff));
1968 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1969 VLV_FIFO(SPRITEB, sprite1_start));
1970
1971 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1972 VLV_FIFO(SPRITEB_HI, 0x1));
1973 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1974 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1975
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001976 intel_uncore_write_fw(uncore, DSPARB, dsparb);
1977 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001978 break;
1979 case PIPE_B:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001980 dsparb = intel_uncore_read_fw(uncore, DSPARB);
1981 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001982
1983 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1984 VLV_FIFO(SPRITED, 0xff));
1985 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1986 VLV_FIFO(SPRITED, sprite1_start));
1987
1988 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
1989 VLV_FIFO(SPRITED_HI, 0xff));
1990 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
1991 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
1992
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001993 intel_uncore_write_fw(uncore, DSPARB, dsparb);
1994 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001995 break;
1996 case PIPE_C:
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01001997 dsparb3 = intel_uncore_read_fw(uncore, DSPARB3);
1998 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001999
2000 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
2001 VLV_FIFO(SPRITEF, 0xff));
2002 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
2003 VLV_FIFO(SPRITEF, sprite1_start));
2004
2005 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
2006 VLV_FIFO(SPRITEF_HI, 0xff));
2007 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
2008 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
2009
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002010 intel_uncore_write_fw(uncore, DSPARB3, dsparb3);
2011 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002012 break;
2013 default:
2014 break;
2015 }
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002016
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002017 intel_uncore_posting_read_fw(uncore, DSPARB);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002018
Tvrtko Ursuline33a4be2019-06-11 11:45:44 +01002019 spin_unlock(&uncore->lock);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002020}
2021
2022#undef VLV_FIFO
2023
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002024static int vlv_compute_intermediate_wm(struct intel_crtc_state *new_crtc_state)
Ville Syrjälä4841da52017-03-02 19:14:59 +02002025{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002026 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002027 struct vlv_wm_state *intermediate = &new_crtc_state->wm.vlv.intermediate;
2028 const struct vlv_wm_state *optimal = &new_crtc_state->wm.vlv.optimal;
2029 struct intel_atomic_state *intel_state =
2030 to_intel_atomic_state(new_crtc_state->base.state);
2031 const struct intel_crtc_state *old_crtc_state =
2032 intel_atomic_get_old_crtc_state(intel_state, crtc);
2033 const struct vlv_wm_state *active = &old_crtc_state->wm.vlv.optimal;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002034 int level;
2035
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002036 if (!new_crtc_state->base.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->base)) {
2037 *intermediate = *optimal;
2038
2039 intermediate->cxsr = false;
2040 goto out;
2041 }
2042
Ville Syrjälä4841da52017-03-02 19:14:59 +02002043 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002044 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002045 !new_crtc_state->disable_cxsr;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002046
2047 for (level = 0; level < intermediate->num_levels; level++) {
2048 enum plane_id plane_id;
2049
2050 for_each_plane_id_on_crtc(crtc, plane_id) {
2051 intermediate->wm[level].plane[plane_id] =
2052 min(optimal->wm[level].plane[plane_id],
2053 active->wm[level].plane[plane_id]);
2054 }
2055
2056 intermediate->sr[level].plane = min(optimal->sr[level].plane,
2057 active->sr[level].plane);
2058 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
2059 active->sr[level].cursor);
2060 }
2061
2062 vlv_invalidate_wms(crtc, intermediate, level);
2063
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002064out:
Ville Syrjälä4841da52017-03-02 19:14:59 +02002065 /*
2066 * If our intermediate WM are identical to the final WM, then we can
2067 * omit the post-vblank programming; only update if it's different.
2068 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002069 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002070 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002071
2072 return 0;
2073}
2074
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002075static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002076 struct vlv_wm_values *wm)
2077{
2078 struct intel_crtc *crtc;
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002079 int num_active_pipes = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002080
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002081 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002082 wm->cxsr = true;
2083
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002084 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002085 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002086
2087 if (!crtc->active)
2088 continue;
2089
2090 if (!wm_state->cxsr)
2091 wm->cxsr = false;
2092
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002093 num_active_pipes++;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002094 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
2095 }
2096
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002097 if (num_active_pipes != 1)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002098 wm->cxsr = false;
2099
Ville Syrjäläc08e9132019-08-21 20:30:32 +03002100 if (num_active_pipes > 1)
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03002101 wm->level = VLV_WM_LEVEL_PM2;
2102
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002103 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002104 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002105 enum pipe pipe = crtc->pipe;
2106
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002107 wm->pipe[pipe] = wm_state->wm[wm->level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02002108 if (crtc->active && wm->cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002109 wm->sr = wm_state->sr[wm->level];
2110
Ville Syrjälä1b313892016-11-28 19:37:08 +02002111 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
2112 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
2113 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
2114 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002115 }
2116}
2117
Ville Syrjäläff32c542017-03-02 19:14:57 +02002118static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002119{
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002120 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
2121 struct vlv_wm_values new_wm = {};
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002122
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002123 vlv_merge_wm(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002124
Ville Syrjäläff32c542017-03-02 19:14:57 +02002125 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002126 return;
2127
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002128 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002129 chv_set_memory_dvfs(dev_priv, false);
2130
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002131 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002132 chv_set_memory_pm5(dev_priv, false);
2133
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002134 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002135 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002136
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002137 vlv_write_wm_values(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002138
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002139 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002140 _intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002141
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002142 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002143 chv_set_memory_pm5(dev_priv, true);
2144
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002145 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002146 chv_set_memory_dvfs(dev_priv, true);
2147
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002148 *old_wm = new_wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03002149}
2150
Ville Syrjäläff32c542017-03-02 19:14:57 +02002151static void vlv_initial_watermarks(struct intel_atomic_state *state,
2152 struct intel_crtc_state *crtc_state)
2153{
2154 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2155 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
2156
2157 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002158 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
2159 vlv_program_watermarks(dev_priv);
2160 mutex_unlock(&dev_priv->wm.wm_mutex);
2161}
2162
2163static void vlv_optimize_watermarks(struct intel_atomic_state *state,
2164 struct intel_crtc_state *crtc_state)
2165{
2166 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä88016a92019-07-01 19:05:45 +03002167 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002168
2169 if (!crtc_state->wm.need_postvbl_update)
2170 return;
2171
2172 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03002173 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02002174 vlv_program_watermarks(dev_priv);
2175 mutex_unlock(&dev_priv->wm.wm_mutex);
2176}
2177
Ville Syrjälä432081b2016-10-31 22:37:03 +02002178static void i965_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002179{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002180 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002181 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002182 int srwm = 1;
2183 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03002184 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002185
2186 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002187 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002188 if (crtc) {
2189 /* self-refresh has much higher latency */
2190 static const int sr_latency_ns = 12000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002191 const struct drm_display_mode *adjusted_mode =
2192 &crtc->config->base.adjusted_mode;
2193 const struct drm_framebuffer *fb =
2194 crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002195 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002196 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002197 int hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002198 int cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002199 int entries;
2200
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002201 entries = intel_wm_method2(clock, htotal,
2202 hdisplay, cpp, sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002203 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
2204 srwm = I965_FIFO_SIZE - entries;
2205 if (srwm < 0)
2206 srwm = 1;
2207 srwm &= 0x1ff;
2208 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
2209 entries, srwm);
2210
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002211 entries = intel_wm_method2(clock, htotal,
2212 crtc->base.cursor->state->crtc_w, 4,
2213 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002214 entries = DIV_ROUND_UP(entries,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002215 i965_cursor_wm_info.cacheline_size) +
2216 i965_cursor_wm_info.guard_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002217
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002218 cursor_sr = i965_cursor_wm_info.fifo_size - entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002219 if (cursor_sr > i965_cursor_wm_info.max_wm)
2220 cursor_sr = i965_cursor_wm_info.max_wm;
2221
2222 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
2223 "cursor %d\n", srwm, cursor_sr);
2224
Imre Deak98584252014-06-13 14:54:20 +03002225 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002226 } else {
Imre Deak98584252014-06-13 14:54:20 +03002227 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002228 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03002229 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002230 }
2231
2232 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2233 srwm);
2234
2235 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002236 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
2237 FW_WM(8, CURSORB) |
2238 FW_WM(8, PLANEB) |
2239 FW_WM(8, PLANEA));
2240 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
2241 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002242 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002243 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03002244
2245 if (cxsr_enabled)
2246 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002247}
2248
Ville Syrjäläf4998962015-03-10 17:02:21 +02002249#undef FW_WM
2250
Ville Syrjälä432081b2016-10-31 22:37:03 +02002251static void i9xx_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002252{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002253 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002254 const struct intel_watermark_params *wm_info;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002255 u32 fwater_lo;
2256 u32 fwater_hi;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002257 int cwm, srwm = 1;
2258 int fifo_size;
2259 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002260 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002261
Ville Syrjäläa9097be2016-10-31 22:37:20 +02002262 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002263 wm_info = &i945_wm_info;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002264 else if (!IS_GEN(dev_priv, 2))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002265 wm_info = &i915_wm_info;
2266 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03002267 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002268
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002269 fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_A);
2270 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_A);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002271 if (intel_crtc_active(crtc)) {
2272 const struct drm_display_mode *adjusted_mode =
2273 &crtc->config->base.adjusted_mode;
2274 const struct drm_framebuffer *fb =
2275 crtc->base.primary->state->fb;
2276 int cpp;
2277
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002278 if (IS_GEN(dev_priv, 2))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002279 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002280 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002281 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002282
Damien Lespiau241bfc32013-09-25 16:45:37 +01002283 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002284 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002285 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002286 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002287 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002288 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002289 if (planea_wm > (long)wm_info->max_wm)
2290 planea_wm = wm_info->max_wm;
2291 }
2292
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002293 if (IS_GEN(dev_priv, 2))
Ville Syrjälä9d539102014-08-15 01:21:53 +03002294 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002295
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002296 fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_B);
2297 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_B);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002298 if (intel_crtc_active(crtc)) {
2299 const struct drm_display_mode *adjusted_mode =
2300 &crtc->config->base.adjusted_mode;
2301 const struct drm_framebuffer *fb =
2302 crtc->base.primary->state->fb;
2303 int cpp;
2304
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002305 if (IS_GEN(dev_priv, 2))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002306 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002307 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002308 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002309
Damien Lespiau241bfc32013-09-25 16:45:37 +01002310 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002311 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002312 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002313 if (enabled == NULL)
2314 enabled = crtc;
2315 else
2316 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002317 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002318 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002319 if (planeb_wm > (long)wm_info->max_wm)
2320 planeb_wm = wm_info->max_wm;
2321 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002322
2323 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
2324
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002325 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07002326 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002327
Ville Syrjäläefc26112016-10-31 22:37:04 +02002328 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002329
2330 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01002331 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002332 enabled = NULL;
2333 }
2334
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002335 /*
2336 * Overlay gets an aggressive default since video jitter is bad.
2337 */
2338 cwm = 2;
2339
2340 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03002341 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002342
2343 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02002344 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002345 /* self-refresh has much higher latency */
2346 static const int sr_latency_ns = 6000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002347 const struct drm_display_mode *adjusted_mode =
2348 &enabled->config->base.adjusted_mode;
2349 const struct drm_framebuffer *fb =
2350 enabled->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002351 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002352 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002353 int hdisplay = enabled->config->pipe_src_w;
2354 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002355 int entries;
2356
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002357 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002358 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002359 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002360 cpp = fb->format->cpp[0];
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002361
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002362 entries = intel_wm_method2(clock, htotal, hdisplay, cpp,
2363 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002364 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
2365 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
2366 srwm = wm_info->fifo_size - entries;
2367 if (srwm < 0)
2368 srwm = 1;
2369
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002370 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002371 I915_WRITE(FW_BLC_SELF,
2372 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03002373 else
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002374 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
2375 }
2376
2377 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
2378 planea_wm, planeb_wm, cwm, srwm);
2379
2380 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2381 fwater_hi = (cwm & 0x1f);
2382
2383 /* Set request length to 8 cachelines per fetch */
2384 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2385 fwater_hi = fwater_hi | (1 << 8);
2386
2387 I915_WRITE(FW_BLC, fwater_lo);
2388 I915_WRITE(FW_BLC2, fwater_hi);
2389
Imre Deak5209b1f2014-07-01 12:36:17 +03002390 if (enabled)
2391 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002392}
2393
Ville Syrjälä432081b2016-10-31 22:37:03 +02002394static void i845_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002395{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002396 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002397 struct intel_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002398 const struct drm_display_mode *adjusted_mode;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002399 u32 fwater_lo;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002400 int planea_wm;
2401
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002402 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002403 if (crtc == NULL)
2404 return;
2405
Ville Syrjäläefc26112016-10-31 22:37:04 +02002406 adjusted_mode = &crtc->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002407 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02002408 &i845_wm_info,
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002409 dev_priv->display.get_fifo_size(dev_priv, PLANE_A),
Chris Wilson5aef6002014-09-03 11:56:07 +01002410 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002411 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
2412 fwater_lo |= (3<<8) | planea_wm;
2413
2414 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
2415
2416 I915_WRITE(FW_BLC, fwater_lo);
2417}
2418
Ville Syrjälä37126462013-08-01 16:18:55 +03002419/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002420static unsigned int ilk_wm_method1(unsigned int pixel_rate,
2421 unsigned int cpp,
2422 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002423{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002424 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002425
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002426 ret = intel_wm_method1(pixel_rate, cpp, latency);
2427 ret = DIV_ROUND_UP(ret, 64) + 2;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002428
2429 return ret;
2430}
2431
Ville Syrjälä37126462013-08-01 16:18:55 +03002432/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002433static unsigned int ilk_wm_method2(unsigned int pixel_rate,
2434 unsigned int htotal,
2435 unsigned int width,
2436 unsigned int cpp,
2437 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002438{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002439 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002440
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002441 ret = intel_wm_method2(pixel_rate, htotal,
2442 width, cpp, latency);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002443 ret = DIV_ROUND_UP(ret, 64) + 2;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002444
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002445 return ret;
2446}
2447
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002448static u32 ilk_wm_fbc(u32 pri_val, u32 horiz_pixels, u8 cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002449{
Matt Roper15126882015-12-03 11:37:40 -08002450 /*
2451 * Neither of these should be possible since this function shouldn't be
2452 * called if the CRTC is off or the plane is invisible. But let's be
2453 * extra paranoid to avoid a potential divide-by-zero if we screw up
2454 * elsewhere in the driver.
2455 */
Ville Syrjäläac484962016-01-20 21:05:26 +02002456 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08002457 return 0;
2458 if (WARN_ON(!horiz_pixels))
2459 return 0;
2460
Ville Syrjäläac484962016-01-20 21:05:26 +02002461 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002462}
2463
Imre Deak820c1982013-12-17 14:46:36 +02002464struct ilk_wm_maximums {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002465 u16 pri;
2466 u16 spr;
2467 u16 cur;
2468 u16 fbc;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002469};
2470
Ville Syrjälä37126462013-08-01 16:18:55 +03002471/*
2472 * For both WM_PIPE and WM_LP.
2473 * mem_value must be in 0.1us units.
2474 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002475static u32 ilk_compute_pri_wm(const struct intel_crtc_state *crtc_state,
2476 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002477 u32 mem_value, bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002478{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002479 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002480 int cpp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002481
Ville Syrjälä03981c62018-11-14 19:34:40 +02002482 if (mem_value == 0)
2483 return U32_MAX;
2484
Maarten Lankhorstec193642019-06-28 10:55:17 +02002485 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002486 return 0;
2487
Maarten Lankhorstec193642019-06-28 10:55:17 +02002488 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002489
Maarten Lankhorstec193642019-06-28 10:55:17 +02002490 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002491
2492 if (!is_lp)
2493 return method1;
2494
Maarten Lankhorstec193642019-06-28 10:55:17 +02002495 method2 = ilk_wm_method2(crtc_state->pixel_rate,
2496 crtc_state->base.adjusted_mode.crtc_htotal,
2497 drm_rect_width(&plane_state->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002498 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002499
2500 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002501}
2502
Ville Syrjälä37126462013-08-01 16:18:55 +03002503/*
2504 * For both WM_PIPE and WM_LP.
2505 * mem_value must be in 0.1us units.
2506 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002507static u32 ilk_compute_spr_wm(const struct intel_crtc_state *crtc_state,
2508 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002509 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002510{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002511 u32 method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002512 int cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002513
Ville Syrjälä03981c62018-11-14 19:34:40 +02002514 if (mem_value == 0)
2515 return U32_MAX;
2516
Maarten Lankhorstec193642019-06-28 10:55:17 +02002517 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002518 return 0;
2519
Maarten Lankhorstec193642019-06-28 10:55:17 +02002520 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002521
Maarten Lankhorstec193642019-06-28 10:55:17 +02002522 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
2523 method2 = ilk_wm_method2(crtc_state->pixel_rate,
2524 crtc_state->base.adjusted_mode.crtc_htotal,
2525 drm_rect_width(&plane_state->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002526 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002527 return min(method1, method2);
2528}
2529
Ville Syrjälä37126462013-08-01 16:18:55 +03002530/*
2531 * For both WM_PIPE and WM_LP.
2532 * mem_value must be in 0.1us units.
2533 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002534static u32 ilk_compute_cur_wm(const struct intel_crtc_state *crtc_state,
2535 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002536 u32 mem_value)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002537{
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002538 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002539
Ville Syrjälä03981c62018-11-14 19:34:40 +02002540 if (mem_value == 0)
2541 return U32_MAX;
2542
Maarten Lankhorstec193642019-06-28 10:55:17 +02002543 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002544 return 0;
2545
Maarten Lankhorstec193642019-06-28 10:55:17 +02002546 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002547
Maarten Lankhorstec193642019-06-28 10:55:17 +02002548 return ilk_wm_method2(crtc_state->pixel_rate,
2549 crtc_state->base.adjusted_mode.crtc_htotal,
Maarten Lankhorst3a612762019-10-04 13:34:54 +02002550 drm_rect_width(&plane_state->base.dst),
2551 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002552}
2553
Paulo Zanonicca32e92013-05-31 11:45:06 -03002554/* Only for WM_LP. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02002555static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
2556 const struct intel_plane_state *plane_state,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002557 u32 pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002558{
Ville Syrjälä83054942016-11-18 21:53:00 +02002559 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002560
Maarten Lankhorstec193642019-06-28 10:55:17 +02002561 if (!intel_wm_plane_visible(crtc_state, plane_state))
Paulo Zanonicca32e92013-05-31 11:45:06 -03002562 return 0;
2563
Maarten Lankhorstec193642019-06-28 10:55:17 +02002564 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002565
Maarten Lankhorstec193642019-06-28 10:55:17 +02002566 return ilk_wm_fbc(pri_val, drm_rect_width(&plane_state->base.dst), cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002567}
2568
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002569static unsigned int
2570ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002571{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002572 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002573 return 3072;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002574 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002575 return 768;
2576 else
2577 return 512;
2578}
2579
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002580static unsigned int
2581ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2582 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002583{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002584 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002585 /* BDW primary/sprite plane watermarks */
2586 return level == 0 ? 255 : 2047;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002587 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002588 /* IVB/HSW primary/sprite plane watermarks */
2589 return level == 0 ? 127 : 1023;
2590 else if (!is_sprite)
2591 /* ILK/SNB primary plane watermarks */
2592 return level == 0 ? 127 : 511;
2593 else
2594 /* ILK/SNB sprite plane watermarks */
2595 return level == 0 ? 63 : 255;
2596}
2597
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002598static unsigned int
2599ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002600{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002601 if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002602 return level == 0 ? 63 : 255;
2603 else
2604 return level == 0 ? 31 : 63;
2605}
2606
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002607static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002608{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002609 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002610 return 31;
2611 else
2612 return 15;
2613}
2614
Ville Syrjälä158ae642013-08-07 13:28:19 +03002615/* Calculate the maximum primary/sprite plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002616static unsigned int ilk_plane_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002617 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002618 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002619 enum intel_ddb_partitioning ddb_partitioning,
2620 bool is_sprite)
2621{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002622 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002623
2624 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002625 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002626 return 0;
2627
2628 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002629 if (level == 0 || config->num_pipes_active > 1) {
Jani Nikula24977872019-09-11 12:26:08 +03002630 fifo_size /= INTEL_NUM_PIPES(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002631
2632 /*
2633 * For some reason the non self refresh
2634 * FIFO size is only half of the self
2635 * refresh FIFO size on ILK/SNB.
2636 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002637 if (INTEL_GEN(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002638 fifo_size /= 2;
2639 }
2640
Ville Syrjälä240264f2013-08-07 13:29:12 +03002641 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002642 /* level 0 is always calculated with 1:1 split */
2643 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2644 if (is_sprite)
2645 fifo_size *= 5;
2646 fifo_size /= 6;
2647 } else {
2648 fifo_size /= 2;
2649 }
2650 }
2651
2652 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002653 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002654}
2655
2656/* Calculate the maximum cursor plane watermark */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002657static unsigned int ilk_cursor_wm_max(const struct drm_i915_private *dev_priv,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002658 int level,
2659 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002660{
2661 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002662 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002663 return 64;
2664
2665 /* otherwise just report max that registers can hold */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002666 return ilk_cursor_wm_reg_max(dev_priv, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002667}
2668
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002669static void ilk_compute_wm_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002670 int level,
2671 const struct intel_wm_config *config,
2672 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002673 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002674{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08002675 max->pri = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, false);
2676 max->spr = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, true);
2677 max->cur = ilk_cursor_wm_max(dev_priv, level, config);
2678 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002679}
2680
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002681static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002682 int level,
2683 struct ilk_wm_maximums *max)
2684{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002685 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2686 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2687 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2688 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002689}
2690
Ville Syrjäläd9395652013-10-09 19:18:10 +03002691static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002692 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002693 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002694{
2695 bool ret;
2696
2697 /* already determined to be invalid? */
2698 if (!result->enable)
2699 return false;
2700
2701 result->enable = result->pri_val <= max->pri &&
2702 result->spr_val <= max->spr &&
2703 result->cur_val <= max->cur;
2704
2705 ret = result->enable;
2706
2707 /*
2708 * HACK until we can pre-compute everything,
2709 * and thus fail gracefully if LP0 watermarks
2710 * are exceeded...
2711 */
2712 if (level == 0 && !result->enable) {
2713 if (result->pri_val > max->pri)
2714 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2715 level, result->pri_val, max->pri);
2716 if (result->spr_val > max->spr)
2717 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2718 level, result->spr_val, max->spr);
2719 if (result->cur_val > max->cur)
2720 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2721 level, result->cur_val, max->cur);
2722
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002723 result->pri_val = min_t(u32, result->pri_val, max->pri);
2724 result->spr_val = min_t(u32, result->spr_val, max->spr);
2725 result->cur_val = min_t(u32, result->cur_val, max->cur);
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002726 result->enable = true;
2727 }
2728
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002729 return ret;
2730}
2731
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002732static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Matt Roper43d59ed2015-09-24 15:53:07 -07002733 const struct intel_crtc *intel_crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002734 int level,
Maarten Lankhorstec193642019-06-28 10:55:17 +02002735 struct intel_crtc_state *crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02002736 const struct intel_plane_state *pristate,
2737 const struct intel_plane_state *sprstate,
2738 const struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002739 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002740{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002741 u16 pri_latency = dev_priv->wm.pri_latency[level];
2742 u16 spr_latency = dev_priv->wm.spr_latency[level];
2743 u16 cur_latency = dev_priv->wm.cur_latency[level];
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002744
2745 /* WM1+ latency values stored in 0.5us units */
2746 if (level > 0) {
2747 pri_latency *= 5;
2748 spr_latency *= 5;
2749 cur_latency *= 5;
2750 }
2751
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002752 if (pristate) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02002753 result->pri_val = ilk_compute_pri_wm(crtc_state, pristate,
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002754 pri_latency, level);
Maarten Lankhorstec193642019-06-28 10:55:17 +02002755 result->fbc_val = ilk_compute_fbc_wm(crtc_state, pristate, result->pri_val);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002756 }
2757
2758 if (sprstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002759 result->spr_val = ilk_compute_spr_wm(crtc_state, sprstate, spr_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002760
2761 if (curstate)
Maarten Lankhorstec193642019-06-28 10:55:17 +02002762 result->cur_val = ilk_compute_cur_wm(crtc_state, curstate, cur_latency);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002763
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002764 result->enable = true;
2765}
2766
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002767static u32
Maarten Lankhorstec193642019-06-28 10:55:17 +02002768hsw_compute_linetime_wm(const struct intel_crtc_state *crtc_state)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002769{
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002770 const struct intel_atomic_state *intel_state =
Maarten Lankhorstec193642019-06-28 10:55:17 +02002771 to_intel_atomic_state(crtc_state->base.state);
Matt Roperee91a152015-12-03 11:37:39 -08002772 const struct drm_display_mode *adjusted_mode =
Maarten Lankhorstec193642019-06-28 10:55:17 +02002773 &crtc_state->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002774 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002775
Maarten Lankhorstec193642019-06-28 10:55:17 +02002776 if (!crtc_state->base.active)
Matt Roperee91a152015-12-03 11:37:39 -08002777 return 0;
2778 if (WARN_ON(adjusted_mode->crtc_clock == 0))
2779 return 0;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002780 if (WARN_ON(intel_state->cdclk.logical.cdclk == 0))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002781 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002782
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002783 /* The WM are computed with base on how long it takes to fill a single
2784 * row at the given clock rate, multiplied by 8.
2785 * */
Ville Syrjälä124abe02015-09-08 13:40:45 +03002786 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2787 adjusted_mode->crtc_clock);
2788 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002789 intel_state->cdclk.logical.cdclk);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002790
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002791 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2792 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002793}
2794
Ville Syrjäläbb726512016-10-31 22:37:24 +02002795static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002796 u16 wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002797{
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002798 struct intel_uncore *uncore = &dev_priv->uncore;
2799
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002800 if (INTEL_GEN(dev_priv) >= 9) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002801 u32 val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002802 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002803 int level, max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002804
2805 /* read the first set of memory latencies[0:3] */
2806 val = 0; /* data0 to be programmed to 0 for first set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002807 ret = sandybridge_pcode_read(dev_priv,
2808 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002809 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002810
2811 if (ret) {
2812 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2813 return;
2814 }
2815
2816 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2817 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2818 GEN9_MEM_LATENCY_LEVEL_MASK;
2819 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2820 GEN9_MEM_LATENCY_LEVEL_MASK;
2821 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2822 GEN9_MEM_LATENCY_LEVEL_MASK;
2823
2824 /* read the second set of memory latencies[4:7] */
2825 val = 1; /* data0 to be programmed to 1 for second set */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002826 ret = sandybridge_pcode_read(dev_priv,
2827 GEN9_PCODE_READ_MEM_LATENCY,
Ville Syrjäläd284d512019-05-21 19:40:24 +03002828 &val, NULL);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002829 if (ret) {
2830 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2831 return;
2832 }
2833
2834 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2835 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2836 GEN9_MEM_LATENCY_LEVEL_MASK;
2837 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2838 GEN9_MEM_LATENCY_LEVEL_MASK;
2839 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2840 GEN9_MEM_LATENCY_LEVEL_MASK;
2841
Vandana Kannan367294b2014-11-04 17:06:46 +00002842 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002843 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2844 * need to be disabled. We make sure to sanitize the values out
2845 * of the punit to satisfy this requirement.
2846 */
2847 for (level = 1; level <= max_level; level++) {
2848 if (wm[level] == 0) {
2849 for (i = level + 1; i <= max_level; i++)
2850 wm[i] = 0;
2851 break;
2852 }
2853 }
2854
2855 /*
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002856 * WaWmMemoryReadLatency:skl+,glk
Damien Lespiau6f972352015-02-09 19:33:07 +00002857 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002858 * punit doesn't take into account the read latency so we need
Paulo Zanoni0727e402016-09-22 18:00:30 -03002859 * to add 2us to the various latency levels we retrieve from the
2860 * punit when level 0 response data us 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002861 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002862 if (wm[0] == 0) {
2863 wm[0] += 2;
2864 for (level = 1; level <= max_level; level++) {
2865 if (wm[level] == 0)
2866 break;
Vandana Kannan367294b2014-11-04 17:06:46 +00002867 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002868 }
Paulo Zanoni0727e402016-09-22 18:00:30 -03002869 }
2870
Mahesh Kumar86b59282018-08-31 16:39:42 +05302871 /*
2872 * WA Level-0 adjustment for 16GB DIMMs: SKL+
2873 * If we could not get dimm info enable this WA to prevent from
2874 * any underrun. If not able to get Dimm info assume 16GB dimm
2875 * to avoid any underrun.
2876 */
Ville Syrjälä5d6f36b2018-10-23 21:21:02 +03002877 if (dev_priv->dram_info.is_16gb_dimm)
Mahesh Kumar86b59282018-08-31 16:39:42 +05302878 wm[0] += 1;
2879
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002880 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002881 u64 sskpd = intel_uncore_read64(uncore, MCH_SSKPD);
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002882
2883 wm[0] = (sskpd >> 56) & 0xFF;
2884 if (wm[0] == 0)
2885 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002886 wm[1] = (sskpd >> 4) & 0xFF;
2887 wm[2] = (sskpd >> 12) & 0xFF;
2888 wm[3] = (sskpd >> 20) & 0x1FF;
2889 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002890 } else if (INTEL_GEN(dev_priv) >= 6) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002891 u32 sskpd = intel_uncore_read(uncore, MCH_SSKPD);
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002892
2893 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2894 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2895 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2896 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002897 } else if (INTEL_GEN(dev_priv) >= 5) {
Tvrtko Ursulin1cea02d2019-06-10 13:06:07 +01002898 u32 mltr = intel_uncore_read(uncore, MLTR_ILK);
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002899
2900 /* ILK primary LP0 latency is 700 ns */
2901 wm[0] = 7;
2902 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2903 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002904 } else {
2905 MISSING_CASE(INTEL_DEVID(dev_priv));
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002906 }
2907}
2908
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002909static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002910 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002911{
2912 /* ILK sprite LP0 latency is 1300 ns */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002913 if (IS_GEN(dev_priv, 5))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002914 wm[0] = 13;
2915}
2916
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002917static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002918 u16 wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002919{
2920 /* ILK cursor LP0 latency is 1300 ns */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002921 if (IS_GEN(dev_priv, 5))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002922 wm[0] = 13;
Ville Syrjälä53615a52013-08-01 16:18:50 +03002923}
2924
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002925int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002926{
2927 /* how many WM levels are we expecting */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002928 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002929 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002930 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002931 return 4;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002932 else if (INTEL_GEN(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002933 return 3;
2934 else
2935 return 2;
2936}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002937
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002938static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002939 const char *name,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002940 const u16 wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002941{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002942 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002943
2944 for (level = 0; level <= max_level; level++) {
2945 unsigned int latency = wm[level];
2946
2947 if (latency == 0) {
Chris Wilson86c1c872018-07-26 17:15:27 +01002948 DRM_DEBUG_KMS("%s WM%d latency not provided\n",
2949 name, level);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002950 continue;
2951 }
2952
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002953 /*
2954 * - latencies are in us on gen9.
2955 * - before then, WM1+ latency values are in 0.5us units
2956 */
Paulo Zanonidfc267a2017-08-09 13:52:46 -07002957 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002958 latency *= 10;
2959 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002960 latency *= 5;
2961
2962 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2963 name, level, wm[level],
2964 latency / 10, latency % 10);
2965 }
2966}
2967
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002968static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002969 u16 wm[5], u16 min)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002970{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002971 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002972
2973 if (wm[0] >= min)
2974 return false;
2975
2976 wm[0] = max(wm[0], min);
2977 for (level = 1; level <= max_level; level++)
Jani Nikula5ce9a6492019-01-18 14:01:20 +02002978 wm[level] = max_t(u16, wm[level], DIV_ROUND_UP(min, 5));
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002979
2980 return true;
2981}
2982
Ville Syrjäläbb726512016-10-31 22:37:24 +02002983static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002984{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002985 bool changed;
2986
2987 /*
2988 * The BIOS provided WM memory latency values are often
2989 * inadequate for high resolution displays. Adjust them.
2990 */
2991 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2992 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2993 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2994
2995 if (!changed)
2996 return;
2997
2998 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002999 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3000 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3001 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003002}
3003
Ville Syrjälä03981c62018-11-14 19:34:40 +02003004static void snb_wm_lp3_irq_quirk(struct drm_i915_private *dev_priv)
3005{
3006 /*
3007 * On some SNB machines (Thinkpad X220 Tablet at least)
3008 * LP3 usage can cause vblank interrupts to be lost.
3009 * The DEIIR bit will go high but it looks like the CPU
3010 * never gets interrupted.
3011 *
3012 * It's not clear whether other interrupt source could
3013 * be affected or if this is somehow limited to vblank
3014 * interrupts only. To play it safe we disable LP3
3015 * watermarks entirely.
3016 */
3017 if (dev_priv->wm.pri_latency[3] == 0 &&
3018 dev_priv->wm.spr_latency[3] == 0 &&
3019 dev_priv->wm.cur_latency[3] == 0)
3020 return;
3021
3022 dev_priv->wm.pri_latency[3] = 0;
3023 dev_priv->wm.spr_latency[3] = 0;
3024 dev_priv->wm.cur_latency[3] = 0;
3025
3026 DRM_DEBUG_KMS("LP3 watermarks disabled due to potential for lost interrupts\n");
3027 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3028 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3029 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
3030}
3031
Ville Syrjäläbb726512016-10-31 22:37:24 +02003032static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03003033{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003034 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03003035
3036 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
3037 sizeof(dev_priv->wm.pri_latency));
3038 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
3039 sizeof(dev_priv->wm.pri_latency));
3040
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003041 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003042 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003043
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003044 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3045 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3046 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003047
Lucas De Marchicf819ef2018-12-12 10:10:43 -08003048 if (IS_GEN(dev_priv, 6)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02003049 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä03981c62018-11-14 19:34:40 +02003050 snb_wm_lp3_irq_quirk(dev_priv);
3051 }
Ville Syrjälä53615a52013-08-01 16:18:50 +03003052}
3053
Ville Syrjäläbb726512016-10-31 22:37:24 +02003054static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003055{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003056 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003057 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003058}
3059
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003060static bool ilk_validate_pipe_wm(const struct drm_i915_private *dev_priv,
Matt Ropered4a6a72016-02-23 17:20:13 -08003061 struct intel_pipe_wm *pipe_wm)
3062{
3063 /* LP0 watermark maximums depend on this pipe alone */
3064 const struct intel_wm_config config = {
3065 .num_pipes_active = 1,
3066 .sprites_enabled = pipe_wm->sprites_enabled,
3067 .sprites_scaled = pipe_wm->sprites_scaled,
3068 };
3069 struct ilk_wm_maximums max;
3070
3071 /* LP0 watermarks always use 1/2 DDB partitioning */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003072 ilk_compute_wm_maximums(dev_priv, 0, &config, INTEL_DDB_PART_1_2, &max);
Matt Ropered4a6a72016-02-23 17:20:13 -08003073
3074 /* At least LP0 must be valid */
3075 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
3076 DRM_DEBUG_KMS("LP0 watermark invalid\n");
3077 return false;
3078 }
3079
3080 return true;
3081}
3082
Matt Roper261a27d2015-10-08 15:28:25 -07003083/* Compute new watermarks for the pipe */
Maarten Lankhorstec193642019-06-28 10:55:17 +02003084static int ilk_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Matt Roper261a27d2015-10-08 15:28:25 -07003085{
Maarten Lankhorstec193642019-06-28 10:55:17 +02003086 struct drm_atomic_state *state = crtc_state->base.state;
3087 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07003088 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003089 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003090 const struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02003091 struct intel_plane *plane;
3092 const struct intel_plane_state *plane_state;
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003093 const struct intel_plane_state *pristate = NULL;
3094 const struct intel_plane_state *sprstate = NULL;
3095 const struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003096 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02003097 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003098
Maarten Lankhorstec193642019-06-28 10:55:17 +02003099 pipe_wm = &crtc_state->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003100
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02003101 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
3102 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
3103 pristate = plane_state;
3104 else if (plane->base.type == DRM_PLANE_TYPE_OVERLAY)
3105 sprstate = plane_state;
3106 else if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
3107 curstate = plane_state;
Matt Roper43d59ed2015-09-24 15:53:07 -07003108 }
3109
Maarten Lankhorstec193642019-06-28 10:55:17 +02003110 pipe_wm->pipe_enabled = crtc_state->base.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003111 if (sprstate) {
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003112 pipe_wm->sprites_enabled = sprstate->base.visible;
3113 pipe_wm->sprites_scaled = sprstate->base.visible &&
3114 (drm_rect_width(&sprstate->base.dst) != drm_rect_width(&sprstate->base.src) >> 16 ||
3115 drm_rect_height(&sprstate->base.dst) != drm_rect_height(&sprstate->base.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003116 }
3117
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003118 usable_level = max_level;
3119
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003120 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003121 if (INTEL_GEN(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003122 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003123
3124 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08003125 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003126 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003127
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003128 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
Maarten Lankhorstec193642019-06-28 10:55:17 +02003129 ilk_compute_wm_level(dev_priv, intel_crtc, 0, crtc_state,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003130 pristate, sprstate, curstate, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003131
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003132 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Maarten Lankhorstec193642019-06-28 10:55:17 +02003133 pipe_wm->linetime = hsw_compute_linetime_wm(crtc_state);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003134
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003135 if (!ilk_validate_pipe_wm(dev_priv, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01003136 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003137
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003138 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003139
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003140 for (level = 1; level <= usable_level; level++) {
3141 struct intel_wm_level *wm = &pipe_wm->wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003142
Maarten Lankhorstec193642019-06-28 10:55:17 +02003143 ilk_compute_wm_level(dev_priv, intel_crtc, level, crtc_state,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003144 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003145
3146 /*
3147 * Disable any watermark level that exceeds the
3148 * register maximums since such watermarks are
3149 * always invalid.
3150 */
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003151 if (!ilk_validate_wm_level(level, &max, wm)) {
3152 memset(wm, 0, sizeof(*wm));
3153 break;
3154 }
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003155 }
3156
Matt Roper86c8bbb2015-09-24 15:53:16 -07003157 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003158}
3159
3160/*
Matt Ropered4a6a72016-02-23 17:20:13 -08003161 * Build a set of 'intermediate' watermark values that satisfy both the old
3162 * state and the new state. These can be programmed to the hardware
3163 * immediately.
3164 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003165static int ilk_compute_intermediate_wm(struct intel_crtc_state *newstate)
Matt Ropered4a6a72016-02-23 17:20:13 -08003166{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003167 struct intel_crtc *intel_crtc = to_intel_crtc(newstate->base.crtc);
3168 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Matt Ropere8f1f022016-05-12 07:05:55 -07003169 struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003170 struct intel_atomic_state *intel_state =
3171 to_intel_atomic_state(newstate->base.state);
3172 const struct intel_crtc_state *oldstate =
3173 intel_atomic_get_old_crtc_state(intel_state, intel_crtc);
3174 const struct intel_pipe_wm *b = &oldstate->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003175 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08003176
3177 /*
3178 * Start with the final, target watermarks, then combine with the
3179 * currently active watermarks to get values that are safe both before
3180 * and after the vblank.
3181 */
Matt Ropere8f1f022016-05-12 07:05:55 -07003182 *a = newstate->wm.ilk.optimal;
Ville Syrjäläf255c622018-11-08 17:10:13 +02003183 if (!newstate->base.active || drm_atomic_crtc_needs_modeset(&newstate->base) ||
3184 intel_state->skip_intermediate_wm)
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003185 return 0;
3186
Matt Ropered4a6a72016-02-23 17:20:13 -08003187 a->pipe_enabled |= b->pipe_enabled;
3188 a->sprites_enabled |= b->sprites_enabled;
3189 a->sprites_scaled |= b->sprites_scaled;
3190
3191 for (level = 0; level <= max_level; level++) {
3192 struct intel_wm_level *a_wm = &a->wm[level];
3193 const struct intel_wm_level *b_wm = &b->wm[level];
3194
3195 a_wm->enable &= b_wm->enable;
3196 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
3197 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
3198 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
3199 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
3200 }
3201
3202 /*
3203 * We need to make sure that these merged watermark values are
3204 * actually a valid configuration themselves. If they're not,
3205 * there's no safe way to transition from the old state to
3206 * the new state, so we need to fail the atomic transaction.
3207 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003208 if (!ilk_validate_pipe_wm(dev_priv, a))
Matt Ropered4a6a72016-02-23 17:20:13 -08003209 return -EINVAL;
3210
3211 /*
3212 * If our intermediate WM are identical to the final WM, then we can
3213 * omit the post-vblank programming; only update if it's different.
3214 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02003215 if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) != 0)
3216 newstate->wm.need_postvbl_update = true;
Matt Ropered4a6a72016-02-23 17:20:13 -08003217
3218 return 0;
3219}
3220
3221/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003222 * Merge the watermarks from all active pipes for a specific level.
3223 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003224static void ilk_merge_wm_level(struct drm_i915_private *dev_priv,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003225 int level,
3226 struct intel_wm_level *ret_wm)
3227{
3228 const struct intel_crtc *intel_crtc;
3229
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003230 ret_wm->enable = true;
3231
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003232 for_each_intel_crtc(&dev_priv->drm, intel_crtc) {
Matt Ropered4a6a72016-02-23 17:20:13 -08003233 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02003234 const struct intel_wm_level *wm = &active->wm[level];
3235
3236 if (!active->pipe_enabled)
3237 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003238
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003239 /*
3240 * The watermark values may have been used in the past,
3241 * so we must maintain them in the registers for some
3242 * time even if the level is now disabled.
3243 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003244 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003245 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003246
3247 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
3248 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
3249 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
3250 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
3251 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003252}
3253
3254/*
3255 * Merge all low power watermarks for all active pipes.
3256 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003257static void ilk_wm_merge(struct drm_i915_private *dev_priv,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003258 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02003259 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003260 struct intel_pipe_wm *merged)
3261{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003262 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003263 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003264
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003265 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003266 if ((INTEL_GEN(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003267 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03003268 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003269
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003270 /* ILK: FBC WM must be disabled always */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003271 merged->fbc_wm_enabled = INTEL_GEN(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003272
3273 /* merge each WM1+ level */
3274 for (level = 1; level <= max_level; level++) {
3275 struct intel_wm_level *wm = &merged->wm[level];
3276
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003277 ilk_merge_wm_level(dev_priv, level, wm);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003278
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003279 if (level > last_enabled_level)
3280 wm->enable = false;
3281 else if (!ilk_validate_wm_level(level, max, wm))
3282 /* make sure all following levels get disabled */
3283 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003284
3285 /*
3286 * The spec says it is preferred to disable
3287 * FBC WMs instead of disabling a WM level.
3288 */
3289 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003290 if (wm->enable)
3291 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003292 wm->fbc_val = 0;
3293 }
3294 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003295
3296 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
3297 /*
3298 * FIXME this is racy. FBC might get enabled later.
3299 * What we should check here is whether FBC can be
3300 * enabled sometime later.
3301 */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08003302 if (IS_GEN(dev_priv, 5) && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03003303 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003304 for (level = 2; level <= max_level; level++) {
3305 struct intel_wm_level *wm = &merged->wm[level];
3306
3307 wm->enable = false;
3308 }
3309 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003310}
3311
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003312static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
3313{
3314 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
3315 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
3316}
3317
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003318/* The value we need to program into the WM_LPx latency field */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003319static unsigned int ilk_wm_lp_latency(struct drm_i915_private *dev_priv,
3320 int level)
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003321{
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003322 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003323 return 2 * level;
3324 else
3325 return dev_priv->wm.pri_latency[level];
3326}
3327
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003328static void ilk_compute_wm_results(struct drm_i915_private *dev_priv,
Ville Syrjälä0362c782013-10-09 19:17:57 +03003329 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03003330 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02003331 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003332{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003333 struct intel_crtc *intel_crtc;
3334 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003335
Ville Syrjälä0362c782013-10-09 19:17:57 +03003336 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03003337 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003338
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003339 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03003340 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03003341 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003342
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003343 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003344
Ville Syrjälä0362c782013-10-09 19:17:57 +03003345 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03003346
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003347 /*
3348 * Maintain the watermark values even if the level is
3349 * disabled. Doing otherwise could cause underruns.
3350 */
3351 results->wm_lp[wm_lp - 1] =
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003352 (ilk_wm_lp_latency(dev_priv, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07003353 (r->pri_val << WM1_LP_SR_SHIFT) |
3354 r->cur_val;
3355
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003356 if (r->enable)
3357 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
3358
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003359 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07003360 results->wm_lp[wm_lp - 1] |=
3361 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
3362 else
3363 results->wm_lp[wm_lp - 1] |=
3364 r->fbc_val << WM1_LP_FBC_SHIFT;
3365
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003366 /*
3367 * Always set WM1S_LP_EN when spr_val != 0, even if the
3368 * level is disabled. Doing otherwise could cause underruns.
3369 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003370 if (INTEL_GEN(dev_priv) <= 6 && r->spr_val) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003371 WARN_ON(wm_lp != 1);
3372 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
3373 } else
3374 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003375 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003376
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003377 /* LP0 register values */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003378 for_each_intel_crtc(&dev_priv->drm, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003379 enum pipe pipe = intel_crtc->pipe;
Matt Ropered4a6a72016-02-23 17:20:13 -08003380 const struct intel_wm_level *r =
3381 &intel_crtc->wm.active.ilk.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003382
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003383 if (WARN_ON(!r->enable))
3384 continue;
3385
Matt Ropered4a6a72016-02-23 17:20:13 -08003386 results->wm_linetime[pipe] = intel_crtc->wm.active.ilk.linetime;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003387
3388 results->wm_pipe[pipe] =
3389 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
3390 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
3391 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003392 }
3393}
3394
Paulo Zanoni861f3382013-05-31 10:19:21 -03003395/* Find the result with the highest level enabled. Check for enable_fbc_wm in
3396 * case both are at the same level. Prefer r1 in case they're the same. */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003397static struct intel_pipe_wm *
3398ilk_find_best_result(struct drm_i915_private *dev_priv,
3399 struct intel_pipe_wm *r1,
3400 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003401{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08003402 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003403 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003404
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003405 for (level = 1; level <= max_level; level++) {
3406 if (r1->wm[level].enable)
3407 level1 = level;
3408 if (r2->wm[level].enable)
3409 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003410 }
3411
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003412 if (level1 == level2) {
3413 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003414 return r2;
3415 else
3416 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003417 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03003418 return r1;
3419 } else {
3420 return r2;
3421 }
3422}
3423
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003424/* dirty bits used to track which watermarks need changes */
3425#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
3426#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
3427#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
3428#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
3429#define WM_DIRTY_FBC (1 << 24)
3430#define WM_DIRTY_DDB (1 << 25)
3431
Damien Lespiau055e3932014-08-18 13:49:10 +01003432static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02003433 const struct ilk_wm_values *old,
3434 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003435{
3436 unsigned int dirty = 0;
3437 enum pipe pipe;
3438 int wm_lp;
3439
Damien Lespiau055e3932014-08-18 13:49:10 +01003440 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003441 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
3442 dirty |= WM_DIRTY_LINETIME(pipe);
3443 /* Must disable LP1+ watermarks too */
3444 dirty |= WM_DIRTY_LP_ALL;
3445 }
3446
3447 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
3448 dirty |= WM_DIRTY_PIPE(pipe);
3449 /* Must disable LP1+ watermarks too */
3450 dirty |= WM_DIRTY_LP_ALL;
3451 }
3452 }
3453
3454 if (old->enable_fbc_wm != new->enable_fbc_wm) {
3455 dirty |= WM_DIRTY_FBC;
3456 /* Must disable LP1+ watermarks too */
3457 dirty |= WM_DIRTY_LP_ALL;
3458 }
3459
3460 if (old->partitioning != new->partitioning) {
3461 dirty |= WM_DIRTY_DDB;
3462 /* Must disable LP1+ watermarks too */
3463 dirty |= WM_DIRTY_LP_ALL;
3464 }
3465
3466 /* LP1+ watermarks already deemed dirty, no need to continue */
3467 if (dirty & WM_DIRTY_LP_ALL)
3468 return dirty;
3469
3470 /* Find the lowest numbered LP1+ watermark in need of an update... */
3471 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3472 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
3473 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
3474 break;
3475 }
3476
3477 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
3478 for (; wm_lp <= 3; wm_lp++)
3479 dirty |= WM_DIRTY_LP(wm_lp);
3480
3481 return dirty;
3482}
3483
Ville Syrjälä8553c182013-12-05 15:51:39 +02003484static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
3485 unsigned int dirty)
3486{
Imre Deak820c1982013-12-17 14:46:36 +02003487 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02003488 bool changed = false;
3489
3490 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
3491 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
3492 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
3493 changed = true;
3494 }
3495 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
3496 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
3497 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
3498 changed = true;
3499 }
3500 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
3501 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
3502 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
3503 changed = true;
3504 }
3505
3506 /*
3507 * Don't touch WM1S_LP_EN here.
3508 * Doing so could cause underruns.
3509 */
3510
3511 return changed;
3512}
3513
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003514/*
3515 * The spec says we shouldn't write when we don't need, because every write
3516 * causes WMs to be re-evaluated, expending some power.
3517 */
Imre Deak820c1982013-12-17 14:46:36 +02003518static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
3519 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003520{
Imre Deak820c1982013-12-17 14:46:36 +02003521 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003522 unsigned int dirty;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02003523 u32 val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003524
Damien Lespiau055e3932014-08-18 13:49:10 +01003525 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003526 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003527 return;
3528
Ville Syrjälä8553c182013-12-05 15:51:39 +02003529 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003530
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003531 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003532 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003533 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003534 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003535 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003536 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
3537
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003538 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003539 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003540 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003541 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003542 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003543 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
3544
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003545 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003546 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003547 val = I915_READ(WM_MISC);
3548 if (results->partitioning == INTEL_DDB_PART_1_2)
3549 val &= ~WM_MISC_DATA_PARTITION_5_6;
3550 else
3551 val |= WM_MISC_DATA_PARTITION_5_6;
3552 I915_WRITE(WM_MISC, val);
3553 } else {
3554 val = I915_READ(DISP_ARB_CTL2);
3555 if (results->partitioning == INTEL_DDB_PART_1_2)
3556 val &= ~DISP_DATA_PARTITION_5_6;
3557 else
3558 val |= DISP_DATA_PARTITION_5_6;
3559 I915_WRITE(DISP_ARB_CTL2, val);
3560 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003561 }
3562
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003563 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03003564 val = I915_READ(DISP_ARB_CTL);
3565 if (results->enable_fbc_wm)
3566 val &= ~DISP_FBC_WM_DIS;
3567 else
3568 val |= DISP_FBC_WM_DIS;
3569 I915_WRITE(DISP_ARB_CTL, val);
3570 }
3571
Imre Deak954911e2013-12-17 14:46:34 +02003572 if (dirty & WM_DIRTY_LP(1) &&
3573 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
3574 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
3575
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003576 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003577 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
3578 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
3579 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
3580 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
3581 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003582
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003583 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003584 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003585 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003586 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003587 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003588 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003589
3590 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003591}
3592
Matt Ropered4a6a72016-02-23 17:20:13 -08003593bool ilk_disable_lp_wm(struct drm_device *dev)
Ville Syrjälä8553c182013-12-05 15:51:39 +02003594{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003595 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003596
3597 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3598}
3599
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303600static u8 intel_enabled_dbuf_slices_num(struct drm_i915_private *dev_priv)
3601{
3602 u8 enabled_slices;
3603
3604 /* Slice 1 will always be enabled */
3605 enabled_slices = 1;
3606
3607 /* Gen prior to GEN11 have only one DBuf slice */
3608 if (INTEL_GEN(dev_priv) < 11)
3609 return enabled_slices;
3610
Imre Deak209d7352019-03-07 12:32:35 +02003611 /*
3612 * FIXME: for now we'll only ever use 1 slice; pretend that we have
3613 * only that 1 slice enabled until we have a proper way for on-demand
3614 * toggling of the second slice.
3615 */
3616 if (0 && I915_READ(DBUF_CTL_S2) & DBUF_POWER_STATE)
Mahesh Kumar74bd8002018-04-26 19:55:15 +05303617 enabled_slices++;
3618
3619 return enabled_slices;
3620}
3621
Matt Roper024c9042015-09-24 15:53:11 -07003622/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003623 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3624 * so assume we'll always need it in order to avoid underruns.
3625 */
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003626static bool skl_needs_memory_bw_wa(struct drm_i915_private *dev_priv)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003627{
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003628 return IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003629}
3630
Paulo Zanoni56feca92016-09-22 18:00:28 -03003631static bool
3632intel_has_sagv(struct drm_i915_private *dev_priv)
3633{
Lucas De Marchi8ffa4392019-09-04 14:34:18 -07003634 /* HACK! */
3635 if (IS_GEN(dev_priv, 12))
3636 return false;
3637
Rodrigo Vivi1ca2b062018-10-26 13:03:17 -07003638 return (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) &&
3639 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED;
Paulo Zanoni56feca92016-09-22 18:00:28 -03003640}
3641
James Ausmusb068a862019-10-09 10:23:14 -07003642static void
3643skl_setup_sagv_block_time(struct drm_i915_private *dev_priv)
3644{
James Ausmusda80f042019-10-09 10:23:15 -07003645 if (INTEL_GEN(dev_priv) >= 12) {
3646 u32 val = 0;
3647 int ret;
3648
3649 ret = sandybridge_pcode_read(dev_priv,
3650 GEN12_PCODE_READ_SAGV_BLOCK_TIME_US,
3651 &val, NULL);
3652 if (!ret) {
3653 dev_priv->sagv_block_time_us = val;
3654 return;
3655 }
3656
3657 DRM_DEBUG_DRIVER("Couldn't read SAGV block time!\n");
3658 } else if (IS_GEN(dev_priv, 11)) {
James Ausmusb068a862019-10-09 10:23:14 -07003659 dev_priv->sagv_block_time_us = 10;
3660 return;
3661 } else if (IS_GEN(dev_priv, 10)) {
3662 dev_priv->sagv_block_time_us = 20;
3663 return;
3664 } else if (IS_GEN(dev_priv, 9)) {
3665 dev_priv->sagv_block_time_us = 30;
3666 return;
3667 } else {
3668 MISSING_CASE(INTEL_GEN(dev_priv));
3669 }
3670
3671 /* Default to an unusable block time */
3672 dev_priv->sagv_block_time_us = -1;
3673}
3674
Lyude656d1b82016-08-17 15:55:54 -04003675/*
3676 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3677 * depending on power and performance requirements. The display engine access
3678 * to system memory is blocked during the adjustment time. Because of the
3679 * blocking time, having this enabled can cause full system hangs and/or pipe
3680 * underruns if we don't meet all of the following requirements:
3681 *
3682 * - <= 1 pipe enabled
3683 * - All planes can enable watermarks for latencies >= SAGV engine block time
3684 * - We're not using an interlaced display configuration
3685 */
3686int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003687intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003688{
3689 int ret;
3690
Paulo Zanoni56feca92016-09-22 18:00:28 -03003691 if (!intel_has_sagv(dev_priv))
3692 return 0;
3693
3694 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003695 return 0;
3696
Ville Syrjäläff61a972018-12-21 19:14:34 +02003697 DRM_DEBUG_KMS("Enabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003698 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3699 GEN9_SAGV_ENABLE);
3700
Ville Syrjäläff61a972018-12-21 19:14:34 +02003701 /* We don't need to wait for SAGV when enabling */
Lyude656d1b82016-08-17 15:55:54 -04003702
3703 /*
3704 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003705 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003706 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003707 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003708 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003709 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003710 return 0;
3711 } else if (ret < 0) {
Ville Syrjäläff61a972018-12-21 19:14:34 +02003712 DRM_ERROR("Failed to enable SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003713 return ret;
3714 }
3715
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003716 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003717 return 0;
3718}
3719
Lyude656d1b82016-08-17 15:55:54 -04003720int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003721intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003722{
Imre Deakb3b8e992016-12-05 18:27:38 +02003723 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003724
Paulo Zanoni56feca92016-09-22 18:00:28 -03003725 if (!intel_has_sagv(dev_priv))
3726 return 0;
3727
3728 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003729 return 0;
3730
Ville Syrjäläff61a972018-12-21 19:14:34 +02003731 DRM_DEBUG_KMS("Disabling SAGV\n");
Lyude656d1b82016-08-17 15:55:54 -04003732 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003733 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3734 GEN9_SAGV_DISABLE,
3735 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3736 1);
Lyude656d1b82016-08-17 15:55:54 -04003737 /*
3738 * Some skl systems, pre-release machines in particular,
Ville Syrjäläff61a972018-12-21 19:14:34 +02003739 * don't actually have SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003740 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003741 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003742 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003743 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003744 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003745 } else if (ret < 0) {
Ville Syrjäläff61a972018-12-21 19:14:34 +02003746 DRM_ERROR("Failed to disable SAGV (%d)\n", ret);
Imre Deakb3b8e992016-12-05 18:27:38 +02003747 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003748 }
3749
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003750 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003751 return 0;
3752}
3753
Maarten Lankhorst855e0d62019-06-28 10:55:13 +02003754bool intel_can_enable_sagv(struct intel_atomic_state *state)
Lyude656d1b82016-08-17 15:55:54 -04003755{
Maarten Lankhorst855e0d62019-06-28 10:55:13 +02003756 struct drm_device *dev = state->base.dev;
Lyude656d1b82016-08-17 15:55:54 -04003757 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003758 struct intel_crtc *crtc;
3759 struct intel_plane *plane;
Maarten Lankhorstec193642019-06-28 10:55:17 +02003760 struct intel_crtc_state *crtc_state;
Lyude656d1b82016-08-17 15:55:54 -04003761 enum pipe pipe;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003762 int level, latency;
Lyude656d1b82016-08-17 15:55:54 -04003763
Paulo Zanoni56feca92016-09-22 18:00:28 -03003764 if (!intel_has_sagv(dev_priv))
3765 return false;
3766
Lyude656d1b82016-08-17 15:55:54 -04003767 /*
Lyude656d1b82016-08-17 15:55:54 -04003768 * If there are no active CRTCs, no additional checks need be performed
3769 */
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003770 if (hweight8(state->active_pipes) == 0)
Lyude656d1b82016-08-17 15:55:54 -04003771 return true;
Lucas De Marchida172232019-04-04 16:04:26 -07003772
3773 /*
3774 * SKL+ workaround: bspec recommends we disable SAGV when we have
3775 * more then one pipe enabled
3776 */
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003777 if (hweight8(state->active_pipes) > 1)
Lyude656d1b82016-08-17 15:55:54 -04003778 return false;
3779
3780 /* Since we're now guaranteed to only have one active CRTC... */
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03003781 pipe = ffs(state->active_pipes) - 1;
Ville Syrjälä98187832016-10-31 22:37:10 +02003782 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Maarten Lankhorstec193642019-06-28 10:55:17 +02003783 crtc_state = to_intel_crtc_state(crtc->base.state);
Lyude656d1b82016-08-17 15:55:54 -04003784
Paulo Zanonic89cadd2016-10-10 17:30:59 -03003785 if (crtc->base.state->adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003786 return false;
3787
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003788 for_each_intel_plane_on_crtc(dev, crtc, plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003789 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02003790 &crtc_state->wm.skl.optimal.planes[plane->id];
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003791
Lyude656d1b82016-08-17 15:55:54 -04003792 /* Skip this plane if it's not enabled */
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003793 if (!wm->wm[0].plane_en)
Lyude656d1b82016-08-17 15:55:54 -04003794 continue;
3795
3796 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003797 for (level = ilk_wm_max_level(dev_priv);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003798 !wm->wm[level].plane_en; --level)
Lyude656d1b82016-08-17 15:55:54 -04003799 { }
3800
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003801 latency = dev_priv->wm.skl_latency[level];
3802
Ville Syrjälä60e983f2018-12-21 19:14:33 +02003803 if (skl_needs_memory_bw_wa(dev_priv) &&
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003804 plane->base.state->fb->modifier ==
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003805 I915_FORMAT_MOD_X_TILED)
3806 latency += 15;
3807
Lyude656d1b82016-08-17 15:55:54 -04003808 /*
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003809 * If any of the planes on this pipe don't enable wm levels that
3810 * incur memory latencies higher than sagv_block_time_us we
Ville Syrjäläff61a972018-12-21 19:14:34 +02003811 * can't enable SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003812 */
James Ausmusb068a862019-10-09 10:23:14 -07003813 if (latency < dev_priv->sagv_block_time_us)
Lyude656d1b82016-08-17 15:55:54 -04003814 return false;
3815 }
3816
3817 return true;
3818}
3819
Mahesh Kumaraaa02372018-07-31 19:54:44 +05303820static u16 intel_get_ddb_size(struct drm_i915_private *dev_priv,
Maarten Lankhorstec193642019-06-28 10:55:17 +02003821 const struct intel_crtc_state *crtc_state,
Maarten Lankhorst24719e92018-10-22 12:20:00 +02003822 const u64 total_data_rate,
Mahesh Kumaraaa02372018-07-31 19:54:44 +05303823 const int num_active,
3824 struct skl_ddb_allocation *ddb)
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303825{
3826 const struct drm_display_mode *adjusted_mode;
3827 u64 total_data_bw;
3828 u16 ddb_size = INTEL_INFO(dev_priv)->ddb_size;
3829
3830 WARN_ON(ddb_size == 0);
3831
3832 if (INTEL_GEN(dev_priv) < 11)
3833 return ddb_size - 4; /* 4 blocks for bypass path allocation */
3834
Maarten Lankhorstec193642019-06-28 10:55:17 +02003835 adjusted_mode = &crtc_state->base.adjusted_mode;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02003836 total_data_bw = total_data_rate * drm_mode_vrefresh(adjusted_mode);
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303837
3838 /*
3839 * 12GB/s is maximum BW supported by single DBuf slice.
Ville Syrjäläad3e7b82019-01-30 17:51:10 +02003840 *
3841 * FIXME dbuf slice code is broken:
3842 * - must wait for planes to stop using the slice before powering it off
3843 * - plane straddling both slices is illegal in multi-pipe scenarios
3844 * - should validate we stay within the hw bandwidth limits
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303845 */
Ville Syrjäläad3e7b82019-01-30 17:51:10 +02003846 if (0 && (num_active > 1 || total_data_bw >= GBps(12))) {
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303847 ddb->enabled_slices = 2;
3848 } else {
3849 ddb->enabled_slices = 1;
3850 ddb_size /= 2;
3851 }
3852
3853 return ddb_size;
3854}
3855
Damien Lespiaub9cec072014-11-04 17:06:43 +00003856static void
Maarten Lankhorstb048a002018-10-18 13:51:30 +02003857skl_ddb_get_pipe_allocation_limits(struct drm_i915_private *dev_priv,
Maarten Lankhorstec193642019-06-28 10:55:17 +02003858 const struct intel_crtc_state *crtc_state,
Maarten Lankhorst24719e92018-10-22 12:20:00 +02003859 const u64 total_data_rate,
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303860 struct skl_ddb_allocation *ddb,
Matt Roperc107acf2016-05-12 07:06:01 -07003861 struct skl_ddb_entry *alloc, /* out */
3862 int *num_active /* out */)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003863{
Maarten Lankhorstec193642019-06-28 10:55:17 +02003864 struct drm_atomic_state *state = crtc_state->base.state;
Matt Roperc107acf2016-05-12 07:06:01 -07003865 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorstec193642019-06-28 10:55:17 +02003866 struct drm_crtc *for_crtc = crtc_state->base.crtc;
3867 const struct intel_crtc *crtc;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303868 u32 pipe_width = 0, total_width = 0, width_before_pipe = 0;
3869 enum pipe for_pipe = to_intel_crtc(for_crtc)->pipe;
3870 u16 ddb_size;
3871 u32 i;
Matt Roperc107acf2016-05-12 07:06:01 -07003872
Maarten Lankhorstec193642019-06-28 10:55:17 +02003873 if (WARN_ON(!state) || !crtc_state->base.active) {
Damien Lespiaub9cec072014-11-04 17:06:43 +00003874 alloc->start = 0;
3875 alloc->end = 0;
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003876 *num_active = hweight8(dev_priv->active_pipes);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003877 return;
3878 }
3879
Matt Ropera6d3460e2016-05-12 07:06:04 -07003880 if (intel_state->active_pipe_changes)
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003881 *num_active = hweight8(intel_state->active_pipes);
Matt Ropera6d3460e2016-05-12 07:06:04 -07003882 else
Ville Syrjälä0b14d962019-08-21 20:30:33 +03003883 *num_active = hweight8(dev_priv->active_pipes);
Matt Ropera6d3460e2016-05-12 07:06:04 -07003884
Maarten Lankhorstec193642019-06-28 10:55:17 +02003885 ddb_size = intel_get_ddb_size(dev_priv, crtc_state, total_data_rate,
Mahesh Kumaraa9664f2018-04-26 19:55:16 +05303886 *num_active, ddb);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003887
Matt Roperc107acf2016-05-12 07:06:01 -07003888 /*
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303889 * If the state doesn't change the active CRTC's or there is no
3890 * modeset request, then there's no need to recalculate;
3891 * the existing pipe allocation limits should remain unchanged.
3892 * Note that we're safe from racing commits since any racing commit
3893 * that changes the active CRTC list or do modeset would need to
3894 * grab _all_ crtc locks, including the one we currently hold.
Matt Roperc107acf2016-05-12 07:06:01 -07003895 */
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303896 if (!intel_state->active_pipe_changes && !intel_state->modeset) {
Maarten Lankhorst512b5522016-11-08 13:55:34 +01003897 /*
3898 * alloc may be cleared by clear_intel_crtc_state,
3899 * copy from old state to be sure
3900 */
3901 *alloc = to_intel_crtc_state(for_crtc->state)->wm.skl.ddb;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003902 return;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003903 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07003904
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303905 /*
3906 * Watermark/ddb requirement highly depends upon width of the
3907 * framebuffer, So instead of allocating DDB equally among pipes
3908 * distribute DDB based on resolution/width of the display.
3909 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02003910 for_each_new_intel_crtc_in_state(intel_state, crtc, crtc_state, i) {
3911 const struct drm_display_mode *adjusted_mode =
3912 &crtc_state->base.adjusted_mode;
3913 enum pipe pipe = crtc->pipe;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303914 int hdisplay, vdisplay;
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303915
Maarten Lankhorstec193642019-06-28 10:55:17 +02003916 if (!crtc_state->base.enable)
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303917 continue;
3918
Mahesh Kumarcf1f6972018-08-01 20:41:13 +05303919 drm_mode_get_hv_timing(adjusted_mode, &hdisplay, &vdisplay);
3920 total_width += hdisplay;
3921
3922 if (pipe < for_pipe)
3923 width_before_pipe += hdisplay;
3924 else if (pipe == for_pipe)
3925 pipe_width = hdisplay;
3926 }
3927
3928 alloc->start = ddb_size * width_before_pipe / total_width;
3929 alloc->end = ddb_size * (width_before_pipe + pipe_width) / total_width;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003930}
3931
Ville Syrjälädf331de2019-03-19 18:03:11 +02003932static int skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
3933 int width, const struct drm_format_info *format,
3934 u64 modifier, unsigned int rotation,
3935 u32 plane_pixel_rate, struct skl_wm_params *wp,
3936 int color_plane);
Maarten Lankhorstec193642019-06-28 10:55:17 +02003937static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Ville Syrjälädf331de2019-03-19 18:03:11 +02003938 int level,
3939 const struct skl_wm_params *wp,
3940 const struct skl_wm_level *result_prev,
3941 struct skl_wm_level *result /* out */);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003942
Ville Syrjälädf331de2019-03-19 18:03:11 +02003943static unsigned int
3944skl_cursor_allocation(const struct intel_crtc_state *crtc_state,
3945 int num_active)
3946{
3947 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
3948 int level, max_level = ilk_wm_max_level(dev_priv);
3949 struct skl_wm_level wm = {};
3950 int ret, min_ddb_alloc = 0;
3951 struct skl_wm_params wp;
3952
3953 ret = skl_compute_wm_params(crtc_state, 256,
3954 drm_format_info(DRM_FORMAT_ARGB8888),
3955 DRM_FORMAT_MOD_LINEAR,
3956 DRM_MODE_ROTATE_0,
3957 crtc_state->pixel_rate, &wp, 0);
3958 WARN_ON(ret);
3959
3960 for (level = 0; level <= max_level; level++) {
Ville Syrjälä6086e472019-03-21 19:51:28 +02003961 skl_compute_plane_wm(crtc_state, level, &wp, &wm, &wm);
Ville Syrjälädf331de2019-03-19 18:03:11 +02003962 if (wm.min_ddb_alloc == U16_MAX)
3963 break;
3964
3965 min_ddb_alloc = wm.min_ddb_alloc;
3966 }
3967
3968 return max(num_active == 1 ? 32 : 8, min_ddb_alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003969}
3970
Mahesh Kumar37cde112018-04-26 19:55:17 +05303971static void skl_ddb_entry_init_from_hw(struct drm_i915_private *dev_priv,
3972 struct skl_ddb_entry *entry, u32 reg)
Damien Lespiaua269c582014-11-04 17:06:49 +00003973{
Mahesh Kumar37cde112018-04-26 19:55:17 +05303974
Ville Syrjäläd7e449a2019-02-05 22:50:56 +02003975 entry->start = reg & DDB_ENTRY_MASK;
3976 entry->end = (reg >> DDB_ENTRY_END_SHIFT) & DDB_ENTRY_MASK;
Mahesh Kumar37cde112018-04-26 19:55:17 +05303977
Damien Lespiau16160e32014-11-04 17:06:53 +00003978 if (entry->end)
3979 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00003980}
3981
Mahesh Kumarddf34312018-04-09 09:11:03 +05303982static void
3983skl_ddb_get_hw_plane_state(struct drm_i915_private *dev_priv,
3984 const enum pipe pipe,
3985 const enum plane_id plane_id,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02003986 struct skl_ddb_entry *ddb_y,
3987 struct skl_ddb_entry *ddb_uv)
Mahesh Kumarddf34312018-04-09 09:11:03 +05303988{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02003989 u32 val, val2;
3990 u32 fourcc = 0;
Mahesh Kumarddf34312018-04-09 09:11:03 +05303991
3992 /* Cursor doesn't support NV12/planar, so no extra calculation needed */
3993 if (plane_id == PLANE_CURSOR) {
3994 val = I915_READ(CUR_BUF_CFG(pipe));
Ville Syrjäläff43bc32018-11-27 18:59:00 +02003995 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
Mahesh Kumarddf34312018-04-09 09:11:03 +05303996 return;
3997 }
3998
3999 val = I915_READ(PLANE_CTL(pipe, plane_id));
4000
4001 /* No DDB allocated for disabled planes */
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004002 if (val & PLANE_CTL_ENABLE)
4003 fourcc = skl_format_to_fourcc(val & PLANE_CTL_FORMAT_MASK,
4004 val & PLANE_CTL_ORDER_RGBX,
4005 val & PLANE_CTL_ALPHA_MASK);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304006
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004007 if (INTEL_GEN(dev_priv) >= 11) {
4008 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
4009 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4010 } else {
4011 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
Paulo Zanoni12a6c932018-07-31 17:46:14 -07004012 val2 = I915_READ(PLANE_NV12_BUF_CFG(pipe, plane_id));
Mahesh Kumarddf34312018-04-09 09:11:03 +05304013
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004014 if (fourcc &&
4015 drm_format_info_is_yuv_semiplanar(drm_format_info(fourcc)))
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004016 swap(val, val2);
4017
4018 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4019 skl_ddb_entry_init_from_hw(dev_priv, ddb_uv, val2);
Mahesh Kumarddf34312018-04-09 09:11:03 +05304020 }
4021}
4022
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004023void skl_pipe_ddb_get_hw_state(struct intel_crtc *crtc,
4024 struct skl_ddb_entry *ddb_y,
4025 struct skl_ddb_entry *ddb_uv)
4026{
4027 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4028 enum intel_display_power_domain power_domain;
4029 enum pipe pipe = crtc->pipe;
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004030 intel_wakeref_t wakeref;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004031 enum plane_id plane_id;
4032
4033 power_domain = POWER_DOMAIN_PIPE(pipe);
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004034 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
4035 if (!wakeref)
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004036 return;
4037
4038 for_each_plane_id_on_crtc(crtc, plane_id)
4039 skl_ddb_get_hw_plane_state(dev_priv, pipe,
4040 plane_id,
4041 &ddb_y[plane_id],
4042 &ddb_uv[plane_id]);
4043
Chris Wilson0e6e0be2019-01-14 14:21:24 +00004044 intel_display_power_put(dev_priv, power_domain, wakeref);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004045}
4046
Damien Lespiau08db6652014-11-04 17:06:52 +00004047void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
4048 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00004049{
Mahesh Kumar74bd8002018-04-26 19:55:15 +05304050 ddb->enabled_slices = intel_enabled_dbuf_slices_num(dev_priv);
Damien Lespiaua269c582014-11-04 17:06:49 +00004051}
4052
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004053/*
4054 * Determines the downscale amount of a plane for the purposes of watermark calculations.
4055 * The bspec defines downscale amount as:
4056 *
4057 * """
4058 * Horizontal down scale amount = maximum[1, Horizontal source size /
4059 * Horizontal destination size]
4060 * Vertical down scale amount = maximum[1, Vertical source size /
4061 * Vertical destination size]
4062 * Total down scale amount = Horizontal down scale amount *
4063 * Vertical down scale amount
4064 * """
4065 *
4066 * Return value is provided in 16.16 fixed point form to retain fractional part.
4067 * Caller should take care of dividing & rounding off the value.
4068 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304069static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02004070skl_plane_downscale_amount(const struct intel_crtc_state *crtc_state,
4071 const struct intel_plane_state *plane_state)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004072{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004073 u32 src_w, src_h, dst_w, dst_h;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304074 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
4075 uint_fixed_16_16_t downscale_h, downscale_w;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004076
Maarten Lankhorstec193642019-06-28 10:55:17 +02004077 if (WARN_ON(!intel_wm_plane_visible(crtc_state, plane_state)))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304078 return u32_to_fixed16(0);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004079
Maarten Lankhorst3a612762019-10-04 13:34:54 +02004080 /*
4081 * Src coordinates are already rotated by 270 degrees for
4082 * the 90/270 degree plane rotation cases (to match the
4083 * GTT mapping), hence no need to account for rotation here.
4084 *
4085 * n.b., src is 16.16 fixed point, dst is whole integer.
4086 */
4087 src_w = drm_rect_width(&plane_state->base.src) >> 16;
4088 src_h = drm_rect_height(&plane_state->base.src) >> 16;
4089 dst_w = drm_rect_width(&plane_state->base.dst);
4090 dst_h = drm_rect_height(&plane_state->base.dst);
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004091
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304092 fp_w_ratio = div_fixed16(src_w, dst_w);
4093 fp_h_ratio = div_fixed16(src_h, dst_h);
4094 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
4095 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004096
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304097 return mul_fixed16(downscale_w, downscale_h);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004098}
4099
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304100static uint_fixed_16_16_t
4101skl_pipe_downscale_amount(const struct intel_crtc_state *crtc_state)
4102{
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304103 uint_fixed_16_16_t pipe_downscale = u32_to_fixed16(1);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304104
4105 if (!crtc_state->base.enable)
4106 return pipe_downscale;
4107
4108 if (crtc_state->pch_pfit.enabled) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004109 u32 src_w, src_h, dst_w, dst_h;
4110 u32 pfit_size = crtc_state->pch_pfit.size;
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304111 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
4112 uint_fixed_16_16_t downscale_h, downscale_w;
4113
4114 src_w = crtc_state->pipe_src_w;
4115 src_h = crtc_state->pipe_src_h;
4116 dst_w = pfit_size >> 16;
4117 dst_h = pfit_size & 0xffff;
4118
4119 if (!dst_w || !dst_h)
4120 return pipe_downscale;
4121
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304122 fp_w_ratio = div_fixed16(src_w, dst_w);
4123 fp_h_ratio = div_fixed16(src_h, dst_h);
4124 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
4125 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304126
4127 pipe_downscale = mul_fixed16(downscale_w, downscale_h);
4128 }
4129
4130 return pipe_downscale;
4131}
4132
4133int skl_check_pipe_max_pixel_rate(struct intel_crtc *intel_crtc,
Maarten Lankhorstec193642019-06-28 10:55:17 +02004134 struct intel_crtc_state *crtc_state)
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304135{
Rodrigo Vivi43037c82017-10-03 15:31:42 -07004136 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Maarten Lankhorstec193642019-06-28 10:55:17 +02004137 struct drm_atomic_state *state = crtc_state->base.state;
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004138 const struct intel_plane_state *plane_state;
4139 struct intel_plane *plane;
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02004140 int crtc_clock, dotclk;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004141 u32 pipe_max_pixel_rate;
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304142 uint_fixed_16_16_t pipe_downscale;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304143 uint_fixed_16_16_t max_downscale = u32_to_fixed16(1);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304144
Maarten Lankhorstec193642019-06-28 10:55:17 +02004145 if (!crtc_state->base.enable)
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304146 return 0;
4147
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004148 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304149 uint_fixed_16_16_t plane_downscale;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304150 uint_fixed_16_16_t fp_9_div_8 = div_fixed16(9, 8);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304151 int bpp;
4152
Maarten Lankhorstec193642019-06-28 10:55:17 +02004153 if (!intel_wm_plane_visible(crtc_state, plane_state))
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304154 continue;
4155
Maarten Lankhorstec193642019-06-28 10:55:17 +02004156 if (WARN_ON(!plane_state->base.fb))
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304157 return -EINVAL;
4158
Maarten Lankhorstec193642019-06-28 10:55:17 +02004159 plane_downscale = skl_plane_downscale_amount(crtc_state, plane_state);
4160 bpp = plane_state->base.fb->format->cpp[0] * 8;
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304161 if (bpp == 64)
4162 plane_downscale = mul_fixed16(plane_downscale,
4163 fp_9_div_8);
4164
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304165 max_downscale = max_fixed16(plane_downscale, max_downscale);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304166 }
Maarten Lankhorstec193642019-06-28 10:55:17 +02004167 pipe_downscale = skl_pipe_downscale_amount(crtc_state);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304168
4169 pipe_downscale = mul_fixed16(pipe_downscale, max_downscale);
4170
Maarten Lankhorstec193642019-06-28 10:55:17 +02004171 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02004172 dotclk = to_intel_atomic_state(state)->cdclk.logical.cdclk;
4173
Rodrigo Vivi43037c82017-10-03 15:31:42 -07004174 if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10)
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02004175 dotclk *= 2;
4176
4177 pipe_max_pixel_rate = div_round_up_u32_fixed16(dotclk, pipe_downscale);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304178
4179 if (pipe_max_pixel_rate < crtc_clock) {
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02004180 DRM_DEBUG_KMS("Max supported pixel clock with scaling exceeded\n");
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304181 return -EINVAL;
4182 }
4183
4184 return 0;
4185}
4186
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004187static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004188skl_plane_relative_data_rate(const struct intel_crtc_state *crtc_state,
4189 const struct intel_plane_state *plane_state,
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004190 int color_plane)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004191{
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004192 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
4193 const struct drm_framebuffer *fb = plane_state->base.fb;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004194 u32 data_rate;
4195 u32 width = 0, height = 0;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304196 uint_fixed_16_16_t down_scale_amount;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004197 u64 rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07004198
Maarten Lankhorstec193642019-06-28 10:55:17 +02004199 if (!plane_state->base.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07004200 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02004201
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004202 if (plane->id == PLANE_CURSOR)
Matt Ropera1de91e2016-05-12 07:05:57 -07004203 return 0;
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004204
4205 if (color_plane == 1 &&
4206 !drm_format_info_is_yuv_semiplanar(fb->format))
Matt Ropera1de91e2016-05-12 07:05:57 -07004207 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004208
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004209 /*
4210 * Src coordinates are already rotated by 270 degrees for
4211 * the 90/270 degree plane rotation cases (to match the
4212 * GTT mapping), hence no need to account for rotation here.
4213 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004214 width = drm_rect_width(&plane_state->base.src) >> 16;
4215 height = drm_rect_height(&plane_state->base.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004216
Mahesh Kumarb879d582018-04-09 09:11:01 +05304217 /* UV plane does 1/2 pixel sub-sampling */
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004218 if (color_plane == 1) {
Mahesh Kumarb879d582018-04-09 09:11:01 +05304219 width /= 2;
4220 height /= 2;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004221 }
4222
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004223 data_rate = width * height;
Mahesh Kumarb879d582018-04-09 09:11:01 +05304224
Maarten Lankhorstec193642019-06-28 10:55:17 +02004225 down_scale_amount = skl_plane_downscale_amount(crtc_state, plane_state);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004226
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004227 rate = mul_round_up_u32_fixed16(data_rate, down_scale_amount);
4228
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004229 rate *= fb->format->cpp[color_plane];
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004230 return rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004231}
4232
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004233static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004234skl_get_total_relative_data_rate(struct intel_crtc_state *crtc_state,
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004235 u64 *plane_data_rate,
4236 u64 *uv_plane_data_rate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004237{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004238 struct drm_atomic_state *state = crtc_state->base.state;
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004239 struct intel_plane *plane;
4240 const struct intel_plane_state *plane_state;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004241 u64 total_data_rate = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07004242
4243 if (WARN_ON(!state))
4244 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004245
Matt Ropera1de91e2016-05-12 07:05:57 -07004246 /* Calculate and cache data rate for each plane */
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004247 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
4248 enum plane_id plane_id = plane->id;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004249 u64 rate;
Matt Roper024c9042015-09-24 15:53:11 -07004250
Mahesh Kumarb879d582018-04-09 09:11:01 +05304251 /* packed/y */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004252 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004253 plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004254 total_data_rate += rate;
Matt Roper9c74d822016-05-12 07:05:58 -07004255
Mahesh Kumarb879d582018-04-09 09:11:01 +05304256 /* uv-plane */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004257 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 1);
Mahesh Kumarb879d582018-04-09 09:11:01 +05304258 uv_plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004259 total_data_rate += rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004260 }
4261
4262 return total_data_rate;
4263}
4264
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004265static u64
Maarten Lankhorstec193642019-06-28 10:55:17 +02004266icl_get_total_relative_data_rate(struct intel_crtc_state *crtc_state,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004267 u64 *plane_data_rate)
4268{
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004269 struct intel_plane *plane;
4270 const struct intel_plane_state *plane_state;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004271 u64 total_data_rate = 0;
4272
Maarten Lankhorstec193642019-06-28 10:55:17 +02004273 if (WARN_ON(!crtc_state->base.state))
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004274 return 0;
4275
4276 /* Calculate and cache data rate for each plane */
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004277 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
4278 enum plane_id plane_id = plane->id;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004279 u64 rate;
4280
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004281 if (!plane_state->planar_linked_plane) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02004282 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004283 plane_data_rate[plane_id] = rate;
4284 total_data_rate += rate;
4285 } else {
4286 enum plane_id y_plane_id;
4287
4288 /*
4289 * The slave plane might not iterate in
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02004290 * intel_atomic_crtc_state_for_each_plane_state(),
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004291 * and needs the master plane state which may be
4292 * NULL if we try get_new_plane_state(), so we
4293 * always calculate from the master.
4294 */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004295 if (plane_state->planar_slave)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004296 continue;
4297
4298 /* Y plane rate is calculated on the slave */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004299 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02004300 y_plane_id = plane_state->planar_linked_plane->id;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004301 plane_data_rate[y_plane_id] = rate;
4302 total_data_rate += rate;
4303
Maarten Lankhorstec193642019-06-28 10:55:17 +02004304 rate = skl_plane_relative_data_rate(crtc_state, plane_state, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004305 plane_data_rate[plane_id] = rate;
4306 total_data_rate += rate;
4307 }
4308 }
4309
4310 return total_data_rate;
4311}
4312
Matt Roperc107acf2016-05-12 07:06:01 -07004313static int
Maarten Lankhorstec193642019-06-28 10:55:17 +02004314skl_allocate_pipe_ddb(struct intel_crtc_state *crtc_state,
Damien Lespiaub9cec072014-11-04 17:06:43 +00004315 struct skl_ddb_allocation *ddb /* out */)
4316{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004317 struct drm_atomic_state *state = crtc_state->base.state;
4318 struct drm_crtc *crtc = crtc_state->base.crtc;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004319 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004320 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstec193642019-06-28 10:55:17 +02004321 struct skl_ddb_entry *alloc = &crtc_state->wm.skl.ddb;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004322 u16 alloc_size, start = 0;
4323 u16 total[I915_MAX_PLANES] = {};
4324 u16 uv_total[I915_MAX_PLANES] = {};
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004325 u64 total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004326 enum plane_id plane_id;
Matt Roperc107acf2016-05-12 07:06:01 -07004327 int num_active;
Maarten Lankhorst24719e92018-10-22 12:20:00 +02004328 u64 plane_data_rate[I915_MAX_PLANES] = {};
4329 u64 uv_plane_data_rate[I915_MAX_PLANES] = {};
Ville Syrjälä0aded172019-02-05 17:50:53 +02004330 u32 blocks;
Matt Roperd8e87492018-12-11 09:31:07 -08004331 int level;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004332
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004333 /* Clear the partitioning for disabled planes. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004334 memset(crtc_state->wm.skl.plane_ddb_y, 0, sizeof(crtc_state->wm.skl.plane_ddb_y));
4335 memset(crtc_state->wm.skl.plane_ddb_uv, 0, sizeof(crtc_state->wm.skl.plane_ddb_uv));
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004336
Matt Ropera6d3460e2016-05-12 07:06:04 -07004337 if (WARN_ON(!state))
4338 return 0;
4339
Maarten Lankhorstec193642019-06-28 10:55:17 +02004340 if (!crtc_state->base.active) {
Lyudece0ba282016-09-15 10:46:35 -04004341 alloc->start = alloc->end = 0;
Matt Roperc107acf2016-05-12 07:06:01 -07004342 return 0;
4343 }
4344
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004345 if (INTEL_GEN(dev_priv) >= 11)
4346 total_data_rate =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004347 icl_get_total_relative_data_rate(crtc_state,
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004348 plane_data_rate);
4349 else
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004350 total_data_rate =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004351 skl_get_total_relative_data_rate(crtc_state,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004352 plane_data_rate,
4353 uv_plane_data_rate);
Lucas De Marchi323b0a82019-04-04 16:04:25 -07004354
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004355
Maarten Lankhorstec193642019-06-28 10:55:17 +02004356 skl_ddb_get_pipe_allocation_limits(dev_priv, crtc_state, total_data_rate,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004357 ddb, alloc, &num_active);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00004358 alloc_size = skl_ddb_entry_size(alloc);
Kumar, Mahesh336031e2017-05-17 17:28:25 +05304359 if (alloc_size == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004360 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004361
Matt Roperd8e87492018-12-11 09:31:07 -08004362 /* Allocate fixed number of blocks for cursor. */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004363 total[PLANE_CURSOR] = skl_cursor_allocation(crtc_state, num_active);
Matt Roperd8e87492018-12-11 09:31:07 -08004364 alloc_size -= total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02004365 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].start =
Matt Roperd8e87492018-12-11 09:31:07 -08004366 alloc->end - total[PLANE_CURSOR];
Maarten Lankhorstec193642019-06-28 10:55:17 +02004367 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].end = alloc->end;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004368
Matt Ropera1de91e2016-05-12 07:05:57 -07004369 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004370 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004371
Matt Roperd8e87492018-12-11 09:31:07 -08004372 /*
4373 * Find the highest watermark level for which we can satisfy the block
4374 * requirement of active planes.
4375 */
4376 for (level = ilk_wm_max_level(dev_priv); level >= 0; level--) {
Matt Roper25db2ea2018-12-12 11:17:20 -08004377 blocks = 0;
Matt Roperd8e87492018-12-11 09:31:07 -08004378 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004379 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004380 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä10a7e072019-03-12 22:58:40 +02004381
4382 if (plane_id == PLANE_CURSOR) {
4383 if (WARN_ON(wm->wm[level].min_ddb_alloc >
4384 total[PLANE_CURSOR])) {
4385 blocks = U32_MAX;
4386 break;
4387 }
4388 continue;
4389 }
4390
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004391 blocks += wm->wm[level].min_ddb_alloc;
4392 blocks += wm->uv_wm[level].min_ddb_alloc;
Matt Roperd8e87492018-12-11 09:31:07 -08004393 }
4394
Ville Syrjälä3cf963c2019-03-12 22:58:36 +02004395 if (blocks <= alloc_size) {
Matt Roperd8e87492018-12-11 09:31:07 -08004396 alloc_size -= blocks;
4397 break;
4398 }
4399 }
4400
4401 if (level < 0) {
4402 DRM_DEBUG_KMS("Requested display configuration exceeds system DDB limitations");
4403 DRM_DEBUG_KMS("minimum required %d/%d\n", blocks,
4404 alloc_size);
4405 return -EINVAL;
4406 }
4407
4408 /*
4409 * Grant each plane the blocks it requires at the highest achievable
4410 * watermark level, plus an extra share of the leftover blocks
4411 * proportional to its relative data rate.
4412 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004413 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004414 const struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004415 &crtc_state->wm.skl.optimal.planes[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08004416 u64 rate;
4417 u16 extra;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004418
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004419 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004420 continue;
4421
Damien Lespiaub9cec072014-11-04 17:06:43 +00004422 /*
Matt Roperd8e87492018-12-11 09:31:07 -08004423 * We've accounted for all active planes; remaining planes are
4424 * all disabled.
Damien Lespiaub9cec072014-11-04 17:06:43 +00004425 */
Matt Roperd8e87492018-12-11 09:31:07 -08004426 if (total_data_rate == 0)
4427 break;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004428
Matt Roperd8e87492018-12-11 09:31:07 -08004429 rate = plane_data_rate[plane_id];
4430 extra = min_t(u16, alloc_size,
4431 DIV64_U64_ROUND_UP(alloc_size * rate,
4432 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004433 total[plane_id] = wm->wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08004434 alloc_size -= extra;
4435 total_data_rate -= rate;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004436
Matt Roperd8e87492018-12-11 09:31:07 -08004437 if (total_data_rate == 0)
4438 break;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004439
Matt Roperd8e87492018-12-11 09:31:07 -08004440 rate = uv_plane_data_rate[plane_id];
4441 extra = min_t(u16, alloc_size,
4442 DIV64_U64_ROUND_UP(alloc_size * rate,
4443 total_data_rate));
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004444 uv_total[plane_id] = wm->uv_wm[level].min_ddb_alloc + extra;
Matt Roperd8e87492018-12-11 09:31:07 -08004445 alloc_size -= extra;
4446 total_data_rate -= rate;
4447 }
4448 WARN_ON(alloc_size != 0 || total_data_rate != 0);
4449
4450 /* Set the actual DDB start/end points for each plane */
4451 start = alloc->start;
4452 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004453 struct skl_ddb_entry *plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004454 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004455 struct skl_ddb_entry *uv_plane_alloc =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004456 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Matt Roperd8e87492018-12-11 09:31:07 -08004457
4458 if (plane_id == PLANE_CURSOR)
4459 continue;
4460
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004461 /* Gen11+ uses a separate plane for UV watermarks */
Matt Roperd8e87492018-12-11 09:31:07 -08004462 WARN_ON(INTEL_GEN(dev_priv) >= 11 && uv_total[plane_id]);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004463
Matt Roperd8e87492018-12-11 09:31:07 -08004464 /* Leave disabled planes at (0,0) */
4465 if (total[plane_id]) {
4466 plane_alloc->start = start;
4467 start += total[plane_id];
4468 plane_alloc->end = start;
Matt Roperc107acf2016-05-12 07:06:01 -07004469 }
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004470
Matt Roperd8e87492018-12-11 09:31:07 -08004471 if (uv_total[plane_id]) {
4472 uv_plane_alloc->start = start;
4473 start += uv_total[plane_id];
4474 uv_plane_alloc->end = start;
4475 }
4476 }
4477
4478 /*
4479 * When we calculated watermark values we didn't know how high
4480 * of a level we'd actually be able to hit, so we just marked
4481 * all levels as "enabled." Go back now and disable the ones
4482 * that aren't actually possible.
4483 */
4484 for (level++; level <= ilk_wm_max_level(dev_priv); level++) {
4485 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004486 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004487 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjäläa301cb02019-03-12 22:58:41 +02004488
4489 /*
4490 * We only disable the watermarks for each plane if
4491 * they exceed the ddb allocation of said plane. This
4492 * is done so that we don't end up touching cursor
4493 * watermarks needlessly when some other plane reduces
4494 * our max possible watermark level.
4495 *
4496 * Bspec has this to say about the PLANE_WM enable bit:
4497 * "All the watermarks at this level for all enabled
4498 * planes must be enabled before the level will be used."
4499 * So this is actually safe to do.
4500 */
4501 if (wm->wm[level].min_ddb_alloc > total[plane_id] ||
4502 wm->uv_wm[level].min_ddb_alloc > uv_total[plane_id])
4503 memset(&wm->wm[level], 0, sizeof(wm->wm[level]));
Ville Syrjälä290248c2019-02-13 18:54:24 +02004504
Ville Syrjäläc384afe2019-02-28 19:36:39 +02004505 /*
Bob Paauwe39564ae2019-04-12 11:09:20 -07004506 * Wa_1408961008:icl, ehl
Ville Syrjäläc384afe2019-02-28 19:36:39 +02004507 * Underruns with WM1+ disabled
4508 */
Bob Paauwe39564ae2019-04-12 11:09:20 -07004509 if (IS_GEN(dev_priv, 11) &&
Ville Syrjälä290248c2019-02-13 18:54:24 +02004510 level == 1 && wm->wm[0].plane_en) {
4511 wm->wm[level].plane_res_b = wm->wm[0].plane_res_b;
Ville Syrjäläc384afe2019-02-28 19:36:39 +02004512 wm->wm[level].plane_res_l = wm->wm[0].plane_res_l;
4513 wm->wm[level].ignore_lines = wm->wm[0].ignore_lines;
Ville Syrjälä290248c2019-02-13 18:54:24 +02004514 }
Matt Roperd8e87492018-12-11 09:31:07 -08004515 }
4516 }
4517
4518 /*
4519 * Go back and disable the transition watermark if it turns out we
4520 * don't have enough DDB blocks for it.
4521 */
4522 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004523 struct skl_plane_wm *wm =
Maarten Lankhorstec193642019-06-28 10:55:17 +02004524 &crtc_state->wm.skl.optimal.planes[plane_id];
Ville Syrjälä5e6037c2019-03-12 22:58:42 +02004525
Ville Syrjäläb19c9bc2018-12-21 19:14:31 +02004526 if (wm->trans_wm.plane_res_b >= total[plane_id])
Matt Roperd8e87492018-12-11 09:31:07 -08004527 memset(&wm->trans_wm, 0, sizeof(wm->trans_wm));
Damien Lespiaub9cec072014-11-04 17:06:43 +00004528 }
4529
Matt Roperc107acf2016-05-12 07:06:01 -07004530 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004531}
4532
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004533/*
4534 * 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 +02004535 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004536 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
4537 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
4538*/
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07004539static uint_fixed_16_16_t
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004540skl_wm_method1(const struct drm_i915_private *dev_priv, u32 pixel_rate,
4541 u8 cpp, u32 latency, u32 dbuf_block_size)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004542{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004543 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304544 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004545
4546 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304547 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004548
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304549 wm_intermediate_val = latency * pixel_rate * cpp;
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004550 ret = div_fixed16(wm_intermediate_val, 1000 * dbuf_block_size);
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07004551
4552 if (INTEL_GEN(dev_priv) >= 10)
4553 ret = add_fixed16_u32(ret, 1);
4554
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004555 return ret;
4556}
4557
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004558static uint_fixed_16_16_t
4559skl_wm_method2(u32 pixel_rate, u32 pipe_htotal, u32 latency,
4560 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004561{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004562 u32 wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304563 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004564
4565 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304566 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004567
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004568 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304569 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
4570 pipe_htotal * 1000);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304571 ret = mul_u32_fixed16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004572 return ret;
4573}
4574
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304575static uint_fixed_16_16_t
Maarten Lankhorstec193642019-06-28 10:55:17 +02004576intel_get_linetime_us(const struct intel_crtc_state *crtc_state)
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304577{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004578 u32 pixel_rate;
4579 u32 crtc_htotal;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304580 uint_fixed_16_16_t linetime_us;
4581
Maarten Lankhorstec193642019-06-28 10:55:17 +02004582 if (!crtc_state->base.active)
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304583 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304584
Maarten Lankhorstec193642019-06-28 10:55:17 +02004585 pixel_rate = crtc_state->pixel_rate;
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304586
4587 if (WARN_ON(pixel_rate == 0))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304588 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304589
Maarten Lankhorstec193642019-06-28 10:55:17 +02004590 crtc_htotal = crtc_state->base.adjusted_mode.crtc_htotal;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304591 linetime_us = div_fixed16(crtc_htotal * 1000, pixel_rate);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304592
4593 return linetime_us;
4594}
4595
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004596static u32
Maarten Lankhorstec193642019-06-28 10:55:17 +02004597skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *crtc_state,
4598 const struct intel_plane_state *plane_state)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004599{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004600 u64 adjusted_pixel_rate;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304601 uint_fixed_16_16_t downscale_amount;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004602
4603 /* Shouldn't reach here on disabled planes... */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004604 if (WARN_ON(!intel_wm_plane_visible(crtc_state, plane_state)))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004605 return 0;
4606
4607 /*
4608 * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
4609 * with additional adjustments for plane-specific scaling.
4610 */
Maarten Lankhorstec193642019-06-28 10:55:17 +02004611 adjusted_pixel_rate = crtc_state->pixel_rate;
4612 downscale_amount = skl_plane_downscale_amount(crtc_state, plane_state);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004613
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304614 return mul_round_up_u32_fixed16(adjusted_pixel_rate,
4615 downscale_amount);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004616}
4617
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304618static int
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004619skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
4620 int width, const struct drm_format_info *format,
4621 u64 modifier, unsigned int rotation,
4622 u32 plane_pixel_rate, struct skl_wm_params *wp,
4623 int color_plane)
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304624{
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004625 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
4626 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004627 u32 interm_pbpl;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304628
Juha-Pekka Heikkiladf7d4152019-03-04 17:26:31 +05304629 /* only planar format has two planes */
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004630 if (color_plane == 1 && !drm_format_info_is_yuv_semiplanar(format)) {
Juha-Pekka Heikkiladf7d4152019-03-04 17:26:31 +05304631 DRM_DEBUG_KMS("Non planar format have single plane\n");
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05304632 return -EINVAL;
4633 }
4634
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004635 wp->y_tiled = modifier == I915_FORMAT_MOD_Y_TILED ||
4636 modifier == I915_FORMAT_MOD_Yf_TILED ||
4637 modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
4638 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
4639 wp->x_tiled = modifier == I915_FORMAT_MOD_X_TILED;
4640 wp->rc_surface = modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
4641 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
Ville Syrjäläd1d23d72019-09-13 22:31:54 +03004642 wp->is_planar = drm_format_info_is_yuv_semiplanar(format);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304643
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004644 wp->width = width;
Ville Syrjälä45bee432018-11-14 23:07:28 +02004645 if (color_plane == 1 && wp->is_planar)
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05304646 wp->width /= 2;
4647
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004648 wp->cpp = format->cpp[color_plane];
4649 wp->plane_pixel_rate = plane_pixel_rate;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304650
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004651 if (INTEL_GEN(dev_priv) >= 11 &&
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004652 modifier == I915_FORMAT_MOD_Yf_TILED && wp->cpp == 1)
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004653 wp->dbuf_block_size = 256;
4654 else
4655 wp->dbuf_block_size = 512;
4656
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004657 if (drm_rotation_90_or_270(rotation)) {
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304658 switch (wp->cpp) {
4659 case 1:
4660 wp->y_min_scanlines = 16;
4661 break;
4662 case 2:
4663 wp->y_min_scanlines = 8;
4664 break;
4665 case 4:
4666 wp->y_min_scanlines = 4;
4667 break;
4668 default:
4669 MISSING_CASE(wp->cpp);
4670 return -EINVAL;
4671 }
4672 } else {
4673 wp->y_min_scanlines = 4;
4674 }
4675
Ville Syrjälä60e983f2018-12-21 19:14:33 +02004676 if (skl_needs_memory_bw_wa(dev_priv))
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304677 wp->y_min_scanlines *= 2;
4678
4679 wp->plane_bytes_per_line = wp->width * wp->cpp;
4680 if (wp->y_tiled) {
4681 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line *
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004682 wp->y_min_scanlines,
4683 wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304684
4685 if (INTEL_GEN(dev_priv) >= 10)
4686 interm_pbpl++;
4687
4688 wp->plane_blocks_per_line = div_fixed16(interm_pbpl,
4689 wp->y_min_scanlines);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004690 } else if (wp->x_tiled && IS_GEN(dev_priv, 9)) {
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004691 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
4692 wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304693 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
4694 } else {
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004695 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
4696 wp->dbuf_block_size) + 1;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304697 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
4698 }
4699
4700 wp->y_tile_minimum = mul_u32_fixed16(wp->y_min_scanlines,
4701 wp->plane_blocks_per_line);
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004702
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304703 wp->linetime_us = fixed16_to_u32_round_up(
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004704 intel_get_linetime_us(crtc_state));
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304705
4706 return 0;
4707}
4708
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004709static int
4710skl_compute_plane_wm_params(const struct intel_crtc_state *crtc_state,
4711 const struct intel_plane_state *plane_state,
4712 struct skl_wm_params *wp, int color_plane)
4713{
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004714 const struct drm_framebuffer *fb = plane_state->base.fb;
4715 int width;
4716
Maarten Lankhorst3a612762019-10-04 13:34:54 +02004717 /*
4718 * Src coordinates are already rotated by 270 degrees for
4719 * the 90/270 degree plane rotation cases (to match the
4720 * GTT mapping), hence no need to account for rotation here.
4721 */
4722 width = drm_rect_width(&plane_state->base.src) >> 16;
Ville Syrjäläc92558a2019-03-12 22:58:38 +02004723
4724 return skl_compute_wm_params(crtc_state, width,
4725 fb->format, fb->modifier,
4726 plane_state->base.rotation,
4727 skl_adjusted_plane_pixel_rate(crtc_state, plane_state),
4728 wp, color_plane);
4729}
4730
Ville Syrjäläb52c2732018-12-21 19:14:28 +02004731static bool skl_wm_has_lines(struct drm_i915_private *dev_priv, int level)
4732{
4733 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
4734 return true;
4735
4736 /* The number of lines are ignored for the level 0 watermark. */
4737 return level > 0;
4738}
4739
Maarten Lankhorstec193642019-06-28 10:55:17 +02004740static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
Matt Roperd8e87492018-12-11 09:31:07 -08004741 int level,
4742 const struct skl_wm_params *wp,
4743 const struct skl_wm_level *result_prev,
4744 struct skl_wm_level *result /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004745{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004746 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004747 u32 latency = dev_priv->wm.skl_latency[level];
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304748 uint_fixed_16_16_t method1, method2;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304749 uint_fixed_16_16_t selected_result;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004750 u32 res_blocks, res_lines, min_ddb_alloc = 0;
Ville Syrjäläce110ec2018-11-14 23:07:21 +02004751
Ville Syrjälä0aded172019-02-05 17:50:53 +02004752 if (latency == 0) {
4753 /* reject it */
4754 result->min_ddb_alloc = U16_MAX;
Ville Syrjälä692927f2018-12-21 19:14:29 +02004755 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02004756 }
Ville Syrjälä692927f2018-12-21 19:14:29 +02004757
Ville Syrjälä25312ef2019-05-03 20:38:05 +03004758 /*
4759 * WaIncreaseLatencyIPCEnabled: kbl,cfl
4760 * Display WA #1141: kbl,cfl
4761 */
Ville Syrjälä5a7d2022019-05-03 20:38:06 +03004762 if ((IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv)) ||
Rodrigo Vivi82525c12017-06-08 08:50:00 -07004763 dev_priv->ipc_enabled)
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05304764 latency += 4;
4765
Ville Syrjälä60e983f2018-12-21 19:14:33 +02004766 if (skl_needs_memory_bw_wa(dev_priv) && wp->x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03004767 latency += 15;
4768
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304769 method1 = skl_wm_method1(dev_priv, wp->plane_pixel_rate,
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004770 wp->cpp, latency, wp->dbuf_block_size);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304771 method2 = skl_wm_method2(wp->plane_pixel_rate,
Maarten Lankhorstec193642019-06-28 10:55:17 +02004772 crtc_state->base.adjusted_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004773 latency,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304774 wp->plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004775
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304776 if (wp->y_tiled) {
4777 selected_result = max_fixed16(method2, wp->y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004778 } else {
Maarten Lankhorstec193642019-06-28 10:55:17 +02004779 if ((wp->cpp * crtc_state->base.adjusted_mode.crtc_htotal /
Mahesh Kumardf8ee192018-01-30 11:49:11 -02004780 wp->dbuf_block_size < 1) &&
Paulo Zanoni077b5822018-10-04 16:15:57 -07004781 (wp->plane_bytes_per_line / wp->dbuf_block_size < 1)) {
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03004782 selected_result = method2;
Paulo Zanoni077b5822018-10-04 16:15:57 -07004783 } else if (latency >= wp->linetime_us) {
Lucas De Marchicf819ef2018-12-12 10:10:43 -08004784 if (IS_GEN(dev_priv, 9) &&
Paulo Zanoni077b5822018-10-04 16:15:57 -07004785 !IS_GEMINILAKE(dev_priv))
4786 selected_result = min_fixed16(method1, method2);
4787 else
4788 selected_result = method2;
4789 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004790 selected_result = method1;
Paulo Zanoni077b5822018-10-04 16:15:57 -07004791 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004792 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004793
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304794 res_blocks = fixed16_to_u32_round_up(selected_result) + 1;
Kumar, Maheshd273ecc2017-05-17 17:28:22 +05304795 res_lines = div_round_up_fixed16(selected_result,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304796 wp->plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00004797
Paulo Zanonia5b79d32018-11-13 17:24:32 -08004798 if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv)) {
4799 /* Display WA #1125: skl,bxt,kbl */
4800 if (level == 0 && wp->rc_surface)
4801 res_blocks +=
4802 fixed16_to_u32_round_up(wp->y_tile_minimum);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07004803
Paulo Zanonia5b79d32018-11-13 17:24:32 -08004804 /* Display WA #1126: skl,bxt,kbl */
4805 if (level >= 1 && level <= 7) {
4806 if (wp->y_tiled) {
4807 res_blocks +=
4808 fixed16_to_u32_round_up(wp->y_tile_minimum);
4809 res_lines += wp->y_min_scanlines;
4810 } else {
4811 res_blocks++;
4812 }
4813
4814 /*
4815 * Make sure result blocks for higher latency levels are
4816 * atleast as high as level below the current level.
4817 * Assumption in DDB algorithm optimization for special
4818 * cases. Also covers Display WA #1125 for RC.
4819 */
4820 if (result_prev->plane_res_b > res_blocks)
4821 res_blocks = result_prev->plane_res_b;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004822 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004823 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004824
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004825 if (INTEL_GEN(dev_priv) >= 11) {
4826 if (wp->y_tiled) {
4827 int extra_lines;
4828
4829 if (res_lines % wp->y_min_scanlines == 0)
4830 extra_lines = wp->y_min_scanlines;
4831 else
4832 extra_lines = wp->y_min_scanlines * 2 -
4833 res_lines % wp->y_min_scanlines;
4834
4835 min_ddb_alloc = mul_round_up_u32_fixed16(res_lines + extra_lines,
4836 wp->plane_blocks_per_line);
4837 } else {
4838 min_ddb_alloc = res_blocks +
4839 DIV_ROUND_UP(res_blocks, 10);
4840 }
4841 }
4842
Ville Syrjäläb52c2732018-12-21 19:14:28 +02004843 if (!skl_wm_has_lines(dev_priv, level))
4844 res_lines = 0;
4845
Ville Syrjälä0aded172019-02-05 17:50:53 +02004846 if (res_lines > 31) {
4847 /* reject it */
4848 result->min_ddb_alloc = U16_MAX;
Matt Roperd8e87492018-12-11 09:31:07 -08004849 return;
Ville Syrjälä0aded172019-02-05 17:50:53 +02004850 }
Matt Roperd8e87492018-12-11 09:31:07 -08004851
4852 /*
4853 * If res_lines is valid, assume we can use this watermark level
4854 * for now. We'll come back and disable it after we calculate the
4855 * DDB allocation if it turns out we don't actually have enough
4856 * blocks to satisfy it.
4857 */
Mahesh Kumar62027b72018-04-09 09:11:05 +05304858 result->plane_res_b = res_blocks;
4859 result->plane_res_l = res_lines;
Ville Syrjälä961d95e2018-12-21 19:14:32 +02004860 /* Bspec says: value >= plane ddb allocation -> invalid, hence the +1 here */
4861 result->min_ddb_alloc = max(min_ddb_alloc, res_blocks) + 1;
Mahesh Kumar62027b72018-04-09 09:11:05 +05304862 result->plane_en = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004863}
4864
Matt Roperd8e87492018-12-11 09:31:07 -08004865static void
Maarten Lankhorstec193642019-06-28 10:55:17 +02004866skl_compute_wm_levels(const struct intel_crtc_state *crtc_state,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304867 const struct skl_wm_params *wm_params,
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004868 struct skl_wm_level *levels)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004869{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004870 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304871 int level, max_level = ilk_wm_max_level(dev_priv);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004872 struct skl_wm_level *result_prev = &levels[0];
Lyudea62163e2016-10-04 14:28:20 -04004873
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304874 for (level = 0; level <= max_level; level++) {
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004875 struct skl_wm_level *result = &levels[level];
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304876
Maarten Lankhorstec193642019-06-28 10:55:17 +02004877 skl_compute_plane_wm(crtc_state, level, wm_params,
Matt Roperd8e87492018-12-11 09:31:07 -08004878 result_prev, result);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004879
4880 result_prev = result;
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304881 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004882}
4883
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004884static u32
Maarten Lankhorstec193642019-06-28 10:55:17 +02004885skl_compute_linetime_wm(const struct intel_crtc_state *crtc_state)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004886{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004887 struct drm_atomic_state *state = crtc_state->base.state;
Mahesh Kumara3a89862016-12-01 21:19:34 +05304888 struct drm_i915_private *dev_priv = to_i915(state->dev);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304889 uint_fixed_16_16_t linetime_us;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004890 u32 linetime_wm;
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03004891
Maarten Lankhorstec193642019-06-28 10:55:17 +02004892 linetime_us = intel_get_linetime_us(crtc_state);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304893 linetime_wm = fixed16_to_u32_round_up(mul_u32_fixed16(8, linetime_us));
Mahesh Kumara3a89862016-12-01 21:19:34 +05304894
Ville Syrjälä717671c2018-12-21 19:14:36 +02004895 /* Display WA #1135: BXT:ALL GLK:ALL */
4896 if (IS_GEN9_LP(dev_priv) && dev_priv->ipc_enabled)
Kumar, Mahesh446e8502017-08-17 19:15:25 +05304897 linetime_wm /= 2;
Mahesh Kumara3a89862016-12-01 21:19:34 +05304898
4899 return linetime_wm;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004900}
4901
Maarten Lankhorstec193642019-06-28 10:55:17 +02004902static void skl_compute_transition_wm(const struct intel_crtc_state *crtc_state,
Ville Syrjälä6a3c910b2018-11-14 23:07:23 +02004903 const struct skl_wm_params *wp,
Matt Roperd8e87492018-12-11 09:31:07 -08004904 struct skl_plane_wm *wm)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004905{
Maarten Lankhorstec193642019-06-28 10:55:17 +02004906 struct drm_device *dev = crtc_state->base.crtc->dev;
Kumar, Maheshca476672017-08-17 19:15:24 +05304907 const struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004908 u16 trans_min, trans_y_tile_min;
4909 const u16 trans_amount = 10; /* This is configurable amount */
4910 u16 wm0_sel_res_b, trans_offset_b, res_blocks;
Damien Lespiau9414f562014-11-04 17:06:58 +00004911
Kumar, Maheshca476672017-08-17 19:15:24 +05304912 /* Transition WM are not recommended by HW team for GEN9 */
4913 if (INTEL_GEN(dev_priv) <= 9)
Ville Syrjälä14a43062018-11-14 23:07:22 +02004914 return;
Kumar, Maheshca476672017-08-17 19:15:24 +05304915
4916 /* Transition WM don't make any sense if ipc is disabled */
4917 if (!dev_priv->ipc_enabled)
Ville Syrjälä14a43062018-11-14 23:07:22 +02004918 return;
Kumar, Maheshca476672017-08-17 19:15:24 +05304919
Paulo Zanoni91961a82018-10-04 16:15:56 -07004920 trans_min = 14;
4921 if (INTEL_GEN(dev_priv) >= 11)
Kumar, Maheshca476672017-08-17 19:15:24 +05304922 trans_min = 4;
4923
4924 trans_offset_b = trans_min + trans_amount;
4925
Paulo Zanonicbacc792018-10-04 16:15:58 -07004926 /*
4927 * The spec asks for Selected Result Blocks for wm0 (the real value),
4928 * not Result Blocks (the integer value). Pay attention to the capital
4929 * letters. The value wm_l0->plane_res_b is actually Result Blocks, but
4930 * since Result Blocks is the ceiling of Selected Result Blocks plus 1,
4931 * and since we later will have to get the ceiling of the sum in the
4932 * transition watermarks calculation, we can just pretend Selected
4933 * Result Blocks is Result Blocks minus 1 and it should work for the
4934 * current platforms.
4935 */
Ville Syrjälä6a3c910b2018-11-14 23:07:23 +02004936 wm0_sel_res_b = wm->wm[0].plane_res_b - 1;
Paulo Zanonicbacc792018-10-04 16:15:58 -07004937
Kumar, Maheshca476672017-08-17 19:15:24 +05304938 if (wp->y_tiled) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02004939 trans_y_tile_min =
4940 (u16)mul_round_up_u32_fixed16(2, wp->y_tile_minimum);
Paulo Zanonicbacc792018-10-04 16:15:58 -07004941 res_blocks = max(wm0_sel_res_b, trans_y_tile_min) +
Kumar, Maheshca476672017-08-17 19:15:24 +05304942 trans_offset_b;
4943 } else {
Paulo Zanonicbacc792018-10-04 16:15:58 -07004944 res_blocks = wm0_sel_res_b + trans_offset_b;
Kumar, Maheshca476672017-08-17 19:15:24 +05304945
4946 /* WA BUG:1938466 add one block for non y-tile planes */
4947 if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_A0))
4948 res_blocks += 1;
4949
4950 }
4951
Matt Roperd8e87492018-12-11 09:31:07 -08004952 /*
4953 * Just assume we can enable the transition watermark. After
4954 * computing the DDB we'll come back and disable it if that
4955 * assumption turns out to be false.
4956 */
4957 wm->trans_wm.plane_res_b = res_blocks + 1;
4958 wm->trans_wm.plane_en = true;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004959}
4960
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004961static int skl_build_plane_wm_single(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004962 const struct intel_plane_state *plane_state,
4963 enum plane_id plane_id, int color_plane)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004964{
Ville Syrjälä83158472018-11-27 18:57:26 +02004965 struct skl_plane_wm *wm = &crtc_state->wm.skl.optimal.planes[plane_id];
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004966 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004967 int ret;
4968
Ville Syrjälä51de9c62018-11-14 23:07:25 +02004969 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004970 &wm_params, color_plane);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004971 if (ret)
4972 return ret;
4973
Ville Syrjälä67155a62019-03-12 22:58:37 +02004974 skl_compute_wm_levels(crtc_state, &wm_params, wm->wm);
Matt Roperd8e87492018-12-11 09:31:07 -08004975 skl_compute_transition_wm(crtc_state, &wm_params, wm);
Ville Syrjälä83158472018-11-27 18:57:26 +02004976
4977 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004978}
4979
Ville Syrjäläff43bc32018-11-27 18:59:00 +02004980static int skl_build_plane_wm_uv(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004981 const struct intel_plane_state *plane_state,
4982 enum plane_id plane_id)
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004983{
Ville Syrjälä83158472018-11-27 18:57:26 +02004984 struct skl_plane_wm *wm = &crtc_state->wm.skl.optimal.planes[plane_id];
4985 struct skl_wm_params wm_params;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004986 int ret;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004987
Ville Syrjälä83158472018-11-27 18:57:26 +02004988 wm->is_planar = true;
4989
4990 /* uv plane watermarks must also be validated for NV12/Planar */
Ville Syrjälä51de9c62018-11-14 23:07:25 +02004991 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02004992 &wm_params, 1);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02004993 if (ret)
4994 return ret;
4995
Ville Syrjälä67155a62019-03-12 22:58:37 +02004996 skl_compute_wm_levels(crtc_state, &wm_params, wm->uv_wm);
Ville Syrjälä83158472018-11-27 18:57:26 +02004997
4998 return 0;
4999}
5000
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005001static int skl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005002 const struct intel_plane_state *plane_state)
5003{
5004 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
5005 const struct drm_framebuffer *fb = plane_state->base.fb;
5006 enum plane_id plane_id = plane->id;
5007 int ret;
5008
5009 if (!intel_wm_plane_visible(crtc_state, plane_state))
5010 return 0;
5011
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005012 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005013 plane_id, 0);
5014 if (ret)
5015 return ret;
5016
5017 if (fb->format->is_yuv && fb->format->num_planes > 1) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005018 ret = skl_build_plane_wm_uv(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005019 plane_id);
5020 if (ret)
5021 return ret;
5022 }
5023
5024 return 0;
5025}
5026
Ville Syrjälä96cb7cd2019-03-12 22:58:43 +02005027static int icl_build_plane_wm(struct intel_crtc_state *crtc_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005028 const struct intel_plane_state *plane_state)
5029{
5030 enum plane_id plane_id = to_intel_plane(plane_state->base.plane)->id;
5031 int ret;
5032
5033 /* Watermarks calculated in master */
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005034 if (plane_state->planar_slave)
Ville Syrjälä83158472018-11-27 18:57:26 +02005035 return 0;
5036
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005037 if (plane_state->planar_linked_plane) {
Ville Syrjälä83158472018-11-27 18:57:26 +02005038 const struct drm_framebuffer *fb = plane_state->base.fb;
Maarten Lankhorstc47b7dd2019-09-20 13:42:20 +02005039 enum plane_id y_plane_id = plane_state->planar_linked_plane->id;
Ville Syrjälä83158472018-11-27 18:57:26 +02005040
5041 WARN_ON(!intel_wm_plane_visible(crtc_state, plane_state));
5042 WARN_ON(!fb->format->is_yuv ||
5043 fb->format->num_planes == 1);
5044
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005045 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005046 y_plane_id, 0);
5047 if (ret)
5048 return ret;
5049
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005050 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005051 plane_id, 1);
5052 if (ret)
5053 return ret;
5054 } else if (intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005055 ret = skl_build_plane_wm_single(crtc_state, plane_state,
Ville Syrjälä83158472018-11-27 18:57:26 +02005056 plane_id, 0);
5057 if (ret)
5058 return ret;
5059 }
5060
5061 return 0;
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005062}
5063
Maarten Lankhorstec193642019-06-28 10:55:17 +02005064static int skl_build_pipe_wm(struct intel_crtc_state *crtc_state)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005065{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005066 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
5067 struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02005068 struct intel_plane *plane;
5069 const struct intel_plane_state *plane_state;
Matt Roper55994c22016-05-12 07:06:08 -07005070 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005071
Lyudea62163e2016-10-04 14:28:20 -04005072 /*
5073 * We'll only calculate watermarks for planes that are actually
5074 * enabled, so make sure all other planes are set as disabled.
5075 */
5076 memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
5077
Maarten Lankhorstaf9fbfa2019-10-04 13:34:53 +02005078 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state,
5079 crtc_state) {
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05305080
Ville Syrjälä83158472018-11-27 18:57:26 +02005081 if (INTEL_GEN(dev_priv) >= 11)
Maarten Lankhorstec193642019-06-28 10:55:17 +02005082 ret = icl_build_plane_wm(crtc_state, plane_state);
Maarten Lankhorstb048a002018-10-18 13:51:30 +02005083 else
Maarten Lankhorstec193642019-06-28 10:55:17 +02005084 ret = skl_build_plane_wm(crtc_state, plane_state);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05305085 if (ret)
5086 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005087 }
Mahesh Kumar942aa2d2018-04-09 09:11:04 +05305088
Maarten Lankhorstec193642019-06-28 10:55:17 +02005089 pipe_wm->linetime = skl_compute_linetime_wm(crtc_state);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005090
Matt Roper55994c22016-05-12 07:06:08 -07005091 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005092}
5093
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005094static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
5095 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00005096 const struct skl_ddb_entry *entry)
5097{
5098 if (entry->end)
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005099 I915_WRITE_FW(reg, (entry->end - 1) << 16 | entry->start);
Damien Lespiau16160e32014-11-04 17:06:53 +00005100 else
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005101 I915_WRITE_FW(reg, 0);
Damien Lespiau16160e32014-11-04 17:06:53 +00005102}
5103
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005104static void skl_write_wm_level(struct drm_i915_private *dev_priv,
5105 i915_reg_t reg,
5106 const struct skl_wm_level *level)
5107{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005108 u32 val = 0;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005109
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005110 if (level->plane_en)
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005111 val |= PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005112 if (level->ignore_lines)
5113 val |= PLANE_WM_IGNORE_LINES;
5114 val |= level->plane_res_b;
5115 val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005116
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005117 I915_WRITE_FW(reg, val);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005118}
5119
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005120void skl_write_plane_wm(struct intel_plane *plane,
5121 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005122{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005123 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005124 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005125 enum plane_id plane_id = plane->id;
5126 enum pipe pipe = plane->pipe;
5127 const struct skl_plane_wm *wm =
5128 &crtc_state->wm.skl.optimal.planes[plane_id];
5129 const struct skl_ddb_entry *ddb_y =
5130 &crtc_state->wm.skl.plane_ddb_y[plane_id];
5131 const struct skl_ddb_entry *ddb_uv =
5132 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005133
5134 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005135 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005136 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04005137 }
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005138 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005139 &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02005140
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005141 if (INTEL_GEN(dev_priv) >= 11) {
Mahesh Kumar234059d2018-01-30 11:49:13 -02005142 skl_ddb_entry_write(dev_priv,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005143 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5144 return;
Mahesh Kumarb879d582018-04-09 09:11:01 +05305145 }
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005146
5147 if (wm->is_planar)
5148 swap(ddb_y, ddb_uv);
5149
5150 skl_ddb_entry_write(dev_priv,
5151 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5152 skl_ddb_entry_write(dev_priv,
5153 PLANE_NV12_BUF_CFG(pipe, plane_id), ddb_uv);
Lyude62e0fb82016-08-22 12:50:08 -04005154}
5155
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005156void skl_write_cursor_wm(struct intel_plane *plane,
5157 const struct intel_crtc_state *crtc_state)
Lyude62e0fb82016-08-22 12:50:08 -04005158{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005159 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005160 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005161 enum plane_id plane_id = plane->id;
5162 enum pipe pipe = plane->pipe;
5163 const struct skl_plane_wm *wm =
5164 &crtc_state->wm.skl.optimal.planes[plane_id];
5165 const struct skl_ddb_entry *ddb =
5166 &crtc_state->wm.skl.plane_ddb_y[plane_id];
Lyude62e0fb82016-08-22 12:50:08 -04005167
5168 for (level = 0; level <= max_level; level++) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005169 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
5170 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04005171 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005172 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02005173
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005174 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe), ddb);
Lyude62e0fb82016-08-22 12:50:08 -04005175}
5176
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005177bool skl_wm_level_equals(const struct skl_wm_level *l1,
5178 const struct skl_wm_level *l2)
5179{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005180 return l1->plane_en == l2->plane_en &&
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005181 l1->ignore_lines == l2->ignore_lines &&
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005182 l1->plane_res_l == l2->plane_res_l &&
5183 l1->plane_res_b == l2->plane_res_b;
5184}
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005185
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005186static bool skl_plane_wm_equals(struct drm_i915_private *dev_priv,
5187 const struct skl_plane_wm *wm1,
5188 const struct skl_plane_wm *wm2)
5189{
5190 int level, max_level = ilk_wm_max_level(dev_priv);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005191
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005192 for (level = 0; level <= max_level; level++) {
5193 if (!skl_wm_level_equals(&wm1->wm[level], &wm2->wm[level]) ||
5194 !skl_wm_level_equals(&wm1->uv_wm[level], &wm2->uv_wm[level]))
5195 return false;
5196 }
5197
5198 return skl_wm_level_equals(&wm1->trans_wm, &wm2->trans_wm);
cpaul@redhat.com45ece232016-10-14 17:31:56 -04005199}
5200
Ville Syrjälä961d95e2018-12-21 19:14:32 +02005201static bool skl_pipe_wm_equals(struct intel_crtc *crtc,
5202 const struct skl_pipe_wm *wm1,
5203 const struct skl_pipe_wm *wm2)
5204{
5205 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5206 enum plane_id plane_id;
5207
5208 for_each_plane_id_on_crtc(crtc, plane_id) {
5209 if (!skl_plane_wm_equals(dev_priv,
5210 &wm1->planes[plane_id],
5211 &wm2->planes[plane_id]))
5212 return false;
5213 }
5214
5215 return wm1->linetime == wm2->linetime;
5216}
5217
Lyude27082492016-08-24 07:48:10 +02005218static inline bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
5219 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005220{
Lyude27082492016-08-24 07:48:10 +02005221 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005222}
5223
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005224bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry *ddb,
Jani Nikula696173b2019-04-05 14:00:15 +03005225 const struct skl_ddb_entry *entries,
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005226 int num_entries, int ignore_idx)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005227{
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005228 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005229
Ville Syrjälä53cc68802018-11-01 17:05:59 +02005230 for (i = 0; i < num_entries; i++) {
5231 if (i != ignore_idx &&
5232 skl_ddb_entries_overlap(ddb, &entries[i]))
Lyude27082492016-08-24 07:48:10 +02005233 return true;
Mika Kahola2b685042017-10-10 13:17:03 +03005234 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005235
Lyude27082492016-08-24 07:48:10 +02005236 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00005237}
5238
Jani Nikulabb7791b2016-10-04 12:29:17 +03005239static int
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005240skl_ddb_add_affected_planes(const struct intel_crtc_state *old_crtc_state,
5241 struct intel_crtc_state *new_crtc_state)
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005242{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005243 struct intel_atomic_state *state = to_intel_atomic_state(new_crtc_state->base.state);
5244 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
5245 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5246 struct intel_plane *plane;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005247
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005248 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5249 struct intel_plane_state *plane_state;
5250 enum plane_id plane_id = plane->id;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005251
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005252 if (skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_y[plane_id],
5253 &new_crtc_state->wm.skl.plane_ddb_y[plane_id]) &&
5254 skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_uv[plane_id],
5255 &new_crtc_state->wm.skl.plane_ddb_uv[plane_id]))
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005256 continue;
5257
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005258 plane_state = intel_atomic_get_plane_state(state, plane);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005259 if (IS_ERR(plane_state))
5260 return PTR_ERR(plane_state);
Maarten Lankhorst1ab554b2018-10-22 15:51:52 +02005261
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005262 new_crtc_state->update_planes |= BIT(plane_id);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005263 }
5264
5265 return 0;
5266}
5267
5268static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005269skl_compute_ddb(struct intel_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07005270{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005271 const struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5272 struct skl_ddb_allocation *ddb = &state->wm_results.ddb;
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005273 struct intel_crtc_state *old_crtc_state;
5274 struct intel_crtc_state *new_crtc_state;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305275 struct intel_crtc *crtc;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305276 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07005277
Paulo Zanoni5a920b82016-10-04 14:37:32 -03005278 memcpy(ddb, &dev_priv->wm.skl_hw.ddb, sizeof(*ddb));
5279
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005280 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005281 new_crtc_state, i) {
5282 ret = skl_allocate_pipe_ddb(new_crtc_state, ddb);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005283 if (ret)
5284 return ret;
5285
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005286 ret = skl_ddb_add_affected_planes(old_crtc_state,
5287 new_crtc_state);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005288 if (ret)
5289 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07005290 }
5291
5292 return 0;
5293}
5294
Ville Syrjäläab98e942019-02-08 22:05:27 +02005295static char enast(bool enable)
5296{
5297 return enable ? '*' : ' ';
5298}
5299
Matt Roper2722efb2016-08-17 15:55:55 -04005300static void
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005301skl_print_wm_changes(struct intel_atomic_state *state)
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005302{
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005303 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5304 const struct intel_crtc_state *old_crtc_state;
5305 const struct intel_crtc_state *new_crtc_state;
5306 struct intel_plane *plane;
5307 struct intel_crtc *crtc;
Maarten Lankhorst75704982016-11-01 12:04:10 +01005308 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005309
Ville Syrjäläab98e942019-02-08 22:05:27 +02005310 if ((drm_debug & DRM_UT_KMS) == 0)
5311 return;
5312
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005313 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
5314 new_crtc_state, i) {
Ville Syrjäläab98e942019-02-08 22:05:27 +02005315 const struct skl_pipe_wm *old_pipe_wm, *new_pipe_wm;
5316
5317 old_pipe_wm = &old_crtc_state->wm.skl.optimal;
5318 new_pipe_wm = &new_crtc_state->wm.skl.optimal;
5319
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005320 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5321 enum plane_id plane_id = plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005322 const struct skl_ddb_entry *old, *new;
5323
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005324 old = &old_crtc_state->wm.skl.plane_ddb_y[plane_id];
5325 new = &new_crtc_state->wm.skl.plane_ddb_y[plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005326
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005327 if (skl_ddb_entry_equal(old, new))
5328 continue;
5329
Ville Syrjäläab98e942019-02-08 22:05:27 +02005330 DRM_DEBUG_KMS("[PLANE:%d:%s] ddb (%4d - %4d) -> (%4d - %4d), size %4d -> %4d\n",
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005331 plane->base.base.id, plane->base.name,
Ville Syrjäläab98e942019-02-08 22:05:27 +02005332 old->start, old->end, new->start, new->end,
5333 skl_ddb_entry_size(old), skl_ddb_entry_size(new));
5334 }
5335
5336 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5337 enum plane_id plane_id = plane->id;
5338 const struct skl_plane_wm *old_wm, *new_wm;
5339
5340 old_wm = &old_pipe_wm->planes[plane_id];
5341 new_wm = &new_pipe_wm->planes[plane_id];
5342
5343 if (skl_plane_wm_equals(dev_priv, old_wm, new_wm))
5344 continue;
5345
5346 DRM_DEBUG_KMS("[PLANE:%d:%s] level %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm"
5347 " -> %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm\n",
5348 plane->base.base.id, plane->base.name,
5349 enast(old_wm->wm[0].plane_en), enast(old_wm->wm[1].plane_en),
5350 enast(old_wm->wm[2].plane_en), enast(old_wm->wm[3].plane_en),
5351 enast(old_wm->wm[4].plane_en), enast(old_wm->wm[5].plane_en),
5352 enast(old_wm->wm[6].plane_en), enast(old_wm->wm[7].plane_en),
5353 enast(old_wm->trans_wm.plane_en),
5354 enast(new_wm->wm[0].plane_en), enast(new_wm->wm[1].plane_en),
5355 enast(new_wm->wm[2].plane_en), enast(new_wm->wm[3].plane_en),
5356 enast(new_wm->wm[4].plane_en), enast(new_wm->wm[5].plane_en),
5357 enast(new_wm->wm[6].plane_en), enast(new_wm->wm[7].plane_en),
5358 enast(new_wm->trans_wm.plane_en));
5359
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005360 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"
5361 " -> %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 +02005362 plane->base.base.id, plane->base.name,
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005363 enast(old_wm->wm[0].ignore_lines), old_wm->wm[0].plane_res_l,
5364 enast(old_wm->wm[1].ignore_lines), old_wm->wm[1].plane_res_l,
5365 enast(old_wm->wm[2].ignore_lines), old_wm->wm[2].plane_res_l,
5366 enast(old_wm->wm[3].ignore_lines), old_wm->wm[3].plane_res_l,
5367 enast(old_wm->wm[4].ignore_lines), old_wm->wm[4].plane_res_l,
5368 enast(old_wm->wm[5].ignore_lines), old_wm->wm[5].plane_res_l,
5369 enast(old_wm->wm[6].ignore_lines), old_wm->wm[6].plane_res_l,
5370 enast(old_wm->wm[7].ignore_lines), old_wm->wm[7].plane_res_l,
5371 enast(old_wm->trans_wm.ignore_lines), old_wm->trans_wm.plane_res_l,
5372
5373 enast(new_wm->wm[0].ignore_lines), new_wm->wm[0].plane_res_l,
5374 enast(new_wm->wm[1].ignore_lines), new_wm->wm[1].plane_res_l,
5375 enast(new_wm->wm[2].ignore_lines), new_wm->wm[2].plane_res_l,
5376 enast(new_wm->wm[3].ignore_lines), new_wm->wm[3].plane_res_l,
5377 enast(new_wm->wm[4].ignore_lines), new_wm->wm[4].plane_res_l,
5378 enast(new_wm->wm[5].ignore_lines), new_wm->wm[5].plane_res_l,
5379 enast(new_wm->wm[6].ignore_lines), new_wm->wm[6].plane_res_l,
5380 enast(new_wm->wm[7].ignore_lines), new_wm->wm[7].plane_res_l,
5381 enast(new_wm->trans_wm.ignore_lines), new_wm->trans_wm.plane_res_l);
Ville Syrjäläab98e942019-02-08 22:05:27 +02005382
5383 DRM_DEBUG_KMS("[PLANE:%d:%s] blocks %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d"
5384 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d\n",
5385 plane->base.base.id, plane->base.name,
5386 old_wm->wm[0].plane_res_b, old_wm->wm[1].plane_res_b,
5387 old_wm->wm[2].plane_res_b, old_wm->wm[3].plane_res_b,
5388 old_wm->wm[4].plane_res_b, old_wm->wm[5].plane_res_b,
5389 old_wm->wm[6].plane_res_b, old_wm->wm[7].plane_res_b,
5390 old_wm->trans_wm.plane_res_b,
5391 new_wm->wm[0].plane_res_b, new_wm->wm[1].plane_res_b,
5392 new_wm->wm[2].plane_res_b, new_wm->wm[3].plane_res_b,
5393 new_wm->wm[4].plane_res_b, new_wm->wm[5].plane_res_b,
5394 new_wm->wm[6].plane_res_b, new_wm->wm[7].plane_res_b,
5395 new_wm->trans_wm.plane_res_b);
5396
5397 DRM_DEBUG_KMS("[PLANE:%d:%s] min_ddb %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d"
5398 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d\n",
5399 plane->base.base.id, plane->base.name,
5400 old_wm->wm[0].min_ddb_alloc, old_wm->wm[1].min_ddb_alloc,
5401 old_wm->wm[2].min_ddb_alloc, old_wm->wm[3].min_ddb_alloc,
5402 old_wm->wm[4].min_ddb_alloc, old_wm->wm[5].min_ddb_alloc,
5403 old_wm->wm[6].min_ddb_alloc, old_wm->wm[7].min_ddb_alloc,
5404 old_wm->trans_wm.min_ddb_alloc,
5405 new_wm->wm[0].min_ddb_alloc, new_wm->wm[1].min_ddb_alloc,
5406 new_wm->wm[2].min_ddb_alloc, new_wm->wm[3].min_ddb_alloc,
5407 new_wm->wm[4].min_ddb_alloc, new_wm->wm[5].min_ddb_alloc,
5408 new_wm->wm[6].min_ddb_alloc, new_wm->wm[7].min_ddb_alloc,
5409 new_wm->trans_wm.min_ddb_alloc);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005410 }
5411 }
5412}
5413
Ville Syrjälä49e0ed32019-10-11 23:09:43 +03005414static int intel_add_all_pipes(struct intel_atomic_state *state)
5415{
5416 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5417 struct intel_crtc *crtc;
5418
5419 for_each_intel_crtc(&dev_priv->drm, crtc) {
5420 struct intel_crtc_state *crtc_state;
5421
5422 crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
5423 if (IS_ERR(crtc_state))
5424 return PTR_ERR(crtc_state);
5425 }
5426
5427 return 0;
5428}
5429
Matt Roper98d39492016-05-12 07:06:03 -07005430static int
Ville Syrjäläd7a14582019-10-11 23:09:42 +03005431skl_ddb_add_affected_pipes(struct intel_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07005432{
Ville Syrjälä49e0ed32019-10-11 23:09:43 +03005433 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Ville Syrjäläd7a14582019-10-11 23:09:42 +03005434 int ret;
Matt Roper98d39492016-05-12 07:06:03 -07005435
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305436 /*
5437 * If this is our first atomic update following hardware readout,
5438 * we can't trust the DDB that the BIOS programmed for us. Let's
5439 * pretend that all pipes switched active status so that we'll
5440 * ensure a full DDB recompute.
5441 */
5442 if (dev_priv->wm.distrust_bios_wm) {
Ville Syrjälä49e0ed32019-10-11 23:09:43 +03005443 ret = drm_modeset_lock(&dev_priv->drm.mode_config.connection_mutex,
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005444 state->base.acquire_ctx);
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305445 if (ret)
5446 return ret;
5447
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005448 state->active_pipe_changes = ~0;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305449
5450 /*
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03005451 * We usually only initialize state->active_pipes if we
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305452 * we're doing a modeset; make sure this field is always
5453 * initialized during the sanitization process that happens
5454 * on the first commit too.
5455 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005456 if (!state->modeset)
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03005457 state->active_pipes = dev_priv->active_pipes;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305458 }
5459
5460 /*
5461 * If the modeset changes which CRTC's are active, we need to
5462 * recompute the DDB allocation for *all* active pipes, even
5463 * those that weren't otherwise being modified in any way by this
5464 * atomic commit. Due to the shrinking of the per-pipe allocations
5465 * when new active CRTC's are added, it's possible for a pipe that
5466 * we were already using and aren't changing at all here to suddenly
5467 * become invalid if its DDB needs exceeds its new allocation.
5468 *
5469 * Note that if we wind up doing a full DDB recompute, we can't let
5470 * any other display updates race with this transaction, so we need
5471 * to grab the lock on *all* CRTC's.
5472 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005473 if (state->active_pipe_changes || state->modeset) {
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005474 state->wm_results.dirty_pipes = ~0;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305475
Ville Syrjälä49e0ed32019-10-11 23:09:43 +03005476 ret = intel_add_all_pipes(state);
5477 if (ret)
5478 return ret;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305479 }
5480
5481 return 0;
5482}
5483
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005484/*
5485 * To make sure the cursor watermark registers are always consistent
5486 * with our computed state the following scenario needs special
5487 * treatment:
5488 *
5489 * 1. enable cursor
5490 * 2. move cursor entirely offscreen
5491 * 3. disable cursor
5492 *
5493 * Step 2. does call .disable_plane() but does not zero the watermarks
5494 * (since we consider an offscreen cursor still active for the purposes
5495 * of watermarks). Step 3. would not normally call .disable_plane()
5496 * because the actual plane visibility isn't changing, and we don't
5497 * deallocate the cursor ddb until the pipe gets disabled. So we must
5498 * force step 3. to call .disable_plane() to update the watermark
5499 * registers properly.
5500 *
5501 * Other planes do not suffer from this issues as their watermarks are
5502 * calculated based on the actual plane visibility. The only time this
5503 * can trigger for the other planes is during the initial readout as the
5504 * default value of the watermarks registers is not zero.
5505 */
5506static int skl_wm_add_affected_planes(struct intel_atomic_state *state,
5507 struct intel_crtc *crtc)
5508{
5509 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5510 const struct intel_crtc_state *old_crtc_state =
5511 intel_atomic_get_old_crtc_state(state, crtc);
5512 struct intel_crtc_state *new_crtc_state =
5513 intel_atomic_get_new_crtc_state(state, crtc);
5514 struct intel_plane *plane;
5515
5516 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5517 struct intel_plane_state *plane_state;
5518 enum plane_id plane_id = plane->id;
5519
5520 /*
5521 * Force a full wm update for every plane on modeset.
5522 * Required because the reset value of the wm registers
5523 * is non-zero, whereas we want all disabled planes to
5524 * have zero watermarks. So if we turn off the relevant
5525 * power well the hardware state will go out of sync
5526 * with the software state.
5527 */
5528 if (!drm_atomic_crtc_needs_modeset(&new_crtc_state->base) &&
5529 skl_plane_wm_equals(dev_priv,
5530 &old_crtc_state->wm.skl.optimal.planes[plane_id],
5531 &new_crtc_state->wm.skl.optimal.planes[plane_id]))
5532 continue;
5533
5534 plane_state = intel_atomic_get_plane_state(state, plane);
5535 if (IS_ERR(plane_state))
5536 return PTR_ERR(plane_state);
5537
5538 new_crtc_state->update_planes |= BIT(plane_id);
5539 }
5540
5541 return 0;
5542}
5543
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305544static int
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005545skl_compute_wm(struct intel_atomic_state *state)
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305546{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005547 struct intel_crtc *crtc;
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02005548 struct intel_crtc_state *new_crtc_state;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005549 struct intel_crtc_state *old_crtc_state;
5550 struct skl_ddb_values *results = &state->wm_results;
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305551 int ret, i;
5552
Matt Roper734fa012016-05-12 15:11:40 -07005553 /* Clear all dirty flags */
5554 results->dirty_pipes = 0;
5555
Ville Syrjäläd7a14582019-10-11 23:09:42 +03005556 ret = skl_ddb_add_affected_pipes(state);
5557 if (ret)
Mahesh Kumare1f96a62018-04-09 09:11:08 +05305558 return ret;
5559
Matt Roper734fa012016-05-12 15:11:40 -07005560 /*
5561 * Calculate WM's for all pipes that are part of this transaction.
Matt Roperd8e87492018-12-11 09:31:07 -08005562 * Note that skl_ddb_add_affected_pipes may have added more CRTC's that
Matt Roper734fa012016-05-12 15:11:40 -07005563 * weren't otherwise being modified (and set bits in dirty_pipes) if
5564 * pipe allocations had to change.
Matt Roper734fa012016-05-12 15:11:40 -07005565 */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005566 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02005567 new_crtc_state, i) {
5568 ret = skl_build_pipe_wm(new_crtc_state);
Ville Syrjäläff43bc32018-11-27 18:59:00 +02005569 if (ret)
5570 return ret;
5571
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005572 ret = skl_wm_add_affected_planes(state, crtc);
Matt Roper734fa012016-05-12 15:11:40 -07005573 if (ret)
5574 return ret;
5575
Ville Syrjälä8cac9fd2019-03-12 22:58:44 +02005576 if (!skl_pipe_wm_equals(crtc,
5577 &old_crtc_state->wm.skl.optimal,
5578 &new_crtc_state->wm.skl.optimal))
Ville Syrjälä36b53a22019-10-11 23:09:44 +03005579 results->dirty_pipes |= BIT(crtc->pipe);
Matt Roper734fa012016-05-12 15:11:40 -07005580 }
5581
Matt Roperd8e87492018-12-11 09:31:07 -08005582 ret = skl_compute_ddb(state);
5583 if (ret)
5584 return ret;
5585
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005586 skl_print_wm_changes(state);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005587
Matt Roper98d39492016-05-12 07:06:03 -07005588 return 0;
5589}
5590
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005591static void skl_atomic_update_crtc_wm(struct intel_atomic_state *state,
Maarten Lankhorstec193642019-06-28 10:55:17 +02005592 struct intel_crtc_state *crtc_state)
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005593{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005594 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005595 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
Maarten Lankhorstec193642019-06-28 10:55:17 +02005596 struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005597 enum pipe pipe = crtc->pipe;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005598
Ville Syrjälä36b53a22019-10-11 23:09:44 +03005599 if ((state->wm_results.dirty_pipes & BIT(crtc->pipe)) == 0)
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005600 return;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005601
5602 I915_WRITE(PIPE_WM_LINETIME(pipe), pipe_wm->linetime);
5603}
5604
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005605static void skl_initial_wm(struct intel_atomic_state *state,
Maarten Lankhorstec193642019-06-28 10:55:17 +02005606 struct intel_crtc_state *crtc_state)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005607{
Ville Syrjälä36b53a22019-10-11 23:09:44 +03005608 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
5609 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Mahesh Kumar60f8e872018-04-09 09:11:00 +05305610 struct skl_ddb_values *results = &state->wm_results;
Bob Paauweadda50b2015-07-21 10:42:53 -07005611
Ville Syrjälä36b53a22019-10-11 23:09:44 +03005612 if ((results->dirty_pipes & BIT(crtc->pipe)) == 0)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005613 return;
5614
Matt Roper734fa012016-05-12 15:11:40 -07005615 mutex_lock(&dev_priv->wm.wm_mutex);
5616
Maarten Lankhorstec193642019-06-28 10:55:17 +02005617 if (crtc_state->base.active_changed)
5618 skl_atomic_update_crtc_wm(state, crtc_state);
Lyude27082492016-08-24 07:48:10 +02005619
Matt Roper734fa012016-05-12 15:11:40 -07005620 mutex_unlock(&dev_priv->wm.wm_mutex);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005621}
5622
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005623static void ilk_compute_wm_config(struct drm_i915_private *dev_priv,
Ville Syrjäläd8905652016-01-14 14:53:35 +02005624 struct intel_wm_config *config)
5625{
5626 struct intel_crtc *crtc;
5627
5628 /* Compute the currently _active_ config */
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005629 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläd8905652016-01-14 14:53:35 +02005630 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
5631
5632 if (!wm->pipe_enabled)
5633 continue;
5634
5635 config->sprites_enabled |= wm->sprites_enabled;
5636 config->sprites_scaled |= wm->sprites_scaled;
5637 config->num_pipes_active++;
5638 }
5639}
5640
Matt Ropered4a6a72016-02-23 17:20:13 -08005641static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03005642{
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005643 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02005644 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02005645 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02005646 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005647 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07005648
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005649 ilk_compute_wm_config(dev_priv, &config);
Ville Syrjäläd8905652016-01-14 14:53:35 +02005650
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005651 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_1_2, &max);
5652 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03005653
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005654 /* 5/6 split only in single pipe config on IVB+ */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00005655 if (INTEL_GEN(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02005656 config.num_pipes_active == 1 && config.sprites_enabled) {
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005657 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_5_6, &max);
5658 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005659
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005660 best_lp_wm = ilk_find_best_result(dev_priv, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03005661 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005662 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005663 }
5664
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005665 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005666 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005667
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005668 ilk_compute_wm_results(dev_priv, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03005669
Imre Deak820c1982013-12-17 14:46:36 +02005670 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03005671}
5672
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005673static void ilk_initial_watermarks(struct intel_atomic_state *state,
Maarten Lankhorstec193642019-06-28 10:55:17 +02005674 struct intel_crtc_state *crtc_state)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005675{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005676 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä88016a92019-07-01 19:05:45 +03005677 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005678
Matt Ropered4a6a72016-02-23 17:20:13 -08005679 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03005680 crtc->wm.active.ilk = crtc_state->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08005681 ilk_program_watermarks(dev_priv);
5682 mutex_unlock(&dev_priv->wm.wm_mutex);
5683}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005684
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005685static void ilk_optimize_watermarks(struct intel_atomic_state *state,
Maarten Lankhorstec193642019-06-28 10:55:17 +02005686 struct intel_crtc_state *crtc_state)
Matt Ropered4a6a72016-02-23 17:20:13 -08005687{
Maarten Lankhorstec193642019-06-28 10:55:17 +02005688 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä88016a92019-07-01 19:05:45 +03005689 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
5690
5691 if (!crtc_state->wm.need_postvbl_update)
5692 return;
Matt Ropered4a6a72016-02-23 17:20:13 -08005693
5694 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä88016a92019-07-01 19:05:45 +03005695 crtc->wm.active.ilk = crtc_state->wm.ilk.optimal;
5696 ilk_program_watermarks(dev_priv);
Matt Ropered4a6a72016-02-23 17:20:13 -08005697 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005698}
5699
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005700static inline void skl_wm_level_from_reg_val(u32 val,
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005701 struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00005702{
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005703 level->plane_en = val & PLANE_WM_EN;
Ville Syrjälä2ed8e1f2019-02-13 18:54:23 +02005704 level->ignore_lines = val & PLANE_WM_IGNORE_LINES;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005705 level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
5706 level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
5707 PLANE_WM_LINES_MASK;
Pradeep Bhat30789992014-11-04 17:06:45 +00005708}
5709
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005710void skl_pipe_wm_get_hw_state(struct intel_crtc *crtc,
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005711 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00005712{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005713 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5714 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005715 int level, max_level;
5716 enum plane_id plane_id;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005717 u32 val;
Pradeep Bhat30789992014-11-04 17:06:45 +00005718
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005719 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00005720
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005721 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005722 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00005723
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005724 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005725 if (plane_id != PLANE_CURSOR)
5726 val = I915_READ(PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005727 else
5728 val = I915_READ(CUR_WM(pipe, level));
5729
5730 skl_wm_level_from_reg_val(val, &wm->wm[level]);
5731 }
5732
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005733 if (plane_id != PLANE_CURSOR)
5734 val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005735 else
5736 val = I915_READ(CUR_WM_TRANS(pipe));
5737
5738 skl_wm_level_from_reg_val(val, &wm->trans_wm);
5739 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005740
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005741 if (!crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00005742 return;
5743
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005744 out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00005745}
5746
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005747void skl_wm_get_hw_state(struct drm_i915_private *dev_priv)
Pradeep Bhat30789992014-11-04 17:06:45 +00005748{
Mahesh Kumar60f8e872018-04-09 09:11:00 +05305749 struct skl_ddb_values *hw = &dev_priv->wm.skl_hw;
Damien Lespiaua269c582014-11-04 17:06:49 +00005750 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005751 struct intel_crtc *crtc;
Maarten Lankhorstec193642019-06-28 10:55:17 +02005752 struct intel_crtc_state *crtc_state;
Pradeep Bhat30789992014-11-04 17:06:45 +00005753
Damien Lespiaua269c582014-11-04 17:06:49 +00005754 skl_ddb_get_hw_state(dev_priv, ddb);
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005755 for_each_intel_crtc(&dev_priv->drm, crtc) {
Maarten Lankhorstec193642019-06-28 10:55:17 +02005756 crtc_state = to_intel_crtc_state(crtc->base.state);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005757
Maarten Lankhorstec193642019-06-28 10:55:17 +02005758 skl_pipe_wm_get_hw_state(crtc, &crtc_state->wm.skl.optimal);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005759
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005760 if (crtc->active)
Ville Syrjälä36b53a22019-10-11 23:09:44 +03005761 hw->dirty_pipes |= BIT(crtc->pipe);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005762 }
Matt Ropera1de91e2016-05-12 07:05:57 -07005763
Ville Syrjäläd06a79d2019-08-21 20:30:29 +03005764 if (dev_priv->active_pipes) {
Matt Roper279e99d2016-05-12 07:06:02 -07005765 /* Fully recompute DDB on first atomic commit */
5766 dev_priv->wm.distrust_bios_wm = true;
Matt Roper279e99d2016-05-12 07:06:02 -07005767 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005768}
5769
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005770static void ilk_pipe_wm_get_hw_state(struct intel_crtc *crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005771{
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005772 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005773 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02005774 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Maarten Lankhorstec193642019-06-28 10:55:17 +02005775 struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
5776 struct intel_pipe_wm *active = &crtc_state->wm.ilk.optimal;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005777 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005778 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005779 [PIPE_A] = WM0_PIPEA_ILK,
5780 [PIPE_B] = WM0_PIPEB_ILK,
5781 [PIPE_C] = WM0_PIPEC_IVB,
5782 };
5783
5784 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005785 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02005786 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005787
Ville Syrjälä15606532016-05-13 17:55:17 +03005788 memset(active, 0, sizeof(*active));
5789
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005790 active->pipe_enabled = crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02005791
5792 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005793 u32 tmp = hw->wm_pipe[pipe];
5794
5795 /*
5796 * For active pipes LP0 watermark is marked as
5797 * enabled, and LP1+ watermaks as disabled since
5798 * we can't really reverse compute them in case
5799 * multiple pipes are active.
5800 */
5801 active->wm[0].enable = true;
5802 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
5803 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
5804 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
5805 active->linetime = hw->wm_linetime[pipe];
5806 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005807 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005808
5809 /*
5810 * For inactive pipes, all watermark levels
5811 * should be marked as enabled but zeroed,
5812 * which is what we'd compute them to.
5813 */
5814 for (level = 0; level <= max_level; level++)
5815 active->wm[level].enable = true;
5816 }
Matt Roper4e0963c2015-09-24 15:53:15 -07005817
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005818 crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005819}
5820
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005821#define _FW_WM(value, plane) \
5822 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
5823#define _FW_WM_VLV(value, plane) \
5824 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
5825
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005826static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
5827 struct g4x_wm_values *wm)
5828{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005829 u32 tmp;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005830
5831 tmp = I915_READ(DSPFW1);
5832 wm->sr.plane = _FW_WM(tmp, SR);
5833 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5834 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
5835 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
5836
5837 tmp = I915_READ(DSPFW2);
5838 wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
5839 wm->sr.fbc = _FW_WM(tmp, FBC_SR);
5840 wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
5841 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
5842 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5843 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
5844
5845 tmp = I915_READ(DSPFW3);
5846 wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
5847 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5848 wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
5849 wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
5850}
5851
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005852static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
5853 struct vlv_wm_values *wm)
5854{
5855 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02005856 u32 tmp;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005857
5858 for_each_pipe(dev_priv, pipe) {
5859 tmp = I915_READ(VLV_DDL(pipe));
5860
Ville Syrjälä1b313892016-11-28 19:37:08 +02005861 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005862 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005863 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005864 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005865 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005866 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005867 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005868 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
5869 }
5870
5871 tmp = I915_READ(DSPFW1);
5872 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005873 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5874 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
5875 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005876
5877 tmp = I915_READ(DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005878 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
5879 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5880 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005881
5882 tmp = I915_READ(DSPFW3);
5883 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5884
5885 if (IS_CHERRYVIEW(dev_priv)) {
5886 tmp = I915_READ(DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005887 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5888 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005889
5890 tmp = I915_READ(DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005891 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
5892 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005893
5894 tmp = I915_READ(DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005895 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
5896 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005897
5898 tmp = I915_READ(DSPHOWM);
5899 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005900 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
5901 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
5902 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
5903 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5904 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5905 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5906 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5907 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5908 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005909 } else {
5910 tmp = I915_READ(DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005911 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5912 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005913
5914 tmp = I915_READ(DSPHOWM);
5915 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005916 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5917 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5918 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5919 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5920 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5921 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005922 }
5923}
5924
5925#undef _FW_WM
5926#undef _FW_WM_VLV
5927
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005928void g4x_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005929{
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005930 struct g4x_wm_values *wm = &dev_priv->wm.g4x;
5931 struct intel_crtc *crtc;
5932
5933 g4x_read_wm_values(dev_priv, wm);
5934
5935 wm->cxsr = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
5936
Matt Ropercd1d3ee2018-12-10 13:54:14 -08005937 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005938 struct intel_crtc_state *crtc_state =
5939 to_intel_crtc_state(crtc->base.state);
5940 struct g4x_wm_state *active = &crtc->wm.active.g4x;
5941 struct g4x_pipe_wm *raw;
5942 enum pipe pipe = crtc->pipe;
5943 enum plane_id plane_id;
5944 int level, max_level;
5945
5946 active->cxsr = wm->cxsr;
5947 active->hpll_en = wm->hpll_en;
5948 active->fbc_en = wm->fbc_en;
5949
5950 active->sr = wm->sr;
5951 active->hpll = wm->hpll;
5952
5953 for_each_plane_id_on_crtc(crtc, plane_id) {
5954 active->wm.plane[plane_id] =
5955 wm->pipe[pipe].plane[plane_id];
5956 }
5957
5958 if (wm->cxsr && wm->hpll_en)
5959 max_level = G4X_WM_LEVEL_HPLL;
5960 else if (wm->cxsr)
5961 max_level = G4X_WM_LEVEL_SR;
5962 else
5963 max_level = G4X_WM_LEVEL_NORMAL;
5964
5965 level = G4X_WM_LEVEL_NORMAL;
5966 raw = &crtc_state->wm.g4x.raw[level];
5967 for_each_plane_id_on_crtc(crtc, plane_id)
5968 raw->plane[plane_id] = active->wm.plane[plane_id];
5969
5970 if (++level > max_level)
5971 goto out;
5972
5973 raw = &crtc_state->wm.g4x.raw[level];
5974 raw->plane[PLANE_PRIMARY] = active->sr.plane;
5975 raw->plane[PLANE_CURSOR] = active->sr.cursor;
5976 raw->plane[PLANE_SPRITE0] = 0;
5977 raw->fbc = active->sr.fbc;
5978
5979 if (++level > max_level)
5980 goto out;
5981
5982 raw = &crtc_state->wm.g4x.raw[level];
5983 raw->plane[PLANE_PRIMARY] = active->hpll.plane;
5984 raw->plane[PLANE_CURSOR] = active->hpll.cursor;
5985 raw->plane[PLANE_SPRITE0] = 0;
5986 raw->fbc = active->hpll.fbc;
5987
5988 out:
5989 for_each_plane_id_on_crtc(crtc, plane_id)
5990 g4x_raw_plane_wm_set(crtc_state, level,
5991 plane_id, USHRT_MAX);
5992 g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
5993
5994 crtc_state->wm.g4x.optimal = *active;
5995 crtc_state->wm.g4x.intermediate = *active;
5996
5997 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
5998 pipe_name(pipe),
5999 wm->pipe[pipe].plane[PLANE_PRIMARY],
6000 wm->pipe[pipe].plane[PLANE_CURSOR],
6001 wm->pipe[pipe].plane[PLANE_SPRITE0]);
6002 }
6003
6004 DRM_DEBUG_KMS("Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
6005 wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
6006 DRM_DEBUG_KMS("Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
6007 wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
6008 DRM_DEBUG_KMS("Initial SR=%s HPLL=%s FBC=%s\n",
6009 yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
6010}
6011
6012void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
6013{
6014 struct intel_plane *plane;
6015 struct intel_crtc *crtc;
6016
6017 mutex_lock(&dev_priv->wm.wm_mutex);
6018
6019 for_each_intel_plane(&dev_priv->drm, plane) {
6020 struct intel_crtc *crtc =
6021 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
6022 struct intel_crtc_state *crtc_state =
6023 to_intel_crtc_state(crtc->base.state);
6024 struct intel_plane_state *plane_state =
6025 to_intel_plane_state(plane->base.state);
6026 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
6027 enum plane_id plane_id = plane->id;
6028 int level;
6029
6030 if (plane_state->base.visible)
6031 continue;
6032
6033 for (level = 0; level < 3; level++) {
6034 struct g4x_pipe_wm *raw =
6035 &crtc_state->wm.g4x.raw[level];
6036
6037 raw->plane[plane_id] = 0;
6038 wm_state->wm.plane[plane_id] = 0;
6039 }
6040
6041 if (plane_id == PLANE_PRIMARY) {
6042 for (level = 0; level < 3; level++) {
6043 struct g4x_pipe_wm *raw =
6044 &crtc_state->wm.g4x.raw[level];
6045 raw->fbc = 0;
6046 }
6047
6048 wm_state->sr.fbc = 0;
6049 wm_state->hpll.fbc = 0;
6050 wm_state->fbc_en = false;
6051 }
6052 }
6053
6054 for_each_intel_crtc(&dev_priv->drm, crtc) {
6055 struct intel_crtc_state *crtc_state =
6056 to_intel_crtc_state(crtc->base.state);
6057
6058 crtc_state->wm.g4x.intermediate =
6059 crtc_state->wm.g4x.optimal;
6060 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
6061 }
6062
6063 g4x_program_watermarks(dev_priv);
6064
6065 mutex_unlock(&dev_priv->wm.wm_mutex);
6066}
6067
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006068void vlv_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006069{
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006070 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02006071 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006072 u32 val;
6073
6074 vlv_read_wm_values(dev_priv, wm);
6075
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006076 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
6077 wm->level = VLV_WM_LEVEL_PM2;
6078
6079 if (IS_CHERRYVIEW(dev_priv)) {
Chris Wilson337fa6e2019-04-26 09:17:20 +01006080 vlv_punit_get(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006081
Ville Syrjäläc11b8132018-11-29 19:55:03 +02006082 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006083 if (val & DSP_MAXFIFO_PM5_ENABLE)
6084 wm->level = VLV_WM_LEVEL_PM5;
6085
Ville Syrjälä58590c12015-09-08 21:05:12 +03006086 /*
6087 * If DDR DVFS is disabled in the BIOS, Punit
6088 * will never ack the request. So if that happens
6089 * assume we don't have to enable/disable DDR DVFS
6090 * dynamically. To test that just set the REQ_ACK
6091 * bit to poke the Punit, but don't change the
6092 * HIGH/LOW bits so that we don't actually change
6093 * the current state.
6094 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006095 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03006096 val |= FORCE_DDR_FREQ_REQ_ACK;
6097 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
6098
6099 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
6100 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
6101 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
6102 "assuming DDR DVFS is disabled\n");
6103 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
6104 } else {
6105 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
6106 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
6107 wm->level = VLV_WM_LEVEL_DDR_DVFS;
6108 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006109
Chris Wilson337fa6e2019-04-26 09:17:20 +01006110 vlv_punit_put(dev_priv);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006111 }
6112
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006113 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02006114 struct intel_crtc_state *crtc_state =
6115 to_intel_crtc_state(crtc->base.state);
6116 struct vlv_wm_state *active = &crtc->wm.active.vlv;
6117 const struct vlv_fifo_state *fifo_state =
6118 &crtc_state->wm.vlv.fifo_state;
6119 enum pipe pipe = crtc->pipe;
6120 enum plane_id plane_id;
6121 int level;
6122
6123 vlv_get_fifo_size(crtc_state);
6124
6125 active->num_levels = wm->level + 1;
6126 active->cxsr = wm->cxsr;
6127
Ville Syrjäläff32c542017-03-02 19:14:57 +02006128 for (level = 0; level < active->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03006129 struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02006130 &crtc_state->wm.vlv.raw[level];
6131
6132 active->sr[level].plane = wm->sr.plane;
6133 active->sr[level].cursor = wm->sr.cursor;
6134
6135 for_each_plane_id_on_crtc(crtc, plane_id) {
6136 active->wm[level].plane[plane_id] =
6137 wm->pipe[pipe].plane[plane_id];
6138
6139 raw->plane[plane_id] =
6140 vlv_invert_wm_value(active->wm[level].plane[plane_id],
6141 fifo_state->plane[plane_id]);
6142 }
6143 }
6144
6145 for_each_plane_id_on_crtc(crtc, plane_id)
6146 vlv_raw_plane_wm_set(crtc_state, level,
6147 plane_id, USHRT_MAX);
6148 vlv_invalidate_wms(crtc, active, level);
6149
6150 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02006151 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02006152
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006153 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
Ville Syrjälä1b313892016-11-28 19:37:08 +02006154 pipe_name(pipe),
6155 wm->pipe[pipe].plane[PLANE_PRIMARY],
6156 wm->pipe[pipe].plane[PLANE_CURSOR],
6157 wm->pipe[pipe].plane[PLANE_SPRITE0],
6158 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02006159 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03006160
6161 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
6162 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
6163}
6164
Ville Syrjälä602ae832017-03-02 19:15:02 +02006165void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
6166{
6167 struct intel_plane *plane;
6168 struct intel_crtc *crtc;
6169
6170 mutex_lock(&dev_priv->wm.wm_mutex);
6171
6172 for_each_intel_plane(&dev_priv->drm, plane) {
6173 struct intel_crtc *crtc =
6174 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
6175 struct intel_crtc_state *crtc_state =
6176 to_intel_crtc_state(crtc->base.state);
6177 struct intel_plane_state *plane_state =
6178 to_intel_plane_state(plane->base.state);
6179 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
6180 const struct vlv_fifo_state *fifo_state =
6181 &crtc_state->wm.vlv.fifo_state;
6182 enum plane_id plane_id = plane->id;
6183 int level;
6184
6185 if (plane_state->base.visible)
6186 continue;
6187
6188 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03006189 struct g4x_pipe_wm *raw =
Ville Syrjälä602ae832017-03-02 19:15:02 +02006190 &crtc_state->wm.vlv.raw[level];
6191
6192 raw->plane[plane_id] = 0;
6193
6194 wm_state->wm[level].plane[plane_id] =
6195 vlv_invert_wm_value(raw->plane[plane_id],
6196 fifo_state->plane[plane_id]);
6197 }
6198 }
6199
6200 for_each_intel_crtc(&dev_priv->drm, crtc) {
6201 struct intel_crtc_state *crtc_state =
6202 to_intel_crtc_state(crtc->base.state);
6203
6204 crtc_state->wm.vlv.intermediate =
6205 crtc_state->wm.vlv.optimal;
6206 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
6207 }
6208
6209 vlv_program_watermarks(dev_priv);
6210
6211 mutex_unlock(&dev_priv->wm.wm_mutex);
6212}
6213
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02006214/*
6215 * FIXME should probably kill this and improve
6216 * the real watermark readout/sanitation instead
6217 */
6218static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
6219{
6220 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
6221 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
6222 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
6223
6224 /*
6225 * Don't touch WM1S_LP_EN here.
6226 * Doing so could cause underruns.
6227 */
6228}
6229
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006230void ilk_wm_get_hw_state(struct drm_i915_private *dev_priv)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006231{
Imre Deak820c1982013-12-17 14:46:36 +02006232 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006233 struct intel_crtc *crtc;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006234
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02006235 ilk_init_lp_watermarks(dev_priv);
6236
Matt Ropercd1d3ee2018-12-10 13:54:14 -08006237 for_each_intel_crtc(&dev_priv->drm, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006238 ilk_pipe_wm_get_hw_state(crtc);
6239
6240 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
6241 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
6242 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
6243
6244 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00006245 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjäläcfa76982014-03-07 18:32:08 +02006246 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
6247 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
6248 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006249
Tvrtko Ursulin86527442016-10-13 11:03:00 +01006250 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02006251 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
6252 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01006253 else if (IS_IVYBRIDGE(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02006254 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
6255 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03006256
6257 hw->enable_fbc_wm =
6258 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
6259}
6260
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006261/**
6262 * intel_update_watermarks - update FIFO watermark values based on current modes
Chris Wilson31383412018-02-14 14:03:03 +00006263 * @crtc: the #intel_crtc on which to compute the WM
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006264 *
6265 * Calculate watermark values for the various WM regs based on current mode
6266 * and plane configuration.
6267 *
6268 * There are several cases to deal with here:
6269 * - normal (i.e. non-self-refresh)
6270 * - self-refresh (SR) mode
6271 * - lines are large relative to FIFO size (buffer can hold up to 2)
6272 * - lines are small relative to FIFO size (buffer can hold more than 2
6273 * lines), so need to account for TLB latency
6274 *
6275 * The normal calculation is:
6276 * watermark = dotclock * bytes per pixel * latency
6277 * where latency is platform & configuration dependent (we assume pessimal
6278 * values here).
6279 *
6280 * The SR calculation is:
6281 * watermark = (trunc(latency/line time)+1) * surface width *
6282 * bytes per pixel
6283 * where
6284 * line time = htotal / dotclock
6285 * surface width = hdisplay for normal plane and 64 for cursor
6286 * and latency is assumed to be high, as above.
6287 *
6288 * The final value programmed to the register should always be rounded up,
6289 * and include an extra 2 entries to account for clock crossings.
6290 *
6291 * We don't use the sprite, so we can ignore that. And on Crestline we have
6292 * to set the non-SR watermarks to 8.
6293 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02006294void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006295{
Ville Syrjälä432081b2016-10-31 22:37:03 +02006296 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006297
6298 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03006299 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03006300}
6301
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306302void intel_enable_ipc(struct drm_i915_private *dev_priv)
6303{
6304 u32 val;
6305
José Roberto de Souzafd847b82018-09-18 13:47:11 -07006306 if (!HAS_IPC(dev_priv))
6307 return;
6308
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306309 val = I915_READ(DISP_ARB_CTL2);
6310
6311 if (dev_priv->ipc_enabled)
6312 val |= DISP_IPC_ENABLE;
6313 else
6314 val &= ~DISP_IPC_ENABLE;
6315
6316 I915_WRITE(DISP_ARB_CTL2, val);
6317}
6318
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03006319static bool intel_can_enable_ipc(struct drm_i915_private *dev_priv)
6320{
6321 /* Display WA #0477 WaDisableIPC: skl */
6322 if (IS_SKYLAKE(dev_priv))
6323 return false;
6324
6325 /* Display WA #1141: SKL:all KBL:all CFL */
6326 if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv))
6327 return dev_priv->dram_info.symmetric_memory;
6328
6329 return true;
6330}
6331
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306332void intel_init_ipc(struct drm_i915_private *dev_priv)
6333{
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306334 if (!HAS_IPC(dev_priv))
6335 return;
6336
Ville Syrjäläc91a45f2019-05-03 20:38:07 +03006337 dev_priv->ipc_enabled = intel_can_enable_ipc(dev_priv);
José Roberto de Souzac9b818d2018-09-18 13:47:13 -07006338
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05306339 intel_enable_ipc(dev_priv);
6340}
6341
Jani Nikulae2828912016-01-18 09:19:47 +02006342/*
Daniel Vetter92703882012-08-09 16:46:01 +02006343 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02006344 */
6345DEFINE_SPINLOCK(mchdev_lock);
6346
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006347bool ironlake_set_drps(struct drm_i915_private *i915, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006348{
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006349 struct intel_uncore *uncore = &i915->uncore;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006350 u16 rgvswctl;
6351
Chris Wilson67520412017-03-02 13:28:01 +00006352 lockdep_assert_held(&mchdev_lock);
Daniel Vetter92703882012-08-09 16:46:01 +02006353
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006354 rgvswctl = intel_uncore_read16(uncore, MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006355 if (rgvswctl & MEMCTL_CMD_STS) {
6356 DRM_DEBUG("gpu busy, RCS change rejected\n");
6357 return false; /* still busy with another command */
6358 }
6359
6360 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
6361 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006362 intel_uncore_write16(uncore, MEMSWCTL, rgvswctl);
6363 intel_uncore_posting_read16(uncore, MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006364
6365 rgvswctl |= MEMCTL_CMD_STS;
Tvrtko Ursuline44d62d2019-06-11 11:45:45 +01006366 intel_uncore_write16(uncore, MEMSWCTL, rgvswctl);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006367
6368 return true;
6369}
6370
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006371static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006372{
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006373 struct intel_uncore *uncore = &dev_priv->uncore;
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00006374 u32 rgvmodectl;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006375 u8 fmax, fmin, fstart, vstart;
6376
Daniel Vetter92703882012-08-09 16:46:01 +02006377 spin_lock_irq(&mchdev_lock);
6378
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006379 rgvmodectl = intel_uncore_read(uncore, MEMMODECTL);
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00006380
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006381 /* Enable temp reporting */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006382 intel_uncore_write16(uncore, PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
6383 intel_uncore_write16(uncore, TSC1, I915_READ(TSC1) | TSE);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006384
6385 /* 100ms RC evaluation intervals */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006386 intel_uncore_write(uncore, RCUPEI, 100000);
6387 intel_uncore_write(uncore, RCDNEI, 100000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006388
6389 /* Set max/min thresholds to 90ms and 80ms respectively */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006390 intel_uncore_write(uncore, RCBMAXAVG, 90000);
6391 intel_uncore_write(uncore, RCBMINAVG, 80000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006392
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006393 intel_uncore_write(uncore, MEMIHYST, 1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006394
6395 /* Set up min, max, and cur for interrupt handling */
6396 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
6397 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
6398 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
6399 MEMMODE_FSTART_SHIFT;
6400
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006401 vstart = (intel_uncore_read(uncore, PXVFREQ(fstart)) &
6402 PXVFREQ_PX_MASK) >> PXVFREQ_PX_SHIFT;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006403
Daniel Vetter20e4d402012-08-08 23:35:39 +02006404 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
6405 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006406
Daniel Vetter20e4d402012-08-08 23:35:39 +02006407 dev_priv->ips.max_delay = fstart;
6408 dev_priv->ips.min_delay = fmin;
6409 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006410
6411 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
6412 fmax, fmin, fstart);
6413
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006414 intel_uncore_write(uncore,
6415 MEMINTREN,
6416 MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006417
6418 /*
6419 * Interrupts will be enabled in ironlake_irq_postinstall
6420 */
6421
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006422 intel_uncore_write(uncore, VIDSTART, vstart);
6423 intel_uncore_posting_read(uncore, VIDSTART);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006424
6425 rgvmodectl |= MEMMODE_SWMODE_EN;
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006426 intel_uncore_write(uncore, MEMMODECTL, rgvmodectl);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006427
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006428 if (wait_for_atomic((intel_uncore_read(uncore, MEMSWCTL) &
6429 MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006430 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02006431 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006432
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006433 ironlake_set_drps(dev_priv, fstart);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006434
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006435 dev_priv->ips.last_count1 =
6436 intel_uncore_read(uncore, DMIEC) +
6437 intel_uncore_read(uncore, DDREC) +
6438 intel_uncore_read(uncore, CSIEC);
Daniel Vetter20e4d402012-08-08 23:35:39 +02006439 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006440 dev_priv->ips.last_count2 = intel_uncore_read(uncore, GFXEC);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00006441 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02006442
6443 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006444}
6445
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006446static void ironlake_disable_drps(struct drm_i915_private *i915)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006447{
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006448 struct intel_uncore *uncore = &i915->uncore;
Daniel Vetter92703882012-08-09 16:46:01 +02006449 u16 rgvswctl;
6450
6451 spin_lock_irq(&mchdev_lock);
6452
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006453 rgvswctl = intel_uncore_read16(uncore, MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006454
6455 /* Ack interrupts, disable EFC interrupt */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006456 intel_uncore_write(uncore,
6457 MEMINTREN,
6458 intel_uncore_read(uncore, MEMINTREN) &
6459 ~MEMINT_EVAL_CHG_EN);
6460 intel_uncore_write(uncore, MEMINTRSTS, MEMINT_EVAL_CHG);
6461 intel_uncore_write(uncore,
6462 DEIER,
6463 intel_uncore_read(uncore, DEIER) & ~DE_PCU_EVENT);
6464 intel_uncore_write(uncore, DEIIR, DE_PCU_EVENT);
6465 intel_uncore_write(uncore,
6466 DEIMR,
6467 intel_uncore_read(uncore, DEIMR) | DE_PCU_EVENT);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006468
6469 /* Go back to the starting frequency */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006470 ironlake_set_drps(i915, i915->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02006471 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006472 rgvswctl |= MEMCTL_CMD_STS;
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01006473 intel_uncore_write(uncore, MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02006474 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006475
Daniel Vetter92703882012-08-09 16:46:01 +02006476 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006477}
6478
Daniel Vetteracbe9472012-07-26 11:50:05 +02006479/* There's a funny hw issue where the hw returns all 0 when reading from
6480 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
6481 * ourselves, instead of doing a rmw cycle (which might result in us clearing
6482 * all limits and the gpu stuck at whatever frequency it is at atm).
6483 */
Akash Goel74ef1172015-03-06 11:07:19 +05306484static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006485{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006486 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006487 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006488
Daniel Vetter20b46e52012-07-26 11:16:14 +02006489 /* Only set the down limit when we've reached the lowest level to avoid
6490 * getting more interrupts, otherwise leave this clear. This prevents a
6491 * race in the hw when coming out of rc6: There's a tiny window where
6492 * the hw runs at the minimal clock before selecting the desired
6493 * frequency, if the down threshold expires in that window we will not
6494 * receive a down interrupt. */
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006495 if (INTEL_GEN(dev_priv) >= 9) {
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006496 limits = (rps->max_freq_softlimit) << 23;
6497 if (val <= rps->min_freq_softlimit)
6498 limits |= (rps->min_freq_softlimit) << 14;
Akash Goel74ef1172015-03-06 11:07:19 +05306499 } else {
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006500 limits = rps->max_freq_softlimit << 24;
6501 if (val <= rps->min_freq_softlimit)
6502 limits |= rps->min_freq_softlimit << 16;
Akash Goel74ef1172015-03-06 11:07:19 +05306503 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02006504
6505 return limits;
6506}
6507
Chris Wilson60548c52018-07-31 14:26:29 +01006508static void rps_set_power(struct drm_i915_private *dev_priv, int new_power)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006509{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006510 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Akash Goel8a586432015-03-06 11:07:18 +05306511 u32 threshold_up = 0, threshold_down = 0; /* in % */
6512 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006513
Chris Wilson60548c52018-07-31 14:26:29 +01006514 lockdep_assert_held(&rps->power.mutex);
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006515
Chris Wilson60548c52018-07-31 14:26:29 +01006516 if (new_power == rps->power.mode)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006517 return;
6518
6519 /* Note the units here are not exactly 1us, but 1280ns. */
6520 switch (new_power) {
6521 case LOW_POWER:
6522 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05306523 ei_up = 16000;
6524 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006525
6526 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306527 ei_down = 32000;
6528 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006529 break;
6530
6531 case BETWEEN:
6532 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05306533 ei_up = 13000;
6534 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006535
6536 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306537 ei_down = 32000;
6538 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006539 break;
6540
6541 case HIGH_POWER:
6542 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05306543 ei_up = 10000;
6544 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006545
6546 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306547 ei_down = 32000;
6548 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006549 break;
6550 }
6551
Mika Kuoppala6067a272017-02-15 15:52:59 +02006552 /* When byt can survive without system hang with dynamic
6553 * sw freq adjustments, this restriction can be lifted.
6554 */
6555 if (IS_VALLEYVIEW(dev_priv))
6556 goto skip_hw_write;
6557
Akash Goel8a586432015-03-06 11:07:18 +05306558 I915_WRITE(GEN6_RP_UP_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01006559 GT_INTERVAL_FROM_US(dev_priv, ei_up));
Akash Goel8a586432015-03-06 11:07:18 +05306560 I915_WRITE(GEN6_RP_UP_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01006561 GT_INTERVAL_FROM_US(dev_priv,
6562 ei_up * threshold_up / 100));
Akash Goel8a586432015-03-06 11:07:18 +05306563
6564 I915_WRITE(GEN6_RP_DOWN_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01006565 GT_INTERVAL_FROM_US(dev_priv, ei_down));
Akash Goel8a586432015-03-06 11:07:18 +05306566 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01006567 GT_INTERVAL_FROM_US(dev_priv,
6568 ei_down * threshold_down / 100));
Akash Goel8a586432015-03-06 11:07:18 +05306569
Chris Wilsona72b5622016-07-02 15:35:59 +01006570 I915_WRITE(GEN6_RP_CONTROL,
Mika Kuoppala1071d0f2019-04-10 16:24:36 +03006571 (INTEL_GEN(dev_priv) > 9 ? 0 : GEN6_RP_MEDIA_TURBO) |
Chris Wilsona72b5622016-07-02 15:35:59 +01006572 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6573 GEN6_RP_MEDIA_IS_GFX |
6574 GEN6_RP_ENABLE |
6575 GEN6_RP_UP_BUSY_AVG |
6576 GEN6_RP_DOWN_IDLE_AVG);
Akash Goel8a586432015-03-06 11:07:18 +05306577
Mika Kuoppala6067a272017-02-15 15:52:59 +02006578skip_hw_write:
Chris Wilson60548c52018-07-31 14:26:29 +01006579 rps->power.mode = new_power;
6580 rps->power.up_threshold = threshold_up;
6581 rps->power.down_threshold = threshold_down;
6582}
6583
6584static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
6585{
6586 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6587 int new_power;
6588
6589 new_power = rps->power.mode;
6590 switch (rps->power.mode) {
6591 case LOW_POWER:
6592 if (val > rps->efficient_freq + 1 &&
6593 val > rps->cur_freq)
6594 new_power = BETWEEN;
6595 break;
6596
6597 case BETWEEN:
6598 if (val <= rps->efficient_freq &&
6599 val < rps->cur_freq)
6600 new_power = LOW_POWER;
6601 else if (val >= rps->rp0_freq &&
6602 val > rps->cur_freq)
6603 new_power = HIGH_POWER;
6604 break;
6605
6606 case HIGH_POWER:
6607 if (val < (rps->rp1_freq + rps->rp0_freq) >> 1 &&
6608 val < rps->cur_freq)
6609 new_power = BETWEEN;
6610 break;
6611 }
6612 /* Max/min bins are special */
6613 if (val <= rps->min_freq_softlimit)
6614 new_power = LOW_POWER;
6615 if (val >= rps->max_freq_softlimit)
6616 new_power = HIGH_POWER;
6617
6618 mutex_lock(&rps->power.mutex);
6619 if (rps->power.interactive)
6620 new_power = HIGH_POWER;
6621 rps_set_power(dev_priv, new_power);
6622 mutex_unlock(&rps->power.mutex);
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006623}
6624
Chris Wilson60548c52018-07-31 14:26:29 +01006625void intel_rps_mark_interactive(struct drm_i915_private *i915, bool interactive)
6626{
6627 struct intel_rps *rps = &i915->gt_pm.rps;
6628
6629 if (INTEL_GEN(i915) < 6)
6630 return;
6631
6632 mutex_lock(&rps->power.mutex);
6633 if (interactive) {
6634 if (!rps->power.interactive++ && READ_ONCE(i915->gt.awake))
6635 rps_set_power(i915, HIGH_POWER);
6636 } else {
6637 GEM_BUG_ON(!rps->power.interactive);
6638 rps->power.interactive--;
6639 }
6640 mutex_unlock(&rps->power.mutex);
6641}
6642
Chris Wilson2876ce72014-03-28 08:03:34 +00006643static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
6644{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006645 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Chris Wilson2876ce72014-03-28 08:03:34 +00006646 u32 mask = 0;
6647
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006648 /* We use UP_EI_EXPIRED interupts for both up/down in manual mode */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006649 if (val > rps->min_freq_softlimit)
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006650 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006651 if (val < rps->max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00006652 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00006653
Chris Wilson7b3c29f2014-07-10 20:31:19 +01006654 mask &= dev_priv->pm_rps_events;
6655
Imre Deak59d02a12014-12-19 19:33:26 +02006656 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00006657}
6658
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006659/* gen6_set_rps is called to update the frequency request, but should also be
6660 * called when the range (min_delay and max_delay) is modified so that we can
6661 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006662static int gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02006663{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006664 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6665
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006666 /* min/max delay may still have been modified so be sure to
6667 * write the limits value.
6668 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006669 if (val != rps->cur_freq) {
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006670 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006671
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006672 if (INTEL_GEN(dev_priv) >= 9)
Akash Goel57041952015-03-06 11:07:17 +05306673 I915_WRITE(GEN6_RPNSWREQ,
6674 GEN9_FREQUENCY(val));
Chris Wilsondc979972016-05-10 14:10:04 +01006675 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006676 I915_WRITE(GEN6_RPNSWREQ,
6677 HSW_FREQUENCY(val));
6678 else
6679 I915_WRITE(GEN6_RPNSWREQ,
6680 GEN6_FREQUENCY(val) |
6681 GEN6_OFFSET(0) |
6682 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006683 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006684
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006685 /* Make sure we continue to get interrupts
6686 * until we hit the minimum or maximum frequencies.
6687 */
Akash Goel74ef1172015-03-06 11:07:19 +05306688 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00006689 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006690
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006691 rps->cur_freq = val;
Mika Kuoppala0f945922015-11-17 18:14:26 +02006692 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006693
6694 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006695}
6696
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006697static int valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006698{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006699 int err;
6700
Chris Wilsondc979972016-05-10 14:10:04 +01006701 if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006702 "Odd GPU freq value\n"))
6703 val &= ~1;
6704
Deepak Scd25dd52015-07-10 18:31:40 +05306705 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
6706
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006707 if (val != dev_priv->gt_pm.rps.cur_freq) {
Chris Wilson337fa6e2019-04-26 09:17:20 +01006708 vlv_punit_get(dev_priv);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006709 err = vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
Chris Wilson337fa6e2019-04-26 09:17:20 +01006710 vlv_punit_put(dev_priv);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006711 if (err)
6712 return err;
6713
Chris Wilsondb4c5e02017-02-10 15:03:46 +00006714 gen6_set_rps_thresholds(dev_priv, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01006715 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006716
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006717 dev_priv->gt_pm.rps.cur_freq = val;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006718 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006719
6720 return 0;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006721}
6722
Deepak Sa7f6e232015-05-09 18:04:44 +05306723/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05306724 *
6725 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05306726 * 1. Forcewake Media well.
6727 * 2. Request idle freq.
6728 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05306729*/
6730static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
6731{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006732 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6733 u32 val = rps->idle_freq;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006734 int err;
Deepak S5549d252014-06-28 11:26:11 +05306735
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006736 if (rps->cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05306737 return;
6738
Chris Wilsonc9efef72017-01-02 15:28:45 +00006739 /* The punit delays the write of the frequency and voltage until it
6740 * determines the GPU is awake. During normal usage we don't want to
6741 * waste power changing the frequency if the GPU is sleeping (rc6).
6742 * However, the GPU and driver is now idle and we do not want to delay
6743 * switching to minimum voltage (reducing power whilst idle) as we do
6744 * not expect to be woken in the near future and so must flush the
6745 * change by waking the device.
6746 *
6747 * We choose to take the media powerwell (either would do to trick the
6748 * punit into committing the voltage change) as that takes a lot less
6749 * power than the render powerwell.
6750 */
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07006751 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006752 err = valleyview_set_rps(dev_priv, val);
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07006753 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006754
6755 if (err)
6756 DRM_ERROR("Failed to set RPS for idle\n");
Deepak S76c3552f2014-01-30 23:08:16 +05306757}
6758
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006759void gen6_rps_busy(struct drm_i915_private *dev_priv)
6760{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006761 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6762
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006763 mutex_lock(&rps->lock);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006764 if (rps->enabled) {
Chris Wilsonbd648182017-02-10 15:03:48 +00006765 u8 freq;
6766
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006767 if (dev_priv->pm_rps_events & GEN6_PM_RP_UP_EI_EXPIRED)
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006768 gen6_rps_reset_ei(dev_priv);
6769 I915_WRITE(GEN6_PMINTRMSK,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006770 gen6_rps_pm_mask(dev_priv, rps->cur_freq));
Michał Winiarski2b83c4c2016-06-20 11:58:27 +02006771
Chris Wilsonc33d2472016-07-04 08:08:36 +01006772 gen6_enable_rps_interrupts(dev_priv);
6773
Chris Wilsonbd648182017-02-10 15:03:48 +00006774 /* Use the user's desired frequency as a guide, but for better
6775 * performance, jump directly to RPe as our starting frequency.
6776 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006777 freq = max(rps->cur_freq,
6778 rps->efficient_freq);
Chris Wilsonbd648182017-02-10 15:03:48 +00006779
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006780 if (intel_set_rps(dev_priv,
Chris Wilsonbd648182017-02-10 15:03:48 +00006781 clamp(freq,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006782 rps->min_freq_softlimit,
6783 rps->max_freq_softlimit)))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006784 DRM_DEBUG_DRIVER("Failed to set idle frequency\n");
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006785 }
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006786 mutex_unlock(&rps->lock);
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006787}
6788
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006789void gen6_rps_idle(struct drm_i915_private *dev_priv)
6790{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006791 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6792
Chris Wilsonc33d2472016-07-04 08:08:36 +01006793 /* Flush our bottom-half so that it does not race with us
6794 * setting the idle frequency and so that it is bounded by
6795 * our rpm wakeref. And then disable the interrupts to stop any
6796 * futher RPS reclocking whilst we are asleep.
6797 */
6798 gen6_disable_rps_interrupts(dev_priv);
6799
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006800 mutex_lock(&rps->lock);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006801 if (rps->enabled) {
Chris Wilsondc979972016-05-10 14:10:04 +01006802 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Deepak S76c3552f2014-01-30 23:08:16 +05306803 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02006804 else
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006805 gen6_set_rps(dev_priv, rps->idle_freq);
6806 rps->last_adj = 0;
Ville Syrjälä12c100b2016-05-23 17:42:48 +03006807 I915_WRITE(GEN6_PMINTRMSK,
6808 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Chris Wilsonc0951f02013-10-10 21:58:50 +01006809 }
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006810 mutex_unlock(&rps->lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006811}
6812
Chris Wilson62eb3c22019-02-13 09:25:04 +00006813void gen6_rps_boost(struct i915_request *rq)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006814{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006815 struct intel_rps *rps = &rq->i915->gt_pm.rps;
Chris Wilson74d290f2017-08-17 13:37:06 +01006816 unsigned long flags;
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006817 bool boost;
6818
Chris Wilson8d3afd72015-05-21 21:01:47 +01006819 /* This is intentionally racy! We peek at the state here, then
6820 * validate inside the RPS worker.
6821 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006822 if (!rps->enabled)
Chris Wilson8d3afd72015-05-21 21:01:47 +01006823 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006824
Chris Wilson0e218342019-01-21 22:21:02 +00006825 if (i915_request_signaled(rq))
Chris Wilson253a2812018-02-06 14:31:37 +00006826 return;
6827
Chris Wilsone61e0f52018-02-21 09:56:36 +00006828 /* Serializes with i915_request_retire() */
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006829 boost = false;
Chris Wilson74d290f2017-08-17 13:37:06 +01006830 spin_lock_irqsave(&rq->lock, flags);
Lionel Landwerlin2a98f4e2019-07-09 17:42:27 +01006831 if (!i915_request_has_waitboost(rq) &&
6832 !dma_fence_is_signaled_locked(&rq->fence)) {
Chris Wilson253a2812018-02-06 14:31:37 +00006833 boost = !atomic_fetch_inc(&rps->num_waiters);
Lionel Landwerlin2a98f4e2019-07-09 17:42:27 +01006834 rq->flags |= I915_REQUEST_WAITBOOST;
Chris Wilsonc0951f02013-10-10 21:58:50 +01006835 }
Chris Wilson74d290f2017-08-17 13:37:06 +01006836 spin_unlock_irqrestore(&rq->lock, flags);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006837 if (!boost)
6838 return;
6839
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006840 if (READ_ONCE(rps->cur_freq) < rps->boost_freq)
6841 schedule_work(&rps->work);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006842
Chris Wilson62eb3c22019-02-13 09:25:04 +00006843 atomic_inc(&rps->boosts);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006844}
6845
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006846int intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006847{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006848 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006849 int err;
6850
Chris Wilsonebb5eb72019-04-26 09:17:21 +01006851 lockdep_assert_held(&rps->lock);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006852 GEM_BUG_ON(val > rps->max_freq);
6853 GEM_BUG_ON(val < rps->min_freq);
Chris Wilsoncfd1c482017-02-20 09:47:07 +00006854
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006855 if (!rps->enabled) {
6856 rps->cur_freq = val;
Chris Wilson76e4e4b2017-02-20 09:47:08 +00006857 return 0;
6858 }
6859
Chris Wilsondc979972016-05-10 14:10:04 +01006860 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006861 err = valleyview_set_rps(dev_priv, val);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006862 else
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006863 err = gen6_set_rps(dev_priv, val);
6864
6865 return err;
Jesse Barnes0a073b82013-04-17 15:54:58 -07006866}
6867
Chris Wilsondc979972016-05-10 14:10:04 +01006868static void gen9_disable_rps(struct drm_i915_private *dev_priv)
Akash Goel2030d682016-04-23 00:05:45 +05306869{
Akash Goel2030d682016-04-23 00:05:45 +05306870 I915_WRITE(GEN6_RP_CONTROL, 0);
6871}
6872
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01006873static void gen6_disable_rps(struct drm_i915_private *dev_priv)
6874{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006875 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Akash Goel2030d682016-04-23 00:05:45 +05306876 I915_WRITE(GEN6_RP_CONTROL, 0);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006877}
6878
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01006879static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
6880{
6881 I915_WRITE(GEN6_RP_CONTROL, 0);
6882}
6883
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01006884static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
6885{
6886 I915_WRITE(GEN6_RP_CONTROL, 0);
6887}
6888
Chris Wilsondc979972016-05-10 14:10:04 +01006889static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
Imre Deake6069ca2014-04-18 16:01:02 +03006890{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006891 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6892
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006893 /* All of these values are in units of 50MHz */
Chris Wilson773ea9a2016-07-13 09:10:33 +01006894
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006895 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02006896 if (IS_GEN9_LP(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006897 u32 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006898 rps->rp0_freq = (rp_state_cap >> 16) & 0xff;
6899 rps->rp1_freq = (rp_state_cap >> 8) & 0xff;
6900 rps->min_freq = (rp_state_cap >> 0) & 0xff;
Bob Paauwe35040562015-06-25 14:54:07 -07006901 } else {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006902 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006903 rps->rp0_freq = (rp_state_cap >> 0) & 0xff;
6904 rps->rp1_freq = (rp_state_cap >> 8) & 0xff;
6905 rps->min_freq = (rp_state_cap >> 16) & 0xff;
Bob Paauwe35040562015-06-25 14:54:07 -07006906 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006907 /* hw_max = RP0 until we check for overclocking */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006908 rps->max_freq = rps->rp0_freq;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006909
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006910 rps->efficient_freq = rps->rp1_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01006911 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
Oscar Mateo2b2874e2018-04-05 17:00:52 +03006912 IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006913 u32 ddcc_status = 0;
6914
6915 if (sandybridge_pcode_read(dev_priv,
6916 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
Ville Syrjäläd284d512019-05-21 19:40:24 +03006917 &ddcc_status, NULL) == 0)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006918 rps->efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08006919 clamp_t(u8,
6920 ((ddcc_status >> 8) & 0xff),
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006921 rps->min_freq,
6922 rps->max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006923 }
6924
Oscar Mateo2b2874e2018-04-05 17:00:52 +03006925 if (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
Akash Goelc5e06882015-06-29 14:50:19 +05306926 /* Store the frequency values in 16.66 MHZ units, which is
Chris Wilson773ea9a2016-07-13 09:10:33 +01006927 * the natural hardware unit for SKL
6928 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006929 rps->rp0_freq *= GEN9_FREQ_SCALER;
6930 rps->rp1_freq *= GEN9_FREQ_SCALER;
6931 rps->min_freq *= GEN9_FREQ_SCALER;
6932 rps->max_freq *= GEN9_FREQ_SCALER;
6933 rps->efficient_freq *= GEN9_FREQ_SCALER;
Akash Goelc5e06882015-06-29 14:50:19 +05306934 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006935}
6936
Chris Wilson3a45b052016-07-13 09:10:32 +01006937static void reset_rps(struct drm_i915_private *dev_priv,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006938 int (*set)(struct drm_i915_private *, u8))
Chris Wilson3a45b052016-07-13 09:10:32 +01006939{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006940 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6941 u8 freq = rps->cur_freq;
Chris Wilson3a45b052016-07-13 09:10:32 +01006942
6943 /* force a reset */
Chris Wilson60548c52018-07-31 14:26:29 +01006944 rps->power.mode = -1;
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006945 rps->cur_freq = -1;
Chris Wilson3a45b052016-07-13 09:10:32 +01006946
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006947 if (set(dev_priv, freq))
6948 DRM_ERROR("Failed to reset RPS to initial values\n");
Chris Wilson3a45b052016-07-13 09:10:32 +01006949}
6950
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006951/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Chris Wilsondc979972016-05-10 14:10:04 +01006952static void gen9_enable_rps(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00006953{
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07006954 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006955
David Weinehall36fe7782017-11-17 10:01:46 +02006956 /* Program defaults and thresholds for RPS */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08006957 if (IS_GEN(dev_priv, 9))
David Weinehall36fe7782017-11-17 10:01:46 +02006958 I915_WRITE(GEN6_RC_VIDEO_FREQ,
6959 GEN9_FREQUENCY(dev_priv->gt_pm.rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006960
Akash Goel0beb0592015-03-06 11:07:20 +05306961 /* 1 second timeout*/
6962 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
6963 GT_INTERVAL_FROM_US(dev_priv, 1000000));
6964
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006965 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006966
Akash Goel0beb0592015-03-06 11:07:20 +05306967 /* Leaning on the below call to gen6_set_rps to program/setup the
6968 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
6969 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
Chris Wilson3a45b052016-07-13 09:10:32 +01006970 reset_rps(dev_priv, gen6_set_rps);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006971
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07006972 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006973}
6974
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01006975static void gen8_enable_rps(struct drm_i915_private *dev_priv)
6976{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006977 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6978
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07006979 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01006980
6981 /* 1 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07006982 I915_WRITE(GEN6_RPNSWREQ,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006983 HSW_FREQUENCY(rps->rp1_freq));
Ben Widawskyf9bdc582014-03-31 17:16:41 -07006984 I915_WRITE(GEN6_RC_VIDEO_FREQ,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006985 HSW_FREQUENCY(rps->rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02006986 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
6987 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006988
Daniel Vetter7526ed72014-09-29 15:07:19 +02006989 /* Docs recommend 900MHz, and 300 MHz respectively */
6990 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006991 rps->max_freq_softlimit << 24 |
6992 rps->min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006993
Daniel Vetter7526ed72014-09-29 15:07:19 +02006994 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
6995 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
6996 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
6997 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006998
Daniel Vetter7526ed72014-09-29 15:07:19 +02006999 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007000
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01007001 /* 2: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02007002 I915_WRITE(GEN6_RP_CONTROL,
7003 GEN6_RP_MEDIA_TURBO |
7004 GEN6_RP_MEDIA_HW_NORMAL_MODE |
7005 GEN6_RP_MEDIA_IS_GFX |
7006 GEN6_RP_ENABLE |
7007 GEN6_RP_UP_BUSY_AVG |
7008 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007009
Chris Wilson3a45b052016-07-13 09:10:32 +01007010 reset_rps(dev_priv, gen6_set_rps);
Daniel Vetter7526ed72014-09-29 15:07:19 +02007011
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007012 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07007013}
7014
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01007015static void gen6_enable_rps(struct drm_i915_private *dev_priv)
7016{
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01007017 /* Here begins a magic sequence of register writes to enable
7018 * auto-downclocking.
7019 *
7020 * Perhaps there might be some value in exposing these to
7021 * userspace...
7022 */
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007023 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01007024
7025 /* Power down if completely idle for over 50ms */
7026 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
7027 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7028
7029 reset_rps(dev_priv, gen6_set_rps);
7030
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007031 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01007032}
7033
Ville Syrjälä03af2042014-06-28 02:03:53 +03007034static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05307035{
7036 u32 val, rp0;
7037
Jani Nikula5b5929c2015-10-07 11:17:46 +03007038 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05307039
Jani Nikula02584042018-12-31 16:56:41 +02007040 switch (RUNTIME_INFO(dev_priv)->sseu.eu_total) {
Jani Nikula5b5929c2015-10-07 11:17:46 +03007041 case 8:
7042 /* (2 * 4) config */
7043 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
7044 break;
7045 case 12:
7046 /* (2 * 6) config */
7047 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
7048 break;
7049 case 16:
7050 /* (2 * 8) config */
7051 default:
7052 /* Setting (2 * 8) Min RP0 for any other combination */
7053 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
7054 break;
Deepak S095acd52015-01-17 11:05:59 +05307055 }
Jani Nikula5b5929c2015-10-07 11:17:46 +03007056
7057 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
7058
Deepak S2b6b3a02014-05-27 15:59:30 +05307059 return rp0;
7060}
7061
7062static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
7063{
7064 u32 val, rpe;
7065
7066 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
7067 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
7068
7069 return rpe;
7070}
7071
Deepak S7707df42014-07-12 18:46:14 +05307072static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
7073{
7074 u32 val, rp1;
7075
Jani Nikula5b5929c2015-10-07 11:17:46 +03007076 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
7077 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
7078
Deepak S7707df42014-07-12 18:46:14 +05307079 return rp1;
7080}
7081
Deepak S96676fe2016-08-12 18:46:41 +05307082static u32 cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
7083{
7084 u32 val, rpn;
7085
7086 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
7087 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
7088 FB_GFX_FREQ_FUSE_MASK);
7089
7090 return rpn;
7091}
7092
Deepak Sf8f2b002014-07-10 13:16:21 +05307093static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
7094{
7095 u32 val, rp1;
7096
7097 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
7098
7099 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
7100
7101 return rp1;
7102}
7103
Ville Syrjälä03af2042014-06-28 02:03:53 +03007104static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07007105{
7106 u32 val, rp0;
7107
Jani Nikula64936252013-05-22 15:36:20 +03007108 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007109
7110 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
7111 /* Clamp to max */
7112 rp0 = min_t(u32, rp0, 0xea);
7113
7114 return rp0;
7115}
7116
7117static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
7118{
7119 u32 val, rpe;
7120
Jani Nikula64936252013-05-22 15:36:20 +03007121 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007122 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03007123 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007124 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
7125
7126 return rpe;
7127}
7128
Ville Syrjälä03af2042014-06-28 02:03:53 +03007129static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07007130{
Imre Deak36146032014-12-04 18:39:35 +02007131 u32 val;
7132
7133 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
7134 /*
7135 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
7136 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
7137 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
7138 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
7139 * to make sure it matches what Punit accepts.
7140 */
7141 return max_t(u32, val, 0xc0);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007142}
7143
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007144static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
7145{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007146 dev_priv->gt_pm.rps.gpll_ref_freq =
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007147 vlv_get_cck_clock(dev_priv, "GPLL ref",
7148 CCK_GPLL_CLOCK_CONTROL,
7149 dev_priv->czclk_freq);
7150
7151 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007152 dev_priv->gt_pm.rps.gpll_ref_freq);
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007153}
7154
Chris Wilsondc979972016-05-10 14:10:04 +01007155static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03007156{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007157 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007158 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03007159
Chris Wilson337fa6e2019-04-26 09:17:20 +01007160 vlv_iosf_sb_get(dev_priv,
7161 BIT(VLV_IOSF_SB_PUNIT) |
7162 BIT(VLV_IOSF_SB_NC) |
7163 BIT(VLV_IOSF_SB_CCK));
7164
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007165 vlv_init_gpll_ref_freq(dev_priv);
7166
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007167 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
7168 switch ((val >> 6) & 3) {
7169 case 0:
7170 case 1:
7171 dev_priv->mem_freq = 800;
7172 break;
7173 case 2:
7174 dev_priv->mem_freq = 1066;
7175 break;
7176 case 3:
7177 dev_priv->mem_freq = 1333;
7178 break;
7179 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02007180 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007181
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007182 rps->max_freq = valleyview_rps_max_freq(dev_priv);
7183 rps->rp0_freq = rps->max_freq;
Imre Deak4e805192014-04-14 20:24:41 +03007184 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007185 intel_gpu_freq(dev_priv, rps->max_freq),
7186 rps->max_freq);
Imre Deak4e805192014-04-14 20:24:41 +03007187
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007188 rps->efficient_freq = valleyview_rps_rpe_freq(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007189 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007190 intel_gpu_freq(dev_priv, rps->efficient_freq),
7191 rps->efficient_freq);
Imre Deak4e805192014-04-14 20:24:41 +03007192
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007193 rps->rp1_freq = valleyview_rps_guar_freq(dev_priv);
Deepak Sf8f2b002014-07-10 13:16:21 +05307194 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007195 intel_gpu_freq(dev_priv, rps->rp1_freq),
7196 rps->rp1_freq);
Deepak Sf8f2b002014-07-10 13:16:21 +05307197
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007198 rps->min_freq = valleyview_rps_min_freq(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007199 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007200 intel_gpu_freq(dev_priv, rps->min_freq),
7201 rps->min_freq);
Chris Wilson337fa6e2019-04-26 09:17:20 +01007202
7203 vlv_iosf_sb_put(dev_priv,
7204 BIT(VLV_IOSF_SB_PUNIT) |
7205 BIT(VLV_IOSF_SB_NC) |
7206 BIT(VLV_IOSF_SB_CCK));
Imre Deak4e805192014-04-14 20:24:41 +03007207}
7208
Chris Wilsondc979972016-05-10 14:10:04 +01007209static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05307210{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007211 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007212 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05307213
Chris Wilson337fa6e2019-04-26 09:17:20 +01007214 vlv_iosf_sb_get(dev_priv,
7215 BIT(VLV_IOSF_SB_PUNIT) |
7216 BIT(VLV_IOSF_SB_NC) |
7217 BIT(VLV_IOSF_SB_CCK));
7218
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007219 vlv_init_gpll_ref_freq(dev_priv);
7220
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02007221 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02007222
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007223 switch ((val >> 2) & 0x7) {
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007224 case 3:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007225 dev_priv->mem_freq = 2000;
7226 break;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03007227 default:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007228 dev_priv->mem_freq = 1600;
7229 break;
7230 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02007231 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007232
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007233 rps->max_freq = cherryview_rps_max_freq(dev_priv);
7234 rps->rp0_freq = rps->max_freq;
Deepak S2b6b3a02014-05-27 15:59:30 +05307235 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007236 intel_gpu_freq(dev_priv, rps->max_freq),
7237 rps->max_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05307238
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007239 rps->efficient_freq = cherryview_rps_rpe_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307240 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007241 intel_gpu_freq(dev_priv, rps->efficient_freq),
7242 rps->efficient_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05307243
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007244 rps->rp1_freq = cherryview_rps_guar_freq(dev_priv);
Deepak S7707df42014-07-12 18:46:14 +05307245 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007246 intel_gpu_freq(dev_priv, rps->rp1_freq),
7247 rps->rp1_freq);
Deepak S7707df42014-07-12 18:46:14 +05307248
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007249 rps->min_freq = cherryview_rps_min_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307250 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007251 intel_gpu_freq(dev_priv, rps->min_freq),
7252 rps->min_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05307253
Chris Wilson337fa6e2019-04-26 09:17:20 +01007254 vlv_iosf_sb_put(dev_priv,
7255 BIT(VLV_IOSF_SB_PUNIT) |
7256 BIT(VLV_IOSF_SB_NC) |
7257 BIT(VLV_IOSF_SB_CCK));
7258
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007259 WARN_ONCE((rps->max_freq | rps->efficient_freq | rps->rp1_freq |
7260 rps->min_freq) & 1,
Ville Syrjälä1c147622014-08-18 14:42:43 +03007261 "Odd GPU freq values\n");
Deepak S38807742014-05-23 21:00:15 +05307262}
7263
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007264static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
7265{
7266 u32 val;
7267
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007268 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007269
7270 /* 1: Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02007271 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05307272 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
7273 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
7274 I915_WRITE(GEN6_RP_UP_EI, 66000);
7275 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
7276
7277 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7278
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007279 /* 2: Enable RPS */
Deepak S2b6b3a02014-05-27 15:59:30 +05307280 I915_WRITE(GEN6_RP_CONTROL,
7281 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02007282 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05307283 GEN6_RP_ENABLE |
7284 GEN6_RP_UP_BUSY_AVG |
7285 GEN6_RP_DOWN_IDLE_AVG);
7286
Deepak S3ef62342015-04-29 08:36:24 +05307287 /* Setting Fixed Bias */
Chris Wilson337fa6e2019-04-26 09:17:20 +01007288 vlv_punit_get(dev_priv);
7289
7290 val = VLV_OVERRIDE_EN | VLV_SOC_TDP_EN | CHV_BIAS_CPU_50_SOC_50;
Deepak S3ef62342015-04-29 08:36:24 +05307291 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
7292
Deepak S2b6b3a02014-05-27 15:59:30 +05307293 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
7294
Chris Wilson337fa6e2019-04-26 09:17:20 +01007295 vlv_punit_put(dev_priv);
7296
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02007297 /* RPS code assumes GPLL is used */
7298 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
7299
Jani Nikula742f4912015-09-03 11:16:09 +03007300 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05307301 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7302
Chris Wilson3a45b052016-07-13 09:10:32 +01007303 reset_rps(dev_priv, valleyview_set_rps);
Deepak S2b6b3a02014-05-27 15:59:30 +05307304
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007305 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05307306}
7307
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01007308static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
7309{
7310 u32 val;
7311
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007312 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01007313
Ville Syrjäläcad725f2015-01-19 13:50:48 +02007314 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007315 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
7316 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
7317 I915_WRITE(GEN6_RP_UP_EI, 66000);
7318 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
7319
7320 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7321
7322 I915_WRITE(GEN6_RP_CONTROL,
7323 GEN6_RP_MEDIA_TURBO |
7324 GEN6_RP_MEDIA_HW_NORMAL_MODE |
7325 GEN6_RP_MEDIA_IS_GFX |
7326 GEN6_RP_ENABLE |
7327 GEN6_RP_UP_BUSY_AVG |
7328 GEN6_RP_DOWN_IDLE_CONT);
7329
Chris Wilson337fa6e2019-04-26 09:17:20 +01007330 vlv_punit_get(dev_priv);
7331
Deepak S3ef62342015-04-29 08:36:24 +05307332 /* Setting Fixed Bias */
Chris Wilson337fa6e2019-04-26 09:17:20 +01007333 val = VLV_OVERRIDE_EN | VLV_SOC_TDP_EN | VLV_BIAS_CPU_125_SOC_875;
Deepak S3ef62342015-04-29 08:36:24 +05307334 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
7335
Jani Nikula64936252013-05-22 15:36:20 +03007336 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007337
Chris Wilson337fa6e2019-04-26 09:17:20 +01007338 vlv_punit_put(dev_priv);
7339
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02007340 /* RPS code assumes GPLL is used */
7341 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
7342
Jani Nikula742f4912015-09-03 11:16:09 +03007343 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07007344 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7345
Chris Wilson3a45b052016-07-13 09:10:32 +01007346 reset_rps(dev_priv, valleyview_set_rps);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007347
Daniele Ceraolo Spurio3ceea6a2019-03-19 11:35:36 -07007348 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007349}
7350
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007351static unsigned long intel_pxfreq(u32 vidfreq)
7352{
7353 unsigned long freq;
7354 int div = (vidfreq & 0x3f0000) >> 16;
7355 int post = (vidfreq & 0x3000) >> 12;
7356 int pre = (vidfreq & 0x7);
7357
7358 if (!pre)
7359 return 0;
7360
7361 freq = ((div * 133333) / ((1<<post) * pre));
7362
7363 return freq;
7364}
7365
Daniel Vettereb48eb02012-04-26 23:28:12 +02007366static const struct cparams {
7367 u16 i;
7368 u16 t;
7369 u16 m;
7370 u16 c;
7371} cparams[] = {
7372 { 1, 1333, 301, 28664 },
7373 { 1, 1066, 294, 24460 },
7374 { 1, 800, 294, 25192 },
7375 { 0, 1333, 276, 27605 },
7376 { 0, 1066, 276, 27605 },
7377 { 0, 800, 231, 23784 },
7378};
7379
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007380static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007381{
7382 u64 total_count, diff, ret;
7383 u32 count1, count2, count3, m = 0, c = 0;
7384 unsigned long now = jiffies_to_msecs(jiffies), diff1;
7385 int i;
7386
Chris Wilson67520412017-03-02 13:28:01 +00007387 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007388
Daniel Vetter20e4d402012-08-08 23:35:39 +02007389 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007390
7391 /* Prevent division-by-zero if we are asking too fast.
7392 * Also, we don't get interesting results if we are polling
7393 * faster than once in 10ms, so just return the saved value
7394 * in such cases.
7395 */
7396 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02007397 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007398
7399 count1 = I915_READ(DMIEC);
7400 count2 = I915_READ(DDREC);
7401 count3 = I915_READ(CSIEC);
7402
7403 total_count = count1 + count2 + count3;
7404
7405 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02007406 if (total_count < dev_priv->ips.last_count1) {
7407 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007408 diff += total_count;
7409 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007410 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007411 }
7412
7413 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007414 if (cparams[i].i == dev_priv->ips.c_m &&
7415 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02007416 m = cparams[i].m;
7417 c = cparams[i].c;
7418 break;
7419 }
7420 }
7421
7422 diff = div_u64(diff, diff1);
7423 ret = ((m * diff) + c);
7424 ret = div_u64(ret, 10);
7425
Daniel Vetter20e4d402012-08-08 23:35:39 +02007426 dev_priv->ips.last_count1 = total_count;
7427 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007428
Daniel Vetter20e4d402012-08-08 23:35:39 +02007429 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007430
7431 return ret;
7432}
7433
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007434unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
7435{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007436 intel_wakeref_t wakeref;
7437 unsigned long val = 0;
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007438
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007439 if (!IS_GEN(dev_priv, 5))
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007440 return 0;
7441
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -07007442 with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref) {
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007443 spin_lock_irq(&mchdev_lock);
7444 val = __i915_chipset_val(dev_priv);
7445 spin_unlock_irq(&mchdev_lock);
7446 }
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007447
7448 return val;
7449}
7450
Tvrtko Ursulinc54f0ba2019-06-11 11:45:43 +01007451unsigned long i915_mch_val(struct drm_i915_private *i915)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007452{
7453 unsigned long m, x, b;
7454 u32 tsfs;
7455
Tvrtko Ursulinc54f0ba2019-06-11 11:45:43 +01007456 tsfs = intel_uncore_read(&i915->uncore, TSFS);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007457
7458 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
Tvrtko Ursulinc54f0ba2019-06-11 11:45:43 +01007459 x = intel_uncore_read8(&i915->uncore, TR1);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007460
7461 b = tsfs & TSFS_INTR_MASK;
7462
7463 return ((m * x) / 127) - b;
7464}
7465
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007466static int _pxvid_to_vd(u8 pxvid)
7467{
7468 if (pxvid == 0)
7469 return 0;
7470
7471 if (pxvid >= 8 && pxvid < 31)
7472 pxvid = 31;
7473
7474 return (pxvid + 2) * 125;
7475}
7476
7477static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007478{
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007479 const int vd = _pxvid_to_vd(pxvid);
7480 const int vm = vd - 1125;
7481
Chris Wilsondc979972016-05-10 14:10:04 +01007482 if (INTEL_INFO(dev_priv)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007483 return vm > 0 ? vm : 0;
7484
7485 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007486}
7487
Daniel Vetter02d71952012-08-09 16:44:54 +02007488static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007489{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00007490 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007491 u32 count;
7492
Chris Wilson67520412017-03-02 13:28:01 +00007493 lockdep_assert_held(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007494
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00007495 now = ktime_get_raw_ns();
7496 diffms = now - dev_priv->ips.last_time2;
7497 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007498
7499 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02007500 if (!diffms)
7501 return;
7502
7503 count = I915_READ(GFXEC);
7504
Daniel Vetter20e4d402012-08-08 23:35:39 +02007505 if (count < dev_priv->ips.last_count2) {
7506 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007507 diff += count;
7508 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007509 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007510 }
7511
Daniel Vetter20e4d402012-08-08 23:35:39 +02007512 dev_priv->ips.last_count2 = count;
7513 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007514
7515 /* More magic constants... */
7516 diff = diff * 1181;
7517 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02007518 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007519}
7520
Daniel Vetter02d71952012-08-09 16:44:54 +02007521void i915_update_gfx_val(struct drm_i915_private *dev_priv)
7522{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007523 intel_wakeref_t wakeref;
7524
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007525 if (!IS_GEN(dev_priv, 5))
Daniel Vetter02d71952012-08-09 16:44:54 +02007526 return;
7527
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -07007528 with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref) {
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007529 spin_lock_irq(&mchdev_lock);
7530 __i915_update_gfx_val(dev_priv);
7531 spin_unlock_irq(&mchdev_lock);
7532 }
Daniel Vetter02d71952012-08-09 16:44:54 +02007533}
7534
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007535static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007536{
7537 unsigned long t, corr, state1, corr2, state2;
7538 u32 pxvid, ext_v;
7539
Chris Wilson67520412017-03-02 13:28:01 +00007540 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007541
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007542 pxvid = I915_READ(PXVFREQ(dev_priv->gt_pm.rps.cur_freq));
Daniel Vettereb48eb02012-04-26 23:28:12 +02007543 pxvid = (pxvid >> 24) & 0x7f;
7544 ext_v = pvid_to_extvid(dev_priv, pxvid);
7545
7546 state1 = ext_v;
7547
7548 t = i915_mch_val(dev_priv);
7549
7550 /* Revel in the empirically derived constants */
7551
7552 /* Correction factor in 1/100000 units */
7553 if (t > 80)
7554 corr = ((t * 2349) + 135940);
7555 else if (t >= 50)
7556 corr = ((t * 964) + 29317);
7557 else /* < 50 */
7558 corr = ((t * 301) + 1004);
7559
7560 corr = corr * ((150142 * state1) / 10000 - 78642);
7561 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02007562 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007563
7564 state2 = (corr2 * state1) / 10000;
7565 state2 /= 100; /* convert to mW */
7566
Daniel Vetter02d71952012-08-09 16:44:54 +02007567 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007568
Daniel Vetter20e4d402012-08-08 23:35:39 +02007569 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007570}
7571
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007572unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
7573{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007574 intel_wakeref_t wakeref;
7575 unsigned long val = 0;
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007576
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007577 if (!IS_GEN(dev_priv, 5))
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007578 return 0;
7579
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -07007580 with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref) {
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007581 spin_lock_irq(&mchdev_lock);
7582 val = __i915_gfx_val(dev_priv);
7583 spin_unlock_irq(&mchdev_lock);
7584 }
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007585
7586 return val;
7587}
7588
Chris Wilsonadc674c2019-04-12 09:53:22 +01007589static struct drm_i915_private __rcu *i915_mch_dev;
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007590
7591static struct drm_i915_private *mchdev_get(void)
7592{
7593 struct drm_i915_private *i915;
7594
7595 rcu_read_lock();
Chris Wilsonadc674c2019-04-12 09:53:22 +01007596 i915 = rcu_dereference(i915_mch_dev);
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007597 if (!kref_get_unless_zero(&i915->drm.ref))
7598 i915 = NULL;
7599 rcu_read_unlock();
7600
7601 return i915;
7602}
7603
Daniel Vettereb48eb02012-04-26 23:28:12 +02007604/**
7605 * i915_read_mch_val - return value for IPS use
7606 *
7607 * Calculate and return a value for the IPS driver to use when deciding whether
7608 * we have thermal and power headroom to increase CPU or GPU power budget.
7609 */
7610unsigned long i915_read_mch_val(void)
7611{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007612 struct drm_i915_private *i915;
7613 unsigned long chipset_val = 0;
7614 unsigned long graphics_val = 0;
7615 intel_wakeref_t wakeref;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007616
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007617 i915 = mchdev_get();
7618 if (!i915)
7619 return 0;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007620
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -07007621 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007622 spin_lock_irq(&mchdev_lock);
7623 chipset_val = __i915_chipset_val(i915);
7624 graphics_val = __i915_gfx_val(i915);
7625 spin_unlock_irq(&mchdev_lock);
7626 }
Daniel Vettereb48eb02012-04-26 23:28:12 +02007627
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007628 drm_dev_put(&i915->drm);
7629 return chipset_val + graphics_val;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007630}
7631EXPORT_SYMBOL_GPL(i915_read_mch_val);
7632
7633/**
7634 * i915_gpu_raise - raise GPU frequency limit
7635 *
7636 * Raise the limit; IPS indicates we have thermal headroom.
7637 */
7638bool i915_gpu_raise(void)
7639{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007640 struct drm_i915_private *i915;
7641
7642 i915 = mchdev_get();
7643 if (!i915)
7644 return false;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007645
Daniel Vetter92703882012-08-09 16:46:01 +02007646 spin_lock_irq(&mchdev_lock);
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007647 if (i915->ips.max_delay > i915->ips.fmax)
7648 i915->ips.max_delay--;
Daniel Vetter92703882012-08-09 16:46:01 +02007649 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007650
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007651 drm_dev_put(&i915->drm);
7652 return true;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007653}
7654EXPORT_SYMBOL_GPL(i915_gpu_raise);
7655
7656/**
7657 * i915_gpu_lower - lower GPU frequency limit
7658 *
7659 * IPS indicates we're close to a thermal limit, so throttle back the GPU
7660 * frequency maximum.
7661 */
7662bool i915_gpu_lower(void)
7663{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007664 struct drm_i915_private *i915;
7665
7666 i915 = mchdev_get();
7667 if (!i915)
7668 return false;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007669
Daniel Vetter92703882012-08-09 16:46:01 +02007670 spin_lock_irq(&mchdev_lock);
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007671 if (i915->ips.max_delay < i915->ips.min_delay)
7672 i915->ips.max_delay++;
Daniel Vetter92703882012-08-09 16:46:01 +02007673 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007674
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007675 drm_dev_put(&i915->drm);
7676 return true;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007677}
7678EXPORT_SYMBOL_GPL(i915_gpu_lower);
7679
7680/**
7681 * i915_gpu_busy - indicate GPU business to IPS
7682 *
7683 * Tell the IPS driver whether or not the GPU is busy.
7684 */
7685bool i915_gpu_busy(void)
7686{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007687 struct drm_i915_private *i915;
7688 bool ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007689
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007690 i915 = mchdev_get();
7691 if (!i915)
7692 return false;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007693
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007694 ret = i915->gt.awake;
7695
7696 drm_dev_put(&i915->drm);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007697 return ret;
7698}
7699EXPORT_SYMBOL_GPL(i915_gpu_busy);
7700
7701/**
7702 * i915_gpu_turbo_disable - disable graphics turbo
7703 *
7704 * Disable graphics turbo by resetting the max frequency and setting the
7705 * current frequency to the default.
7706 */
7707bool i915_gpu_turbo_disable(void)
7708{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007709 struct drm_i915_private *i915;
7710 bool ret;
7711
7712 i915 = mchdev_get();
7713 if (!i915)
7714 return false;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007715
Daniel Vetter92703882012-08-09 16:46:01 +02007716 spin_lock_irq(&mchdev_lock);
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007717 i915->ips.max_delay = i915->ips.fstart;
7718 ret = ironlake_set_drps(i915, i915->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02007719 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007720
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007721 drm_dev_put(&i915->drm);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007722 return ret;
7723}
7724EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
7725
7726/**
7727 * Tells the intel_ips driver that the i915 driver is now loaded, if
7728 * IPS got loaded first.
7729 *
7730 * This awkward dance is so that neither module has to depend on the
7731 * other in order for IPS to do the appropriate communication of
7732 * GPU turbo limits to i915.
7733 */
7734static void
7735ips_ping_for_i915_load(void)
7736{
7737 void (*link)(void);
7738
7739 link = symbol_get(ips_link_to_i915_driver);
7740 if (link) {
7741 link();
7742 symbol_put(ips_link_to_i915_driver);
7743 }
7744}
7745
7746void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
7747{
Daniel Vetter02d71952012-08-09 16:44:54 +02007748 /* We only register the i915 ips part with intel-ips once everything is
7749 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007750 rcu_assign_pointer(i915_mch_dev, dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007751
7752 ips_ping_for_i915_load();
7753}
7754
7755void intel_gpu_ips_teardown(void)
7756{
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00007757 rcu_assign_pointer(i915_mch_dev, NULL);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007758}
Deepak S76c3552f2014-01-30 23:08:16 +05307759
Chris Wilsondc979972016-05-10 14:10:04 +01007760static void intel_init_emon(struct drm_i915_private *dev_priv)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007761{
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007762 u32 lcfuse;
7763 u8 pxw[16];
7764 int i;
7765
7766 /* Disable to program */
7767 I915_WRITE(ECR, 0);
7768 POSTING_READ(ECR);
7769
7770 /* Program energy weights for various events */
7771 I915_WRITE(SDEW, 0x15040d00);
7772 I915_WRITE(CSIEW0, 0x007f0000);
7773 I915_WRITE(CSIEW1, 0x1e220004);
7774 I915_WRITE(CSIEW2, 0x04000004);
7775
7776 for (i = 0; i < 5; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007777 I915_WRITE(PEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007778 for (i = 0; i < 3; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007779 I915_WRITE(DEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007780
7781 /* Program P-state weights to account for frequency power adjustment */
7782 for (i = 0; i < 16; i++) {
Ville Syrjälä616847e2015-09-18 20:03:19 +03007783 u32 pxvidfreq = I915_READ(PXVFREQ(i));
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007784 unsigned long freq = intel_pxfreq(pxvidfreq);
7785 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
7786 PXVFREQ_PX_SHIFT;
7787 unsigned long val;
7788
7789 val = vid * vid;
7790 val *= (freq / 1000);
7791 val *= 255;
7792 val /= (127*127*900);
7793 if (val > 0xff)
7794 DRM_ERROR("bad pxval: %ld\n", val);
7795 pxw[i] = val;
7796 }
7797 /* Render standby states get 0 weight */
7798 pxw[14] = 0;
7799 pxw[15] = 0;
7800
7801 for (i = 0; i < 4; i++) {
7802 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
7803 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
Ville Syrjälä616847e2015-09-18 20:03:19 +03007804 I915_WRITE(PXW(i), val);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007805 }
7806
7807 /* Adjust magic regs to magic values (more experimental results) */
7808 I915_WRITE(OGW0, 0);
7809 I915_WRITE(OGW1, 0);
7810 I915_WRITE(EG0, 0x00007f00);
7811 I915_WRITE(EG1, 0x0000000e);
7812 I915_WRITE(EG2, 0x000e0000);
7813 I915_WRITE(EG3, 0x68000300);
7814 I915_WRITE(EG4, 0x42000000);
7815 I915_WRITE(EG5, 0x00140031);
7816 I915_WRITE(EG6, 0);
7817 I915_WRITE(EG7, 0);
7818
7819 for (i = 0; i < 8; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007820 I915_WRITE(PXWL(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007821
7822 /* Enable PMON + select events */
7823 I915_WRITE(ECR, 0x80000019);
7824
7825 lcfuse = I915_READ(LCFUSE02);
7826
Daniel Vetter20e4d402012-08-08 23:35:39 +02007827 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007828}
7829
Chris Wilsondc979972016-05-10 14:10:04 +01007830void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007831{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007832 struct intel_rps *rps = &dev_priv->gt_pm.rps;
7833
Andi Shytic1132362019-09-27 12:08:49 +01007834 /* Powersaving is controlled by the host when inside a VM */
7835 if (intel_vgpu_active(dev_priv))
7836 mkwrite_device_info(dev_priv)->has_rps = false;
Imre Deake6069ca2014-04-18 16:01:02 +03007837
Chris Wilson773ea9a2016-07-13 09:10:33 +01007838 /* Initialize RPS limits (for userspace) */
Chris Wilsondc979972016-05-10 14:10:04 +01007839 if (IS_CHERRYVIEW(dev_priv))
7840 cherryview_init_gt_powersave(dev_priv);
7841 else if (IS_VALLEYVIEW(dev_priv))
7842 valleyview_init_gt_powersave(dev_priv);
Chris Wilson2a13ae72016-08-02 11:15:27 +01007843 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson773ea9a2016-07-13 09:10:33 +01007844 gen6_init_rps_frequencies(dev_priv);
7845
7846 /* Derive initial user preferences/limits from the hardware limits */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007847 rps->max_freq_softlimit = rps->max_freq;
7848 rps->min_freq_softlimit = rps->min_freq;
Chris Wilson773ea9a2016-07-13 09:10:33 +01007849
Chris Wilson99ac9612016-07-13 09:10:34 +01007850 /* After setting max-softlimit, find the overclock max freq */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08007851 if (IS_GEN(dev_priv, 6) ||
Chris Wilson99ac9612016-07-13 09:10:34 +01007852 IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
7853 u32 params = 0;
7854
Ville Syrjäläd284d512019-05-21 19:40:24 +03007855 sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS,
7856 &params, NULL);
Chris Wilson99ac9612016-07-13 09:10:34 +01007857 if (params & BIT(31)) { /* OC supported */
7858 DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007859 (rps->max_freq & 0xff) * 50,
Chris Wilson99ac9612016-07-13 09:10:34 +01007860 (params & 0xff) * 50);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007861 rps->max_freq = params & 0xff;
Chris Wilson99ac9612016-07-13 09:10:34 +01007862 }
7863 }
7864
Chris Wilson29ecd78d2016-07-13 09:10:35 +01007865 /* Finally allow us to boost to max by default */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007866 rps->boost_freq = rps->max_freq;
Chris Wilson844e3312019-04-18 21:53:58 +01007867 rps->idle_freq = rps->min_freq;
7868 rps->cur_freq = rps->idle_freq;
Imre Deakae484342014-03-31 15:10:44 +03007869}
7870
Chris Wilsonb7137e02016-07-13 09:10:37 +01007871void intel_sanitize_gt_powersave(struct drm_i915_private *dev_priv)
7872{
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01007873 dev_priv->gt_pm.rps.enabled = true; /* force RPS disabling */
Chris Wilsonb7137e02016-07-13 09:10:37 +01007874 intel_disable_gt_powersave(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007875
Oscar Mateod02b98b2018-04-05 17:00:50 +03007876 if (INTEL_GEN(dev_priv) >= 11)
7877 gen11_reset_rps_interrupts(dev_priv);
Chris Wilson61e1e372018-08-12 23:36:30 +01007878 else if (INTEL_GEN(dev_priv) >= 6)
Oscar Mateod02b98b2018-04-05 17:00:50 +03007879 gen6_reset_rps_interrupts(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07007880}
7881
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01007882static void intel_disable_rps(struct drm_i915_private *dev_priv)
7883{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01007884 lockdep_assert_held(&dev_priv->gt_pm.rps.lock);
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01007885
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01007886 if (!dev_priv->gt_pm.rps.enabled)
7887 return;
7888
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01007889 if (INTEL_GEN(dev_priv) >= 9)
7890 gen9_disable_rps(dev_priv);
7891 else if (IS_CHERRYVIEW(dev_priv))
7892 cherryview_disable_rps(dev_priv);
7893 else if (IS_VALLEYVIEW(dev_priv))
7894 valleyview_disable_rps(dev_priv);
7895 else if (INTEL_GEN(dev_priv) >= 6)
7896 gen6_disable_rps(dev_priv);
7897 else if (IS_IRONLAKE_M(dev_priv))
7898 ironlake_disable_drps(dev_priv);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01007899
7900 dev_priv->gt_pm.rps.enabled = false;
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01007901}
7902
Chris Wilsondc979972016-05-10 14:10:04 +01007903void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
Daniel Vetter8090c6b2012-06-24 16:42:32 +02007904{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01007905 mutex_lock(&dev_priv->gt_pm.rps.lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007906
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01007907 intel_disable_rps(dev_priv);
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01007908 if (HAS_LLC(dev_priv))
Andi Shyti0dc3c562019-10-20 19:41:39 +01007909 intel_llc_disable(&dev_priv->gt.llc);
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01007910
Chris Wilsonebb5eb72019-04-26 09:17:21 +01007911 mutex_unlock(&dev_priv->gt_pm.rps.lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007912}
7913
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01007914static void intel_enable_rps(struct drm_i915_private *dev_priv)
7915{
7916 struct intel_rps *rps = &dev_priv->gt_pm.rps;
7917
Chris Wilsonebb5eb72019-04-26 09:17:21 +01007918 lockdep_assert_held(&rps->lock);
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01007919
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01007920 if (rps->enabled)
7921 return;
7922
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01007923 if (IS_CHERRYVIEW(dev_priv)) {
7924 cherryview_enable_rps(dev_priv);
7925 } else if (IS_VALLEYVIEW(dev_priv)) {
7926 valleyview_enable_rps(dev_priv);
7927 } else if (INTEL_GEN(dev_priv) >= 9) {
7928 gen9_enable_rps(dev_priv);
7929 } else if (IS_BROADWELL(dev_priv)) {
7930 gen8_enable_rps(dev_priv);
7931 } else if (INTEL_GEN(dev_priv) >= 6) {
7932 gen6_enable_rps(dev_priv);
7933 } else if (IS_IRONLAKE_M(dev_priv)) {
7934 ironlake_enable_drps(dev_priv);
7935 intel_init_emon(dev_priv);
7936 }
7937
7938 WARN_ON(rps->max_freq < rps->min_freq);
7939 WARN_ON(rps->idle_freq > rps->max_freq);
7940
7941 WARN_ON(rps->efficient_freq < rps->min_freq);
7942 WARN_ON(rps->efficient_freq > rps->max_freq);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01007943
7944 rps->enabled = true;
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01007945}
7946
Chris Wilsonb7137e02016-07-13 09:10:37 +01007947void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
7948{
Chris Wilsonb7137e02016-07-13 09:10:37 +01007949 /* Powersaving is controlled by the host when inside a VM */
7950 if (intel_vgpu_active(dev_priv))
7951 return;
7952
Chris Wilsonebb5eb72019-04-26 09:17:21 +01007953 mutex_lock(&dev_priv->gt_pm.rps.lock);
Imre Deak3cc134e2014-11-19 15:30:03 +02007954
Chris Wilson91cbdb82019-04-19 14:48:36 +01007955 if (HAS_RPS(dev_priv))
7956 intel_enable_rps(dev_priv);
Andi Shyti0dc3c562019-10-20 19:41:39 +01007957
7958 intel_llc_enable(&dev_priv->gt.llc);
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01007959
Chris Wilsonebb5eb72019-04-26 09:17:21 +01007960 mutex_unlock(&dev_priv->gt_pm.rps.lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007961}
Imre Deakc6df39b2014-04-14 20:24:29 +03007962
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007963static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007964{
Daniel Vetter3107bd42012-10-31 22:52:31 +01007965 /*
7966 * On Ibex Peak and Cougar Point, we need to disable clock
7967 * gating for the panel power sequencer or it will fail to
7968 * start up when no ports are active.
7969 */
7970 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
7971}
7972
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007973static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007974{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007975 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007976
Damien Lespiau055e3932014-08-18 13:49:10 +01007977 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007978 I915_WRITE(DSPCNTR(pipe),
7979 I915_READ(DSPCNTR(pipe)) |
7980 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007981
7982 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
7983 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007984 }
7985}
7986
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007987static void ilk_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007988{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02007989 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007990
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01007991 /*
7992 * Required for FBC
7993 * WaFbcDisableDpfcClockGating:ilk
7994 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007995 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
7996 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
7997 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007998
7999 I915_WRITE(PCH_3DCGDIS0,
8000 MARIUNIT_CLOCK_GATE_DISABLE |
8001 SVSMUNIT_CLOCK_GATE_DISABLE);
8002 I915_WRITE(PCH_3DCGDIS1,
8003 VFMUNIT_CLOCK_GATE_DISABLE);
8004
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008005 /*
8006 * According to the spec the following bits should be set in
8007 * order to enable memory self-refresh
8008 * The bit 22/21 of 0x42004
8009 * The bit 5 of 0x42020
8010 * The bit 15 of 0x45000
8011 */
8012 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8013 (I915_READ(ILK_DISPLAY_CHICKEN2) |
8014 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008015 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008016 I915_WRITE(DISP_ARB_CTL,
8017 (I915_READ(DISP_ARB_CTL) |
8018 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02008019
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008020 /*
8021 * Based on the document from hardware guys the following bits
8022 * should be set unconditionally in order to enable FBC.
8023 * The bit 22 of 0x42000
8024 * The bit 22 of 0x42004
8025 * The bit 7,8,9 of 0x42020.
8026 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008027 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01008028 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008029 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8030 I915_READ(ILK_DISPLAY_CHICKEN1) |
8031 ILK_FBCQ_DIS);
8032 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8033 I915_READ(ILK_DISPLAY_CHICKEN2) |
8034 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008035 }
8036
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008037 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
8038
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008039 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8040 I915_READ(ILK_DISPLAY_CHICKEN2) |
8041 ILK_ELPIN_409_SELECT);
8042 I915_WRITE(_3D_CHICKEN2,
8043 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
8044 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02008045
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008046 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02008047 I915_WRITE(CACHE_MODE_0,
8048 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01008049
Akash Goel4e046322014-04-04 17:14:38 +05308050 /* WaDisable_RenderCache_OperationalFlush:ilk */
8051 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8052
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008053 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03008054
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008055 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008056}
8057
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008058static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01008059{
Ville Syrjäläd048a262019-08-21 20:30:31 +03008060 enum pipe pipe;
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008061 u32 val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01008062
8063 /*
8064 * On Ibex Peak and Cougar Point, we need to disable clock
8065 * gating for the panel power sequencer or it will fail to
8066 * start up when no ports are active.
8067 */
Jesse Barnescd664072013-10-02 10:34:19 -07008068 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
8069 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
8070 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008071 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
8072 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01008073 /* The below fixes the weird display corruption, a few pixels shifted
8074 * downward, on (only) LVDS of some HP laptops with IVY.
8075 */
Damien Lespiau055e3932014-08-18 13:49:10 +01008076 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03008077 val = I915_READ(TRANS_CHICKEN2(pipe));
8078 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
8079 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008080 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008081 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03008082 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
8083 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
8084 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008085 I915_WRITE(TRANS_CHICKEN2(pipe), val);
8086 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01008087 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01008088 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01008089 I915_WRITE(TRANS_CHICKEN1(pipe),
8090 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
8091 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008092}
8093
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008094static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008095{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008096 u32 tmp;
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008097
8098 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02008099 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
8100 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
8101 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008102}
8103
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008104static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008105{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008106 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008107
Damien Lespiau231e54f2012-10-19 17:55:41 +01008108 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008109
8110 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8111 I915_READ(ILK_DISPLAY_CHICKEN2) |
8112 ILK_ELPIN_409_SELECT);
8113
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008114 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01008115 I915_WRITE(_3D_CHICKEN,
8116 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
8117
Akash Goel4e046322014-04-04 17:14:38 +05308118 /* WaDisable_RenderCache_OperationalFlush:snb */
8119 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8120
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008121 /*
8122 * BSpec recoomends 8x4 when MSAA is used,
8123 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008124 *
8125 * Note that PS/WM thread counts depend on the WIZ hashing
8126 * disable bit, which we don't touch here, but it's good
8127 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008128 */
8129 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008130 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008131
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008132 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02008133 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008134
8135 I915_WRITE(GEN6_UCGCTL1,
8136 I915_READ(GEN6_UCGCTL1) |
8137 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
8138 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
8139
8140 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
8141 * gating disable must be set. Failure to set it results in
8142 * flickering pixels due to Z write ordering failures after
8143 * some amount of runtime in the Mesa "fire" demo, and Unigine
8144 * Sanctuary and Tropics, and apparently anything else with
8145 * alpha test or pixel discard.
8146 *
8147 * According to the spec, bit 11 (RCCUNIT) must also be set,
8148 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008149 *
Ville Syrjäläef593182014-01-22 21:32:47 +02008150 * WaDisableRCCUnitClockGating:snb
8151 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008152 */
8153 I915_WRITE(GEN6_UCGCTL2,
8154 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
8155 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
8156
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02008157 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02008158 I915_WRITE(_3D_CHICKEN3,
8159 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008160
8161 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02008162 * Bspec says:
8163 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
8164 * 3DSTATE_SF number of SF output attributes is more than 16."
8165 */
8166 I915_WRITE(_3D_CHICKEN3,
8167 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
8168
8169 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008170 * According to the spec the following bits should be
8171 * set in order to enable memory self-refresh and fbc:
8172 * The bit21 and bit22 of 0x42000
8173 * The bit21 and bit22 of 0x42004
8174 * The bit5 and bit7 of 0x42020
8175 * The bit14 of 0x70180
8176 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01008177 *
8178 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008179 */
8180 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8181 I915_READ(ILK_DISPLAY_CHICKEN1) |
8182 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
8183 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8184 I915_READ(ILK_DISPLAY_CHICKEN2) |
8185 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01008186 I915_WRITE(ILK_DSPCLK_GATE_D,
8187 I915_READ(ILK_DSPCLK_GATE_D) |
8188 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
8189 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008190
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008191 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07008192
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008193 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008194
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008195 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008196}
8197
8198static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
8199{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008200 u32 reg = I915_READ(GEN7_FF_THREAD_MODE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008201
Ville Syrjälä3aad9052014-01-22 21:32:59 +02008202 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02008203 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02008204 *
8205 * This actually overrides the dispatch
8206 * mode for all thread types.
8207 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008208 reg &= ~GEN7_FF_SCHED_MASK;
8209 reg |= GEN7_FF_TS_SCHED_HW;
8210 reg |= GEN7_FF_VS_SCHED_HW;
8211 reg |= GEN7_FF_DS_SCHED_HW;
8212
8213 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
8214}
8215
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008216static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008217{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008218 /*
8219 * TODO: this bit should only be enabled when really needed, then
8220 * disabled when not needed anymore in order to save power.
8221 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008222 if (HAS_PCH_LPT_LP(dev_priv))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008223 I915_WRITE(SOUTH_DSPCLK_GATE_D,
8224 I915_READ(SOUTH_DSPCLK_GATE_D) |
8225 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03008226
8227 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03008228 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
8229 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03008230 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008231}
8232
Ville Syrjälä712bf362016-10-31 22:37:23 +02008233static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03008234{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008235 if (HAS_PCH_LPT_LP(dev_priv)) {
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008236 u32 val = I915_READ(SOUTH_DSPCLK_GATE_D);
Imre Deak7d708ee2013-04-17 14:04:50 +03008237
8238 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8239 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8240 }
8241}
8242
Imre Deak450174f2016-05-03 15:54:21 +03008243static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
8244 int general_prio_credits,
8245 int high_prio_credits)
8246{
8247 u32 misccpctl;
Oscar Mateo930a7842017-10-17 13:25:45 -07008248 u32 val;
Imre Deak450174f2016-05-03 15:54:21 +03008249
8250 /* WaTempDisableDOPClkGating:bdw */
8251 misccpctl = I915_READ(GEN7_MISCCPCTL);
8252 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
8253
Oscar Mateo930a7842017-10-17 13:25:45 -07008254 val = I915_READ(GEN8_L3SQCREG1);
8255 val &= ~L3_PRIO_CREDITS_MASK;
8256 val |= L3_GENERAL_PRIO_CREDITS(general_prio_credits);
8257 val |= L3_HIGH_PRIO_CREDITS(high_prio_credits);
8258 I915_WRITE(GEN8_L3SQCREG1, val);
Imre Deak450174f2016-05-03 15:54:21 +03008259
8260 /*
8261 * Wait at least 100 clocks before re-enabling clock gating.
8262 * See the definition of L3SQCREG1 in BSpec.
8263 */
8264 POSTING_READ(GEN8_L3SQCREG1);
8265 udelay(1);
8266 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
8267}
8268
Oscar Mateod65dc3e2018-05-08 14:29:24 -07008269static void icl_init_clock_gating(struct drm_i915_private *dev_priv)
8270{
8271 /* This is not an Wa. Enable to reduce Sampler power */
8272 I915_WRITE(GEN10_DFR_RATIO_EN_AND_CHICKEN,
8273 I915_READ(GEN10_DFR_RATIO_EN_AND_CHICKEN) & ~DFR_DISABLE);
Radhakrishna Sripada622b3f62018-10-30 01:45:01 -07008274
8275 /* WaEnable32PlaneMode:icl */
8276 I915_WRITE(GEN9_CSFE_CHICKEN1_RCS,
8277 _MASKED_BIT_ENABLE(GEN11_ENABLE_32_PLANE_MODE));
Oscar Mateod65dc3e2018-05-08 14:29:24 -07008278}
8279
Michel Thierry5d869232019-08-23 01:20:34 -07008280static void tgl_init_clock_gating(struct drm_i915_private *dev_priv)
8281{
8282 u32 vd_pg_enable = 0;
8283 unsigned int i;
8284
8285 /* This is not a WA. Enable VD HCP & MFX_ENC powergate */
8286 for (i = 0; i < I915_MAX_VCS; i++) {
8287 if (HAS_ENGINE(dev_priv, _VCS(i)))
8288 vd_pg_enable |= VDN_HCP_POWERGATE_ENABLE(i) |
8289 VDN_MFX_POWERGATE_ENABLE(i);
8290 }
8291
8292 I915_WRITE(POWERGATE_ENABLE,
8293 I915_READ(POWERGATE_ENABLE) | vd_pg_enable);
8294}
8295
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008296static void cnp_init_clock_gating(struct drm_i915_private *dev_priv)
8297{
8298 if (!HAS_PCH_CNP(dev_priv))
8299 return;
8300
Rodrigo Vivi470e7c62018-03-05 17:28:12 -08008301 /* Display WA #1181 WaSouthDisplayDisablePWMCGEGating: cnp */
Rodrigo Vivi4cc6feb2017-09-08 16:45:33 -07008302 I915_WRITE(SOUTH_DSPCLK_GATE_D, I915_READ(SOUTH_DSPCLK_GATE_D) |
8303 CNP_PWM_CGE_GATING_DISABLE);
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008304}
8305
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008306static void cnl_init_clock_gating(struct drm_i915_private *dev_priv)
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008307{
Rodrigo Vivi8f067832017-09-05 12:30:13 -07008308 u32 val;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008309 cnp_init_clock_gating(dev_priv);
8310
Rodrigo Vivi1a25db62017-08-15 16:16:51 -07008311 /* This is not an Wa. Enable for better image quality */
8312 I915_WRITE(_3D_CHICKEN3,
8313 _MASKED_BIT_ENABLE(_3D_CHICKEN3_AA_LINE_QUALITY_FIX_ENABLE));
8314
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008315 /* WaEnableChickenDCPR:cnl */
8316 I915_WRITE(GEN8_CHICKEN_DCPR_1,
8317 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
8318
8319 /* WaFbcWakeMemOn:cnl */
8320 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
8321 DISP_FBC_MEMORY_WAKE);
8322
Chris Wilson34991bd2017-11-11 10:03:36 +00008323 val = I915_READ(SLICE_UNIT_LEVEL_CLKGATE);
8324 /* ReadHitWriteOnlyDisable:cnl */
8325 val |= RCCUNIT_CLKGATE_DIS;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008326 /* WaSarbUnitClockGatingDisable:cnl (pre-prod) */
8327 if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_B0))
Chris Wilson34991bd2017-11-11 10:03:36 +00008328 val |= SARBUNIT_CLKGATE_DIS;
8329 I915_WRITE(SLICE_UNIT_LEVEL_CLKGATE, val);
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08008330
Rodrigo Vivia4713c52018-03-07 14:09:12 -08008331 /* Wa_2201832410:cnl */
8332 val = I915_READ(SUBSLICE_UNIT_LEVEL_CLKGATE);
8333 val |= GWUNIT_CLKGATE_DIS;
8334 I915_WRITE(SUBSLICE_UNIT_LEVEL_CLKGATE, val);
8335
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08008336 /* WaDisableVFclkgate:cnl */
Rodrigo Vivi14941b62018-03-05 17:20:00 -08008337 /* WaVFUnitClockGatingDisable:cnl */
Rafael Antognolli01ab0f92017-12-15 16:11:16 -08008338 val = I915_READ(UNSLICE_UNIT_LEVEL_CLKGATE);
8339 val |= VFUNIT_CLKGATE_DIS;
8340 I915_WRITE(UNSLICE_UNIT_LEVEL_CLKGATE, val);
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008341}
8342
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008343static void cfl_init_clock_gating(struct drm_i915_private *dev_priv)
8344{
8345 cnp_init_clock_gating(dev_priv);
8346 gen9_init_clock_gating(dev_priv);
8347
8348 /* WaFbcNukeOnHostModify:cfl */
8349 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8350 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
8351}
8352
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008353static void kbl_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008354{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008355 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008356
8357 /* WaDisableSDEUnitClockGating:kbl */
8358 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
8359 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8360 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03008361
8362 /* WaDisableGamClockGating:kbl */
8363 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
8364 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
8365 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008366
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008367 /* WaFbcNukeOnHostModify:kbl */
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008368 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8369 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008370}
8371
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008372static void skl_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008373{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008374 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03008375
8376 /* WAC6entrylatency:skl */
8377 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
8378 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008379
8380 /* WaFbcNukeOnHostModify:skl */
8381 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8382 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008383}
8384
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008385static void bdw_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008386{
Damien Lespiau07d27e22014-03-03 17:31:46 +00008387 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008388
Ben Widawskyab57fff2013-12-12 15:28:04 -08008389 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07008390 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008391
Ben Widawskyab57fff2013-12-12 15:28:04 -08008392 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008393 I915_WRITE(CHICKEN_PAR1_1,
8394 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
8395
Ben Widawskyab57fff2013-12-12 15:28:04 -08008396 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01008397 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00008398 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02008399 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02008400 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008401 }
Ben Widawsky63801f22013-12-12 17:26:03 -08008402
Ben Widawskyab57fff2013-12-12 15:28:04 -08008403 /* WaVSRefCountFullforceMissDisable:bdw */
8404 /* WaDSRefCountFullforceMissDisable:bdw */
8405 I915_WRITE(GEN7_FF_THREAD_MODE,
8406 I915_READ(GEN7_FF_THREAD_MODE) &
8407 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02008408
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02008409 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
8410 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02008411
8412 /* WaDisableSDEUnitClockGating:bdw */
8413 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8414 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00008415
Imre Deak450174f2016-05-03 15:54:21 +03008416 /* WaProgramL3SqcReg1Default:bdw */
8417 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03008418
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03008419 /* WaKVMNotificationOnConfigChange:bdw */
8420 I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
8421 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
8422
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008423 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00008424
8425 /* WaDisableDopClockGating:bdw
8426 *
8427 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
8428 * clock gating.
8429 */
8430 I915_WRITE(GEN6_UCGCTL1,
8431 I915_READ(GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008432}
8433
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008434static void hsw_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008435{
Francisco Jerezf3fc4882013-10-02 15:53:16 -07008436 /* L3 caching of data atomics doesn't work -- disable it. */
8437 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
8438 I915_WRITE(HSW_ROW_CHICKEN3,
8439 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
8440
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008441 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008442 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8443 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8444 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8445
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02008446 /* WaVSRefCountFullforceMissDisable:hsw */
8447 I915_WRITE(GEN7_FF_THREAD_MODE,
8448 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008449
Akash Goel4e046322014-04-04 17:14:38 +05308450 /* WaDisable_RenderCache_OperationalFlush:hsw */
8451 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8452
Chia-I Wufe27c602014-01-28 13:29:33 +08008453 /* enable HiZ Raw Stall Optimization */
8454 I915_WRITE(CACHE_MODE_0_GEN7,
8455 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
8456
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008457 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008458 I915_WRITE(CACHE_MODE_1,
8459 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03008460
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008461 /*
8462 * BSpec recommends 8x4 when MSAA is used,
8463 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008464 *
8465 * Note that PS/WM thread counts depend on the WIZ hashing
8466 * disable bit, which we don't touch here, but it's good
8467 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008468 */
8469 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008470 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008471
Kenneth Graunke94411592014-12-31 16:23:00 -08008472 /* WaSampleCChickenBitEnable:hsw */
8473 I915_WRITE(HALF_SLICE_CHICKEN3,
8474 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
8475
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008476 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07008477 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
8478
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008479 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008480}
8481
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008482static void ivb_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008483{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008484 u32 snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008485
Damien Lespiau231e54f2012-10-19 17:55:41 +01008486 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008487
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008488 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05008489 I915_WRITE(_3D_CHICKEN3,
8490 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
8491
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008492 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008493 I915_WRITE(IVB_CHICKEN3,
8494 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8495 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8496
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008497 /* WaDisablePSDDualDispatchEnable:ivb */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008498 if (IS_IVB_GT1(dev_priv))
Jesse Barnes12f33822012-10-25 12:15:45 -07008499 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
8500 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07008501
Akash Goel4e046322014-04-04 17:14:38 +05308502 /* WaDisable_RenderCache_OperationalFlush:ivb */
8503 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8504
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008505 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008506 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
8507 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
8508
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008509 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008510 I915_WRITE(GEN7_L3CNTLREG1,
8511 GEN7_WA_FOR_GEN7_L3_CONTROL);
8512 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07008513 GEN7_WA_L3_CHICKEN_MODE);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008514 if (IS_IVB_GT1(dev_priv))
Jesse Barnes8ab43972012-10-25 12:15:42 -07008515 I915_WRITE(GEN7_ROW_CHICKEN2,
8516 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02008517 else {
8518 /* must write both registers */
8519 I915_WRITE(GEN7_ROW_CHICKEN2,
8520 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07008521 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
8522 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02008523 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008524
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008525 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05008526 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
8527 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
8528
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02008529 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07008530 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008531 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008532 */
8533 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02008534 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07008535
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008536 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008537 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8538 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8539 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8540
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008541 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008542
8543 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02008544
Chris Wilson22721342014-03-04 09:41:43 +00008545 if (0) { /* causes HiZ corruption on ivb:gt1 */
8546 /* enable HiZ Raw Stall Optimization */
8547 I915_WRITE(CACHE_MODE_0_GEN7,
8548 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
8549 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08008550
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008551 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02008552 I915_WRITE(CACHE_MODE_1,
8553 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07008554
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008555 /*
8556 * BSpec recommends 8x4 when MSAA is used,
8557 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008558 *
8559 * Note that PS/WM thread counts depend on the WIZ hashing
8560 * disable bit, which we don't touch here, but it's good
8561 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008562 */
8563 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008564 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008565
Ben Widawsky20848222012-05-04 18:58:59 -07008566 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
8567 snpcr &= ~GEN6_MBC_SNPCR_MASK;
8568 snpcr |= GEN6_MBC_SNPCR_MED;
8569 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008570
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008571 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008572 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008573
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008574 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008575}
8576
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008577static void vlv_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008578{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008579 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05008580 I915_WRITE(_3D_CHICKEN3,
8581 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
8582
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008583 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008584 I915_WRITE(IVB_CHICKEN3,
8585 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8586 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8587
Ville Syrjäläfad7d362014-01-22 21:32:39 +02008588 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008589 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07008590 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08008591 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
8592 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07008593
Akash Goel4e046322014-04-04 17:14:38 +05308594 /* WaDisable_RenderCache_OperationalFlush:vlv */
8595 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8596
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008597 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05008598 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
8599 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
8600
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008601 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07008602 I915_WRITE(GEN7_ROW_CHICKEN2,
8603 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
8604
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008605 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008606 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8607 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8608 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8609
Ville Syrjälä46680e02014-01-22 21:33:01 +02008610 gen7_setup_fixed_func_scheduler(dev_priv);
8611
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02008612 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07008613 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008614 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008615 */
8616 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02008617 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07008618
Akash Goelc98f5062014-03-24 23:00:07 +05308619 /* WaDisableL3Bank2xClockGate:vlv
8620 * Disabling L3 clock gating- MMIO 940c[25] = 1
8621 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
8622 I915_WRITE(GEN7_UCGCTL4,
8623 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07008624
Ville Syrjäläafd58e72014-01-22 21:33:03 +02008625 /*
8626 * BSpec says this must be set, even though
8627 * WaDisable4x2SubspanOptimization isn't listed for VLV.
8628 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02008629 I915_WRITE(CACHE_MODE_1,
8630 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07008631
8632 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02008633 * BSpec recommends 8x4 when MSAA is used,
8634 * however in practice 16x4 seems fastest.
8635 *
8636 * Note that PS/WM thread counts depend on the WIZ hashing
8637 * disable bit, which we don't touch here, but it's good
8638 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
8639 */
8640 I915_WRITE(GEN7_GT_MODE,
8641 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
8642
8643 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02008644 * WaIncreaseL3CreditsForVLVB0:vlv
8645 * This is the hardware default actually.
8646 */
8647 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
8648
8649 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008650 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07008651 * Disable clock gating on th GCFG unit to prevent a delay
8652 * in the reporting of vblank events.
8653 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02008654 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008655}
8656
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008657static void chv_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03008658{
Ville Syrjälä232ce332014-04-09 13:28:35 +03008659 /* WaVSRefCountFullforceMissDisable:chv */
8660 /* WaDSRefCountFullforceMissDisable:chv */
8661 I915_WRITE(GEN7_FF_THREAD_MODE,
8662 I915_READ(GEN7_FF_THREAD_MODE) &
8663 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03008664
8665 /* WaDisableSemaphoreAndSyncFlipWait:chv */
8666 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
8667 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03008668
8669 /* WaDisableCSUnitClockGating:chv */
8670 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
8671 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03008672
8673 /* WaDisableSDEUnitClockGating:chv */
8674 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8675 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008676
8677 /*
Imre Deak450174f2016-05-03 15:54:21 +03008678 * WaProgramL3SqcReg1Default:chv
8679 * See gfxspecs/Related Documents/Performance Guide/
8680 * LSQC Setting Recommendations.
8681 */
8682 gen8_set_l3sqc_credits(dev_priv, 38, 2);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03008683}
8684
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008685static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008686{
Jani Nikula5ce9a6492019-01-18 14:01:20 +02008687 u32 dspclk_gate;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008688
8689 I915_WRITE(RENCLK_GATE_D1, 0);
8690 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
8691 GS_UNIT_CLOCK_GATE_DISABLE |
8692 CL_UNIT_CLOCK_GATE_DISABLE);
8693 I915_WRITE(RAMCLK_GATE_D, 0);
8694 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
8695 OVRUNIT_CLOCK_GATE_DISABLE |
8696 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008697 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008698 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
8699 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02008700
8701 /* WaDisableRenderCachePipelinedFlush */
8702 I915_WRITE(CACHE_MODE_0,
8703 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03008704
Akash Goel4e046322014-04-04 17:14:38 +05308705 /* WaDisable_RenderCache_OperationalFlush:g4x */
8706 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8707
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008708 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008709}
8710
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008711static void i965gm_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008712{
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01008713 struct intel_uncore *uncore = &dev_priv->uncore;
8714
8715 intel_uncore_write(uncore, RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
8716 intel_uncore_write(uncore, RENCLK_GATE_D2, 0);
8717 intel_uncore_write(uncore, DSPCLK_GATE_D, 0);
8718 intel_uncore_write(uncore, RAMCLK_GATE_D, 0);
8719 intel_uncore_write16(uncore, DEUC, 0);
8720 intel_uncore_write(uncore,
8721 MI_ARB_STATE,
8722 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05308723
8724 /* WaDisable_RenderCache_OperationalFlush:gen4 */
Tvrtko Ursulin4f5fd912019-06-11 11:45:48 +01008725 intel_uncore_write(uncore,
8726 CACHE_MODE_0,
8727 _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008728}
8729
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008730static void i965g_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008731{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008732 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
8733 I965_RCC_CLOCK_GATE_DISABLE |
8734 I965_RCPB_CLOCK_GATE_DISABLE |
8735 I965_ISC_CLOCK_GATE_DISABLE |
8736 I965_FBC_CLOCK_GATE_DISABLE);
8737 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03008738 I915_WRITE(MI_ARB_STATE,
8739 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05308740
8741 /* WaDisable_RenderCache_OperationalFlush:gen4 */
8742 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008743}
8744
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008745static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008746{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008747 u32 dstate = I915_READ(D_STATE);
8748
8749 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
8750 DSTATE_DOT_CLOCK_GATING;
8751 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01008752
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008753 if (IS_PINEVIEW(dev_priv))
Chris Wilson13a86b82012-04-24 14:51:43 +01008754 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02008755
8756 /* IIR "flip pending" means done if this bit is set */
8757 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02008758
8759 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02008760 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02008761
8762 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
8763 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03008764
8765 I915_WRITE(MI_ARB_STATE,
8766 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008767}
8768
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008769static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008770{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008771 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02008772
8773 /* interrupts should cause a wake up from C3 */
8774 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
8775 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03008776
8777 I915_WRITE(MEM_MODE,
8778 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008779}
8780
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008781static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008782{
Ville Syrjälä10383922014-08-15 01:21:54 +03008783 I915_WRITE(MEM_MODE,
8784 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
8785 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008786}
8787
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008788void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008789{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008790 dev_priv->display.init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008791}
8792
Ville Syrjälä712bf362016-10-31 22:37:23 +02008793void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03008794{
Ville Syrjälä712bf362016-10-31 22:37:23 +02008795 if (HAS_PCH_LPT(dev_priv))
8796 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03008797}
8798
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008799static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02008800{
8801 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
8802}
8803
8804/**
8805 * intel_init_clock_gating_hooks - setup the clock gating hooks
8806 * @dev_priv: device private
8807 *
8808 * Setup the hooks that configure which clocks of a given platform can be
8809 * gated and also apply various GT and display specific workarounds for these
8810 * platforms. Note that some GT specific workarounds are applied separately
8811 * when GPU contexts or batchbuffers start their execution.
8812 */
8813void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
8814{
Lucas De Marchi13e53c52019-08-17 02:38:42 -07008815 if (IS_GEN(dev_priv, 12))
Michel Thierry5d869232019-08-23 01:20:34 -07008816 dev_priv->display.init_clock_gating = tgl_init_clock_gating;
Lucas De Marchi13e53c52019-08-17 02:38:42 -07008817 else if (IS_GEN(dev_priv, 11))
Oscar Mateod65dc3e2018-05-08 14:29:24 -07008818 dev_priv->display.init_clock_gating = icl_init_clock_gating;
Oscar Mateocc38cae2018-05-08 14:29:23 -07008819 else if (IS_CANNONLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008820 dev_priv->display.init_clock_gating = cnl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008821 else if (IS_COFFEELAKE(dev_priv))
8822 dev_priv->display.init_clock_gating = cfl_init_clock_gating;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008823 else if (IS_SKYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008824 dev_priv->display.init_clock_gating = skl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008825 else if (IS_KABYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008826 dev_priv->display.init_clock_gating = kbl_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02008827 else if (IS_BROXTON(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02008828 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02008829 else if (IS_GEMINILAKE(dev_priv))
8830 dev_priv->display.init_clock_gating = glk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008831 else if (IS_BROADWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008832 dev_priv->display.init_clock_gating = bdw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008833 else if (IS_CHERRYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008834 dev_priv->display.init_clock_gating = chv_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008835 else if (IS_HASWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008836 dev_priv->display.init_clock_gating = hsw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008837 else if (IS_IVYBRIDGE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008838 dev_priv->display.init_clock_gating = ivb_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008839 else if (IS_VALLEYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008840 dev_priv->display.init_clock_gating = vlv_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008841 else if (IS_GEN(dev_priv, 6))
Imre Deakbb400da2016-03-16 13:38:54 +02008842 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008843 else if (IS_GEN(dev_priv, 5))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008844 dev_priv->display.init_clock_gating = ilk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008845 else if (IS_G4X(dev_priv))
8846 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02008847 else if (IS_I965GM(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008848 dev_priv->display.init_clock_gating = i965gm_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02008849 else if (IS_I965G(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008850 dev_priv->display.init_clock_gating = i965g_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008851 else if (IS_GEN(dev_priv, 3))
Imre Deakbb400da2016-03-16 13:38:54 +02008852 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
8853 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
8854 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008855 else if (IS_GEN(dev_priv, 2))
Imre Deakbb400da2016-03-16 13:38:54 +02008856 dev_priv->display.init_clock_gating = i830_init_clock_gating;
8857 else {
8858 MISSING_CASE(INTEL_DEVID(dev_priv));
8859 dev_priv->display.init_clock_gating = nop_init_clock_gating;
8860 }
8861}
8862
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008863/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008864void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008865{
Daniel Vetterc921aba2012-04-26 23:28:17 +02008866 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008867 if (IS_PINEVIEW(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02008868 i915_pineview_get_mem_freq(dev_priv);
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008869 else if (IS_GEN(dev_priv, 5))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02008870 i915_ironlake_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02008871
James Ausmusb068a862019-10-09 10:23:14 -07008872 if (intel_has_sagv(dev_priv))
8873 skl_setup_sagv_block_time(dev_priv);
8874
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008875 /* For FIFO watermark updates */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008876 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008877 skl_setup_wm_latency(dev_priv);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01008878 dev_priv->display.initial_watermarks = skl_initial_wm;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01008879 dev_priv->display.atomic_update_watermarks = skl_atomic_update_crtc_wm;
Matt Roper98d39492016-05-12 07:06:03 -07008880 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008881 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008882 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03008883
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008884 if ((IS_GEN(dev_priv, 5) && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008885 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008886 (!IS_GEN(dev_priv, 5) && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008887 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07008888 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08008889 dev_priv->display.compute_intermediate_wm =
8890 ilk_compute_intermediate_wm;
8891 dev_priv->display.initial_watermarks =
8892 ilk_initial_watermarks;
8893 dev_priv->display.optimize_watermarks =
8894 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02008895 } else {
8896 DRM_DEBUG_KMS("Failed to read display plane latency. "
8897 "Disable CxSR\n");
8898 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02008899 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008900 vlv_setup_wm_latency(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02008901 dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
Ville Syrjälä4841da52017-03-02 19:14:59 +02008902 dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02008903 dev_priv->display.initial_watermarks = vlv_initial_watermarks;
Ville Syrjälä4841da52017-03-02 19:14:59 +02008904 dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
Ville Syrjäläff32c542017-03-02 19:14:57 +02008905 dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03008906 } else if (IS_G4X(dev_priv)) {
8907 g4x_setup_wm_latency(dev_priv);
8908 dev_priv->display.compute_pipe_wm = g4x_compute_pipe_wm;
8909 dev_priv->display.compute_intermediate_wm = g4x_compute_intermediate_wm;
8910 dev_priv->display.initial_watermarks = g4x_initial_watermarks;
8911 dev_priv->display.optimize_watermarks = g4x_optimize_watermarks;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008912 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin86d35d42019-03-26 07:40:54 +00008913 if (!intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008914 dev_priv->is_ddr3,
8915 dev_priv->fsb_freq,
8916 dev_priv->mem_freq)) {
8917 DRM_INFO("failed to find known CxSR latency "
8918 "(found ddr%s fsb freq %d, mem freq %d), "
8919 "disabling CxSR\n",
8920 (dev_priv->is_ddr3 == 1) ? "3" : "2",
8921 dev_priv->fsb_freq, dev_priv->mem_freq);
8922 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03008923 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008924 dev_priv->display.update_wm = NULL;
8925 } else
8926 dev_priv->display.update_wm = pineview_update_wm;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008927 } else if (IS_GEN(dev_priv, 4)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008928 dev_priv->display.update_wm = i965_update_wm;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008929 } else if (IS_GEN(dev_priv, 3)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008930 dev_priv->display.update_wm = i9xx_update_wm;
8931 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Lucas De Marchicf819ef2018-12-12 10:10:43 -08008932 } else if (IS_GEN(dev_priv, 2)) {
Jani Nikula24977872019-09-11 12:26:08 +03008933 if (INTEL_NUM_PIPES(dev_priv) == 1) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008934 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008935 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008936 } else {
8937 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008938 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008939 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008940 } else {
8941 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008942 }
8943}
8944
Ville Syrjälädd06f882014-11-10 22:55:12 +02008945static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
8946{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01008947 struct intel_rps *rps = &dev_priv->gt_pm.rps;
8948
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008949 /*
8950 * N = val - 0xb7
8951 * Slow = Fast = GPLL ref * N
8952 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01008953 return DIV_ROUND_CLOSEST(rps->gpll_ref_freq * (val - 0xb7), 1000);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07008954}
8955
Fengguang Wub55dd642014-07-12 11:21:39 +02008956static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07008957{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01008958 struct intel_rps *rps = &dev_priv->gt_pm.rps;
8959
8960 return DIV_ROUND_CLOSEST(1000 * val, rps->gpll_ref_freq) + 0xb7;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07008961}
8962
Fengguang Wub55dd642014-07-12 11:21:39 +02008963static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05308964{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01008965 struct intel_rps *rps = &dev_priv->gt_pm.rps;
8966
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008967 /*
8968 * N = val / 2
8969 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
8970 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01008971 return DIV_ROUND_CLOSEST(rps->gpll_ref_freq * val, 2 * 2 * 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05308972}
8973
Fengguang Wub55dd642014-07-12 11:21:39 +02008974static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05308975{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01008976 struct intel_rps *rps = &dev_priv->gt_pm.rps;
8977
Ville Syrjälä1c147622014-08-18 14:42:43 +03008978 /* CHV needs even values */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01008979 return DIV_ROUND_CLOSEST(2 * 1000 * val, rps->gpll_ref_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05308980}
8981
Ville Syrjälä616bc822015-01-23 21:04:25 +02008982int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
8983{
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07008984 if (INTEL_GEN(dev_priv) >= 9)
Mika Kuoppala500a3d22015-11-13 19:29:41 +02008985 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
8986 GEN9_FREQ_SCALER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008987 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008988 return chv_gpu_freq(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008989 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008990 return byt_gpu_freq(dev_priv, val);
8991 else
8992 return val * GT_FREQUENCY_MULTIPLIER;
8993}
8994
Ville Syrjälä616bc822015-01-23 21:04:25 +02008995int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
8996{
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07008997 if (INTEL_GEN(dev_priv) >= 9)
Mika Kuoppala500a3d22015-11-13 19:29:41 +02008998 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
8999 GT_FREQUENCY_MULTIPLIER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009000 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009001 return chv_freq_opcode(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009002 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009003 return byt_freq_opcode(dev_priv, val);
9004 else
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009005 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
Deepak S22b1b2f2014-07-12 14:54:33 +05309006}
9007
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00009008void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01009009{
Chris Wilsonebb5eb72019-04-26 09:17:21 +01009010 mutex_init(&dev_priv->gt_pm.rps.lock);
Chris Wilson60548c52018-07-31 14:26:29 +01009011 mutex_init(&dev_priv->gt_pm.rps.power.mutex);
Daniel Vetterf742a552013-12-06 10:17:53 +01009012
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009013 atomic_set(&dev_priv->gt_pm.rps.num_waiters, 0);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03009014
Sagar Arun Kamblead1443f2017-10-10 22:30:04 +01009015 dev_priv->runtime_pm.suspended = false;
9016 atomic_set(&dev_priv->runtime_pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01009017}
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009018
Tvrtko Ursulinc84b2702017-11-21 18:18:44 +00009019u32 intel_get_cagf(struct drm_i915_private *dev_priv, u32 rpstat)
9020{
9021 u32 cagf;
9022
9023 if (INTEL_GEN(dev_priv) >= 9)
9024 cagf = (rpstat & GEN9_CAGF_MASK) >> GEN9_CAGF_SHIFT;
9025 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
9026 cagf = (rpstat & HSW_CAGF_MASK) >> HSW_CAGF_SHIFT;
9027 else
9028 cagf = (rpstat & GEN6_CAGF_MASK) >> GEN6_CAGF_SHIFT;
9029
9030 return cagf;
9031}