blob: a80c322c5b43eafdfc9201948a98c62c2c13b268 [file] [log] [blame]
Eugeni Dodonov85208be2012-04-16 22:20:34 -03001/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eugeni Dodonov <eugeni.dodonov@intel.com>
25 *
26 */
27
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -030028#include <linux/cpufreq.h>
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -070029#include <drm/drm_plane_helper.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030030#include "i915_drv.h"
31#include "intel_drv.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020032#include "../../../platform/x86/intel_ips.h"
33#include <linux/module.h>
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +020034#include <drm/drm_atomic_helper.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030035
Ben Widawskydc39fff2013-10-18 12:32:07 -070036/**
Jani Nikula18afd442016-01-18 09:19:48 +020037 * DOC: RC6
38 *
Ben Widawskydc39fff2013-10-18 12:32:07 -070039 * RC6 is a special power stage which allows the GPU to enter an very
40 * low-voltage mode when idle, using down to 0V while at this stage. This
41 * stage is entered automatically when the GPU is idle when RC6 support is
42 * enabled, and as soon as new workload arises GPU wakes up automatically as well.
43 *
44 * There are different RC6 modes available in Intel GPU, which differentiate
45 * among each other with the latency required to enter and leave RC6 and
46 * voltage consumed by the GPU in different states.
47 *
48 * The combination of the following flags define which states GPU is allowed
49 * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
50 * RC6pp is deepest RC6. Their support by hardware varies according to the
51 * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
52 * which brings the most power savings; deeper states save more power, but
53 * require higher latency to switch to and wake up.
54 */
55#define INTEL_RC6_ENABLE (1<<0)
56#define INTEL_RC6p_ENABLE (1<<1)
57#define INTEL_RC6pp_ENABLE (1<<2)
58
Ville Syrjälä46f16e62016-10-31 22:37:22 +020059static void gen9_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppalab033bb62016-06-07 17:19:04 +030060{
Ville Syrjälä93564042017-08-24 22:10:51 +030061 if (HAS_LLC(dev_priv)) {
62 /*
63 * WaCompressedResourceDisplayNewHashMode:skl,kbl
64 * Display WA#0390: skl,kbl
65 *
66 * Must match Sampler, Pixel Back End, and Media. See
67 * WaCompressedResourceSamplerPbeMediaNewHashMode.
68 */
69 I915_WRITE(CHICKEN_PAR1_1,
70 I915_READ(CHICKEN_PAR1_1) |
71 SKL_DE_COMPRESSED_HASH_MODE);
72 }
73
Rodrigo Vivi82525c12017-06-08 08:50:00 -070074 /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl,cfl */
Mika Kuoppalab033bb62016-06-07 17:19:04 +030075 I915_WRITE(CHICKEN_PAR1_1,
76 I915_READ(CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
77
Rodrigo Vivi82525c12017-06-08 08:50:00 -070078 /* WaEnableChickenDCPR:skl,bxt,kbl,glk,cfl */
Mika Kuoppala590e8ff2016-06-07 17:19:13 +030079 I915_WRITE(GEN8_CHICKEN_DCPR_1,
80 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
Mika Kuoppala0f78dee2016-06-07 17:19:16 +030081
Rodrigo Vivi82525c12017-06-08 08:50:00 -070082 /* WaFbcTurnOffFbcWatermark:skl,bxt,kbl,cfl */
83 /* WaFbcWakeMemOn:skl,bxt,kbl,glk,cfl */
Mika Kuoppala303d4ea2016-06-07 17:19:17 +030084 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
85 DISP_FBC_WM_DIS |
86 DISP_FBC_MEMORY_WAKE);
Mika Kuoppalad1b4eef2016-06-07 17:19:19 +030087
Rodrigo Vivi82525c12017-06-08 08:50:00 -070088 /* WaFbcHighMemBwCorruptionAvoidance:skl,bxt,kbl,cfl */
Mika Kuoppalad1b4eef2016-06-07 17:19:19 +030089 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
90 ILK_DPFC_DISABLE_DUMMY0);
Praveen Paneri32087d12017-08-03 23:02:10 +053091
92 if (IS_SKYLAKE(dev_priv)) {
93 /* WaDisableDopClockGating */
94 I915_WRITE(GEN7_MISCCPCTL, I915_READ(GEN7_MISCCPCTL)
95 & ~GEN7_DOP_CLOCK_GATE_ENABLE);
96 }
Mika Kuoppalab033bb62016-06-07 17:19:04 +030097}
98
Ville Syrjälä46f16e62016-10-31 22:37:22 +020099static void bxt_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deaka82abe42015-03-27 14:00:04 +0200100{
Ville Syrjälä46f16e62016-10-31 22:37:22 +0200101 gen9_init_clock_gating(dev_priv);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +0200102
Nick Hoatha7546152015-06-29 14:07:32 +0100103 /* WaDisableSDEUnitClockGating:bxt */
104 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
105 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
106
Imre Deak32608ca2015-03-11 11:10:27 +0200107 /*
108 * FIXME:
Ben Widawsky868434c2015-03-11 10:49:32 +0200109 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
Imre Deak32608ca2015-03-11 11:10:27 +0200110 */
Imre Deak32608ca2015-03-11 11:10:27 +0200111 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
Ben Widawsky868434c2015-03-11 10:49:32 +0200112 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
Imre Deakd965e7ac2015-12-01 10:23:52 +0200113
114 /*
115 * Wa: Backlight PWM may stop in the asserted state, causing backlight
116 * to stay fully on.
117 */
Jani Nikula8aeaf642017-02-15 17:21:37 +0200118 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
119 PWM1_GATING_DIS | PWM2_GATING_DIS);
Imre Deaka82abe42015-03-27 14:00:04 +0200120}
121
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200122static void glk_init_clock_gating(struct drm_i915_private *dev_priv)
123{
124 gen9_init_clock_gating(dev_priv);
125
126 /*
127 * WaDisablePWMClockGating:glk
128 * Backlight PWM may stop in the asserted state, causing backlight
129 * to stay fully on.
130 */
131 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
132 PWM1_GATING_DIS | PWM2_GATING_DIS);
Ander Conselvan de Oliveiraf4f4b592017-02-22 08:34:29 +0200133
134 /* WaDDIIOTimeout:glk */
135 if (IS_GLK_REVID(dev_priv, 0, GLK_REVID_A1)) {
136 u32 val = I915_READ(CHICKEN_MISC_2);
137 val &= ~(GLK_CL0_PWR_DOWN |
138 GLK_CL1_PWR_DOWN |
139 GLK_CL2_PWR_DOWN);
140 I915_WRITE(CHICKEN_MISC_2, val);
141 }
142
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200143}
144
Ville Syrjälä148ac1f2016-10-31 22:37:16 +0200145static void i915_pineview_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200146{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200147 u32 tmp;
148
149 tmp = I915_READ(CLKCFG);
150
151 switch (tmp & CLKCFG_FSB_MASK) {
152 case CLKCFG_FSB_533:
153 dev_priv->fsb_freq = 533; /* 133*4 */
154 break;
155 case CLKCFG_FSB_800:
156 dev_priv->fsb_freq = 800; /* 200*4 */
157 break;
158 case CLKCFG_FSB_667:
159 dev_priv->fsb_freq = 667; /* 167*4 */
160 break;
161 case CLKCFG_FSB_400:
162 dev_priv->fsb_freq = 400; /* 100*4 */
163 break;
164 }
165
166 switch (tmp & CLKCFG_MEM_MASK) {
167 case CLKCFG_MEM_533:
168 dev_priv->mem_freq = 533;
169 break;
170 case CLKCFG_MEM_667:
171 dev_priv->mem_freq = 667;
172 break;
173 case CLKCFG_MEM_800:
174 dev_priv->mem_freq = 800;
175 break;
176 }
177
178 /* detect pineview DDR3 setting */
179 tmp = I915_READ(CSHRDDR3CTL);
180 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
181}
182
Ville Syrjälä148ac1f2016-10-31 22:37:16 +0200183static void i915_ironlake_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200184{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200185 u16 ddrpll, csipll;
186
187 ddrpll = I915_READ16(DDRMPLL1);
188 csipll = I915_READ16(CSIPLL0);
189
190 switch (ddrpll & 0xff) {
191 case 0xc:
192 dev_priv->mem_freq = 800;
193 break;
194 case 0x10:
195 dev_priv->mem_freq = 1066;
196 break;
197 case 0x14:
198 dev_priv->mem_freq = 1333;
199 break;
200 case 0x18:
201 dev_priv->mem_freq = 1600;
202 break;
203 default:
204 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
205 ddrpll & 0xff);
206 dev_priv->mem_freq = 0;
207 break;
208 }
209
Daniel Vetter20e4d402012-08-08 23:35:39 +0200210 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200211
212 switch (csipll & 0x3ff) {
213 case 0x00c:
214 dev_priv->fsb_freq = 3200;
215 break;
216 case 0x00e:
217 dev_priv->fsb_freq = 3733;
218 break;
219 case 0x010:
220 dev_priv->fsb_freq = 4266;
221 break;
222 case 0x012:
223 dev_priv->fsb_freq = 4800;
224 break;
225 case 0x014:
226 dev_priv->fsb_freq = 5333;
227 break;
228 case 0x016:
229 dev_priv->fsb_freq = 5866;
230 break;
231 case 0x018:
232 dev_priv->fsb_freq = 6400;
233 break;
234 default:
235 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
236 csipll & 0x3ff);
237 dev_priv->fsb_freq = 0;
238 break;
239 }
240
241 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200242 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200243 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200244 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200245 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200246 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200247 }
248}
249
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300250static const struct cxsr_latency cxsr_latency_table[] = {
251 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
252 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
253 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
254 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
255 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
256
257 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
258 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
259 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
260 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
261 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
262
263 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
264 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
265 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
266 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
267 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
268
269 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
270 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
271 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
272 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
273 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
274
275 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
276 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
277 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
278 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
279 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
280
281 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
282 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
283 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
284 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
285 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
286};
287
Tvrtko Ursulin44a655c2016-10-13 11:09:23 +0100288static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
289 bool is_ddr3,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300290 int fsb,
291 int mem)
292{
293 const struct cxsr_latency *latency;
294 int i;
295
296 if (fsb == 0 || mem == 0)
297 return NULL;
298
299 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
300 latency = &cxsr_latency_table[i];
301 if (is_desktop == latency->is_desktop &&
302 is_ddr3 == latency->is_ddr3 &&
303 fsb == latency->fsb_freq && mem == latency->mem_freq)
304 return latency;
305 }
306
307 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
308
309 return NULL;
310}
311
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200312static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
313{
314 u32 val;
315
Sagar Arun Kamble9f817502017-10-10 22:30:05 +0100316 mutex_lock(&dev_priv->pcu_lock);
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200317
318 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
319 if (enable)
320 val &= ~FORCE_DDR_HIGH_FREQ;
321 else
322 val |= FORCE_DDR_HIGH_FREQ;
323 val &= ~FORCE_DDR_LOW_FREQ;
324 val |= FORCE_DDR_FREQ_REQ_ACK;
325 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
326
327 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
328 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
329 DRM_ERROR("timed out waiting for Punit DDR DVFS request\n");
330
Sagar Arun Kamble9f817502017-10-10 22:30:05 +0100331 mutex_unlock(&dev_priv->pcu_lock);
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200332}
333
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200334static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
335{
336 u32 val;
337
Sagar Arun Kamble9f817502017-10-10 22:30:05 +0100338 mutex_lock(&dev_priv->pcu_lock);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200339
340 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
341 if (enable)
342 val |= DSP_MAXFIFO_PM5_ENABLE;
343 else
344 val &= ~DSP_MAXFIFO_PM5_ENABLE;
345 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
346
Sagar Arun Kamble9f817502017-10-10 22:30:05 +0100347 mutex_unlock(&dev_priv->pcu_lock);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200348}
349
Ville Syrjäläf4998962015-03-10 17:02:21 +0200350#define FW_WM(value, plane) \
351 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
352
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200353static bool _intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300354{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200355 bool was_enabled;
Imre Deak5209b1f2014-07-01 12:36:17 +0300356 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300357
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100358 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200359 was_enabled = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300360 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300361 POSTING_READ(FW_BLC_SELF_VLV);
Jani Nikulac0f86832016-12-07 12:13:04 +0200362 } else if (IS_G4X(dev_priv) || IS_I965GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200363 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300364 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300365 POSTING_READ(FW_BLC_SELF);
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +0200366 } else if (IS_PINEVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200367 val = I915_READ(DSPFW3);
368 was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
369 if (enable)
370 val |= PINEVIEW_SELF_REFRESH_EN;
371 else
372 val &= ~PINEVIEW_SELF_REFRESH_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300373 I915_WRITE(DSPFW3, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300374 POSTING_READ(DSPFW3);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100375 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200376 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300377 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
378 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
379 I915_WRITE(FW_BLC_SELF, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300380 POSTING_READ(FW_BLC_SELF);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100381 } else if (IS_I915GM(dev_priv)) {
Ville Syrjäläacb91352016-07-29 17:57:02 +0300382 /*
383 * FIXME can't find a bit like this for 915G, and
384 * and yet it does have the related watermark in
385 * FW_BLC_SELF. What's going on?
386 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200387 was_enabled = I915_READ(INSTPM) & INSTPM_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300388 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
389 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
390 I915_WRITE(INSTPM, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300391 POSTING_READ(INSTPM);
Imre Deak5209b1f2014-07-01 12:36:17 +0300392 } else {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200393 return false;
Imre Deak5209b1f2014-07-01 12:36:17 +0300394 }
395
Ville Syrjälä1489bba2017-03-02 19:15:07 +0200396 trace_intel_memory_cxsr(dev_priv, was_enabled, enable);
397
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200398 DRM_DEBUG_KMS("memory self-refresh is %s (was %s)\n",
399 enableddisabled(enable),
400 enableddisabled(was_enabled));
401
402 return was_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300403}
404
Ville Syrjälä62571fc2017-04-21 21:14:23 +0300405/**
406 * intel_set_memory_cxsr - Configure CxSR state
407 * @dev_priv: i915 device
408 * @enable: Allow vs. disallow CxSR
409 *
410 * Allow or disallow the system to enter a special CxSR
411 * (C-state self refresh) state. What typically happens in CxSR mode
412 * is that several display FIFOs may get combined into a single larger
413 * FIFO for a particular plane (so called max FIFO mode) to allow the
414 * system to defer memory fetches longer, and the memory will enter
415 * self refresh.
416 *
417 * Note that enabling CxSR does not guarantee that the system enter
418 * this special mode, nor does it guarantee that the system stays
419 * in that mode once entered. So this just allows/disallows the system
420 * to autonomously utilize the CxSR mode. Other factors such as core
421 * C-states will affect when/if the system actually enters/exits the
422 * CxSR mode.
423 *
424 * Note that on VLV/CHV this actually only controls the max FIFO mode,
425 * and the system is free to enter/exit memory self refresh at any time
426 * even when the use of CxSR has been disallowed.
427 *
428 * While the system is actually in the CxSR/max FIFO mode, some plane
429 * control registers will not get latched on vblank. Thus in order to
430 * guarantee the system will respond to changes in the plane registers
431 * we must always disallow CxSR prior to making changes to those registers.
432 * Unfortunately the system will re-evaluate the CxSR conditions at
433 * frame start which happens after vblank start (which is when the plane
434 * registers would get latched), so we can't proceed with the plane update
435 * during the same frame where we disallowed CxSR.
436 *
437 * Certain platforms also have a deeper HPLL SR mode. Fortunately the
438 * HPLL SR mode depends on CxSR itself, so we don't have to hand hold
439 * the hardware w.r.t. HPLL SR when writing to plane registers.
440 * Disallowing just CxSR is sufficient.
441 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200442bool intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200443{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200444 bool ret;
445
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200446 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200447 ret = _intel_set_memory_cxsr(dev_priv, enable);
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300448 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
449 dev_priv->wm.vlv.cxsr = enable;
450 else if (IS_G4X(dev_priv))
451 dev_priv->wm.g4x.cxsr = enable;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200452 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200453
454 return ret;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200455}
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200456
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300457/*
458 * Latency for FIFO fetches is dependent on several factors:
459 * - memory configuration (speed, channels)
460 * - chipset
461 * - current MCH state
462 * It can be fairly high in some situations, so here we assume a fairly
463 * pessimal value. It's a tradeoff between extra memory fetches (if we
464 * set this value too high, the FIFO will fetch frequently to stay full)
465 * and power consumption (set it too low to save power and we might see
466 * FIFO underruns and display "flicker").
467 *
468 * A value of 5us seems to be a good balance; safe for very low end
469 * platforms but not overly aggressive on lower latency configs.
470 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100471static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300472
Ville Syrjäläb5004722015-03-05 21:19:47 +0200473#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
474 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
475
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200476static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
Ville Syrjäläb5004722015-03-05 21:19:47 +0200477{
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200478 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200479 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200480 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200481 enum pipe pipe = crtc->pipe;
482 int sprite0_start, sprite1_start;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200483
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200484 switch (pipe) {
Ville Syrjäläb5004722015-03-05 21:19:47 +0200485 uint32_t dsparb, dsparb2, dsparb3;
486 case PIPE_A:
487 dsparb = I915_READ(DSPARB);
488 dsparb2 = I915_READ(DSPARB2);
489 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
490 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
491 break;
492 case PIPE_B:
493 dsparb = I915_READ(DSPARB);
494 dsparb2 = I915_READ(DSPARB2);
495 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
496 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
497 break;
498 case PIPE_C:
499 dsparb2 = I915_READ(DSPARB2);
500 dsparb3 = I915_READ(DSPARB3);
501 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
502 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
503 break;
504 default:
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200505 MISSING_CASE(pipe);
506 return;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200507 }
508
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200509 fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
510 fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
511 fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
512 fifo_state->plane[PLANE_CURSOR] = 63;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200513}
514
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200515static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv,
516 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300517{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300518 uint32_t dsparb = I915_READ(DSPARB);
519 int size;
520
521 size = dsparb & 0x7f;
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200522 if (i9xx_plane == PLANE_B)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300523 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
524
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200525 DRM_DEBUG_KMS("FIFO size - (0x%08x) %c: %d\n",
526 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300527
528 return size;
529}
530
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200531static int i830_get_fifo_size(struct drm_i915_private *dev_priv,
532 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300533{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300534 uint32_t dsparb = I915_READ(DSPARB);
535 int size;
536
537 size = dsparb & 0x1ff;
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200538 if (i9xx_plane == PLANE_B)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300539 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
540 size >>= 1; /* Convert to cachelines */
541
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200542 DRM_DEBUG_KMS("FIFO size - (0x%08x) %c: %d\n",
543 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300544
545 return size;
546}
547
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200548static int i845_get_fifo_size(struct drm_i915_private *dev_priv,
549 enum i9xx_plane_id i9xx_plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300550{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300551 uint32_t dsparb = I915_READ(DSPARB);
552 int size;
553
554 size = dsparb & 0x7f;
555 size >>= 2; /* Convert to cachelines */
556
Ville Syrjäläbdaf8432017-11-17 21:19:11 +0200557 DRM_DEBUG_KMS("FIFO size - (0x%08x) %c: %d\n",
558 dsparb, plane_name(i9xx_plane), size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300559
560 return size;
561}
562
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300563/* Pineview has different values for various configs */
564static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300565 .fifo_size = PINEVIEW_DISPLAY_FIFO,
566 .max_wm = PINEVIEW_MAX_WM,
567 .default_wm = PINEVIEW_DFT_WM,
568 .guard_size = PINEVIEW_GUARD_WM,
569 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300570};
571static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300572 .fifo_size = PINEVIEW_DISPLAY_FIFO,
573 .max_wm = PINEVIEW_MAX_WM,
574 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
575 .guard_size = PINEVIEW_GUARD_WM,
576 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300577};
578static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300579 .fifo_size = PINEVIEW_CURSOR_FIFO,
580 .max_wm = PINEVIEW_CURSOR_MAX_WM,
581 .default_wm = PINEVIEW_CURSOR_DFT_WM,
582 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
583 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300584};
585static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300586 .fifo_size = PINEVIEW_CURSOR_FIFO,
587 .max_wm = PINEVIEW_CURSOR_MAX_WM,
588 .default_wm = PINEVIEW_CURSOR_DFT_WM,
589 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
590 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300591};
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300592static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300593 .fifo_size = I965_CURSOR_FIFO,
594 .max_wm = I965_CURSOR_MAX_WM,
595 .default_wm = I965_CURSOR_DFT_WM,
596 .guard_size = 2,
597 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300598};
599static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300600 .fifo_size = I945_FIFO_SIZE,
601 .max_wm = I915_MAX_WM,
602 .default_wm = 1,
603 .guard_size = 2,
604 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300605};
606static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300607 .fifo_size = I915_FIFO_SIZE,
608 .max_wm = I915_MAX_WM,
609 .default_wm = 1,
610 .guard_size = 2,
611 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300612};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300613static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300614 .fifo_size = I855GM_FIFO_SIZE,
615 .max_wm = I915_MAX_WM,
616 .default_wm = 1,
617 .guard_size = 2,
618 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300619};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300620static const struct intel_watermark_params i830_bc_wm_info = {
621 .fifo_size = I855GM_FIFO_SIZE,
622 .max_wm = I915_MAX_WM/2,
623 .default_wm = 1,
624 .guard_size = 2,
625 .cacheline_size = I830_FIFO_LINE_SIZE,
626};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200627static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300628 .fifo_size = I830_FIFO_SIZE,
629 .max_wm = I915_MAX_WM,
630 .default_wm = 1,
631 .guard_size = 2,
632 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300633};
634
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300635/**
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300636 * intel_wm_method1 - Method 1 / "small buffer" watermark formula
637 * @pixel_rate: Pipe pixel rate in kHz
638 * @cpp: Plane bytes per pixel
639 * @latency: Memory wakeup latency in 0.1us units
640 *
641 * Compute the watermark using the method 1 or "small buffer"
642 * formula. The caller may additonally add extra cachelines
643 * to account for TLB misses and clock crossings.
644 *
645 * This method is concerned with the short term drain rate
646 * of the FIFO, ie. it does not account for blanking periods
647 * which would effectively reduce the average drain rate across
648 * a longer period. The name "small" refers to the fact the
649 * FIFO is relatively small compared to the amount of data
650 * fetched.
651 *
652 * The FIFO level vs. time graph might look something like:
653 *
654 * |\ |\
655 * | \ | \
656 * __---__---__ (- plane active, _ blanking)
657 * -> time
658 *
659 * or perhaps like this:
660 *
661 * |\|\ |\|\
662 * __----__----__ (- plane active, _ blanking)
663 * -> time
664 *
665 * Returns:
666 * The watermark in bytes
667 */
668static unsigned int intel_wm_method1(unsigned int pixel_rate,
669 unsigned int cpp,
670 unsigned int latency)
671{
672 uint64_t ret;
673
674 ret = (uint64_t) pixel_rate * cpp * latency;
675 ret = DIV_ROUND_UP_ULL(ret, 10000);
676
677 return ret;
678}
679
680/**
681 * intel_wm_method2 - Method 2 / "large buffer" watermark formula
682 * @pixel_rate: Pipe pixel rate in kHz
683 * @htotal: Pipe horizontal total
684 * @width: Plane width in pixels
685 * @cpp: Plane bytes per pixel
686 * @latency: Memory wakeup latency in 0.1us units
687 *
688 * Compute the watermark using the method 2 or "large buffer"
689 * formula. The caller may additonally add extra cachelines
690 * to account for TLB misses and clock crossings.
691 *
692 * This method is concerned with the long term drain rate
693 * of the FIFO, ie. it does account for blanking periods
694 * which effectively reduce the average drain rate across
695 * a longer period. The name "large" refers to the fact the
696 * FIFO is relatively large compared to the amount of data
697 * fetched.
698 *
699 * The FIFO level vs. time graph might look something like:
700 *
701 * |\___ |\___
702 * | \___ | \___
703 * | \ | \
704 * __ --__--__--__--__--__--__ (- plane active, _ blanking)
705 * -> time
706 *
707 * Returns:
708 * The watermark in bytes
709 */
710static unsigned int intel_wm_method2(unsigned int pixel_rate,
711 unsigned int htotal,
712 unsigned int width,
713 unsigned int cpp,
714 unsigned int latency)
715{
716 unsigned int ret;
717
718 /*
719 * FIXME remove once all users are computing
720 * watermarks in the correct place.
721 */
722 if (WARN_ON_ONCE(htotal == 0))
723 htotal = 1;
724
725 ret = (latency * pixel_rate) / (htotal * 10000);
726 ret = (ret + 1) * width * cpp;
727
728 return ret;
729}
730
731/**
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300732 * intel_calculate_wm - calculate watermark level
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300733 * @pixel_rate: pixel clock
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300734 * @wm: chip FIFO params
Ville Syrjäläac484962016-01-20 21:05:26 +0200735 * @cpp: bytes per pixel
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300736 * @latency_ns: memory latency for the platform
737 *
738 * Calculate the watermark level (the level at which the display plane will
739 * start fetching from memory again). Each chip has a different display
740 * FIFO size and allocation, so the caller needs to figure that out and pass
741 * in the correct intel_watermark_params structure.
742 *
743 * As the pixel clock runs, the FIFO will be drained at a rate that depends
744 * on the pixel size. When it reaches the watermark level, it'll start
745 * fetching FIFO line sized based chunks from memory until the FIFO fills
746 * past the watermark point. If the FIFO drains completely, a FIFO underrun
747 * will occur, and a display engine hang could result.
748 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300749static unsigned int intel_calculate_wm(int pixel_rate,
750 const struct intel_watermark_params *wm,
751 int fifo_size, int cpp,
752 unsigned int latency_ns)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300753{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300754 int entries, wm_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300755
756 /*
757 * Note: we need to make sure we don't overflow for various clock &
758 * latency values.
759 * clocks go from a few thousand to several hundred thousand.
760 * latency is usually a few thousand
761 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300762 entries = intel_wm_method1(pixel_rate, cpp,
763 latency_ns / 100);
764 entries = DIV_ROUND_UP(entries, wm->cacheline_size) +
765 wm->guard_size;
766 DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300767
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300768 wm_size = fifo_size - entries;
769 DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300770
771 /* Don't promote wm_size to unsigned... */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300772 if (wm_size > wm->max_wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300773 wm_size = wm->max_wm;
774 if (wm_size <= 0)
775 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300776
777 /*
778 * Bspec seems to indicate that the value shouldn't be lower than
779 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
780 * Lets go for 8 which is the burst size since certain platforms
781 * already use a hardcoded 8 (which is what the spec says should be
782 * done).
783 */
784 if (wm_size <= 8)
785 wm_size = 8;
786
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300787 return wm_size;
788}
789
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300790static bool is_disabling(int old, int new, int threshold)
791{
792 return old >= threshold && new < threshold;
793}
794
795static bool is_enabling(int old, int new, int threshold)
796{
797 return old < threshold && new >= threshold;
798}
799
Ville Syrjälä6d5019b2017-04-21 21:14:20 +0300800static int intel_wm_num_levels(struct drm_i915_private *dev_priv)
801{
802 return dev_priv->wm.max_level + 1;
803}
804
Ville Syrjälä24304d812017-03-14 17:10:49 +0200805static bool intel_wm_plane_visible(const struct intel_crtc_state *crtc_state,
806 const struct intel_plane_state *plane_state)
807{
808 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
809
810 /* FIXME check the 'enable' instead */
811 if (!crtc_state->base.active)
812 return false;
813
814 /*
815 * Treat cursor with fb as always visible since cursor updates
816 * can happen faster than the vrefresh rate, and the current
817 * watermark code doesn't handle that correctly. Cursor updates
818 * which set/clear the fb or change the cursor size are going
819 * to get throttled by intel_legacy_cursor_update() to work
820 * around this problem with the watermark code.
821 */
822 if (plane->id == PLANE_CURSOR)
823 return plane_state->base.fb != NULL;
824 else
825 return plane_state->base.visible;
826}
827
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200828static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300829{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200830 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300831
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200832 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200833 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300834 if (enabled)
835 return NULL;
836 enabled = crtc;
837 }
838 }
839
840 return enabled;
841}
842
Ville Syrjälä432081b2016-10-31 22:37:03 +0200843static void pineview_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300844{
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200845 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200846 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300847 const struct cxsr_latency *latency;
848 u32 reg;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300849 unsigned int wm;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300850
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100851 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
852 dev_priv->is_ddr3,
853 dev_priv->fsb_freq,
854 dev_priv->mem_freq);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300855 if (!latency) {
856 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300857 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300858 return;
859 }
860
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200861 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300862 if (crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200863 const struct drm_display_mode *adjusted_mode =
864 &crtc->config->base.adjusted_mode;
865 const struct drm_framebuffer *fb =
866 crtc->base.primary->state->fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200867 int cpp = fb->format->cpp[0];
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300868 int clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300869
870 /* Display SR */
871 wm = intel_calculate_wm(clock, &pineview_display_wm,
872 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200873 cpp, latency->display_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300874 reg = I915_READ(DSPFW1);
875 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200876 reg |= FW_WM(wm, SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300877 I915_WRITE(DSPFW1, reg);
878 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
879
880 /* cursor SR */
881 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
882 pineview_display_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300883 4, latency->cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300884 reg = I915_READ(DSPFW3);
885 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200886 reg |= FW_WM(wm, CURSOR_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300887 I915_WRITE(DSPFW3, reg);
888
889 /* Display HPLL off SR */
890 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
891 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200892 cpp, latency->display_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300893 reg = I915_READ(DSPFW3);
894 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200895 reg |= FW_WM(wm, HPLL_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300896 I915_WRITE(DSPFW3, reg);
897
898 /* cursor HPLL off SR */
899 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
900 pineview_display_hplloff_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300901 4, latency->cursor_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300902 reg = I915_READ(DSPFW3);
903 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200904 reg |= FW_WM(wm, HPLL_CURSOR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300905 I915_WRITE(DSPFW3, reg);
906 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
907
Imre Deak5209b1f2014-07-01 12:36:17 +0300908 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300909 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300910 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300911 }
912}
913
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300914/*
915 * Documentation says:
916 * "If the line size is small, the TLB fetches can get in the way of the
917 * data fetches, causing some lag in the pixel data return which is not
918 * accounted for in the above formulas. The following adjustment only
919 * needs to be applied if eight whole lines fit in the buffer at once.
920 * The WM is adjusted upwards by the difference between the FIFO size
921 * and the size of 8 whole lines. This adjustment is always performed
922 * in the actual pixel depth regardless of whether FBC is enabled or not."
923 */
Chris Wilson1a1f1282017-11-07 14:03:38 +0000924static unsigned int g4x_tlb_miss_wa(int fifo_size, int width, int cpp)
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300925{
926 int tlb_miss = fifo_size * 64 - width * cpp * 8;
927
928 return max(0, tlb_miss);
929}
930
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300931static void g4x_write_wm_values(struct drm_i915_private *dev_priv,
932 const struct g4x_wm_values *wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300933{
Ville Syrjäläe93329a2017-04-21 21:14:31 +0300934 enum pipe pipe;
935
936 for_each_pipe(dev_priv, pipe)
937 trace_g4x_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
938
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300939 I915_WRITE(DSPFW1,
940 FW_WM(wm->sr.plane, SR) |
941 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
942 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
943 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
944 I915_WRITE(DSPFW2,
945 (wm->fbc_en ? DSPFW_FBC_SR_EN : 0) |
946 FW_WM(wm->sr.fbc, FBC_SR) |
947 FW_WM(wm->hpll.fbc, FBC_HPLL_SR) |
948 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEB) |
949 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
950 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
951 I915_WRITE(DSPFW3,
952 (wm->hpll_en ? DSPFW_HPLL_SR_EN : 0) |
953 FW_WM(wm->sr.cursor, CURSOR_SR) |
954 FW_WM(wm->hpll.cursor, HPLL_CURSOR) |
955 FW_WM(wm->hpll.plane, HPLL_SR));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300956
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300957 POSTING_READ(DSPFW1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300958}
959
Ville Syrjälä15665972015-03-10 16:16:28 +0200960#define FW_WM_VLV(value, plane) \
961 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
962
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200963static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200964 const struct vlv_wm_values *wm)
965{
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200966 enum pipe pipe;
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200967
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200968 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläc137d662017-03-02 19:15:06 +0200969 trace_vlv_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
970
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200971 I915_WRITE(VLV_DDL(pipe),
972 (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
973 (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
974 (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
975 (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
976 }
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200977
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +0200978 /*
979 * Zero the (unused) WM1 watermarks, and also clear all the
980 * high order bits so that there are no out of bounds values
981 * present in the registers during the reprogramming.
982 */
983 I915_WRITE(DSPHOWM, 0);
984 I915_WRITE(DSPHOWM1, 0);
985 I915_WRITE(DSPFW4, 0);
986 I915_WRITE(DSPFW5, 0);
987 I915_WRITE(DSPFW6, 0);
988
Ville Syrjäläae801522015-03-05 21:19:49 +0200989 I915_WRITE(DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +0200990 FW_WM(wm->sr.plane, SR) |
Ville Syrjälä1b313892016-11-28 19:37:08 +0200991 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
992 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
993 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200994 I915_WRITE(DSPFW2,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200995 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
996 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
997 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200998 I915_WRITE(DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +0200999 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +02001000
1001 if (IS_CHERRYVIEW(dev_priv)) {
1002 I915_WRITE(DSPFW7_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001003 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1004 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001005 I915_WRITE(DSPFW8_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001006 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
1007 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
Ville Syrjäläae801522015-03-05 21:19:49 +02001008 I915_WRITE(DSPFW9_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001009 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
1010 FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001011 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001012 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001013 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
1014 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
1015 FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
1016 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1017 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1018 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1019 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1020 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1021 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001022 } else {
1023 I915_WRITE(DSPFW7,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001024 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1025 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001026 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001027 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001028 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1029 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1030 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1031 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1032 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1033 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001034 }
1035
1036 POSTING_READ(DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001037}
1038
Ville Syrjälä15665972015-03-10 16:16:28 +02001039#undef FW_WM_VLV
1040
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001041static void g4x_setup_wm_latency(struct drm_i915_private *dev_priv)
1042{
1043 /* all latencies in usec */
1044 dev_priv->wm.pri_latency[G4X_WM_LEVEL_NORMAL] = 5;
1045 dev_priv->wm.pri_latency[G4X_WM_LEVEL_SR] = 12;
Ville Syrjälä79d94302017-04-21 21:14:30 +03001046 dev_priv->wm.pri_latency[G4X_WM_LEVEL_HPLL] = 35;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001047
Ville Syrjälä79d94302017-04-21 21:14:30 +03001048 dev_priv->wm.max_level = G4X_WM_LEVEL_HPLL;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001049}
1050
1051static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
1052{
1053 /*
1054 * DSPCNTR[13] supposedly controls whether the
1055 * primary plane can use the FIFO space otherwise
1056 * reserved for the sprite plane. It's not 100% clear
1057 * what the actual FIFO size is, but it looks like we
1058 * can happily set both primary and sprite watermarks
1059 * up to 127 cachelines. So that would seem to mean
1060 * that either DSPCNTR[13] doesn't do anything, or that
1061 * the total FIFO is >= 256 cachelines in size. Either
1062 * way, we don't seem to have to worry about this
1063 * repartitioning as the maximum watermark value the
1064 * register can hold for each plane is lower than the
1065 * minimum FIFO size.
1066 */
1067 switch (plane_id) {
1068 case PLANE_CURSOR:
1069 return 63;
1070 case PLANE_PRIMARY:
1071 return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
1072 case PLANE_SPRITE0:
1073 return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
1074 default:
1075 MISSING_CASE(plane_id);
1076 return 0;
1077 }
1078}
1079
1080static int g4x_fbc_fifo_size(int level)
1081{
1082 switch (level) {
1083 case G4X_WM_LEVEL_SR:
1084 return 7;
1085 case G4X_WM_LEVEL_HPLL:
1086 return 15;
1087 default:
1088 MISSING_CASE(level);
1089 return 0;
1090 }
1091}
1092
1093static uint16_t g4x_compute_wm(const struct intel_crtc_state *crtc_state,
1094 const struct intel_plane_state *plane_state,
1095 int level)
1096{
1097 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1098 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1099 const struct drm_display_mode *adjusted_mode =
1100 &crtc_state->base.adjusted_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001101 unsigned int latency = dev_priv->wm.pri_latency[level] * 10;
1102 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001103
1104 if (latency == 0)
1105 return USHRT_MAX;
1106
1107 if (!intel_wm_plane_visible(crtc_state, plane_state))
1108 return 0;
1109
1110 /*
1111 * Not 100% sure which way ELK should go here as the
1112 * spec only says CL/CTG should assume 32bpp and BW
1113 * doesn't need to. But as these things followed the
1114 * mobile vs. desktop lines on gen3 as well, let's
1115 * assume ELK doesn't need this.
1116 *
1117 * The spec also fails to list such a restriction for
1118 * the HPLL watermark, which seems a little strange.
1119 * Let's use 32bpp for the HPLL watermark as well.
1120 */
1121 if (IS_GM45(dev_priv) && plane->id == PLANE_PRIMARY &&
1122 level != G4X_WM_LEVEL_NORMAL)
1123 cpp = 4;
1124 else
1125 cpp = plane_state->base.fb->format->cpp[0];
1126
1127 clock = adjusted_mode->crtc_clock;
1128 htotal = adjusted_mode->crtc_htotal;
1129
1130 if (plane->id == PLANE_CURSOR)
1131 width = plane_state->base.crtc_w;
1132 else
1133 width = drm_rect_width(&plane_state->base.dst);
1134
1135 if (plane->id == PLANE_CURSOR) {
1136 wm = intel_wm_method2(clock, htotal, width, cpp, latency);
1137 } else if (plane->id == PLANE_PRIMARY &&
1138 level == G4X_WM_LEVEL_NORMAL) {
1139 wm = intel_wm_method1(clock, cpp, latency);
1140 } else {
Chris Wilson1a1f1282017-11-07 14:03:38 +00001141 unsigned int small, large;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001142
1143 small = intel_wm_method1(clock, cpp, latency);
1144 large = intel_wm_method2(clock, htotal, width, cpp, latency);
1145
1146 wm = min(small, large);
1147 }
1148
1149 wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
1150 width, cpp);
1151
1152 wm = DIV_ROUND_UP(wm, 64) + 2;
1153
Chris Wilson1a1f1282017-11-07 14:03:38 +00001154 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001155}
1156
1157static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1158 int level, enum plane_id plane_id, u16 value)
1159{
1160 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1161 bool dirty = false;
1162
1163 for (; level < intel_wm_num_levels(dev_priv); level++) {
1164 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1165
1166 dirty |= raw->plane[plane_id] != value;
1167 raw->plane[plane_id] = value;
1168 }
1169
1170 return dirty;
1171}
1172
1173static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
1174 int level, u16 value)
1175{
1176 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1177 bool dirty = false;
1178
1179 /* NORMAL level doesn't have an FBC watermark */
1180 level = max(level, G4X_WM_LEVEL_SR);
1181
1182 for (; level < intel_wm_num_levels(dev_priv); level++) {
1183 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1184
1185 dirty |= raw->fbc != value;
1186 raw->fbc = value;
1187 }
1188
1189 return dirty;
1190}
1191
1192static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
1193 const struct intel_plane_state *pstate,
1194 uint32_t pri_val);
1195
1196static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1197 const struct intel_plane_state *plane_state)
1198{
1199 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1200 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
1201 enum plane_id plane_id = plane->id;
1202 bool dirty = false;
1203 int level;
1204
1205 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1206 dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1207 if (plane_id == PLANE_PRIMARY)
1208 dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
1209 goto out;
1210 }
1211
1212 for (level = 0; level < num_levels; level++) {
1213 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1214 int wm, max_wm;
1215
1216 wm = g4x_compute_wm(crtc_state, plane_state, level);
1217 max_wm = g4x_plane_fifo_size(plane_id, level);
1218
1219 if (wm > max_wm)
1220 break;
1221
1222 dirty |= raw->plane[plane_id] != wm;
1223 raw->plane[plane_id] = wm;
1224
1225 if (plane_id != PLANE_PRIMARY ||
1226 level == G4X_WM_LEVEL_NORMAL)
1227 continue;
1228
1229 wm = ilk_compute_fbc_wm(crtc_state, plane_state,
1230 raw->plane[plane_id]);
1231 max_wm = g4x_fbc_fifo_size(level);
1232
1233 /*
1234 * FBC wm is not mandatory as we
1235 * can always just disable its use.
1236 */
1237 if (wm > max_wm)
1238 wm = USHRT_MAX;
1239
1240 dirty |= raw->fbc != wm;
1241 raw->fbc = wm;
1242 }
1243
1244 /* mark watermarks as invalid */
1245 dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1246
1247 if (plane_id == PLANE_PRIMARY)
1248 dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
1249
1250 out:
1251 if (dirty) {
1252 DRM_DEBUG_KMS("%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
1253 plane->base.name,
1254 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
1255 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
1256 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
1257
1258 if (plane_id == PLANE_PRIMARY)
1259 DRM_DEBUG_KMS("FBC watermarks: SR=%d, HPLL=%d\n",
1260 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
1261 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
1262 }
1263
1264 return dirty;
1265}
1266
1267static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1268 enum plane_id plane_id, int level)
1269{
1270 const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1271
1272 return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
1273}
1274
1275static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
1276 int level)
1277{
1278 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1279
1280 if (level > dev_priv->wm.max_level)
1281 return false;
1282
1283 return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1284 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1285 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1286}
1287
1288/* mark all levels starting from 'level' as invalid */
1289static void g4x_invalidate_wms(struct intel_crtc *crtc,
1290 struct g4x_wm_state *wm_state, int level)
1291{
1292 if (level <= G4X_WM_LEVEL_NORMAL) {
1293 enum plane_id plane_id;
1294
1295 for_each_plane_id_on_crtc(crtc, plane_id)
1296 wm_state->wm.plane[plane_id] = USHRT_MAX;
1297 }
1298
1299 if (level <= G4X_WM_LEVEL_SR) {
1300 wm_state->cxsr = false;
1301 wm_state->sr.cursor = USHRT_MAX;
1302 wm_state->sr.plane = USHRT_MAX;
1303 wm_state->sr.fbc = USHRT_MAX;
1304 }
1305
1306 if (level <= G4X_WM_LEVEL_HPLL) {
1307 wm_state->hpll_en = false;
1308 wm_state->hpll.cursor = USHRT_MAX;
1309 wm_state->hpll.plane = USHRT_MAX;
1310 wm_state->hpll.fbc = USHRT_MAX;
1311 }
1312}
1313
1314static int g4x_compute_pipe_wm(struct intel_crtc_state *crtc_state)
1315{
1316 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1317 struct intel_atomic_state *state =
1318 to_intel_atomic_state(crtc_state->base.state);
1319 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
1320 int num_active_planes = hweight32(crtc_state->active_planes &
1321 ~BIT(PLANE_CURSOR));
1322 const struct g4x_pipe_wm *raw;
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001323 const struct intel_plane_state *old_plane_state;
1324 const struct intel_plane_state *new_plane_state;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001325 struct intel_plane *plane;
1326 enum plane_id plane_id;
1327 int i, level;
1328 unsigned int dirty = 0;
1329
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001330 for_each_oldnew_intel_plane_in_state(state, plane,
1331 old_plane_state,
1332 new_plane_state, i) {
1333 if (new_plane_state->base.crtc != &crtc->base &&
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001334 old_plane_state->base.crtc != &crtc->base)
1335 continue;
1336
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001337 if (g4x_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001338 dirty |= BIT(plane->id);
1339 }
1340
1341 if (!dirty)
1342 return 0;
1343
1344 level = G4X_WM_LEVEL_NORMAL;
1345 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1346 goto out;
1347
1348 raw = &crtc_state->wm.g4x.raw[level];
1349 for_each_plane_id_on_crtc(crtc, plane_id)
1350 wm_state->wm.plane[plane_id] = raw->plane[plane_id];
1351
1352 level = G4X_WM_LEVEL_SR;
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->sr.plane = raw->plane[PLANE_PRIMARY];
1359 wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
1360 wm_state->sr.fbc = raw->fbc;
1361
1362 wm_state->cxsr = num_active_planes == BIT(PLANE_PRIMARY);
1363
1364 level = G4X_WM_LEVEL_HPLL;
1365
1366 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1367 goto out;
1368
1369 raw = &crtc_state->wm.g4x.raw[level];
1370 wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
1371 wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
1372 wm_state->hpll.fbc = raw->fbc;
1373
1374 wm_state->hpll_en = wm_state->cxsr;
1375
1376 level++;
1377
1378 out:
1379 if (level == G4X_WM_LEVEL_NORMAL)
1380 return -EINVAL;
1381
1382 /* invalidate the higher levels */
1383 g4x_invalidate_wms(crtc, wm_state, level);
1384
1385 /*
1386 * Determine if the FBC watermark(s) can be used. IF
1387 * this isn't the case we prefer to disable the FBC
1388 ( watermark(s) rather than disable the SR/HPLL
1389 * level(s) entirely.
1390 */
1391 wm_state->fbc_en = level > G4X_WM_LEVEL_NORMAL;
1392
1393 if (level >= G4X_WM_LEVEL_SR &&
1394 wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
1395 wm_state->fbc_en = false;
1396 else if (level >= G4X_WM_LEVEL_HPLL &&
1397 wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
1398 wm_state->fbc_en = false;
1399
1400 return 0;
1401}
1402
1403static int g4x_compute_intermediate_wm(struct drm_device *dev,
1404 struct intel_crtc *crtc,
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001405 struct intel_crtc_state *new_crtc_state)
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001406{
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001407 struct g4x_wm_state *intermediate = &new_crtc_state->wm.g4x.intermediate;
1408 const struct g4x_wm_state *optimal = &new_crtc_state->wm.g4x.optimal;
1409 struct intel_atomic_state *intel_state =
1410 to_intel_atomic_state(new_crtc_state->base.state);
1411 const struct intel_crtc_state *old_crtc_state =
1412 intel_atomic_get_old_crtc_state(intel_state, crtc);
1413 const struct g4x_wm_state *active = &old_crtc_state->wm.g4x.optimal;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001414 enum plane_id plane_id;
1415
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001416 if (!new_crtc_state->base.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->base)) {
1417 *intermediate = *optimal;
1418
1419 intermediate->cxsr = false;
1420 intermediate->hpll_en = false;
1421 goto out;
1422 }
1423
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001424 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001425 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001426 intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001427 !new_crtc_state->disable_cxsr;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001428 intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
1429
1430 for_each_plane_id_on_crtc(crtc, plane_id) {
1431 intermediate->wm.plane[plane_id] =
1432 max(optimal->wm.plane[plane_id],
1433 active->wm.plane[plane_id]);
1434
1435 WARN_ON(intermediate->wm.plane[plane_id] >
1436 g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
1437 }
1438
1439 intermediate->sr.plane = max(optimal->sr.plane,
1440 active->sr.plane);
1441 intermediate->sr.cursor = max(optimal->sr.cursor,
1442 active->sr.cursor);
1443 intermediate->sr.fbc = max(optimal->sr.fbc,
1444 active->sr.fbc);
1445
1446 intermediate->hpll.plane = max(optimal->hpll.plane,
1447 active->hpll.plane);
1448 intermediate->hpll.cursor = max(optimal->hpll.cursor,
1449 active->hpll.cursor);
1450 intermediate->hpll.fbc = max(optimal->hpll.fbc,
1451 active->hpll.fbc);
1452
1453 WARN_ON((intermediate->sr.plane >
1454 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
1455 intermediate->sr.cursor >
1456 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
1457 intermediate->cxsr);
1458 WARN_ON((intermediate->sr.plane >
1459 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
1460 intermediate->sr.cursor >
1461 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
1462 intermediate->hpll_en);
1463
1464 WARN_ON(intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
1465 intermediate->fbc_en && intermediate->cxsr);
1466 WARN_ON(intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
1467 intermediate->fbc_en && intermediate->hpll_en);
1468
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001469out:
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001470 /*
1471 * If our intermediate WM are identical to the final WM, then we can
1472 * omit the post-vblank programming; only update if it's different.
1473 */
1474 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst248c2432017-11-15 17:31:57 +01001475 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001476
1477 return 0;
1478}
1479
1480static void g4x_merge_wm(struct drm_i915_private *dev_priv,
1481 struct g4x_wm_values *wm)
1482{
1483 struct intel_crtc *crtc;
1484 int num_active_crtcs = 0;
1485
1486 wm->cxsr = true;
1487 wm->hpll_en = true;
1488 wm->fbc_en = true;
1489
1490 for_each_intel_crtc(&dev_priv->drm, crtc) {
1491 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1492
1493 if (!crtc->active)
1494 continue;
1495
1496 if (!wm_state->cxsr)
1497 wm->cxsr = false;
1498 if (!wm_state->hpll_en)
1499 wm->hpll_en = false;
1500 if (!wm_state->fbc_en)
1501 wm->fbc_en = false;
1502
1503 num_active_crtcs++;
1504 }
1505
1506 if (num_active_crtcs != 1) {
1507 wm->cxsr = false;
1508 wm->hpll_en = false;
1509 wm->fbc_en = false;
1510 }
1511
1512 for_each_intel_crtc(&dev_priv->drm, crtc) {
1513 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1514 enum pipe pipe = crtc->pipe;
1515
1516 wm->pipe[pipe] = wm_state->wm;
1517 if (crtc->active && wm->cxsr)
1518 wm->sr = wm_state->sr;
1519 if (crtc->active && wm->hpll_en)
1520 wm->hpll = wm_state->hpll;
1521 }
1522}
1523
1524static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
1525{
1526 struct g4x_wm_values *old_wm = &dev_priv->wm.g4x;
1527 struct g4x_wm_values new_wm = {};
1528
1529 g4x_merge_wm(dev_priv, &new_wm);
1530
1531 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
1532 return;
1533
1534 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
1535 _intel_set_memory_cxsr(dev_priv, false);
1536
1537 g4x_write_wm_values(dev_priv, &new_wm);
1538
1539 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
1540 _intel_set_memory_cxsr(dev_priv, true);
1541
1542 *old_wm = new_wm;
1543}
1544
1545static void g4x_initial_watermarks(struct intel_atomic_state *state,
1546 struct intel_crtc_state *crtc_state)
1547{
1548 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1549 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1550
1551 mutex_lock(&dev_priv->wm.wm_mutex);
1552 crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
1553 g4x_program_watermarks(dev_priv);
1554 mutex_unlock(&dev_priv->wm.wm_mutex);
1555}
1556
1557static void g4x_optimize_watermarks(struct intel_atomic_state *state,
1558 struct intel_crtc_state *crtc_state)
1559{
1560 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1561 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
1562
1563 if (!crtc_state->wm.need_postvbl_update)
1564 return;
1565
1566 mutex_lock(&dev_priv->wm.wm_mutex);
1567 intel_crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
1568 g4x_program_watermarks(dev_priv);
1569 mutex_unlock(&dev_priv->wm.wm_mutex);
1570}
1571
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001572/* latency must be in 0.1us units. */
1573static unsigned int vlv_wm_method2(unsigned int pixel_rate,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001574 unsigned int htotal,
1575 unsigned int width,
Ville Syrjäläac484962016-01-20 21:05:26 +02001576 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001577 unsigned int latency)
1578{
1579 unsigned int ret;
1580
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001581 ret = intel_wm_method2(pixel_rate, htotal,
1582 width, cpp, latency);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001583 ret = DIV_ROUND_UP(ret, 64);
1584
1585 return ret;
1586}
1587
Ville Syrjäläbb726512016-10-31 22:37:24 +02001588static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001589{
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001590 /* all latencies in usec */
1591 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1592
Ville Syrjälä58590c12015-09-08 21:05:12 +03001593 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
1594
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001595 if (IS_CHERRYVIEW(dev_priv)) {
1596 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1597 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001598
1599 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001600 }
1601}
1602
Ville Syrjäläe339d672016-11-28 19:37:17 +02001603static uint16_t vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1604 const struct intel_plane_state *plane_state,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001605 int level)
1606{
Ville Syrjäläe339d672016-11-28 19:37:17 +02001607 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001608 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläe339d672016-11-28 19:37:17 +02001609 const struct drm_display_mode *adjusted_mode =
1610 &crtc_state->base.adjusted_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001611 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001612
1613 if (dev_priv->wm.pri_latency[level] == 0)
1614 return USHRT_MAX;
1615
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001616 if (!intel_wm_plane_visible(crtc_state, plane_state))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001617 return 0;
1618
Daniel Vetteref426c12017-01-04 11:41:10 +01001619 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjäläe339d672016-11-28 19:37:17 +02001620 clock = adjusted_mode->crtc_clock;
1621 htotal = adjusted_mode->crtc_htotal;
1622 width = crtc_state->pipe_src_w;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001623
Ville Syrjälä709f3fc2017-03-03 17:19:26 +02001624 if (plane->id == PLANE_CURSOR) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001625 /*
1626 * FIXME the formula gives values that are
1627 * too big for the cursor FIFO, and hence we
1628 * would never be able to use cursors. For
1629 * now just hardcode the watermark.
1630 */
1631 wm = 63;
1632 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +02001633 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001634 dev_priv->wm.pri_latency[level] * 10);
1635 }
1636
Chris Wilson1a1f1282017-11-07 14:03:38 +00001637 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001638}
1639
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001640static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1641{
1642 return (active_planes & (BIT(PLANE_SPRITE0) |
1643 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1644}
1645
Ville Syrjälä5012e602017-03-02 19:14:56 +02001646static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001647{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001648 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001649 const struct g4x_pipe_wm *raw =
Ville Syrjälä5012e602017-03-02 19:14:56 +02001650 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001651 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001652 unsigned int active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1653 int num_active_planes = hweight32(active_planes);
1654 const int fifo_size = 511;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001655 int fifo_extra, fifo_left = fifo_size;
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001656 int sprite0_fifo_extra = 0;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001657 unsigned int total_rate;
1658 enum plane_id plane_id;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001659
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001660 /*
1661 * When enabling sprite0 after sprite1 has already been enabled
1662 * we tend to get an underrun unless sprite0 already has some
1663 * FIFO space allcoated. Hence we always allocate at least one
1664 * cacheline for sprite0 whenever sprite1 is enabled.
1665 *
1666 * All other plane enable sequences appear immune to this problem.
1667 */
1668 if (vlv_need_sprite0_fifo_workaround(active_planes))
1669 sprite0_fifo_extra = 1;
1670
Ville Syrjälä5012e602017-03-02 19:14:56 +02001671 total_rate = raw->plane[PLANE_PRIMARY] +
1672 raw->plane[PLANE_SPRITE0] +
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001673 raw->plane[PLANE_SPRITE1] +
1674 sprite0_fifo_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001675
Ville Syrjälä5012e602017-03-02 19:14:56 +02001676 if (total_rate > fifo_size)
1677 return -EINVAL;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001678
Ville Syrjälä5012e602017-03-02 19:14:56 +02001679 if (total_rate == 0)
1680 total_rate = 1;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001681
Ville Syrjälä5012e602017-03-02 19:14:56 +02001682 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001683 unsigned int rate;
1684
Ville Syrjälä5012e602017-03-02 19:14:56 +02001685 if ((active_planes & BIT(plane_id)) == 0) {
1686 fifo_state->plane[plane_id] = 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001687 continue;
1688 }
1689
Ville Syrjälä5012e602017-03-02 19:14:56 +02001690 rate = raw->plane[plane_id];
1691 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1692 fifo_left -= fifo_state->plane[plane_id];
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001693 }
1694
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001695 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1696 fifo_left -= sprite0_fifo_extra;
1697
Ville Syrjälä5012e602017-03-02 19:14:56 +02001698 fifo_state->plane[PLANE_CURSOR] = 63;
1699
1700 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001701
1702 /* spread the remainder evenly */
Ville Syrjälä5012e602017-03-02 19:14:56 +02001703 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001704 int plane_extra;
1705
1706 if (fifo_left == 0)
1707 break;
1708
Ville Syrjälä5012e602017-03-02 19:14:56 +02001709 if ((active_planes & BIT(plane_id)) == 0)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001710 continue;
1711
1712 plane_extra = min(fifo_extra, fifo_left);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001713 fifo_state->plane[plane_id] += plane_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001714 fifo_left -= plane_extra;
1715 }
1716
Ville Syrjälä5012e602017-03-02 19:14:56 +02001717 WARN_ON(active_planes != 0 && fifo_left != 0);
1718
1719 /* give it all to the first plane if none are active */
1720 if (active_planes == 0) {
1721 WARN_ON(fifo_left != fifo_size);
1722 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1723 }
1724
1725 return 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001726}
1727
Ville Syrjäläff32c542017-03-02 19:14:57 +02001728/* mark all levels starting from 'level' as invalid */
1729static void vlv_invalidate_wms(struct intel_crtc *crtc,
1730 struct vlv_wm_state *wm_state, int level)
1731{
1732 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1733
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001734 for (; level < intel_wm_num_levels(dev_priv); level++) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02001735 enum plane_id plane_id;
1736
1737 for_each_plane_id_on_crtc(crtc, plane_id)
1738 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1739
1740 wm_state->sr[level].cursor = USHRT_MAX;
1741 wm_state->sr[level].plane = USHRT_MAX;
1742 }
1743}
1744
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001745static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1746{
1747 if (wm > fifo_size)
1748 return USHRT_MAX;
1749 else
1750 return fifo_size - wm;
1751}
1752
Ville Syrjäläff32c542017-03-02 19:14:57 +02001753/*
1754 * Starting from 'level' set all higher
1755 * levels to 'value' in the "raw" watermarks.
1756 */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001757static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001758 int level, enum plane_id plane_id, u16 value)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001759{
Ville Syrjäläff32c542017-03-02 19:14:57 +02001760 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001761 int num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001762 bool dirty = false;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001763
Ville Syrjäläff32c542017-03-02 19:14:57 +02001764 for (; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001765 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001766
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001767 dirty |= raw->plane[plane_id] != value;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001768 raw->plane[plane_id] = value;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001769 }
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001770
1771 return dirty;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001772}
1773
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001774static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1775 const struct intel_plane_state *plane_state)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001776{
1777 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1778 enum plane_id plane_id = plane->id;
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001779 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
Ville Syrjäläff32c542017-03-02 19:14:57 +02001780 int level;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001781 bool dirty = false;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001782
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001783 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001784 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1785 goto out;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001786 }
1787
1788 for (level = 0; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001789 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001790 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1791 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1792
Ville Syrjäläff32c542017-03-02 19:14:57 +02001793 if (wm > max_wm)
1794 break;
1795
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001796 dirty |= raw->plane[plane_id] != wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001797 raw->plane[plane_id] = wm;
1798 }
1799
1800 /* mark all higher levels as invalid */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001801 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001802
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001803out:
1804 if (dirty)
Ville Syrjälä57a65282017-04-21 21:14:22 +03001805 DRM_DEBUG_KMS("%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001806 plane->base.name,
1807 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1808 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1809 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
1810
1811 return dirty;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001812}
1813
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001814static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1815 enum plane_id plane_id, int level)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001816{
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001817 const struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02001818 &crtc_state->wm.vlv.raw[level];
1819 const struct vlv_fifo_state *fifo_state =
1820 &crtc_state->wm.vlv.fifo_state;
1821
1822 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1823}
1824
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001825static 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 +02001826{
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001827 return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1828 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1829 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1830 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001831}
1832
1833static int vlv_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001834{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001835 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001836 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001837 struct intel_atomic_state *state =
1838 to_intel_atomic_state(crtc_state->base.state);
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001839 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001840 const struct vlv_fifo_state *fifo_state =
1841 &crtc_state->wm.vlv.fifo_state;
1842 int num_active_planes = hweight32(crtc_state->active_planes &
1843 ~BIT(PLANE_CURSOR));
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001844 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->base);
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001845 const struct intel_plane_state *old_plane_state;
1846 const struct intel_plane_state *new_plane_state;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001847 struct intel_plane *plane;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001848 enum plane_id plane_id;
1849 int level, ret, i;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001850 unsigned int dirty = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001851
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001852 for_each_oldnew_intel_plane_in_state(state, plane,
1853 old_plane_state,
1854 new_plane_state, i) {
1855 if (new_plane_state->base.crtc != &crtc->base &&
Ville Syrjäläff32c542017-03-02 19:14:57 +02001856 old_plane_state->base.crtc != &crtc->base)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001857 continue;
1858
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001859 if (vlv_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001860 dirty |= BIT(plane->id);
1861 }
1862
1863 /*
1864 * DSPARB registers may have been reset due to the
1865 * power well being turned off. Make sure we restore
1866 * them to a consistent state even if no primary/sprite
1867 * planes are initially active.
1868 */
1869 if (needs_modeset)
1870 crtc_state->fifo_changed = true;
1871
1872 if (!dirty)
1873 return 0;
1874
1875 /* cursor changes don't warrant a FIFO recompute */
1876 if (dirty & ~BIT(PLANE_CURSOR)) {
1877 const struct intel_crtc_state *old_crtc_state =
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001878 intel_atomic_get_old_crtc_state(state, crtc);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001879 const struct vlv_fifo_state *old_fifo_state =
1880 &old_crtc_state->wm.vlv.fifo_state;
1881
1882 ret = vlv_compute_fifo(crtc_state);
1883 if (ret)
1884 return ret;
1885
1886 if (needs_modeset ||
1887 memcmp(old_fifo_state, fifo_state,
1888 sizeof(*fifo_state)) != 0)
1889 crtc_state->fifo_changed = true;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001890 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001891
Ville Syrjäläff32c542017-03-02 19:14:57 +02001892 /* initially allow all levels */
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001893 wm_state->num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001894 /*
1895 * Note that enabling cxsr with no primary/sprite planes
1896 * enabled can wedge the pipe. Hence we only allow cxsr
1897 * with exactly one enabled primary/sprite plane.
1898 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001899 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001900
Ville Syrjälä5012e602017-03-02 19:14:56 +02001901 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001902 const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001903 const int sr_fifo_size = INTEL_INFO(dev_priv)->num_pipes * 512 - 1;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001904
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001905 if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
Ville Syrjäläff32c542017-03-02 19:14:57 +02001906 break;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001907
Ville Syrjäläff32c542017-03-02 19:14:57 +02001908 for_each_plane_id_on_crtc(crtc, plane_id) {
1909 wm_state->wm[level].plane[plane_id] =
1910 vlv_invert_wm_value(raw->plane[plane_id],
1911 fifo_state->plane[plane_id]);
1912 }
1913
1914 wm_state->sr[level].plane =
1915 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
Ville Syrjälä5012e602017-03-02 19:14:56 +02001916 raw->plane[PLANE_SPRITE0],
Ville Syrjäläff32c542017-03-02 19:14:57 +02001917 raw->plane[PLANE_SPRITE1]),
1918 sr_fifo_size);
1919
1920 wm_state->sr[level].cursor =
1921 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1922 63);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001923 }
1924
Ville Syrjäläff32c542017-03-02 19:14:57 +02001925 if (level == 0)
1926 return -EINVAL;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001927
Ville Syrjäläff32c542017-03-02 19:14:57 +02001928 /* limit to only levels we can actually handle */
1929 wm_state->num_levels = level;
1930
1931 /* invalidate the higher levels */
1932 vlv_invalidate_wms(crtc, wm_state, level);
1933
1934 return 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001935}
1936
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001937#define VLV_FIFO(plane, value) \
1938 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1939
Ville Syrjäläff32c542017-03-02 19:14:57 +02001940static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
1941 struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001942{
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001943 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001944 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001945 const struct vlv_fifo_state *fifo_state =
1946 &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001947 int sprite0_start, sprite1_start, fifo_size;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001948
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001949 if (!crtc_state->fifo_changed)
1950 return;
1951
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001952 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
1953 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
1954 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001955
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001956 WARN_ON(fifo_state->plane[PLANE_CURSOR] != 63);
1957 WARN_ON(fifo_size != 511);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001958
Ville Syrjäläc137d662017-03-02 19:15:06 +02001959 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
1960
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001961 /*
1962 * uncore.lock serves a double purpose here. It allows us to
1963 * use the less expensive I915_{READ,WRITE}_FW() functions, and
1964 * it protects the DSPARB registers from getting clobbered by
1965 * parallel updates from multiple pipes.
1966 *
1967 * intel_pipe_update_start() has already disabled interrupts
1968 * for us, so a plain spin_lock() is sufficient here.
1969 */
1970 spin_lock(&dev_priv->uncore.lock);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001971
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001972 switch (crtc->pipe) {
1973 uint32_t dsparb, dsparb2, dsparb3;
1974 case PIPE_A:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001975 dsparb = I915_READ_FW(DSPARB);
1976 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001977
1978 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1979 VLV_FIFO(SPRITEB, 0xff));
1980 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1981 VLV_FIFO(SPRITEB, sprite1_start));
1982
1983 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1984 VLV_FIFO(SPRITEB_HI, 0x1));
1985 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1986 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1987
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001988 I915_WRITE_FW(DSPARB, dsparb);
1989 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001990 break;
1991 case PIPE_B:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001992 dsparb = I915_READ_FW(DSPARB);
1993 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001994
1995 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1996 VLV_FIFO(SPRITED, 0xff));
1997 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1998 VLV_FIFO(SPRITED, sprite1_start));
1999
2000 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
2001 VLV_FIFO(SPRITED_HI, 0xff));
2002 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
2003 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
2004
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002005 I915_WRITE_FW(DSPARB, dsparb);
2006 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002007 break;
2008 case PIPE_C:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002009 dsparb3 = I915_READ_FW(DSPARB3);
2010 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002011
2012 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
2013 VLV_FIFO(SPRITEF, 0xff));
2014 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
2015 VLV_FIFO(SPRITEF, sprite1_start));
2016
2017 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
2018 VLV_FIFO(SPRITEF_HI, 0xff));
2019 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
2020 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
2021
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002022 I915_WRITE_FW(DSPARB3, dsparb3);
2023 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002024 break;
2025 default:
2026 break;
2027 }
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002028
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002029 POSTING_READ_FW(DSPARB);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002030
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002031 spin_unlock(&dev_priv->uncore.lock);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002032}
2033
2034#undef VLV_FIFO
2035
Ville Syrjälä4841da52017-03-02 19:14:59 +02002036static int vlv_compute_intermediate_wm(struct drm_device *dev,
2037 struct intel_crtc *crtc,
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002038 struct intel_crtc_state *new_crtc_state)
Ville Syrjälä4841da52017-03-02 19:14:59 +02002039{
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002040 struct vlv_wm_state *intermediate = &new_crtc_state->wm.vlv.intermediate;
2041 const struct vlv_wm_state *optimal = &new_crtc_state->wm.vlv.optimal;
2042 struct intel_atomic_state *intel_state =
2043 to_intel_atomic_state(new_crtc_state->base.state);
2044 const struct intel_crtc_state *old_crtc_state =
2045 intel_atomic_get_old_crtc_state(intel_state, crtc);
2046 const struct vlv_wm_state *active = &old_crtc_state->wm.vlv.optimal;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002047 int level;
2048
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002049 if (!new_crtc_state->base.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->base)) {
2050 *intermediate = *optimal;
2051
2052 intermediate->cxsr = false;
2053 goto out;
2054 }
2055
Ville Syrjälä4841da52017-03-02 19:14:59 +02002056 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002057 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002058 !new_crtc_state->disable_cxsr;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002059
2060 for (level = 0; level < intermediate->num_levels; level++) {
2061 enum plane_id plane_id;
2062
2063 for_each_plane_id_on_crtc(crtc, plane_id) {
2064 intermediate->wm[level].plane[plane_id] =
2065 min(optimal->wm[level].plane[plane_id],
2066 active->wm[level].plane[plane_id]);
2067 }
2068
2069 intermediate->sr[level].plane = min(optimal->sr[level].plane,
2070 active->sr[level].plane);
2071 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
2072 active->sr[level].cursor);
2073 }
2074
2075 vlv_invalidate_wms(crtc, intermediate, level);
2076
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002077out:
Ville Syrjälä4841da52017-03-02 19:14:59 +02002078 /*
2079 * If our intermediate WM are identical to the final WM, then we can
2080 * omit the post-vblank programming; only update if it's different.
2081 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002082 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002083 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002084
2085 return 0;
2086}
2087
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002088static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002089 struct vlv_wm_values *wm)
2090{
2091 struct intel_crtc *crtc;
2092 int num_active_crtcs = 0;
2093
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002094 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002095 wm->cxsr = true;
2096
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002097 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002098 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002099
2100 if (!crtc->active)
2101 continue;
2102
2103 if (!wm_state->cxsr)
2104 wm->cxsr = false;
2105
2106 num_active_crtcs++;
2107 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
2108 }
2109
2110 if (num_active_crtcs != 1)
2111 wm->cxsr = false;
2112
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03002113 if (num_active_crtcs > 1)
2114 wm->level = VLV_WM_LEVEL_PM2;
2115
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002116 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002117 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002118 enum pipe pipe = crtc->pipe;
2119
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002120 wm->pipe[pipe] = wm_state->wm[wm->level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02002121 if (crtc->active && wm->cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002122 wm->sr = wm_state->sr[wm->level];
2123
Ville Syrjälä1b313892016-11-28 19:37:08 +02002124 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
2125 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
2126 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
2127 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002128 }
2129}
2130
Ville Syrjäläff32c542017-03-02 19:14:57 +02002131static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002132{
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002133 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
2134 struct vlv_wm_values new_wm = {};
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002135
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002136 vlv_merge_wm(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002137
Ville Syrjäläff32c542017-03-02 19:14:57 +02002138 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002139 return;
2140
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002141 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002142 chv_set_memory_dvfs(dev_priv, false);
2143
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002144 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002145 chv_set_memory_pm5(dev_priv, false);
2146
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002147 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002148 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002149
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002150 vlv_write_wm_values(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002151
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002152 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002153 _intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002154
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002155 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002156 chv_set_memory_pm5(dev_priv, true);
2157
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002158 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002159 chv_set_memory_dvfs(dev_priv, true);
2160
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002161 *old_wm = new_wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03002162}
2163
Ville Syrjäläff32c542017-03-02 19:14:57 +02002164static void vlv_initial_watermarks(struct intel_atomic_state *state,
2165 struct intel_crtc_state *crtc_state)
2166{
2167 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2168 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
2169
2170 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002171 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
2172 vlv_program_watermarks(dev_priv);
2173 mutex_unlock(&dev_priv->wm.wm_mutex);
2174}
2175
2176static void vlv_optimize_watermarks(struct intel_atomic_state *state,
2177 struct intel_crtc_state *crtc_state)
2178{
2179 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2180 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2181
2182 if (!crtc_state->wm.need_postvbl_update)
2183 return;
2184
2185 mutex_lock(&dev_priv->wm.wm_mutex);
2186 intel_crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02002187 vlv_program_watermarks(dev_priv);
2188 mutex_unlock(&dev_priv->wm.wm_mutex);
2189}
2190
Ville Syrjälä432081b2016-10-31 22:37:03 +02002191static void i965_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002192{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002193 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002194 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002195 int srwm = 1;
2196 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03002197 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002198
2199 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002200 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002201 if (crtc) {
2202 /* self-refresh has much higher latency */
2203 static const int sr_latency_ns = 12000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002204 const struct drm_display_mode *adjusted_mode =
2205 &crtc->config->base.adjusted_mode;
2206 const struct drm_framebuffer *fb =
2207 crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002208 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002209 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002210 int hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002211 int cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002212 int entries;
2213
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002214 entries = intel_wm_method2(clock, htotal,
2215 hdisplay, cpp, sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002216 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
2217 srwm = I965_FIFO_SIZE - entries;
2218 if (srwm < 0)
2219 srwm = 1;
2220 srwm &= 0x1ff;
2221 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
2222 entries, srwm);
2223
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002224 entries = intel_wm_method2(clock, htotal,
2225 crtc->base.cursor->state->crtc_w, 4,
2226 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002227 entries = DIV_ROUND_UP(entries,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002228 i965_cursor_wm_info.cacheline_size) +
2229 i965_cursor_wm_info.guard_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002230
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002231 cursor_sr = i965_cursor_wm_info.fifo_size - entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002232 if (cursor_sr > i965_cursor_wm_info.max_wm)
2233 cursor_sr = i965_cursor_wm_info.max_wm;
2234
2235 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
2236 "cursor %d\n", srwm, cursor_sr);
2237
Imre Deak98584252014-06-13 14:54:20 +03002238 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002239 } else {
Imre Deak98584252014-06-13 14:54:20 +03002240 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002241 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03002242 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002243 }
2244
2245 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2246 srwm);
2247
2248 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002249 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
2250 FW_WM(8, CURSORB) |
2251 FW_WM(8, PLANEB) |
2252 FW_WM(8, PLANEA));
2253 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
2254 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002255 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002256 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03002257
2258 if (cxsr_enabled)
2259 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002260}
2261
Ville Syrjäläf4998962015-03-10 17:02:21 +02002262#undef FW_WM
2263
Ville Syrjälä432081b2016-10-31 22:37:03 +02002264static void i9xx_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002265{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002266 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002267 const struct intel_watermark_params *wm_info;
2268 uint32_t fwater_lo;
2269 uint32_t fwater_hi;
2270 int cwm, srwm = 1;
2271 int fifo_size;
2272 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002273 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002274
Ville Syrjäläa9097be2016-10-31 22:37:20 +02002275 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002276 wm_info = &i945_wm_info;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002277 else if (!IS_GEN2(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002278 wm_info = &i915_wm_info;
2279 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03002280 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002281
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002282 fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_A);
2283 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_A);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002284 if (intel_crtc_active(crtc)) {
2285 const struct drm_display_mode *adjusted_mode =
2286 &crtc->config->base.adjusted_mode;
2287 const struct drm_framebuffer *fb =
2288 crtc->base.primary->state->fb;
2289 int cpp;
2290
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002291 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002292 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002293 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002294 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002295
Damien Lespiau241bfc32013-09-25 16:45:37 +01002296 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002297 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002298 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002299 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002300 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002301 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002302 if (planea_wm > (long)wm_info->max_wm)
2303 planea_wm = wm_info->max_wm;
2304 }
2305
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002306 if (IS_GEN2(dev_priv))
Ville Syrjälä9d539102014-08-15 01:21:53 +03002307 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002308
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002309 fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_B);
2310 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_B);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002311 if (intel_crtc_active(crtc)) {
2312 const struct drm_display_mode *adjusted_mode =
2313 &crtc->config->base.adjusted_mode;
2314 const struct drm_framebuffer *fb =
2315 crtc->base.primary->state->fb;
2316 int cpp;
2317
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002318 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002319 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002320 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002321 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002322
Damien Lespiau241bfc32013-09-25 16:45:37 +01002323 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002324 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002325 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002326 if (enabled == NULL)
2327 enabled = crtc;
2328 else
2329 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002330 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002331 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002332 if (planeb_wm > (long)wm_info->max_wm)
2333 planeb_wm = wm_info->max_wm;
2334 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002335
2336 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
2337
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002338 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07002339 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002340
Ville Syrjäläefc26112016-10-31 22:37:04 +02002341 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002342
2343 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01002344 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002345 enabled = NULL;
2346 }
2347
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002348 /*
2349 * Overlay gets an aggressive default since video jitter is bad.
2350 */
2351 cwm = 2;
2352
2353 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03002354 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002355
2356 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02002357 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002358 /* self-refresh has much higher latency */
2359 static const int sr_latency_ns = 6000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002360 const struct drm_display_mode *adjusted_mode =
2361 &enabled->config->base.adjusted_mode;
2362 const struct drm_framebuffer *fb =
2363 enabled->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002364 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002365 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002366 int hdisplay = enabled->config->pipe_src_w;
2367 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002368 int entries;
2369
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002370 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002371 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002372 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002373 cpp = fb->format->cpp[0];
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002374
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002375 entries = intel_wm_method2(clock, htotal, hdisplay, cpp,
2376 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002377 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
2378 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
2379 srwm = wm_info->fifo_size - entries;
2380 if (srwm < 0)
2381 srwm = 1;
2382
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002383 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002384 I915_WRITE(FW_BLC_SELF,
2385 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03002386 else
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002387 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
2388 }
2389
2390 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
2391 planea_wm, planeb_wm, cwm, srwm);
2392
2393 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2394 fwater_hi = (cwm & 0x1f);
2395
2396 /* Set request length to 8 cachelines per fetch */
2397 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2398 fwater_hi = fwater_hi | (1 << 8);
2399
2400 I915_WRITE(FW_BLC, fwater_lo);
2401 I915_WRITE(FW_BLC2, fwater_hi);
2402
Imre Deak5209b1f2014-07-01 12:36:17 +03002403 if (enabled)
2404 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002405}
2406
Ville Syrjälä432081b2016-10-31 22:37:03 +02002407static void i845_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002408{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002409 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002410 struct intel_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002411 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002412 uint32_t fwater_lo;
2413 int planea_wm;
2414
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002415 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002416 if (crtc == NULL)
2417 return;
2418
Ville Syrjäläefc26112016-10-31 22:37:04 +02002419 adjusted_mode = &crtc->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002420 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02002421 &i845_wm_info,
Ville Syrjäläbdaf8432017-11-17 21:19:11 +02002422 dev_priv->display.get_fifo_size(dev_priv, PLANE_A),
Chris Wilson5aef6002014-09-03 11:56:07 +01002423 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002424 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
2425 fwater_lo |= (3<<8) | planea_wm;
2426
2427 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
2428
2429 I915_WRITE(FW_BLC, fwater_lo);
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_method1(unsigned int pixel_rate,
2434 unsigned int cpp,
2435 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002436{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002437 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002438
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002439 ret = intel_wm_method1(pixel_rate, cpp, latency);
2440 ret = DIV_ROUND_UP(ret, 64) + 2;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002441
2442 return ret;
2443}
2444
Ville Syrjälä37126462013-08-01 16:18:55 +03002445/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002446static unsigned int ilk_wm_method2(unsigned int pixel_rate,
2447 unsigned int htotal,
2448 unsigned int width,
2449 unsigned int cpp,
2450 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002451{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002452 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002453
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002454 ret = intel_wm_method2(pixel_rate, htotal,
2455 width, cpp, latency);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002456 ret = DIV_ROUND_UP(ret, 64) + 2;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002457
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002458 return ret;
2459}
2460
Ville Syrjälä23297042013-07-05 11:57:17 +03002461static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Ville Syrjäläac484962016-01-20 21:05:26 +02002462 uint8_t cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002463{
Matt Roper15126882015-12-03 11:37:40 -08002464 /*
2465 * Neither of these should be possible since this function shouldn't be
2466 * called if the CRTC is off or the plane is invisible. But let's be
2467 * extra paranoid to avoid a potential divide-by-zero if we screw up
2468 * elsewhere in the driver.
2469 */
Ville Syrjäläac484962016-01-20 21:05:26 +02002470 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08002471 return 0;
2472 if (WARN_ON(!horiz_pixels))
2473 return 0;
2474
Ville Syrjäläac484962016-01-20 21:05:26 +02002475 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002476}
2477
Imre Deak820c1982013-12-17 14:46:36 +02002478struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002479 uint16_t pri;
2480 uint16_t spr;
2481 uint16_t cur;
2482 uint16_t fbc;
2483};
2484
Ville Syrjälä37126462013-08-01 16:18:55 +03002485/*
2486 * For both WM_PIPE and WM_LP.
2487 * mem_value must be in 0.1us units.
2488 */
Matt Roper7221fc32015-09-24 15:53:08 -07002489static uint32_t ilk_compute_pri_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002490 const struct intel_plane_state *pstate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03002491 uint32_t mem_value,
2492 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002493{
Paulo Zanonicca32e92013-05-31 11:45:06 -03002494 uint32_t method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002495 int cpp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002496
Ville Syrjälä24304d812017-03-14 17:10:49 +02002497 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002498 return 0;
2499
Ville Syrjälä353c8592016-12-14 23:30:57 +02002500 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002501
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002502 method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002503
2504 if (!is_lp)
2505 return method1;
2506
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002507 method2 = ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002508 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002509 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002510 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002511
2512 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002513}
2514
Ville Syrjälä37126462013-08-01 16:18:55 +03002515/*
2516 * For both WM_PIPE and WM_LP.
2517 * mem_value must be in 0.1us units.
2518 */
Matt Roper7221fc32015-09-24 15:53:08 -07002519static uint32_t ilk_compute_spr_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002520 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002521 uint32_t mem_value)
2522{
2523 uint32_t method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002524 int cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002525
Ville Syrjälä24304d812017-03-14 17:10:49 +02002526 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002527 return 0;
2528
Ville Syrjälä353c8592016-12-14 23:30:57 +02002529 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002530
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002531 method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
2532 method2 = ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002533 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002534 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002535 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002536 return min(method1, method2);
2537}
2538
Ville Syrjälä37126462013-08-01 16:18:55 +03002539/*
2540 * For both WM_PIPE and WM_LP.
2541 * mem_value must be in 0.1us units.
2542 */
Matt Roper7221fc32015-09-24 15:53:08 -07002543static uint32_t ilk_compute_cur_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002544 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002545 uint32_t mem_value)
2546{
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002547 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002548
Ville Syrjälä24304d812017-03-14 17:10:49 +02002549 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002550 return 0;
2551
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002552 cpp = pstate->base.fb->format->cpp[0];
2553
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002554 return ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002555 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002556 pstate->base.crtc_w, cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002557}
2558
Paulo Zanonicca32e92013-05-31 11:45:06 -03002559/* Only for WM_LP. */
Matt Roper7221fc32015-09-24 15:53:08 -07002560static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002561 const struct intel_plane_state *pstate,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03002562 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002563{
Ville Syrjälä83054942016-11-18 21:53:00 +02002564 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002565
Ville Syrjälä24304d812017-03-14 17:10:49 +02002566 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanonicca32e92013-05-31 11:45:06 -03002567 return 0;
2568
Ville Syrjälä353c8592016-12-14 23:30:57 +02002569 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002570
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002571 return ilk_wm_fbc(pri_val, drm_rect_width(&pstate->base.dst), cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002572}
2573
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002574static unsigned int
2575ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002576{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002577 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002578 return 3072;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002579 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002580 return 768;
2581 else
2582 return 512;
2583}
2584
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002585static unsigned int
2586ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2587 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002588{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002589 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002590 /* BDW primary/sprite plane watermarks */
2591 return level == 0 ? 255 : 2047;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002592 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002593 /* IVB/HSW primary/sprite plane watermarks */
2594 return level == 0 ? 127 : 1023;
2595 else if (!is_sprite)
2596 /* ILK/SNB primary plane watermarks */
2597 return level == 0 ? 127 : 511;
2598 else
2599 /* ILK/SNB sprite plane watermarks */
2600 return level == 0 ? 63 : 255;
2601}
2602
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002603static unsigned int
2604ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002605{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002606 if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002607 return level == 0 ? 63 : 255;
2608 else
2609 return level == 0 ? 31 : 63;
2610}
2611
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002612static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002613{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002614 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002615 return 31;
2616 else
2617 return 15;
2618}
2619
Ville Syrjälä158ae642013-08-07 13:28:19 +03002620/* Calculate the maximum primary/sprite plane watermark */
2621static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
2622 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002623 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002624 enum intel_ddb_partitioning ddb_partitioning,
2625 bool is_sprite)
2626{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002627 struct drm_i915_private *dev_priv = to_i915(dev);
2628 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002629
2630 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002631 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002632 return 0;
2633
2634 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002635 if (level == 0 || config->num_pipes_active > 1) {
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002636 fifo_size /= INTEL_INFO(dev_priv)->num_pipes;
Ville Syrjälä158ae642013-08-07 13:28:19 +03002637
2638 /*
2639 * For some reason the non self refresh
2640 * FIFO size is only half of the self
2641 * refresh FIFO size on ILK/SNB.
2642 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002643 if (INTEL_GEN(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002644 fifo_size /= 2;
2645 }
2646
Ville Syrjälä240264f2013-08-07 13:29:12 +03002647 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002648 /* level 0 is always calculated with 1:1 split */
2649 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2650 if (is_sprite)
2651 fifo_size *= 5;
2652 fifo_size /= 6;
2653 } else {
2654 fifo_size /= 2;
2655 }
2656 }
2657
2658 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002659 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002660}
2661
2662/* Calculate the maximum cursor plane watermark */
2663static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002664 int level,
2665 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002666{
2667 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002668 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002669 return 64;
2670
2671 /* otherwise just report max that registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002672 return ilk_cursor_wm_reg_max(to_i915(dev), level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002673}
2674
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002675static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002676 int level,
2677 const struct intel_wm_config *config,
2678 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002679 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002680{
Ville Syrjälä240264f2013-08-07 13:29:12 +03002681 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
2682 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
2683 max->cur = ilk_cursor_wm_max(dev, level, config);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002684 max->fbc = ilk_fbc_wm_reg_max(to_i915(dev));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002685}
2686
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002687static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002688 int level,
2689 struct ilk_wm_maximums *max)
2690{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002691 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2692 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2693 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2694 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002695}
2696
Ville Syrjäläd9395652013-10-09 19:18:10 +03002697static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002698 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002699 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002700{
2701 bool ret;
2702
2703 /* already determined to be invalid? */
2704 if (!result->enable)
2705 return false;
2706
2707 result->enable = result->pri_val <= max->pri &&
2708 result->spr_val <= max->spr &&
2709 result->cur_val <= max->cur;
2710
2711 ret = result->enable;
2712
2713 /*
2714 * HACK until we can pre-compute everything,
2715 * and thus fail gracefully if LP0 watermarks
2716 * are exceeded...
2717 */
2718 if (level == 0 && !result->enable) {
2719 if (result->pri_val > max->pri)
2720 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2721 level, result->pri_val, max->pri);
2722 if (result->spr_val > max->spr)
2723 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2724 level, result->spr_val, max->spr);
2725 if (result->cur_val > max->cur)
2726 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2727 level, result->cur_val, max->cur);
2728
2729 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
2730 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
2731 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
2732 result->enable = true;
2733 }
2734
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002735 return ret;
2736}
2737
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002738static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Matt Roper43d59ed2015-09-24 15:53:07 -07002739 const struct intel_crtc *intel_crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002740 int level,
Matt Roper7221fc32015-09-24 15:53:08 -07002741 struct intel_crtc_state *cstate,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02002742 const struct intel_plane_state *pristate,
2743 const struct intel_plane_state *sprstate,
2744 const struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002745 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002746{
2747 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2748 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2749 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2750
2751 /* WM1+ latency values stored in 0.5us units */
2752 if (level > 0) {
2753 pri_latency *= 5;
2754 spr_latency *= 5;
2755 cur_latency *= 5;
2756 }
2757
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002758 if (pristate) {
2759 result->pri_val = ilk_compute_pri_wm(cstate, pristate,
2760 pri_latency, level);
2761 result->fbc_val = ilk_compute_fbc_wm(cstate, pristate, result->pri_val);
2762 }
2763
2764 if (sprstate)
2765 result->spr_val = ilk_compute_spr_wm(cstate, sprstate, spr_latency);
2766
2767 if (curstate)
2768 result->cur_val = ilk_compute_cur_wm(cstate, curstate, cur_latency);
2769
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002770 result->enable = true;
2771}
2772
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002773static uint32_t
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002774hsw_compute_linetime_wm(const struct intel_crtc_state *cstate)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002775{
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002776 const struct intel_atomic_state *intel_state =
2777 to_intel_atomic_state(cstate->base.state);
Matt Roperee91a152015-12-03 11:37:39 -08002778 const struct drm_display_mode *adjusted_mode =
2779 &cstate->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002780 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002781
Matt Roperee91a152015-12-03 11:37:39 -08002782 if (!cstate->base.active)
2783 return 0;
2784 if (WARN_ON(adjusted_mode->crtc_clock == 0))
2785 return 0;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002786 if (WARN_ON(intel_state->cdclk.logical.cdclk == 0))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002787 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002788
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002789 /* The WM are computed with base on how long it takes to fill a single
2790 * row at the given clock rate, multiplied by 8.
2791 * */
Ville Syrjälä124abe02015-09-08 13:40:45 +03002792 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2793 adjusted_mode->crtc_clock);
2794 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002795 intel_state->cdclk.logical.cdclk);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002796
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002797 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2798 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002799}
2800
Ville Syrjäläbb726512016-10-31 22:37:24 +02002801static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
2802 uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002803{
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002804 if (INTEL_GEN(dev_priv) >= 9) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002805 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002806 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002807 int level, max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002808
2809 /* read the first set of memory latencies[0:3] */
2810 val = 0; /* data0 to be programmed to 0 for first set */
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01002811 mutex_lock(&dev_priv->pcu_lock);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002812 ret = sandybridge_pcode_read(dev_priv,
2813 GEN9_PCODE_READ_MEM_LATENCY,
2814 &val);
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01002815 mutex_unlock(&dev_priv->pcu_lock);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002816
2817 if (ret) {
2818 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2819 return;
2820 }
2821
2822 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2823 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2824 GEN9_MEM_LATENCY_LEVEL_MASK;
2825 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2826 GEN9_MEM_LATENCY_LEVEL_MASK;
2827 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2828 GEN9_MEM_LATENCY_LEVEL_MASK;
2829
2830 /* read the second set of memory latencies[4:7] */
2831 val = 1; /* data0 to be programmed to 1 for second set */
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01002832 mutex_lock(&dev_priv->pcu_lock);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002833 ret = sandybridge_pcode_read(dev_priv,
2834 GEN9_PCODE_READ_MEM_LATENCY,
2835 &val);
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01002836 mutex_unlock(&dev_priv->pcu_lock);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002837 if (ret) {
2838 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2839 return;
2840 }
2841
2842 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2843 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2844 GEN9_MEM_LATENCY_LEVEL_MASK;
2845 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2846 GEN9_MEM_LATENCY_LEVEL_MASK;
2847 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2848 GEN9_MEM_LATENCY_LEVEL_MASK;
2849
Vandana Kannan367294b2014-11-04 17:06:46 +00002850 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002851 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2852 * need to be disabled. We make sure to sanitize the values out
2853 * of the punit to satisfy this requirement.
2854 */
2855 for (level = 1; level <= max_level; level++) {
2856 if (wm[level] == 0) {
2857 for (i = level + 1; i <= max_level; i++)
2858 wm[i] = 0;
2859 break;
2860 }
2861 }
2862
2863 /*
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002864 * WaWmMemoryReadLatency:skl+,glk
Damien Lespiau6f972352015-02-09 19:33:07 +00002865 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002866 * punit doesn't take into account the read latency so we need
Paulo Zanoni0727e402016-09-22 18:00:30 -03002867 * to add 2us to the various latency levels we retrieve from the
2868 * punit when level 0 response data us 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002869 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002870 if (wm[0] == 0) {
2871 wm[0] += 2;
2872 for (level = 1; level <= max_level; level++) {
2873 if (wm[level] == 0)
2874 break;
Vandana Kannan367294b2014-11-04 17:06:46 +00002875 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002876 }
Paulo Zanoni0727e402016-09-22 18:00:30 -03002877 }
2878
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002879 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002880 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2881
2882 wm[0] = (sskpd >> 56) & 0xFF;
2883 if (wm[0] == 0)
2884 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002885 wm[1] = (sskpd >> 4) & 0xFF;
2886 wm[2] = (sskpd >> 12) & 0xFF;
2887 wm[3] = (sskpd >> 20) & 0x1FF;
2888 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002889 } else if (INTEL_GEN(dev_priv) >= 6) {
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002890 uint32_t sskpd = I915_READ(MCH_SSKPD);
2891
2892 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2893 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2894 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2895 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002896 } else if (INTEL_GEN(dev_priv) >= 5) {
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002897 uint32_t mltr = I915_READ(MLTR_ILK);
2898
2899 /* ILK primary LP0 latency is 700 ns */
2900 wm[0] = 7;
2901 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2902 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002903 } else {
2904 MISSING_CASE(INTEL_DEVID(dev_priv));
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002905 }
2906}
2907
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002908static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
2909 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002910{
2911 /* ILK sprite LP0 latency is 1300 ns */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002912 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002913 wm[0] = 13;
2914}
2915
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002916static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
2917 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002918{
2919 /* ILK cursor LP0 latency is 1300 ns */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002920 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002921 wm[0] = 13;
2922
2923 /* WaDoubleCursorLP3Latency:ivb */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002924 if (IS_IVYBRIDGE(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002925 wm[3] *= 2;
2926}
2927
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002928int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002929{
2930 /* how many WM levels are we expecting */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002931 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002932 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002933 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002934 return 4;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002935 else if (INTEL_GEN(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002936 return 3;
2937 else
2938 return 2;
2939}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002940
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002941static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002942 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002943 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002944{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002945 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002946
2947 for (level = 0; level <= max_level; level++) {
2948 unsigned int latency = wm[level];
2949
2950 if (latency == 0) {
2951 DRM_ERROR("%s WM%d latency not provided\n",
2952 name, level);
2953 continue;
2954 }
2955
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002956 /*
2957 * - latencies are in us on gen9.
2958 * - before then, WM1+ latency values are in 0.5us units
2959 */
Paulo Zanonidfc267a2017-08-09 13:52:46 -07002960 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002961 latency *= 10;
2962 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002963 latency *= 5;
2964
2965 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2966 name, level, wm[level],
2967 latency / 10, latency % 10);
2968 }
2969}
2970
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002971static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2972 uint16_t wm[5], uint16_t min)
2973{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002974 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002975
2976 if (wm[0] >= min)
2977 return false;
2978
2979 wm[0] = max(wm[0], min);
2980 for (level = 1; level <= max_level; level++)
2981 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2982
2983 return true;
2984}
2985
Ville Syrjäläbb726512016-10-31 22:37:24 +02002986static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002987{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002988 bool changed;
2989
2990 /*
2991 * The BIOS provided WM memory latency values are often
2992 * inadequate for high resolution displays. Adjust them.
2993 */
2994 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2995 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2996 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2997
2998 if (!changed)
2999 return;
3000
3001 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003002 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3003 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3004 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003005}
3006
Ville Syrjäläbb726512016-10-31 22:37:24 +02003007static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03003008{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003009 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03003010
3011 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
3012 sizeof(dev_priv->wm.pri_latency));
3013 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
3014 sizeof(dev_priv->wm.pri_latency));
3015
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003016 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003017 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003018
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003019 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3020 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3021 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003022
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003023 if (IS_GEN6(dev_priv))
Ville Syrjäläbb726512016-10-31 22:37:24 +02003024 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03003025}
3026
Ville Syrjäläbb726512016-10-31 22:37:24 +02003027static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003028{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003029 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003030 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003031}
3032
Matt Ropered4a6a72016-02-23 17:20:13 -08003033static bool ilk_validate_pipe_wm(struct drm_device *dev,
3034 struct intel_pipe_wm *pipe_wm)
3035{
3036 /* LP0 watermark maximums depend on this pipe alone */
3037 const struct intel_wm_config config = {
3038 .num_pipes_active = 1,
3039 .sprites_enabled = pipe_wm->sprites_enabled,
3040 .sprites_scaled = pipe_wm->sprites_scaled,
3041 };
3042 struct ilk_wm_maximums max;
3043
3044 /* LP0 watermarks always use 1/2 DDB partitioning */
3045 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
3046
3047 /* At least LP0 must be valid */
3048 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
3049 DRM_DEBUG_KMS("LP0 watermark invalid\n");
3050 return false;
3051 }
3052
3053 return true;
3054}
3055
Matt Roper261a27d2015-10-08 15:28:25 -07003056/* Compute new watermarks for the pipe */
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003057static int ilk_compute_pipe_wm(struct intel_crtc_state *cstate)
Matt Roper261a27d2015-10-08 15:28:25 -07003058{
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003059 struct drm_atomic_state *state = cstate->base.state;
3060 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07003061 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003062 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003063 const struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003064 struct drm_plane *plane;
3065 const struct drm_plane_state *plane_state;
3066 const struct intel_plane_state *pristate = NULL;
3067 const struct intel_plane_state *sprstate = NULL;
3068 const struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003069 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02003070 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003071
Matt Ropere8f1f022016-05-12 07:05:55 -07003072 pipe_wm = &cstate->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003073
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003074 drm_atomic_crtc_state_for_each_plane_state(plane, plane_state, &cstate->base) {
3075 const struct intel_plane_state *ps = to_intel_plane_state(plane_state);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003076
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003077 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003078 pristate = ps;
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003079 else if (plane->type == DRM_PLANE_TYPE_OVERLAY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003080 sprstate = ps;
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003081 else if (plane->type == DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003082 curstate = ps;
Matt Roper43d59ed2015-09-24 15:53:07 -07003083 }
3084
Matt Ropered4a6a72016-02-23 17:20:13 -08003085 pipe_wm->pipe_enabled = cstate->base.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003086 if (sprstate) {
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003087 pipe_wm->sprites_enabled = sprstate->base.visible;
3088 pipe_wm->sprites_scaled = sprstate->base.visible &&
3089 (drm_rect_width(&sprstate->base.dst) != drm_rect_width(&sprstate->base.src) >> 16 ||
3090 drm_rect_height(&sprstate->base.dst) != drm_rect_height(&sprstate->base.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003091 }
3092
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003093 usable_level = max_level;
3094
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003095 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003096 if (INTEL_GEN(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003097 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003098
3099 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08003100 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003101 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003102
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003103 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003104 ilk_compute_wm_level(dev_priv, intel_crtc, 0, cstate,
3105 pristate, sprstate, curstate, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003106
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003107 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjälä532f7a72016-04-29 17:31:17 +03003108 pipe_wm->linetime = hsw_compute_linetime_wm(cstate);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003109
Matt Ropered4a6a72016-02-23 17:20:13 -08003110 if (!ilk_validate_pipe_wm(dev, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01003111 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003112
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003113 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003114
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003115 for (level = 1; level <= usable_level; level++) {
3116 struct intel_wm_level *wm = &pipe_wm->wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003117
Matt Roper86c8bbb2015-09-24 15:53:16 -07003118 ilk_compute_wm_level(dev_priv, intel_crtc, level, cstate,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003119 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003120
3121 /*
3122 * Disable any watermark level that exceeds the
3123 * register maximums since such watermarks are
3124 * always invalid.
3125 */
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003126 if (!ilk_validate_wm_level(level, &max, wm)) {
3127 memset(wm, 0, sizeof(*wm));
3128 break;
3129 }
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003130 }
3131
Matt Roper86c8bbb2015-09-24 15:53:16 -07003132 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003133}
3134
3135/*
Matt Ropered4a6a72016-02-23 17:20:13 -08003136 * Build a set of 'intermediate' watermark values that satisfy both the old
3137 * state and the new state. These can be programmed to the hardware
3138 * immediately.
3139 */
3140static int ilk_compute_intermediate_wm(struct drm_device *dev,
3141 struct intel_crtc *intel_crtc,
3142 struct intel_crtc_state *newstate)
3143{
Matt Ropere8f1f022016-05-12 07:05:55 -07003144 struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003145 struct intel_atomic_state *intel_state =
3146 to_intel_atomic_state(newstate->base.state);
3147 const struct intel_crtc_state *oldstate =
3148 intel_atomic_get_old_crtc_state(intel_state, intel_crtc);
3149 const struct intel_pipe_wm *b = &oldstate->wm.ilk.optimal;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003150 int level, max_level = ilk_wm_max_level(to_i915(dev));
Matt Ropered4a6a72016-02-23 17:20:13 -08003151
3152 /*
3153 * Start with the final, target watermarks, then combine with the
3154 * currently active watermarks to get values that are safe both before
3155 * and after the vblank.
3156 */
Matt Ropere8f1f022016-05-12 07:05:55 -07003157 *a = newstate->wm.ilk.optimal;
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003158 if (!newstate->base.active || drm_atomic_crtc_needs_modeset(&newstate->base))
3159 return 0;
3160
Matt Ropered4a6a72016-02-23 17:20:13 -08003161 a->pipe_enabled |= b->pipe_enabled;
3162 a->sprites_enabled |= b->sprites_enabled;
3163 a->sprites_scaled |= b->sprites_scaled;
3164
3165 for (level = 0; level <= max_level; level++) {
3166 struct intel_wm_level *a_wm = &a->wm[level];
3167 const struct intel_wm_level *b_wm = &b->wm[level];
3168
3169 a_wm->enable &= b_wm->enable;
3170 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
3171 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
3172 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
3173 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
3174 }
3175
3176 /*
3177 * We need to make sure that these merged watermark values are
3178 * actually a valid configuration themselves. If they're not,
3179 * there's no safe way to transition from the old state to
3180 * the new state, so we need to fail the atomic transaction.
3181 */
3182 if (!ilk_validate_pipe_wm(dev, a))
3183 return -EINVAL;
3184
3185 /*
3186 * If our intermediate WM are identical to the final WM, then we can
3187 * omit the post-vblank programming; only update if it's different.
3188 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02003189 if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) != 0)
3190 newstate->wm.need_postvbl_update = true;
Matt Ropered4a6a72016-02-23 17:20:13 -08003191
3192 return 0;
3193}
3194
3195/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003196 * Merge the watermarks from all active pipes for a specific level.
3197 */
3198static void ilk_merge_wm_level(struct drm_device *dev,
3199 int level,
3200 struct intel_wm_level *ret_wm)
3201{
3202 const struct intel_crtc *intel_crtc;
3203
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003204 ret_wm->enable = true;
3205
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003206 for_each_intel_crtc(dev, intel_crtc) {
Matt Ropered4a6a72016-02-23 17:20:13 -08003207 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02003208 const struct intel_wm_level *wm = &active->wm[level];
3209
3210 if (!active->pipe_enabled)
3211 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003212
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003213 /*
3214 * The watermark values may have been used in the past,
3215 * so we must maintain them in the registers for some
3216 * time even if the level is now disabled.
3217 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003218 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003219 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003220
3221 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
3222 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
3223 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
3224 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
3225 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003226}
3227
3228/*
3229 * Merge all low power watermarks for all active pipes.
3230 */
3231static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003232 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02003233 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003234 struct intel_pipe_wm *merged)
3235{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003236 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003237 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003238 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003239
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003240 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003241 if ((INTEL_GEN(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003242 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03003243 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003244
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003245 /* ILK: FBC WM must be disabled always */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003246 merged->fbc_wm_enabled = INTEL_GEN(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003247
3248 /* merge each WM1+ level */
3249 for (level = 1; level <= max_level; level++) {
3250 struct intel_wm_level *wm = &merged->wm[level];
3251
3252 ilk_merge_wm_level(dev, level, wm);
3253
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003254 if (level > last_enabled_level)
3255 wm->enable = false;
3256 else if (!ilk_validate_wm_level(level, max, wm))
3257 /* make sure all following levels get disabled */
3258 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003259
3260 /*
3261 * The spec says it is preferred to disable
3262 * FBC WMs instead of disabling a WM level.
3263 */
3264 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003265 if (wm->enable)
3266 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003267 wm->fbc_val = 0;
3268 }
3269 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003270
3271 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
3272 /*
3273 * FIXME this is racy. FBC might get enabled later.
3274 * What we should check here is whether FBC can be
3275 * enabled sometime later.
3276 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003277 if (IS_GEN5(dev_priv) && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03003278 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003279 for (level = 2; level <= max_level; level++) {
3280 struct intel_wm_level *wm = &merged->wm[level];
3281
3282 wm->enable = false;
3283 }
3284 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003285}
3286
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003287static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
3288{
3289 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
3290 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
3291}
3292
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003293/* The value we need to program into the WM_LPx latency field */
3294static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
3295{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003296 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003297
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003298 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003299 return 2 * level;
3300 else
3301 return dev_priv->wm.pri_latency[level];
3302}
3303
Imre Deak820c1982013-12-17 14:46:36 +02003304static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03003305 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03003306 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02003307 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003308{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003309 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003310 struct intel_crtc *intel_crtc;
3311 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003312
Ville Syrjälä0362c782013-10-09 19:17:57 +03003313 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03003314 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003315
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003316 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03003317 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03003318 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003319
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003320 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003321
Ville Syrjälä0362c782013-10-09 19:17:57 +03003322 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03003323
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003324 /*
3325 * Maintain the watermark values even if the level is
3326 * disabled. Doing otherwise could cause underruns.
3327 */
3328 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003329 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07003330 (r->pri_val << WM1_LP_SR_SHIFT) |
3331 r->cur_val;
3332
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003333 if (r->enable)
3334 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
3335
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003336 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07003337 results->wm_lp[wm_lp - 1] |=
3338 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
3339 else
3340 results->wm_lp[wm_lp - 1] |=
3341 r->fbc_val << WM1_LP_FBC_SHIFT;
3342
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003343 /*
3344 * Always set WM1S_LP_EN when spr_val != 0, even if the
3345 * level is disabled. Doing otherwise could cause underruns.
3346 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003347 if (INTEL_GEN(dev_priv) <= 6 && r->spr_val) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003348 WARN_ON(wm_lp != 1);
3349 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
3350 } else
3351 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003352 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003353
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003354 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003355 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003356 enum pipe pipe = intel_crtc->pipe;
Matt Ropered4a6a72016-02-23 17:20:13 -08003357 const struct intel_wm_level *r =
3358 &intel_crtc->wm.active.ilk.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003359
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003360 if (WARN_ON(!r->enable))
3361 continue;
3362
Matt Ropered4a6a72016-02-23 17:20:13 -08003363 results->wm_linetime[pipe] = intel_crtc->wm.active.ilk.linetime;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003364
3365 results->wm_pipe[pipe] =
3366 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
3367 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
3368 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003369 }
3370}
3371
Paulo Zanoni861f3382013-05-31 10:19:21 -03003372/* Find the result with the highest level enabled. Check for enable_fbc_wm in
3373 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02003374static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003375 struct intel_pipe_wm *r1,
3376 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003377{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003378 int level, max_level = ilk_wm_max_level(to_i915(dev));
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003379 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003380
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003381 for (level = 1; level <= max_level; level++) {
3382 if (r1->wm[level].enable)
3383 level1 = level;
3384 if (r2->wm[level].enable)
3385 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003386 }
3387
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003388 if (level1 == level2) {
3389 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003390 return r2;
3391 else
3392 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003393 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03003394 return r1;
3395 } else {
3396 return r2;
3397 }
3398}
3399
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003400/* dirty bits used to track which watermarks need changes */
3401#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
3402#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
3403#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
3404#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
3405#define WM_DIRTY_FBC (1 << 24)
3406#define WM_DIRTY_DDB (1 << 25)
3407
Damien Lespiau055e3932014-08-18 13:49:10 +01003408static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02003409 const struct ilk_wm_values *old,
3410 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003411{
3412 unsigned int dirty = 0;
3413 enum pipe pipe;
3414 int wm_lp;
3415
Damien Lespiau055e3932014-08-18 13:49:10 +01003416 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003417 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
3418 dirty |= WM_DIRTY_LINETIME(pipe);
3419 /* Must disable LP1+ watermarks too */
3420 dirty |= WM_DIRTY_LP_ALL;
3421 }
3422
3423 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
3424 dirty |= WM_DIRTY_PIPE(pipe);
3425 /* Must disable LP1+ watermarks too */
3426 dirty |= WM_DIRTY_LP_ALL;
3427 }
3428 }
3429
3430 if (old->enable_fbc_wm != new->enable_fbc_wm) {
3431 dirty |= WM_DIRTY_FBC;
3432 /* Must disable LP1+ watermarks too */
3433 dirty |= WM_DIRTY_LP_ALL;
3434 }
3435
3436 if (old->partitioning != new->partitioning) {
3437 dirty |= WM_DIRTY_DDB;
3438 /* Must disable LP1+ watermarks too */
3439 dirty |= WM_DIRTY_LP_ALL;
3440 }
3441
3442 /* LP1+ watermarks already deemed dirty, no need to continue */
3443 if (dirty & WM_DIRTY_LP_ALL)
3444 return dirty;
3445
3446 /* Find the lowest numbered LP1+ watermark in need of an update... */
3447 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3448 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
3449 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
3450 break;
3451 }
3452
3453 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
3454 for (; wm_lp <= 3; wm_lp++)
3455 dirty |= WM_DIRTY_LP(wm_lp);
3456
3457 return dirty;
3458}
3459
Ville Syrjälä8553c182013-12-05 15:51:39 +02003460static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
3461 unsigned int dirty)
3462{
Imre Deak820c1982013-12-17 14:46:36 +02003463 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02003464 bool changed = false;
3465
3466 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
3467 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
3468 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
3469 changed = true;
3470 }
3471 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
3472 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
3473 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
3474 changed = true;
3475 }
3476 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
3477 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
3478 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
3479 changed = true;
3480 }
3481
3482 /*
3483 * Don't touch WM1S_LP_EN here.
3484 * Doing so could cause underruns.
3485 */
3486
3487 return changed;
3488}
3489
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003490/*
3491 * The spec says we shouldn't write when we don't need, because every write
3492 * causes WMs to be re-evaluated, expending some power.
3493 */
Imre Deak820c1982013-12-17 14:46:36 +02003494static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
3495 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003496{
Imre Deak820c1982013-12-17 14:46:36 +02003497 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003498 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003499 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003500
Damien Lespiau055e3932014-08-18 13:49:10 +01003501 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003502 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003503 return;
3504
Ville Syrjälä8553c182013-12-05 15:51:39 +02003505 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003506
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003507 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003508 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003509 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003510 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003511 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003512 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
3513
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003514 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003515 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003516 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003517 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003518 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003519 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
3520
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003521 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003522 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003523 val = I915_READ(WM_MISC);
3524 if (results->partitioning == INTEL_DDB_PART_1_2)
3525 val &= ~WM_MISC_DATA_PARTITION_5_6;
3526 else
3527 val |= WM_MISC_DATA_PARTITION_5_6;
3528 I915_WRITE(WM_MISC, val);
3529 } else {
3530 val = I915_READ(DISP_ARB_CTL2);
3531 if (results->partitioning == INTEL_DDB_PART_1_2)
3532 val &= ~DISP_DATA_PARTITION_5_6;
3533 else
3534 val |= DISP_DATA_PARTITION_5_6;
3535 I915_WRITE(DISP_ARB_CTL2, val);
3536 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003537 }
3538
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003539 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03003540 val = I915_READ(DISP_ARB_CTL);
3541 if (results->enable_fbc_wm)
3542 val &= ~DISP_FBC_WM_DIS;
3543 else
3544 val |= DISP_FBC_WM_DIS;
3545 I915_WRITE(DISP_ARB_CTL, val);
3546 }
3547
Imre Deak954911e2013-12-17 14:46:34 +02003548 if (dirty & WM_DIRTY_LP(1) &&
3549 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
3550 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
3551
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003552 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003553 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
3554 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
3555 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
3556 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
3557 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003558
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003559 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003560 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003561 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003562 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003563 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003564 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003565
3566 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003567}
3568
Matt Ropered4a6a72016-02-23 17:20:13 -08003569bool ilk_disable_lp_wm(struct drm_device *dev)
Ville Syrjälä8553c182013-12-05 15:51:39 +02003570{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003571 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003572
3573 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3574}
3575
Matt Roper024c9042015-09-24 15:53:11 -07003576/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003577 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3578 * so assume we'll always need it in order to avoid underruns.
3579 */
3580static bool skl_needs_memory_bw_wa(struct intel_atomic_state *state)
3581{
3582 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3583
Rodrigo Vivib976dc52017-01-23 10:32:37 -08003584 if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv))
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003585 return true;
3586
3587 return false;
3588}
3589
Paulo Zanoni56feca92016-09-22 18:00:28 -03003590static bool
3591intel_has_sagv(struct drm_i915_private *dev_priv)
3592{
Rodrigo Vivi01971812017-08-09 13:52:44 -07003593 if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv) ||
3594 IS_CANNONLAKE(dev_priv))
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003595 return true;
3596
3597 if (IS_SKYLAKE(dev_priv) &&
3598 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED)
3599 return true;
3600
3601 return false;
Paulo Zanoni56feca92016-09-22 18:00:28 -03003602}
3603
Lyude656d1b82016-08-17 15:55:54 -04003604/*
3605 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3606 * depending on power and performance requirements. The display engine access
3607 * to system memory is blocked during the adjustment time. Because of the
3608 * blocking time, having this enabled can cause full system hangs and/or pipe
3609 * underruns if we don't meet all of the following requirements:
3610 *
3611 * - <= 1 pipe enabled
3612 * - All planes can enable watermarks for latencies >= SAGV engine block time
3613 * - We're not using an interlaced display configuration
3614 */
3615int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003616intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003617{
3618 int ret;
3619
Paulo Zanoni56feca92016-09-22 18:00:28 -03003620 if (!intel_has_sagv(dev_priv))
3621 return 0;
3622
3623 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003624 return 0;
3625
3626 DRM_DEBUG_KMS("Enabling the SAGV\n");
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01003627 mutex_lock(&dev_priv->pcu_lock);
Lyude656d1b82016-08-17 15:55:54 -04003628
3629 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3630 GEN9_SAGV_ENABLE);
3631
3632 /* We don't need to wait for the SAGV when enabling */
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01003633 mutex_unlock(&dev_priv->pcu_lock);
Lyude656d1b82016-08-17 15:55:54 -04003634
3635 /*
3636 * Some skl systems, pre-release machines in particular,
3637 * don't actually have an SAGV.
3638 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003639 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003640 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003641 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003642 return 0;
3643 } else if (ret < 0) {
3644 DRM_ERROR("Failed to enable the SAGV\n");
3645 return ret;
3646 }
3647
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003648 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003649 return 0;
3650}
3651
Lyude656d1b82016-08-17 15:55:54 -04003652int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003653intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003654{
Imre Deakb3b8e992016-12-05 18:27:38 +02003655 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003656
Paulo Zanoni56feca92016-09-22 18:00:28 -03003657 if (!intel_has_sagv(dev_priv))
3658 return 0;
3659
3660 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003661 return 0;
3662
3663 DRM_DEBUG_KMS("Disabling the SAGV\n");
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01003664 mutex_lock(&dev_priv->pcu_lock);
Lyude656d1b82016-08-17 15:55:54 -04003665
3666 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003667 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3668 GEN9_SAGV_DISABLE,
3669 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3670 1);
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01003671 mutex_unlock(&dev_priv->pcu_lock);
Lyude656d1b82016-08-17 15:55:54 -04003672
Lyude656d1b82016-08-17 15:55:54 -04003673 /*
3674 * Some skl systems, pre-release machines in particular,
3675 * don't actually have an SAGV.
3676 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003677 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003678 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003679 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003680 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003681 } else if (ret < 0) {
3682 DRM_ERROR("Failed to disable the SAGV (%d)\n", ret);
3683 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003684 }
3685
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003686 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003687 return 0;
3688}
3689
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003690bool intel_can_enable_sagv(struct drm_atomic_state *state)
Lyude656d1b82016-08-17 15:55:54 -04003691{
3692 struct drm_device *dev = state->dev;
3693 struct drm_i915_private *dev_priv = to_i915(dev);
3694 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003695 struct intel_crtc *crtc;
3696 struct intel_plane *plane;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003697 struct intel_crtc_state *cstate;
Lyude656d1b82016-08-17 15:55:54 -04003698 enum pipe pipe;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003699 int level, latency;
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003700 int sagv_block_time_us = IS_GEN9(dev_priv) ? 30 : 20;
Lyude656d1b82016-08-17 15:55:54 -04003701
Paulo Zanoni56feca92016-09-22 18:00:28 -03003702 if (!intel_has_sagv(dev_priv))
3703 return false;
3704
Lyude656d1b82016-08-17 15:55:54 -04003705 /*
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003706 * SKL+ workaround: bspec recommends we disable the SAGV when we have
Lyude656d1b82016-08-17 15:55:54 -04003707 * more then one pipe enabled
3708 *
3709 * If there are no active CRTCs, no additional checks need be performed
3710 */
3711 if (hweight32(intel_state->active_crtcs) == 0)
3712 return true;
3713 else if (hweight32(intel_state->active_crtcs) > 1)
3714 return false;
3715
3716 /* Since we're now guaranteed to only have one active CRTC... */
3717 pipe = ffs(intel_state->active_crtcs) - 1;
Ville Syrjälä98187832016-10-31 22:37:10 +02003718 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003719 cstate = to_intel_crtc_state(crtc->base.state);
Lyude656d1b82016-08-17 15:55:54 -04003720
Paulo Zanonic89cadd2016-10-10 17:30:59 -03003721 if (crtc->base.state->adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003722 return false;
3723
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003724 for_each_intel_plane_on_crtc(dev, crtc, plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003725 struct skl_plane_wm *wm =
3726 &cstate->wm.skl.optimal.planes[plane->id];
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003727
Lyude656d1b82016-08-17 15:55:54 -04003728 /* Skip this plane if it's not enabled */
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003729 if (!wm->wm[0].plane_en)
Lyude656d1b82016-08-17 15:55:54 -04003730 continue;
3731
3732 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003733 for (level = ilk_wm_max_level(dev_priv);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003734 !wm->wm[level].plane_en; --level)
Lyude656d1b82016-08-17 15:55:54 -04003735 { }
3736
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003737 latency = dev_priv->wm.skl_latency[level];
3738
3739 if (skl_needs_memory_bw_wa(intel_state) &&
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003740 plane->base.state->fb->modifier ==
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003741 I915_FORMAT_MOD_X_TILED)
3742 latency += 15;
3743
Lyude656d1b82016-08-17 15:55:54 -04003744 /*
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003745 * If any of the planes on this pipe don't enable wm levels that
3746 * incur memory latencies higher than sagv_block_time_us we
3747 * can't enable the SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003748 */
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003749 if (latency < sagv_block_time_us)
Lyude656d1b82016-08-17 15:55:54 -04003750 return false;
3751 }
3752
3753 return true;
3754}
3755
Damien Lespiaub9cec072014-11-04 17:06:43 +00003756static void
3757skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
Matt Roper024c9042015-09-24 15:53:11 -07003758 const struct intel_crtc_state *cstate,
Matt Roperc107acf2016-05-12 07:06:01 -07003759 struct skl_ddb_entry *alloc, /* out */
3760 int *num_active /* out */)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003761{
Matt Roperc107acf2016-05-12 07:06:01 -07003762 struct drm_atomic_state *state = cstate->base.state;
3763 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
3764 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper024c9042015-09-24 15:53:11 -07003765 struct drm_crtc *for_crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003766 unsigned int pipe_size, ddb_size;
3767 int nth_active_pipe;
Matt Roperc107acf2016-05-12 07:06:01 -07003768
Matt Ropera6d3460e2016-05-12 07:06:04 -07003769 if (WARN_ON(!state) || !cstate->base.active) {
Damien Lespiaub9cec072014-11-04 17:06:43 +00003770 alloc->start = 0;
3771 alloc->end = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003772 *num_active = hweight32(dev_priv->active_crtcs);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003773 return;
3774 }
3775
Matt Ropera6d3460e2016-05-12 07:06:04 -07003776 if (intel_state->active_pipe_changes)
3777 *num_active = hweight32(intel_state->active_crtcs);
3778 else
3779 *num_active = hweight32(dev_priv->active_crtcs);
3780
Deepak M6f3fff62016-09-15 15:01:10 +05303781 ddb_size = INTEL_INFO(dev_priv)->ddb_size;
3782 WARN_ON(ddb_size == 0);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003783
3784 ddb_size -= 4; /* 4 blocks for bypass path allocation */
3785
Matt Roperc107acf2016-05-12 07:06:01 -07003786 /*
Matt Ropera6d3460e2016-05-12 07:06:04 -07003787 * If the state doesn't change the active CRTC's, then there's
3788 * no need to recalculate; the existing pipe allocation limits
3789 * should remain unchanged. Note that we're safe from racing
3790 * commits since any racing commit that changes the active CRTC
3791 * list would need to grab _all_ crtc locks, including the one
3792 * we currently hold.
Matt Roperc107acf2016-05-12 07:06:01 -07003793 */
Matt Ropera6d3460e2016-05-12 07:06:04 -07003794 if (!intel_state->active_pipe_changes) {
Maarten Lankhorst512b5522016-11-08 13:55:34 +01003795 /*
3796 * alloc may be cleared by clear_intel_crtc_state,
3797 * copy from old state to be sure
3798 */
3799 *alloc = to_intel_crtc_state(for_crtc->state)->wm.skl.ddb;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003800 return;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003801 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07003802
3803 nth_active_pipe = hweight32(intel_state->active_crtcs &
3804 (drm_crtc_mask(for_crtc) - 1));
3805 pipe_size = ddb_size / hweight32(intel_state->active_crtcs);
3806 alloc->start = nth_active_pipe * ddb_size / *num_active;
3807 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003808}
3809
Matt Roperc107acf2016-05-12 07:06:01 -07003810static unsigned int skl_cursor_allocation(int num_active)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003811{
Matt Roperc107acf2016-05-12 07:06:01 -07003812 if (num_active == 1)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003813 return 32;
3814
3815 return 8;
3816}
3817
Damien Lespiaua269c582014-11-04 17:06:49 +00003818static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
3819{
3820 entry->start = reg & 0x3ff;
3821 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00003822 if (entry->end)
3823 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00003824}
3825
Damien Lespiau08db6652014-11-04 17:06:52 +00003826void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
3827 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00003828{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003829 struct intel_crtc *crtc;
Damien Lespiaua269c582014-11-04 17:06:49 +00003830
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003831 memset(ddb, 0, sizeof(*ddb));
3832
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003833 for_each_intel_crtc(&dev_priv->drm, crtc) {
Imre Deak4d800032016-02-17 16:31:29 +02003834 enum intel_display_power_domain power_domain;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003835 enum plane_id plane_id;
3836 enum pipe pipe = crtc->pipe;
Imre Deak4d800032016-02-17 16:31:29 +02003837
3838 power_domain = POWER_DOMAIN_PIPE(pipe);
3839 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003840 continue;
3841
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003842 for_each_plane_id_on_crtc(crtc, plane_id) {
3843 u32 val;
Damien Lespiaua269c582014-11-04 17:06:49 +00003844
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003845 if (plane_id != PLANE_CURSOR)
3846 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
3847 else
3848 val = I915_READ(CUR_BUF_CFG(pipe));
3849
3850 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane_id], val);
3851 }
Imre Deak4d800032016-02-17 16:31:29 +02003852
3853 intel_display_power_put(dev_priv, power_domain);
Damien Lespiaua269c582014-11-04 17:06:49 +00003854 }
3855}
3856
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003857/*
3858 * Determines the downscale amount of a plane for the purposes of watermark calculations.
3859 * The bspec defines downscale amount as:
3860 *
3861 * """
3862 * Horizontal down scale amount = maximum[1, Horizontal source size /
3863 * Horizontal destination size]
3864 * Vertical down scale amount = maximum[1, Vertical source size /
3865 * Vertical destination size]
3866 * Total down scale amount = Horizontal down scale amount *
3867 * Vertical down scale amount
3868 * """
3869 *
3870 * Return value is provided in 16.16 fixed point form to retain fractional part.
3871 * Caller should take care of dividing & rounding off the value.
3872 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303873static uint_fixed_16_16_t
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003874skl_plane_downscale_amount(const struct intel_crtc_state *cstate,
3875 const struct intel_plane_state *pstate)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003876{
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003877 struct intel_plane *plane = to_intel_plane(pstate->base.plane);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003878 uint32_t src_w, src_h, dst_w, dst_h;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303879 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
3880 uint_fixed_16_16_t downscale_h, downscale_w;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003881
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003882 if (WARN_ON(!intel_wm_plane_visible(cstate, pstate)))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303883 return u32_to_fixed16(0);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003884
3885 /* n.b., src is 16.16 fixed point, dst is whole integer */
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003886 if (plane->id == PLANE_CURSOR) {
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03003887 /*
3888 * Cursors only support 0/180 degree rotation,
3889 * hence no need to account for rotation here.
3890 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303891 src_w = pstate->base.src_w >> 16;
3892 src_h = pstate->base.src_h >> 16;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003893 dst_w = pstate->base.crtc_w;
3894 dst_h = pstate->base.crtc_h;
3895 } else {
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03003896 /*
3897 * Src coordinates are already rotated by 270 degrees for
3898 * the 90/270 degree plane rotation cases (to match the
3899 * GTT mapping), hence no need to account for rotation here.
3900 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303901 src_w = drm_rect_width(&pstate->base.src) >> 16;
3902 src_h = drm_rect_height(&pstate->base.src) >> 16;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003903 dst_w = drm_rect_width(&pstate->base.dst);
3904 dst_h = drm_rect_height(&pstate->base.dst);
3905 }
3906
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303907 fp_w_ratio = div_fixed16(src_w, dst_w);
3908 fp_h_ratio = div_fixed16(src_h, dst_h);
3909 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
3910 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003911
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303912 return mul_fixed16(downscale_w, downscale_h);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003913}
3914
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303915static uint_fixed_16_16_t
3916skl_pipe_downscale_amount(const struct intel_crtc_state *crtc_state)
3917{
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303918 uint_fixed_16_16_t pipe_downscale = u32_to_fixed16(1);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303919
3920 if (!crtc_state->base.enable)
3921 return pipe_downscale;
3922
3923 if (crtc_state->pch_pfit.enabled) {
3924 uint32_t src_w, src_h, dst_w, dst_h;
3925 uint32_t pfit_size = crtc_state->pch_pfit.size;
3926 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
3927 uint_fixed_16_16_t downscale_h, downscale_w;
3928
3929 src_w = crtc_state->pipe_src_w;
3930 src_h = crtc_state->pipe_src_h;
3931 dst_w = pfit_size >> 16;
3932 dst_h = pfit_size & 0xffff;
3933
3934 if (!dst_w || !dst_h)
3935 return pipe_downscale;
3936
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303937 fp_w_ratio = div_fixed16(src_w, dst_w);
3938 fp_h_ratio = div_fixed16(src_h, dst_h);
3939 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
3940 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303941
3942 pipe_downscale = mul_fixed16(downscale_w, downscale_h);
3943 }
3944
3945 return pipe_downscale;
3946}
3947
3948int skl_check_pipe_max_pixel_rate(struct intel_crtc *intel_crtc,
3949 struct intel_crtc_state *cstate)
3950{
Rodrigo Vivi43037c82017-10-03 15:31:42 -07003951 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303952 struct drm_crtc_state *crtc_state = &cstate->base;
3953 struct drm_atomic_state *state = crtc_state->state;
3954 struct drm_plane *plane;
3955 const struct drm_plane_state *pstate;
3956 struct intel_plane_state *intel_pstate;
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02003957 int crtc_clock, dotclk;
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303958 uint32_t pipe_max_pixel_rate;
3959 uint_fixed_16_16_t pipe_downscale;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303960 uint_fixed_16_16_t max_downscale = u32_to_fixed16(1);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303961
3962 if (!cstate->base.enable)
3963 return 0;
3964
3965 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, crtc_state) {
3966 uint_fixed_16_16_t plane_downscale;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303967 uint_fixed_16_16_t fp_9_div_8 = div_fixed16(9, 8);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303968 int bpp;
3969
3970 if (!intel_wm_plane_visible(cstate,
3971 to_intel_plane_state(pstate)))
3972 continue;
3973
3974 if (WARN_ON(!pstate->fb))
3975 return -EINVAL;
3976
3977 intel_pstate = to_intel_plane_state(pstate);
3978 plane_downscale = skl_plane_downscale_amount(cstate,
3979 intel_pstate);
3980 bpp = pstate->fb->format->cpp[0] * 8;
3981 if (bpp == 64)
3982 plane_downscale = mul_fixed16(plane_downscale,
3983 fp_9_div_8);
3984
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303985 max_downscale = max_fixed16(plane_downscale, max_downscale);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303986 }
3987 pipe_downscale = skl_pipe_downscale_amount(cstate);
3988
3989 pipe_downscale = mul_fixed16(pipe_downscale, max_downscale);
3990
3991 crtc_clock = crtc_state->adjusted_mode.crtc_clock;
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02003992 dotclk = to_intel_atomic_state(state)->cdclk.logical.cdclk;
3993
Rodrigo Vivi43037c82017-10-03 15:31:42 -07003994 if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10)
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02003995 dotclk *= 2;
3996
3997 pipe_max_pixel_rate = div_round_up_u32_fixed16(dotclk, pipe_downscale);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303998
3999 if (pipe_max_pixel_rate < crtc_clock) {
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02004000 DRM_DEBUG_KMS("Max supported pixel clock with scaling exceeded\n");
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05304001 return -EINVAL;
4002 }
4003
4004 return 0;
4005}
4006
Damien Lespiaub9cec072014-11-04 17:06:43 +00004007static unsigned int
Matt Roper024c9042015-09-24 15:53:11 -07004008skl_plane_relative_data_rate(const struct intel_crtc_state *cstate,
4009 const struct drm_plane_state *pstate,
4010 int y)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004011{
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004012 struct intel_plane *plane = to_intel_plane(pstate->plane);
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004013 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304014 uint32_t data_rate;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004015 uint32_t width = 0, height = 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02004016 struct drm_framebuffer *fb;
4017 u32 format;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304018 uint_fixed_16_16_t down_scale_amount;
Matt Ropera1de91e2016-05-12 07:05:57 -07004019
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004020 if (!intel_pstate->base.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07004021 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02004022
4023 fb = pstate->fb;
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004024 format = fb->format->format;
Ville Syrjälä83054942016-11-18 21:53:00 +02004025
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004026 if (plane->id == PLANE_CURSOR)
Matt Ropera1de91e2016-05-12 07:05:57 -07004027 return 0;
4028 if (y && format != DRM_FORMAT_NV12)
4029 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004030
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004031 /*
4032 * Src coordinates are already rotated by 270 degrees for
4033 * the 90/270 degree plane rotation cases (to match the
4034 * GTT mapping), hence no need to account for rotation here.
4035 */
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004036 width = drm_rect_width(&intel_pstate->base.src) >> 16;
4037 height = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004038
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004039 /* for planar format */
Matt Ropera1de91e2016-05-12 07:05:57 -07004040 if (format == DRM_FORMAT_NV12) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004041 if (y) /* y-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004042 data_rate = width * height *
Ville Syrjälä353c8592016-12-14 23:30:57 +02004043 fb->format->cpp[0];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004044 else /* uv-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004045 data_rate = (width / 2) * (height / 2) *
Ville Syrjälä353c8592016-12-14 23:30:57 +02004046 fb->format->cpp[1];
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004047 } else {
4048 /* for packed formats */
Ville Syrjälä353c8592016-12-14 23:30:57 +02004049 data_rate = width * height * fb->format->cpp[0];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004050 }
4051
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004052 down_scale_amount = skl_plane_downscale_amount(cstate, intel_pstate);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004053
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304054 return mul_round_up_u32_fixed16(data_rate, down_scale_amount);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004055}
4056
4057/*
4058 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
4059 * a 8192x4096@32bpp framebuffer:
4060 * 3 * 4096 * 8192 * 4 < 2^32
4061 */
4062static unsigned int
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004063skl_get_total_relative_data_rate(struct intel_crtc_state *intel_cstate,
4064 unsigned *plane_data_rate,
4065 unsigned *plane_y_data_rate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004066{
Matt Roper9c74d822016-05-12 07:05:58 -07004067 struct drm_crtc_state *cstate = &intel_cstate->base;
4068 struct drm_atomic_state *state = cstate->state;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02004069 struct drm_plane *plane;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02004070 const struct drm_plane_state *pstate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004071 unsigned int total_data_rate = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07004072
4073 if (WARN_ON(!state))
4074 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004075
Matt Ropera1de91e2016-05-12 07:05:57 -07004076 /* Calculate and cache data rate for each plane */
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02004077 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, cstate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004078 enum plane_id plane_id = to_intel_plane(plane)->id;
4079 unsigned int rate;
Matt Roper024c9042015-09-24 15:53:11 -07004080
Matt Ropera6d3460e2016-05-12 07:06:04 -07004081 /* packed/uv */
4082 rate = skl_plane_relative_data_rate(intel_cstate,
4083 pstate, 0);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004084 plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004085
4086 total_data_rate += rate;
Matt Roper9c74d822016-05-12 07:05:58 -07004087
Matt Ropera6d3460e2016-05-12 07:06:04 -07004088 /* y-plane */
4089 rate = skl_plane_relative_data_rate(intel_cstate,
4090 pstate, 1);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004091 plane_y_data_rate[plane_id] = rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07004092
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004093 total_data_rate += rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004094 }
4095
4096 return total_data_rate;
4097}
4098
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004099static uint16_t
4100skl_ddb_min_alloc(const struct drm_plane_state *pstate,
4101 const int y)
4102{
4103 struct drm_framebuffer *fb = pstate->fb;
4104 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
4105 uint32_t src_w, src_h;
4106 uint32_t min_scanlines = 8;
4107 uint8_t plane_bpp;
4108
4109 if (WARN_ON(!fb))
4110 return 0;
4111
4112 /* For packed formats, no y-plane, return 0 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004113 if (y && fb->format->format != DRM_FORMAT_NV12)
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004114 return 0;
4115
4116 /* For Non Y-tile return 8-blocks */
Ville Syrjäläbae781b2016-11-16 13:33:16 +02004117 if (fb->modifier != I915_FORMAT_MOD_Y_TILED &&
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07004118 fb->modifier != I915_FORMAT_MOD_Yf_TILED &&
4119 fb->modifier != I915_FORMAT_MOD_Y_TILED_CCS &&
4120 fb->modifier != I915_FORMAT_MOD_Yf_TILED_CCS)
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004121 return 8;
4122
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004123 /*
4124 * Src coordinates are already rotated by 270 degrees for
4125 * the 90/270 degree plane rotation cases (to match the
4126 * GTT mapping), hence no need to account for rotation here.
4127 */
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004128 src_w = drm_rect_width(&intel_pstate->base.src) >> 16;
4129 src_h = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004130
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004131 /* Halve UV plane width and height for NV12 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004132 if (fb->format->format == DRM_FORMAT_NV12 && !y) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004133 src_w /= 2;
4134 src_h /= 2;
4135 }
4136
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004137 if (fb->format->format == DRM_FORMAT_NV12 && !y)
Ville Syrjälä353c8592016-12-14 23:30:57 +02004138 plane_bpp = fb->format->cpp[1];
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004139 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02004140 plane_bpp = fb->format->cpp[0];
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004141
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03004142 if (drm_rotation_90_or_270(pstate->rotation)) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004143 switch (plane_bpp) {
4144 case 1:
4145 min_scanlines = 32;
4146 break;
4147 case 2:
4148 min_scanlines = 16;
4149 break;
4150 case 4:
4151 min_scanlines = 8;
4152 break;
4153 case 8:
4154 min_scanlines = 4;
4155 break;
4156 default:
4157 WARN(1, "Unsupported pixel depth %u for rotation",
4158 plane_bpp);
4159 min_scanlines = 32;
4160 }
4161 }
4162
4163 return DIV_ROUND_UP((4 * src_w * plane_bpp), 512) * min_scanlines/4 + 3;
4164}
4165
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004166static void
4167skl_ddb_calc_min(const struct intel_crtc_state *cstate, int num_active,
4168 uint16_t *minimum, uint16_t *y_minimum)
4169{
4170 const struct drm_plane_state *pstate;
4171 struct drm_plane *plane;
4172
4173 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, &cstate->base) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004174 enum plane_id plane_id = to_intel_plane(plane)->id;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004175
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004176 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004177 continue;
4178
4179 if (!pstate->visible)
4180 continue;
4181
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004182 minimum[plane_id] = skl_ddb_min_alloc(pstate, 0);
4183 y_minimum[plane_id] = skl_ddb_min_alloc(pstate, 1);
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004184 }
4185
4186 minimum[PLANE_CURSOR] = skl_cursor_allocation(num_active);
4187}
4188
Matt Roperc107acf2016-05-12 07:06:01 -07004189static int
Matt Roper024c9042015-09-24 15:53:11 -07004190skl_allocate_pipe_ddb(struct intel_crtc_state *cstate,
Damien Lespiaub9cec072014-11-04 17:06:43 +00004191 struct skl_ddb_allocation *ddb /* out */)
4192{
Matt Roperc107acf2016-05-12 07:06:01 -07004193 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07004194 struct drm_crtc *crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004195 struct drm_device *dev = crtc->dev;
4196 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4197 enum pipe pipe = intel_crtc->pipe;
Lyudece0ba282016-09-15 10:46:35 -04004198 struct skl_ddb_entry *alloc = &cstate->wm.skl.ddb;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004199 uint16_t alloc_size, start;
Maarten Lankhorstfefdd812016-10-26 15:41:33 +02004200 uint16_t minimum[I915_MAX_PLANES] = {};
4201 uint16_t y_minimum[I915_MAX_PLANES] = {};
Damien Lespiaub9cec072014-11-04 17:06:43 +00004202 unsigned int total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004203 enum plane_id plane_id;
Matt Roperc107acf2016-05-12 07:06:01 -07004204 int num_active;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004205 unsigned plane_data_rate[I915_MAX_PLANES] = {};
4206 unsigned plane_y_data_rate[I915_MAX_PLANES] = {};
Kumar, Mahesh5ba6faa2017-05-17 17:28:26 +05304207 uint16_t total_min_blocks = 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004208
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004209 /* Clear the partitioning for disabled planes. */
4210 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
4211 memset(ddb->y_plane[pipe], 0, sizeof(ddb->y_plane[pipe]));
4212
Matt Ropera6d3460e2016-05-12 07:06:04 -07004213 if (WARN_ON(!state))
4214 return 0;
4215
Matt Roperc107acf2016-05-12 07:06:01 -07004216 if (!cstate->base.active) {
Lyudece0ba282016-09-15 10:46:35 -04004217 alloc->start = alloc->end = 0;
Matt Roperc107acf2016-05-12 07:06:01 -07004218 return 0;
4219 }
4220
Matt Ropera6d3460e2016-05-12 07:06:04 -07004221 skl_ddb_get_pipe_allocation_limits(dev, cstate, alloc, &num_active);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00004222 alloc_size = skl_ddb_entry_size(alloc);
Kumar, Mahesh336031e2017-05-17 17:28:25 +05304223 if (alloc_size == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004224 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004225
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004226 skl_ddb_calc_min(cstate, num_active, minimum, y_minimum);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004227
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004228 /*
4229 * 1. Allocate the mininum required blocks for each active plane
4230 * and allocate the cursor, it doesn't require extra allocation
4231 * proportional to the data rate.
4232 */
Damien Lespiaub9cec072014-11-04 17:06:43 +00004233
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004234 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Kumar, Mahesh5ba6faa2017-05-17 17:28:26 +05304235 total_min_blocks += minimum[plane_id];
4236 total_min_blocks += y_minimum[plane_id];
Damien Lespiau80958152015-02-09 13:35:10 +00004237 }
4238
Kumar, Mahesh5ba6faa2017-05-17 17:28:26 +05304239 if (total_min_blocks > alloc_size) {
4240 DRM_DEBUG_KMS("Requested display configuration exceeds system DDB limitations");
4241 DRM_DEBUG_KMS("minimum required %d/%d\n", total_min_blocks,
4242 alloc_size);
4243 return -EINVAL;
4244 }
4245
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004246 alloc_size -= total_min_blocks;
4247 ddb->plane[pipe][PLANE_CURSOR].start = alloc->end - minimum[PLANE_CURSOR];
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004248 ddb->plane[pipe][PLANE_CURSOR].end = alloc->end;
4249
Damien Lespiaub9cec072014-11-04 17:06:43 +00004250 /*
Damien Lespiau80958152015-02-09 13:35:10 +00004251 * 2. Distribute the remaining space in proportion to the amount of
4252 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00004253 *
4254 * FIXME: we may not allocate every single block here.
4255 */
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004256 total_data_rate = skl_get_total_relative_data_rate(cstate,
4257 plane_data_rate,
4258 plane_y_data_rate);
Matt Ropera1de91e2016-05-12 07:05:57 -07004259 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004260 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004261
Damien Lespiau34bb56a2014-11-04 17:07:01 +00004262 start = alloc->start;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004263 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004264 unsigned int data_rate, y_data_rate;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004265 uint16_t plane_blocks, y_plane_blocks = 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004266
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004267 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004268 continue;
4269
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004270 data_rate = plane_data_rate[plane_id];
Damien Lespiaub9cec072014-11-04 17:06:43 +00004271
4272 /*
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004273 * allocation for (packed formats) or (uv-plane part of planar format):
Damien Lespiaub9cec072014-11-04 17:06:43 +00004274 * promote the expression to 64 bits to avoid overflowing, the
4275 * result is < available as data_rate / total_data_rate < 1
4276 */
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004277 plane_blocks = minimum[plane_id];
4278 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
4279 total_data_rate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004280
Matt Roperc107acf2016-05-12 07:06:01 -07004281 /* Leave disabled planes at (0,0) */
4282 if (data_rate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004283 ddb->plane[pipe][plane_id].start = start;
4284 ddb->plane[pipe][plane_id].end = start + plane_blocks;
Matt Roperc107acf2016-05-12 07:06:01 -07004285 }
Damien Lespiaub9cec072014-11-04 17:06:43 +00004286
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004287 start += plane_blocks;
4288
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004289 /*
4290 * allocation for y_plane part of planar format:
4291 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004292 y_data_rate = plane_y_data_rate[plane_id];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004293
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004294 y_plane_blocks = y_minimum[plane_id];
4295 y_plane_blocks += div_u64((uint64_t)alloc_size * y_data_rate,
4296 total_data_rate);
4297
Matt Roperc107acf2016-05-12 07:06:01 -07004298 if (y_data_rate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004299 ddb->y_plane[pipe][plane_id].start = start;
4300 ddb->y_plane[pipe][plane_id].end = start + y_plane_blocks;
Matt Roperc107acf2016-05-12 07:06:01 -07004301 }
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004302
4303 start += y_plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004304 }
4305
Matt Roperc107acf2016-05-12 07:06:01 -07004306 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004307}
4308
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004309/*
4310 * 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 +02004311 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004312 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
4313 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
4314*/
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07004315static uint_fixed_16_16_t
4316skl_wm_method1(const struct drm_i915_private *dev_priv, uint32_t pixel_rate,
4317 uint8_t cpp, uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004318{
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304319 uint32_t wm_intermediate_val;
4320 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004321
4322 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304323 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004324
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304325 wm_intermediate_val = latency * pixel_rate * cpp;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304326 ret = div_fixed16(wm_intermediate_val, 1000 * 512);
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07004327
4328 if (INTEL_GEN(dev_priv) >= 10)
4329 ret = add_fixed16_u32(ret, 1);
4330
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004331 return ret;
4332}
4333
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304334static uint_fixed_16_16_t skl_wm_method2(uint32_t pixel_rate,
4335 uint32_t pipe_htotal,
4336 uint32_t latency,
4337 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004338{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004339 uint32_t wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304340 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004341
4342 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304343 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004344
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004345 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304346 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
4347 pipe_htotal * 1000);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304348 ret = mul_u32_fixed16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004349 return ret;
4350}
4351
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304352static uint_fixed_16_16_t
4353intel_get_linetime_us(struct intel_crtc_state *cstate)
4354{
4355 uint32_t pixel_rate;
4356 uint32_t crtc_htotal;
4357 uint_fixed_16_16_t linetime_us;
4358
4359 if (!cstate->base.active)
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304360 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304361
4362 pixel_rate = cstate->pixel_rate;
4363
4364 if (WARN_ON(pixel_rate == 0))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304365 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304366
4367 crtc_htotal = cstate->base.adjusted_mode.crtc_htotal;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304368 linetime_us = div_fixed16(crtc_htotal * 1000, pixel_rate);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304369
4370 return linetime_us;
4371}
4372
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304373static uint32_t
4374skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *cstate,
4375 const struct intel_plane_state *pstate)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004376{
4377 uint64_t adjusted_pixel_rate;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304378 uint_fixed_16_16_t downscale_amount;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004379
4380 /* Shouldn't reach here on disabled planes... */
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004381 if (WARN_ON(!intel_wm_plane_visible(cstate, pstate)))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004382 return 0;
4383
4384 /*
4385 * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
4386 * with additional adjustments for plane-specific scaling.
4387 */
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02004388 adjusted_pixel_rate = cstate->pixel_rate;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004389 downscale_amount = skl_plane_downscale_amount(cstate, pstate);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004390
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304391 return mul_round_up_u32_fixed16(adjusted_pixel_rate,
4392 downscale_amount);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004393}
4394
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304395static int
4396skl_compute_plane_wm_params(const struct drm_i915_private *dev_priv,
4397 struct intel_crtc_state *cstate,
4398 const struct intel_plane_state *intel_pstate,
4399 struct skl_wm_params *wp)
4400{
4401 struct intel_plane *plane = to_intel_plane(intel_pstate->base.plane);
4402 const struct drm_plane_state *pstate = &intel_pstate->base;
4403 const struct drm_framebuffer *fb = pstate->fb;
4404 uint32_t interm_pbpl;
4405 struct intel_atomic_state *state =
4406 to_intel_atomic_state(cstate->base.state);
4407 bool apply_memory_bw_wa = skl_needs_memory_bw_wa(state);
4408
4409 if (!intel_wm_plane_visible(cstate, intel_pstate))
4410 return 0;
4411
4412 wp->y_tiled = fb->modifier == I915_FORMAT_MOD_Y_TILED ||
4413 fb->modifier == I915_FORMAT_MOD_Yf_TILED ||
4414 fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
4415 fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
4416 wp->x_tiled = fb->modifier == I915_FORMAT_MOD_X_TILED;
4417 wp->rc_surface = fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
4418 fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
4419
4420 if (plane->id == PLANE_CURSOR) {
4421 wp->width = intel_pstate->base.crtc_w;
4422 } else {
4423 /*
4424 * Src coordinates are already rotated by 270 degrees for
4425 * the 90/270 degree plane rotation cases (to match the
4426 * GTT mapping), hence no need to account for rotation here.
4427 */
4428 wp->width = drm_rect_width(&intel_pstate->base.src) >> 16;
4429 }
4430
4431 wp->cpp = (fb->format->format == DRM_FORMAT_NV12) ? fb->format->cpp[1] :
4432 fb->format->cpp[0];
4433 wp->plane_pixel_rate = skl_adjusted_plane_pixel_rate(cstate,
4434 intel_pstate);
4435
4436 if (drm_rotation_90_or_270(pstate->rotation)) {
4437
4438 switch (wp->cpp) {
4439 case 1:
4440 wp->y_min_scanlines = 16;
4441 break;
4442 case 2:
4443 wp->y_min_scanlines = 8;
4444 break;
4445 case 4:
4446 wp->y_min_scanlines = 4;
4447 break;
4448 default:
4449 MISSING_CASE(wp->cpp);
4450 return -EINVAL;
4451 }
4452 } else {
4453 wp->y_min_scanlines = 4;
4454 }
4455
4456 if (apply_memory_bw_wa)
4457 wp->y_min_scanlines *= 2;
4458
4459 wp->plane_bytes_per_line = wp->width * wp->cpp;
4460 if (wp->y_tiled) {
4461 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line *
4462 wp->y_min_scanlines, 512);
4463
4464 if (INTEL_GEN(dev_priv) >= 10)
4465 interm_pbpl++;
4466
4467 wp->plane_blocks_per_line = div_fixed16(interm_pbpl,
4468 wp->y_min_scanlines);
4469 } else if (wp->x_tiled && IS_GEN9(dev_priv)) {
4470 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line, 512);
4471 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
4472 } else {
4473 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line, 512) + 1;
4474 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
4475 }
4476
4477 wp->y_tile_minimum = mul_u32_fixed16(wp->y_min_scanlines,
4478 wp->plane_blocks_per_line);
4479 wp->linetime_us = fixed16_to_u32_round_up(
4480 intel_get_linetime_us(cstate));
4481
4482 return 0;
4483}
4484
Matt Roper55994c22016-05-12 07:06:08 -07004485static int skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
4486 struct intel_crtc_state *cstate,
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304487 const struct intel_plane_state *intel_pstate,
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004488 uint16_t ddb_allocation,
Matt Roper55994c22016-05-12 07:06:08 -07004489 int level,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304490 const struct skl_wm_params *wp,
Matt Roper55994c22016-05-12 07:06:08 -07004491 uint16_t *out_blocks, /* out */
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004492 uint8_t *out_lines, /* out */
4493 bool *enabled /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004494{
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304495 const struct drm_plane_state *pstate = &intel_pstate->base;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004496 uint32_t latency = dev_priv->wm.skl_latency[level];
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304497 uint_fixed_16_16_t method1, method2;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304498 uint_fixed_16_16_t selected_result;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004499 uint32_t res_blocks, res_lines;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03004500 struct intel_atomic_state *state =
4501 to_intel_atomic_state(cstate->base.state);
4502 bool apply_memory_bw_wa = skl_needs_memory_bw_wa(state);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004503
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004504 if (latency == 0 ||
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004505 !intel_wm_plane_visible(cstate, intel_pstate)) {
4506 *enabled = false;
Matt Roper55994c22016-05-12 07:06:08 -07004507 return 0;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004508 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004509
Rodrigo Vivi82525c12017-06-08 08:50:00 -07004510 /* Display WA #1141: kbl,cfl */
Kumar, Maheshd86ba622017-08-17 19:15:26 +05304511 if ((IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv) ||
4512 IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_B0)) &&
Rodrigo Vivi82525c12017-06-08 08:50:00 -07004513 dev_priv->ipc_enabled)
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05304514 latency += 4;
4515
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304516 if (apply_memory_bw_wa && wp->x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03004517 latency += 15;
4518
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304519 method1 = skl_wm_method1(dev_priv, wp->plane_pixel_rate,
4520 wp->cpp, latency);
4521 method2 = skl_wm_method2(wp->plane_pixel_rate,
Matt Roper024c9042015-09-24 15:53:11 -07004522 cstate->base.adjusted_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004523 latency,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304524 wp->plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004525
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304526 if (wp->y_tiled) {
4527 selected_result = max_fixed16(method2, wp->y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004528 } else {
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304529 if ((wp->cpp * cstate->base.adjusted_mode.crtc_htotal /
4530 512 < 1) && (wp->plane_bytes_per_line / 512 < 1))
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03004531 selected_result = method2;
Maarten Lankhorst54d20ed2017-07-17 14:02:30 +02004532 else if (ddb_allocation >=
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304533 fixed16_to_u32_round_up(wp->plane_blocks_per_line))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304534 selected_result = min_fixed16(method1, method2);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304535 else if (latency >= wp->linetime_us)
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304536 selected_result = min_fixed16(method1, method2);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004537 else
4538 selected_result = method1;
4539 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004540
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304541 res_blocks = fixed16_to_u32_round_up(selected_result) + 1;
Kumar, Maheshd273ecc2017-05-17 17:28:22 +05304542 res_lines = div_round_up_fixed16(selected_result,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304543 wp->plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00004544
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07004545 /* Display WA #1125: skl,bxt,kbl,glk */
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304546 if (level == 0 && wp->rc_surface)
4547 res_blocks += fixed16_to_u32_round_up(wp->y_tile_minimum);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07004548
4549 /* Display WA #1126: skl,bxt,kbl,glk */
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004550 if (level >= 1 && level <= 7) {
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304551 if (wp->y_tiled) {
4552 res_blocks += fixed16_to_u32_round_up(
4553 wp->y_tile_minimum);
4554 res_lines += wp->y_min_scanlines;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004555 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004556 res_blocks++;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004557 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004558 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004559
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004560 if (res_blocks >= ddb_allocation || res_lines > 31) {
4561 *enabled = false;
Matt Roper6b6bada2016-05-12 07:06:10 -07004562
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004563 /*
4564 * If there are no valid level 0 watermarks, then we can't
4565 * support this display configuration.
4566 */
4567 if (level) {
4568 return 0;
4569 } else {
4570 struct drm_plane *plane = pstate->plane;
4571
4572 DRM_DEBUG_KMS("Requested display configuration exceeds system watermark limitations\n");
4573 DRM_DEBUG_KMS("[PLANE:%d:%s] blocks required = %u/%u, lines required = %u/31\n",
4574 plane->base.id, plane->name,
4575 res_blocks, ddb_allocation, res_lines);
4576 return -EINVAL;
4577 }
Matt Roper55994c22016-05-12 07:06:08 -07004578 }
Damien Lespiaue6d66172014-11-04 17:06:55 +00004579
4580 *out_blocks = res_blocks;
4581 *out_lines = res_lines;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004582 *enabled = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004583
Matt Roper55994c22016-05-12 07:06:08 -07004584 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004585}
4586
Matt Roperf4a96752016-05-12 07:06:06 -07004587static int
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304588skl_compute_wm_levels(const struct drm_i915_private *dev_priv,
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004589 struct skl_ddb_allocation *ddb,
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304590 struct intel_crtc_state *cstate,
4591 const struct intel_plane_state *intel_pstate,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304592 const struct skl_wm_params *wm_params,
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304593 struct skl_plane_wm *wm)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004594{
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004595 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
4596 struct drm_plane *plane = intel_pstate->base.plane;
4597 struct intel_plane *intel_plane = to_intel_plane(plane);
4598 uint16_t ddb_blocks;
4599 enum pipe pipe = intel_crtc->pipe;
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304600 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Roper55994c22016-05-12 07:06:08 -07004601 int ret;
Lyudea62163e2016-10-04 14:28:20 -04004602
Kumar, Mahesh7b751192017-05-17 17:28:24 +05304603 if (WARN_ON(!intel_pstate->base.fb))
4604 return -EINVAL;
Matt Roper024c9042015-09-24 15:53:11 -07004605
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004606 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][intel_plane->id]);
4607
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304608 for (level = 0; level <= max_level; level++) {
4609 struct skl_wm_level *result = &wm->wm[level];
4610
4611 ret = skl_compute_plane_wm(dev_priv,
4612 cstate,
4613 intel_pstate,
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004614 ddb_blocks,
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304615 level,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304616 wm_params,
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304617 &result->plane_res_b,
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004618 &result->plane_res_l,
4619 &result->plane_en);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304620 if (ret)
4621 return ret;
4622 }
Matt Roperf4a96752016-05-12 07:06:06 -07004623
4624 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004625}
4626
Damien Lespiau407b50f2014-11-04 17:06:57 +00004627static uint32_t
Matt Roper024c9042015-09-24 15:53:11 -07004628skl_compute_linetime_wm(struct intel_crtc_state *cstate)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004629{
Mahesh Kumara3a89862016-12-01 21:19:34 +05304630 struct drm_atomic_state *state = cstate->base.state;
4631 struct drm_i915_private *dev_priv = to_i915(state->dev);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304632 uint_fixed_16_16_t linetime_us;
Mahesh Kumara3a89862016-12-01 21:19:34 +05304633 uint32_t linetime_wm;
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03004634
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304635 linetime_us = intel_get_linetime_us(cstate);
4636
4637 if (is_fixed16_zero(linetime_us))
Damien Lespiau407b50f2014-11-04 17:06:57 +00004638 return 0;
4639
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304640 linetime_wm = fixed16_to_u32_round_up(mul_u32_fixed16(8, linetime_us));
Mahesh Kumara3a89862016-12-01 21:19:34 +05304641
Kumar, Mahesh446e8502017-08-17 19:15:25 +05304642 /* Display WA #1135: bxt:ALL GLK:ALL */
4643 if ((IS_BROXTON(dev_priv) || IS_GEMINILAKE(dev_priv)) &&
4644 dev_priv->ipc_enabled)
4645 linetime_wm /= 2;
Mahesh Kumara3a89862016-12-01 21:19:34 +05304646
4647 return linetime_wm;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004648}
4649
Matt Roper024c9042015-09-24 15:53:11 -07004650static void skl_compute_transition_wm(struct intel_crtc_state *cstate,
Kumar, Maheshca476672017-08-17 19:15:24 +05304651 struct skl_wm_params *wp,
4652 struct skl_wm_level *wm_l0,
4653 uint16_t ddb_allocation,
Damien Lespiau9414f562014-11-04 17:06:58 +00004654 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004655{
Kumar, Maheshca476672017-08-17 19:15:24 +05304656 struct drm_device *dev = cstate->base.crtc->dev;
4657 const struct drm_i915_private *dev_priv = to_i915(dev);
4658 uint16_t trans_min, trans_y_tile_min;
4659 const uint16_t trans_amount = 10; /* This is configurable amount */
4660 uint16_t trans_offset_b, res_blocks;
Damien Lespiau9414f562014-11-04 17:06:58 +00004661
Kumar, Maheshca476672017-08-17 19:15:24 +05304662 if (!cstate->base.active)
4663 goto exit;
4664
4665 /* Transition WM are not recommended by HW team for GEN9 */
4666 if (INTEL_GEN(dev_priv) <= 9)
4667 goto exit;
4668
4669 /* Transition WM don't make any sense if ipc is disabled */
4670 if (!dev_priv->ipc_enabled)
4671 goto exit;
4672
4673 if (INTEL_GEN(dev_priv) >= 10)
4674 trans_min = 4;
4675
4676 trans_offset_b = trans_min + trans_amount;
4677
4678 if (wp->y_tiled) {
4679 trans_y_tile_min = (uint16_t) mul_round_up_u32_fixed16(2,
4680 wp->y_tile_minimum);
4681 res_blocks = max(wm_l0->plane_res_b, trans_y_tile_min) +
4682 trans_offset_b;
4683 } else {
4684 res_blocks = wm_l0->plane_res_b + trans_offset_b;
4685
4686 /* WA BUG:1938466 add one block for non y-tile planes */
4687 if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_A0))
4688 res_blocks += 1;
4689
4690 }
4691
4692 res_blocks += 1;
4693
4694 if (res_blocks < ddb_allocation) {
4695 trans_wm->plane_res_b = res_blocks;
4696 trans_wm->plane_en = true;
4697 return;
4698 }
4699
4700exit:
Lyudea62163e2016-10-04 14:28:20 -04004701 trans_wm->plane_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004702}
4703
Matt Roper55994c22016-05-12 07:06:08 -07004704static int skl_build_pipe_wm(struct intel_crtc_state *cstate,
4705 struct skl_ddb_allocation *ddb,
4706 struct skl_pipe_wm *pipe_wm)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004707{
Matt Roper024c9042015-09-24 15:53:11 -07004708 struct drm_device *dev = cstate->base.crtc->dev;
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304709 struct drm_crtc_state *crtc_state = &cstate->base;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004710 const struct drm_i915_private *dev_priv = to_i915(dev);
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304711 struct drm_plane *plane;
4712 const struct drm_plane_state *pstate;
Lyudea62163e2016-10-04 14:28:20 -04004713 struct skl_plane_wm *wm;
Matt Roper55994c22016-05-12 07:06:08 -07004714 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004715
Lyudea62163e2016-10-04 14:28:20 -04004716 /*
4717 * We'll only calculate watermarks for planes that are actually
4718 * enabled, so make sure all other planes are set as disabled.
4719 */
4720 memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
4721
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304722 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, crtc_state) {
4723 const struct intel_plane_state *intel_pstate =
4724 to_intel_plane_state(pstate);
4725 enum plane_id plane_id = to_intel_plane(plane)->id;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304726 struct skl_wm_params wm_params;
Kumar, Maheshca476672017-08-17 19:15:24 +05304727 enum pipe pipe = to_intel_crtc(cstate->base.crtc)->pipe;
4728 uint16_t ddb_blocks;
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304729
4730 wm = &pipe_wm->planes[plane_id];
Kumar, Maheshca476672017-08-17 19:15:24 +05304731 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][plane_id]);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304732 memset(&wm_params, 0, sizeof(struct skl_wm_params));
4733
4734 ret = skl_compute_plane_wm_params(dev_priv, cstate,
4735 intel_pstate, &wm_params);
4736 if (ret)
4737 return ret;
Lyudea62163e2016-10-04 14:28:20 -04004738
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004739 ret = skl_compute_wm_levels(dev_priv, ddb, cstate,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304740 intel_pstate, &wm_params, wm);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304741 if (ret)
4742 return ret;
Kumar, Maheshca476672017-08-17 19:15:24 +05304743 skl_compute_transition_wm(cstate, &wm_params, &wm->wm[0],
4744 ddb_blocks, &wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004745 }
Matt Roper024c9042015-09-24 15:53:11 -07004746 pipe_wm->linetime = skl_compute_linetime_wm(cstate);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004747
Matt Roper55994c22016-05-12 07:06:08 -07004748 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004749}
4750
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004751static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
4752 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00004753 const struct skl_ddb_entry *entry)
4754{
4755 if (entry->end)
4756 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
4757 else
4758 I915_WRITE(reg, 0);
4759}
4760
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004761static void skl_write_wm_level(struct drm_i915_private *dev_priv,
4762 i915_reg_t reg,
4763 const struct skl_wm_level *level)
4764{
4765 uint32_t val = 0;
4766
4767 if (level->plane_en) {
4768 val |= PLANE_WM_EN;
4769 val |= level->plane_res_b;
4770 val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
4771 }
4772
4773 I915_WRITE(reg, val);
4774}
4775
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004776static void skl_write_plane_wm(struct intel_crtc *intel_crtc,
4777 const struct skl_plane_wm *wm,
4778 const struct skl_ddb_allocation *ddb,
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004779 enum plane_id plane_id)
Lyude62e0fb82016-08-22 12:50:08 -04004780{
4781 struct drm_crtc *crtc = &intel_crtc->base;
4782 struct drm_device *dev = crtc->dev;
4783 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004784 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004785 enum pipe pipe = intel_crtc->pipe;
4786
4787 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004788 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004789 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004790 }
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004791 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004792 &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004793
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004794 skl_ddb_entry_write(dev_priv, PLANE_BUF_CFG(pipe, plane_id),
4795 &ddb->plane[pipe][plane_id]);
4796 skl_ddb_entry_write(dev_priv, PLANE_NV12_BUF_CFG(pipe, plane_id),
4797 &ddb->y_plane[pipe][plane_id]);
Lyude62e0fb82016-08-22 12:50:08 -04004798}
4799
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004800static void skl_write_cursor_wm(struct intel_crtc *intel_crtc,
4801 const struct skl_plane_wm *wm,
4802 const struct skl_ddb_allocation *ddb)
Lyude62e0fb82016-08-22 12:50:08 -04004803{
4804 struct drm_crtc *crtc = &intel_crtc->base;
4805 struct drm_device *dev = crtc->dev;
4806 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004807 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004808 enum pipe pipe = intel_crtc->pipe;
4809
4810 for (level = 0; level <= max_level; level++) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004811 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
4812 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004813 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004814 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004815
4816 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004817 &ddb->plane[pipe][PLANE_CURSOR]);
Lyude62e0fb82016-08-22 12:50:08 -04004818}
4819
cpaul@redhat.com45ece232016-10-14 17:31:56 -04004820bool skl_wm_level_equals(const struct skl_wm_level *l1,
4821 const struct skl_wm_level *l2)
4822{
4823 if (l1->plane_en != l2->plane_en)
4824 return false;
4825
4826 /* If both planes aren't enabled, the rest shouldn't matter */
4827 if (!l1->plane_en)
4828 return true;
4829
4830 return (l1->plane_res_l == l2->plane_res_l &&
4831 l1->plane_res_b == l2->plane_res_b);
4832}
4833
Lyude27082492016-08-24 07:48:10 +02004834static inline bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
4835 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004836{
Lyude27082492016-08-24 07:48:10 +02004837 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004838}
4839
Mika Kahola2b685042017-10-10 13:17:03 +03004840bool skl_ddb_allocation_overlaps(struct drm_i915_private *dev_priv,
4841 const struct skl_ddb_entry **entries,
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01004842 const struct skl_ddb_entry *ddb,
4843 int ignore)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004844{
Mika Kahola2b685042017-10-10 13:17:03 +03004845 enum pipe pipe;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004846
Mika Kahola2b685042017-10-10 13:17:03 +03004847 for_each_pipe(dev_priv, pipe) {
4848 if (pipe != ignore && entries[pipe] &&
4849 skl_ddb_entries_overlap(ddb, entries[pipe]))
Lyude27082492016-08-24 07:48:10 +02004850 return true;
Mika Kahola2b685042017-10-10 13:17:03 +03004851 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004852
Lyude27082492016-08-24 07:48:10 +02004853 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004854}
4855
Matt Roper55994c22016-05-12 07:06:08 -07004856static int skl_update_pipe_wm(struct drm_crtc_state *cstate,
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004857 const struct skl_pipe_wm *old_pipe_wm,
Matt Roper55994c22016-05-12 07:06:08 -07004858 struct skl_pipe_wm *pipe_wm, /* out */
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004859 struct skl_ddb_allocation *ddb, /* out */
Matt Roper55994c22016-05-12 07:06:08 -07004860 bool *changed /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004861{
Matt Roperf4a96752016-05-12 07:06:06 -07004862 struct intel_crtc_state *intel_cstate = to_intel_crtc_state(cstate);
Matt Roper55994c22016-05-12 07:06:08 -07004863 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004864
Matt Roper55994c22016-05-12 07:06:08 -07004865 ret = skl_build_pipe_wm(intel_cstate, ddb, pipe_wm);
4866 if (ret)
4867 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004868
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004869 if (!memcmp(old_pipe_wm, pipe_wm, sizeof(*pipe_wm)))
Matt Roper55994c22016-05-12 07:06:08 -07004870 *changed = false;
4871 else
4872 *changed = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004873
Matt Roper55994c22016-05-12 07:06:08 -07004874 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004875}
4876
Matt Roper9b613022016-06-27 16:42:44 -07004877static uint32_t
4878pipes_modified(struct drm_atomic_state *state)
4879{
4880 struct drm_crtc *crtc;
4881 struct drm_crtc_state *cstate;
4882 uint32_t i, ret = 0;
4883
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004884 for_each_new_crtc_in_state(state, crtc, cstate, i)
Matt Roper9b613022016-06-27 16:42:44 -07004885 ret |= drm_crtc_mask(crtc);
4886
4887 return ret;
4888}
4889
Jani Nikulabb7791b2016-10-04 12:29:17 +03004890static int
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004891skl_ddb_add_affected_planes(struct intel_crtc_state *cstate)
4892{
4893 struct drm_atomic_state *state = cstate->base.state;
4894 struct drm_device *dev = state->dev;
4895 struct drm_crtc *crtc = cstate->base.crtc;
4896 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4897 struct drm_i915_private *dev_priv = to_i915(dev);
4898 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4899 struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
4900 struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
4901 struct drm_plane_state *plane_state;
4902 struct drm_plane *plane;
4903 enum pipe pipe = intel_crtc->pipe;
4904
4905 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
4906
4907 drm_for_each_plane_mask(plane, dev, cstate->base.plane_mask) {
4908 enum plane_id plane_id = to_intel_plane(plane)->id;
4909
4910 if (skl_ddb_entry_equal(&cur_ddb->plane[pipe][plane_id],
4911 &new_ddb->plane[pipe][plane_id]) &&
4912 skl_ddb_entry_equal(&cur_ddb->y_plane[pipe][plane_id],
4913 &new_ddb->y_plane[pipe][plane_id]))
4914 continue;
4915
4916 plane_state = drm_atomic_get_plane_state(state, plane);
4917 if (IS_ERR(plane_state))
4918 return PTR_ERR(plane_state);
4919 }
4920
4921 return 0;
4922}
4923
4924static int
4925skl_compute_ddb(struct drm_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07004926{
4927 struct drm_device *dev = state->dev;
4928 struct drm_i915_private *dev_priv = to_i915(dev);
4929 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4930 struct intel_crtc *intel_crtc;
Matt Roper734fa012016-05-12 15:11:40 -07004931 struct skl_ddb_allocation *ddb = &intel_state->wm_results.ddb;
Matt Roper9b613022016-06-27 16:42:44 -07004932 uint32_t realloc_pipes = pipes_modified(state);
Matt Roper98d39492016-05-12 07:06:03 -07004933 int ret;
4934
4935 /*
4936 * If this is our first atomic update following hardware readout,
4937 * we can't trust the DDB that the BIOS programmed for us. Let's
4938 * pretend that all pipes switched active status so that we'll
4939 * ensure a full DDB recompute.
4940 */
Matt Roper1b54a882016-06-17 13:42:18 -07004941 if (dev_priv->wm.distrust_bios_wm) {
4942 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
4943 state->acquire_ctx);
4944 if (ret)
4945 return ret;
4946
Matt Roper98d39492016-05-12 07:06:03 -07004947 intel_state->active_pipe_changes = ~0;
4948
Matt Roper1b54a882016-06-17 13:42:18 -07004949 /*
4950 * We usually only initialize intel_state->active_crtcs if we
4951 * we're doing a modeset; make sure this field is always
4952 * initialized during the sanitization process that happens
4953 * on the first commit too.
4954 */
4955 if (!intel_state->modeset)
4956 intel_state->active_crtcs = dev_priv->active_crtcs;
4957 }
4958
Matt Roper98d39492016-05-12 07:06:03 -07004959 /*
4960 * If the modeset changes which CRTC's are active, we need to
4961 * recompute the DDB allocation for *all* active pipes, even
4962 * those that weren't otherwise being modified in any way by this
4963 * atomic commit. Due to the shrinking of the per-pipe allocations
4964 * when new active CRTC's are added, it's possible for a pipe that
4965 * we were already using and aren't changing at all here to suddenly
4966 * become invalid if its DDB needs exceeds its new allocation.
4967 *
4968 * Note that if we wind up doing a full DDB recompute, we can't let
4969 * any other display updates race with this transaction, so we need
4970 * to grab the lock on *all* CRTC's.
4971 */
Matt Roper734fa012016-05-12 15:11:40 -07004972 if (intel_state->active_pipe_changes) {
Matt Roper98d39492016-05-12 07:06:03 -07004973 realloc_pipes = ~0;
Matt Roper734fa012016-05-12 15:11:40 -07004974 intel_state->wm_results.dirty_pipes = ~0;
4975 }
Matt Roper98d39492016-05-12 07:06:03 -07004976
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004977 /*
4978 * We're not recomputing for the pipes not included in the commit, so
4979 * make sure we start with the current state.
4980 */
4981 memcpy(ddb, &dev_priv->wm.skl_hw.ddb, sizeof(*ddb));
4982
Matt Roper98d39492016-05-12 07:06:03 -07004983 for_each_intel_crtc_mask(dev, intel_crtc, realloc_pipes) {
4984 struct intel_crtc_state *cstate;
4985
4986 cstate = intel_atomic_get_crtc_state(state, intel_crtc);
4987 if (IS_ERR(cstate))
4988 return PTR_ERR(cstate);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004989
4990 ret = skl_allocate_pipe_ddb(cstate, ddb);
4991 if (ret)
4992 return ret;
4993
4994 ret = skl_ddb_add_affected_planes(cstate);
4995 if (ret)
4996 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07004997 }
4998
4999 return 0;
5000}
5001
Matt Roper2722efb2016-08-17 15:55:55 -04005002static void
5003skl_copy_wm_for_pipe(struct skl_wm_values *dst,
5004 struct skl_wm_values *src,
5005 enum pipe pipe)
5006{
Matt Roper2722efb2016-08-17 15:55:55 -04005007 memcpy(dst->ddb.y_plane[pipe], src->ddb.y_plane[pipe],
5008 sizeof(dst->ddb.y_plane[pipe]));
5009 memcpy(dst->ddb.plane[pipe], src->ddb.plane[pipe],
5010 sizeof(dst->ddb.plane[pipe]));
5011}
5012
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005013static void
5014skl_print_wm_changes(const struct drm_atomic_state *state)
5015{
5016 const struct drm_device *dev = state->dev;
5017 const struct drm_i915_private *dev_priv = to_i915(dev);
5018 const struct intel_atomic_state *intel_state =
5019 to_intel_atomic_state(state);
5020 const struct drm_crtc *crtc;
5021 const struct drm_crtc_state *cstate;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005022 const struct intel_plane *intel_plane;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005023 const struct skl_ddb_allocation *old_ddb = &dev_priv->wm.skl_hw.ddb;
5024 const struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
Maarten Lankhorst75704982016-11-01 12:04:10 +01005025 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005026
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01005027 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst75704982016-11-01 12:04:10 +01005028 const struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5029 enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005030
Maarten Lankhorst75704982016-11-01 12:04:10 +01005031 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005032 enum plane_id plane_id = intel_plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005033 const struct skl_ddb_entry *old, *new;
5034
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005035 old = &old_ddb->plane[pipe][plane_id];
5036 new = &new_ddb->plane[pipe][plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005037
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005038 if (skl_ddb_entry_equal(old, new))
5039 continue;
5040
Maarten Lankhorst75704982016-11-01 12:04:10 +01005041 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] ddb (%d - %d) -> (%d - %d)\n",
5042 intel_plane->base.base.id,
5043 intel_plane->base.name,
5044 old->start, old->end,
5045 new->start, new->end);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005046 }
5047 }
5048}
5049
Matt Roper98d39492016-05-12 07:06:03 -07005050static int
5051skl_compute_wm(struct drm_atomic_state *state)
5052{
5053 struct drm_crtc *crtc;
5054 struct drm_crtc_state *cstate;
Matt Roper734fa012016-05-12 15:11:40 -07005055 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
5056 struct skl_wm_values *results = &intel_state->wm_results;
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02005057 struct drm_device *dev = state->dev;
Matt Roper734fa012016-05-12 15:11:40 -07005058 struct skl_pipe_wm *pipe_wm;
Matt Roper98d39492016-05-12 07:06:03 -07005059 bool changed = false;
Matt Roper734fa012016-05-12 15:11:40 -07005060 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07005061
5062 /*
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02005063 * When we distrust bios wm we always need to recompute to set the
5064 * expected DDB allocations for each CRTC.
5065 */
5066 if (to_i915(dev)->wm.distrust_bios_wm)
5067 changed = true;
5068
5069 /*
Matt Roper98d39492016-05-12 07:06:03 -07005070 * If this transaction isn't actually touching any CRTC's, don't
5071 * bother with watermark calculation. Note that if we pass this
5072 * test, we're guaranteed to hold at least one CRTC state mutex,
5073 * which means we can safely use values like dev_priv->active_crtcs
5074 * since any racing commits that want to update them would need to
5075 * hold _all_ CRTC state mutexes.
5076 */
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01005077 for_each_new_crtc_in_state(state, crtc, cstate, i)
Matt Roper98d39492016-05-12 07:06:03 -07005078 changed = true;
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02005079
Matt Roper98d39492016-05-12 07:06:03 -07005080 if (!changed)
5081 return 0;
5082
Matt Roper734fa012016-05-12 15:11:40 -07005083 /* Clear all dirty flags */
5084 results->dirty_pipes = 0;
5085
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005086 ret = skl_compute_ddb(state);
Matt Roper98d39492016-05-12 07:06:03 -07005087 if (ret)
5088 return ret;
5089
Matt Roper734fa012016-05-12 15:11:40 -07005090 /*
5091 * Calculate WM's for all pipes that are part of this transaction.
5092 * Note that the DDB allocation above may have added more CRTC's that
5093 * weren't otherwise being modified (and set bits in dirty_pipes) if
5094 * pipe allocations had to change.
5095 *
5096 * FIXME: Now that we're doing this in the atomic check phase, we
5097 * should allow skl_update_pipe_wm() to return failure in cases where
5098 * no suitable watermark values can be found.
5099 */
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01005100 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Matt Roper734fa012016-05-12 15:11:40 -07005101 struct intel_crtc_state *intel_cstate =
5102 to_intel_crtc_state(cstate);
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02005103 const struct skl_pipe_wm *old_pipe_wm =
5104 &to_intel_crtc_state(crtc->state)->wm.skl.optimal;
Matt Roper734fa012016-05-12 15:11:40 -07005105
5106 pipe_wm = &intel_cstate->wm.skl.optimal;
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02005107 ret = skl_update_pipe_wm(cstate, old_pipe_wm, pipe_wm,
5108 &results->ddb, &changed);
Matt Roper734fa012016-05-12 15:11:40 -07005109 if (ret)
5110 return ret;
5111
5112 if (changed)
5113 results->dirty_pipes |= drm_crtc_mask(crtc);
5114
5115 if ((results->dirty_pipes & drm_crtc_mask(crtc)) == 0)
5116 /* This pipe's WM's did not change */
5117 continue;
5118
5119 intel_cstate->update_wm_pre = true;
Matt Roper734fa012016-05-12 15:11:40 -07005120 }
5121
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005122 skl_print_wm_changes(state);
5123
Matt Roper98d39492016-05-12 07:06:03 -07005124 return 0;
5125}
5126
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005127static void skl_atomic_update_crtc_wm(struct intel_atomic_state *state,
5128 struct intel_crtc_state *cstate)
5129{
5130 struct intel_crtc *crtc = to_intel_crtc(cstate->base.crtc);
5131 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5132 struct skl_pipe_wm *pipe_wm = &cstate->wm.skl.optimal;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005133 const struct skl_ddb_allocation *ddb = &state->wm_results.ddb;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005134 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005135 enum plane_id plane_id;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005136
5137 if (!(state->wm_results.dirty_pipes & drm_crtc_mask(&crtc->base)))
5138 return;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005139
5140 I915_WRITE(PIPE_WM_LINETIME(pipe), pipe_wm->linetime);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005141
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005142 for_each_plane_id_on_crtc(crtc, plane_id) {
5143 if (plane_id != PLANE_CURSOR)
5144 skl_write_plane_wm(crtc, &pipe_wm->planes[plane_id],
5145 ddb, plane_id);
5146 else
5147 skl_write_cursor_wm(crtc, &pipe_wm->planes[plane_id],
5148 ddb);
5149 }
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005150}
5151
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005152static void skl_initial_wm(struct intel_atomic_state *state,
5153 struct intel_crtc_state *cstate)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005154{
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005155 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02005156 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005157 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005158 struct skl_wm_values *results = &state->wm_results;
Matt Roper2722efb2016-08-17 15:55:55 -04005159 struct skl_wm_values *hw_vals = &dev_priv->wm.skl_hw;
Lyude27082492016-08-24 07:48:10 +02005160 enum pipe pipe = intel_crtc->pipe;
Bob Paauweadda50b2015-07-21 10:42:53 -07005161
Ville Syrjälä432081b2016-10-31 22:37:03 +02005162 if ((results->dirty_pipes & drm_crtc_mask(&intel_crtc->base)) == 0)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005163 return;
5164
Matt Roper734fa012016-05-12 15:11:40 -07005165 mutex_lock(&dev_priv->wm.wm_mutex);
5166
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005167 if (cstate->base.active_changed)
5168 skl_atomic_update_crtc_wm(state, cstate);
Lyude27082492016-08-24 07:48:10 +02005169
5170 skl_copy_wm_for_pipe(hw_vals, results, pipe);
Matt Roper734fa012016-05-12 15:11:40 -07005171
5172 mutex_unlock(&dev_priv->wm.wm_mutex);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005173}
5174
Ville Syrjäläd8905652016-01-14 14:53:35 +02005175static void ilk_compute_wm_config(struct drm_device *dev,
5176 struct intel_wm_config *config)
5177{
5178 struct intel_crtc *crtc;
5179
5180 /* Compute the currently _active_ config */
5181 for_each_intel_crtc(dev, crtc) {
5182 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
5183
5184 if (!wm->pipe_enabled)
5185 continue;
5186
5187 config->sprites_enabled |= wm->sprites_enabled;
5188 config->sprites_scaled |= wm->sprites_scaled;
5189 config->num_pipes_active++;
5190 }
5191}
5192
Matt Ropered4a6a72016-02-23 17:20:13 -08005193static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03005194{
Chris Wilson91c8a322016-07-05 10:40:23 +01005195 struct drm_device *dev = &dev_priv->drm;
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005196 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02005197 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02005198 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02005199 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005200 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07005201
Ville Syrjäläd8905652016-01-14 14:53:35 +02005202 ilk_compute_wm_config(dev, &config);
5203
5204 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
5205 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03005206
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005207 /* 5/6 split only in single pipe config on IVB+ */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00005208 if (INTEL_GEN(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02005209 config.num_pipes_active == 1 && config.sprites_enabled) {
5210 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
5211 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005212
Imre Deak820c1982013-12-17 14:46:36 +02005213 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03005214 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005215 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005216 }
5217
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005218 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005219 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005220
Imre Deak820c1982013-12-17 14:46:36 +02005221 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03005222
Imre Deak820c1982013-12-17 14:46:36 +02005223 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03005224}
5225
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005226static void ilk_initial_watermarks(struct intel_atomic_state *state,
5227 struct intel_crtc_state *cstate)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005228{
Matt Ropered4a6a72016-02-23 17:20:13 -08005229 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
5230 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005231
Matt Ropered4a6a72016-02-23 17:20:13 -08005232 mutex_lock(&dev_priv->wm.wm_mutex);
Matt Ropere8f1f022016-05-12 07:05:55 -07005233 intel_crtc->wm.active.ilk = cstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08005234 ilk_program_watermarks(dev_priv);
5235 mutex_unlock(&dev_priv->wm.wm_mutex);
5236}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005237
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005238static void ilk_optimize_watermarks(struct intel_atomic_state *state,
5239 struct intel_crtc_state *cstate)
Matt Ropered4a6a72016-02-23 17:20:13 -08005240{
5241 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
5242 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
5243
5244 mutex_lock(&dev_priv->wm.wm_mutex);
5245 if (cstate->wm.need_postvbl_update) {
Matt Ropere8f1f022016-05-12 07:05:55 -07005246 intel_crtc->wm.active.ilk = cstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08005247 ilk_program_watermarks(dev_priv);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005248 }
Matt Ropered4a6a72016-02-23 17:20:13 -08005249 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005250}
5251
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005252static inline void skl_wm_level_from_reg_val(uint32_t val,
5253 struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00005254{
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005255 level->plane_en = val & PLANE_WM_EN;
5256 level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
5257 level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
5258 PLANE_WM_LINES_MASK;
Pradeep Bhat30789992014-11-04 17:06:45 +00005259}
5260
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005261void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc,
5262 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00005263{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005264 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Pradeep Bhat30789992014-11-04 17:06:45 +00005265 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Pradeep Bhat30789992014-11-04 17:06:45 +00005266 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005267 int level, max_level;
5268 enum plane_id plane_id;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005269 uint32_t val;
Pradeep Bhat30789992014-11-04 17:06:45 +00005270
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005271 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00005272
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005273 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
5274 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00005275
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005276 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005277 if (plane_id != PLANE_CURSOR)
5278 val = I915_READ(PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005279 else
5280 val = I915_READ(CUR_WM(pipe, level));
5281
5282 skl_wm_level_from_reg_val(val, &wm->wm[level]);
5283 }
5284
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005285 if (plane_id != PLANE_CURSOR)
5286 val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005287 else
5288 val = I915_READ(CUR_WM_TRANS(pipe));
5289
5290 skl_wm_level_from_reg_val(val, &wm->trans_wm);
5291 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005292
Matt Roper3ef00282015-03-09 10:19:24 -07005293 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00005294 return;
5295
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005296 out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00005297}
5298
5299void skl_wm_get_hw_state(struct drm_device *dev)
5300{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005301 struct drm_i915_private *dev_priv = to_i915(dev);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005302 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
Damien Lespiaua269c582014-11-04 17:06:49 +00005303 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00005304 struct drm_crtc *crtc;
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005305 struct intel_crtc *intel_crtc;
5306 struct intel_crtc_state *cstate;
Pradeep Bhat30789992014-11-04 17:06:45 +00005307
Damien Lespiaua269c582014-11-04 17:06:49 +00005308 skl_ddb_get_hw_state(dev_priv, ddb);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005309 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5310 intel_crtc = to_intel_crtc(crtc);
5311 cstate = to_intel_crtc_state(crtc->state);
5312
5313 skl_pipe_wm_get_hw_state(crtc, &cstate->wm.skl.optimal);
5314
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02005315 if (intel_crtc->active)
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005316 hw->dirty_pipes |= drm_crtc_mask(crtc);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005317 }
Matt Ropera1de91e2016-05-12 07:05:57 -07005318
Matt Roper279e99d2016-05-12 07:06:02 -07005319 if (dev_priv->active_crtcs) {
5320 /* Fully recompute DDB on first atomic commit */
5321 dev_priv->wm.distrust_bios_wm = true;
5322 } else {
5323 /* Easy/common case; just sanitize DDB now if everything off */
5324 memset(ddb, 0, sizeof(*ddb));
5325 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005326}
5327
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005328static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
5329{
5330 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005331 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02005332 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005333 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper4e0963c2015-09-24 15:53:15 -07005334 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Matt Ropere8f1f022016-05-12 07:05:55 -07005335 struct intel_pipe_wm *active = &cstate->wm.ilk.optimal;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005336 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005337 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005338 [PIPE_A] = WM0_PIPEA_ILK,
5339 [PIPE_B] = WM0_PIPEB_ILK,
5340 [PIPE_C] = WM0_PIPEC_IVB,
5341 };
5342
5343 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005344 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02005345 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005346
Ville Syrjälä15606532016-05-13 17:55:17 +03005347 memset(active, 0, sizeof(*active));
5348
Matt Roper3ef00282015-03-09 10:19:24 -07005349 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02005350
5351 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005352 u32 tmp = hw->wm_pipe[pipe];
5353
5354 /*
5355 * For active pipes LP0 watermark is marked as
5356 * enabled, and LP1+ watermaks as disabled since
5357 * we can't really reverse compute them in case
5358 * multiple pipes are active.
5359 */
5360 active->wm[0].enable = true;
5361 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
5362 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
5363 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
5364 active->linetime = hw->wm_linetime[pipe];
5365 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005366 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005367
5368 /*
5369 * For inactive pipes, all watermark levels
5370 * should be marked as enabled but zeroed,
5371 * which is what we'd compute them to.
5372 */
5373 for (level = 0; level <= max_level; level++)
5374 active->wm[level].enable = true;
5375 }
Matt Roper4e0963c2015-09-24 15:53:15 -07005376
5377 intel_crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005378}
5379
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005380#define _FW_WM(value, plane) \
5381 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
5382#define _FW_WM_VLV(value, plane) \
5383 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
5384
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005385static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
5386 struct g4x_wm_values *wm)
5387{
5388 uint32_t tmp;
5389
5390 tmp = I915_READ(DSPFW1);
5391 wm->sr.plane = _FW_WM(tmp, SR);
5392 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5393 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
5394 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
5395
5396 tmp = I915_READ(DSPFW2);
5397 wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
5398 wm->sr.fbc = _FW_WM(tmp, FBC_SR);
5399 wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
5400 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
5401 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5402 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
5403
5404 tmp = I915_READ(DSPFW3);
5405 wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
5406 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5407 wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
5408 wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
5409}
5410
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005411static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
5412 struct vlv_wm_values *wm)
5413{
5414 enum pipe pipe;
5415 uint32_t tmp;
5416
5417 for_each_pipe(dev_priv, pipe) {
5418 tmp = I915_READ(VLV_DDL(pipe));
5419
Ville Syrjälä1b313892016-11-28 19:37:08 +02005420 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005421 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005422 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005423 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005424 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005425 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005426 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005427 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
5428 }
5429
5430 tmp = I915_READ(DSPFW1);
5431 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005432 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5433 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
5434 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005435
5436 tmp = I915_READ(DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005437 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
5438 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5439 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005440
5441 tmp = I915_READ(DSPFW3);
5442 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5443
5444 if (IS_CHERRYVIEW(dev_priv)) {
5445 tmp = I915_READ(DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005446 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5447 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005448
5449 tmp = I915_READ(DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005450 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
5451 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005452
5453 tmp = I915_READ(DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005454 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
5455 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005456
5457 tmp = I915_READ(DSPHOWM);
5458 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005459 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
5460 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
5461 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
5462 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5463 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5464 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5465 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5466 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5467 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005468 } else {
5469 tmp = I915_READ(DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005470 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5471 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005472
5473 tmp = I915_READ(DSPHOWM);
5474 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005475 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5476 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5477 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5478 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5479 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5480 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005481 }
5482}
5483
5484#undef _FW_WM
5485#undef _FW_WM_VLV
5486
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005487void g4x_wm_get_hw_state(struct drm_device *dev)
5488{
5489 struct drm_i915_private *dev_priv = to_i915(dev);
5490 struct g4x_wm_values *wm = &dev_priv->wm.g4x;
5491 struct intel_crtc *crtc;
5492
5493 g4x_read_wm_values(dev_priv, wm);
5494
5495 wm->cxsr = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
5496
5497 for_each_intel_crtc(dev, crtc) {
5498 struct intel_crtc_state *crtc_state =
5499 to_intel_crtc_state(crtc->base.state);
5500 struct g4x_wm_state *active = &crtc->wm.active.g4x;
5501 struct g4x_pipe_wm *raw;
5502 enum pipe pipe = crtc->pipe;
5503 enum plane_id plane_id;
5504 int level, max_level;
5505
5506 active->cxsr = wm->cxsr;
5507 active->hpll_en = wm->hpll_en;
5508 active->fbc_en = wm->fbc_en;
5509
5510 active->sr = wm->sr;
5511 active->hpll = wm->hpll;
5512
5513 for_each_plane_id_on_crtc(crtc, plane_id) {
5514 active->wm.plane[plane_id] =
5515 wm->pipe[pipe].plane[plane_id];
5516 }
5517
5518 if (wm->cxsr && wm->hpll_en)
5519 max_level = G4X_WM_LEVEL_HPLL;
5520 else if (wm->cxsr)
5521 max_level = G4X_WM_LEVEL_SR;
5522 else
5523 max_level = G4X_WM_LEVEL_NORMAL;
5524
5525 level = G4X_WM_LEVEL_NORMAL;
5526 raw = &crtc_state->wm.g4x.raw[level];
5527 for_each_plane_id_on_crtc(crtc, plane_id)
5528 raw->plane[plane_id] = active->wm.plane[plane_id];
5529
5530 if (++level > max_level)
5531 goto out;
5532
5533 raw = &crtc_state->wm.g4x.raw[level];
5534 raw->plane[PLANE_PRIMARY] = active->sr.plane;
5535 raw->plane[PLANE_CURSOR] = active->sr.cursor;
5536 raw->plane[PLANE_SPRITE0] = 0;
5537 raw->fbc = active->sr.fbc;
5538
5539 if (++level > max_level)
5540 goto out;
5541
5542 raw = &crtc_state->wm.g4x.raw[level];
5543 raw->plane[PLANE_PRIMARY] = active->hpll.plane;
5544 raw->plane[PLANE_CURSOR] = active->hpll.cursor;
5545 raw->plane[PLANE_SPRITE0] = 0;
5546 raw->fbc = active->hpll.fbc;
5547
5548 out:
5549 for_each_plane_id_on_crtc(crtc, plane_id)
5550 g4x_raw_plane_wm_set(crtc_state, level,
5551 plane_id, USHRT_MAX);
5552 g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
5553
5554 crtc_state->wm.g4x.optimal = *active;
5555 crtc_state->wm.g4x.intermediate = *active;
5556
5557 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
5558 pipe_name(pipe),
5559 wm->pipe[pipe].plane[PLANE_PRIMARY],
5560 wm->pipe[pipe].plane[PLANE_CURSOR],
5561 wm->pipe[pipe].plane[PLANE_SPRITE0]);
5562 }
5563
5564 DRM_DEBUG_KMS("Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
5565 wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
5566 DRM_DEBUG_KMS("Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
5567 wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
5568 DRM_DEBUG_KMS("Initial SR=%s HPLL=%s FBC=%s\n",
5569 yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
5570}
5571
5572void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
5573{
5574 struct intel_plane *plane;
5575 struct intel_crtc *crtc;
5576
5577 mutex_lock(&dev_priv->wm.wm_mutex);
5578
5579 for_each_intel_plane(&dev_priv->drm, plane) {
5580 struct intel_crtc *crtc =
5581 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
5582 struct intel_crtc_state *crtc_state =
5583 to_intel_crtc_state(crtc->base.state);
5584 struct intel_plane_state *plane_state =
5585 to_intel_plane_state(plane->base.state);
5586 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
5587 enum plane_id plane_id = plane->id;
5588 int level;
5589
5590 if (plane_state->base.visible)
5591 continue;
5592
5593 for (level = 0; level < 3; level++) {
5594 struct g4x_pipe_wm *raw =
5595 &crtc_state->wm.g4x.raw[level];
5596
5597 raw->plane[plane_id] = 0;
5598 wm_state->wm.plane[plane_id] = 0;
5599 }
5600
5601 if (plane_id == PLANE_PRIMARY) {
5602 for (level = 0; level < 3; level++) {
5603 struct g4x_pipe_wm *raw =
5604 &crtc_state->wm.g4x.raw[level];
5605 raw->fbc = 0;
5606 }
5607
5608 wm_state->sr.fbc = 0;
5609 wm_state->hpll.fbc = 0;
5610 wm_state->fbc_en = false;
5611 }
5612 }
5613
5614 for_each_intel_crtc(&dev_priv->drm, crtc) {
5615 struct intel_crtc_state *crtc_state =
5616 to_intel_crtc_state(crtc->base.state);
5617
5618 crtc_state->wm.g4x.intermediate =
5619 crtc_state->wm.g4x.optimal;
5620 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
5621 }
5622
5623 g4x_program_watermarks(dev_priv);
5624
5625 mutex_unlock(&dev_priv->wm.wm_mutex);
5626}
5627
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005628void vlv_wm_get_hw_state(struct drm_device *dev)
5629{
5630 struct drm_i915_private *dev_priv = to_i915(dev);
5631 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02005632 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005633 u32 val;
5634
5635 vlv_read_wm_values(dev_priv, wm);
5636
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005637 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
5638 wm->level = VLV_WM_LEVEL_PM2;
5639
5640 if (IS_CHERRYVIEW(dev_priv)) {
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01005641 mutex_lock(&dev_priv->pcu_lock);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005642
5643 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5644 if (val & DSP_MAXFIFO_PM5_ENABLE)
5645 wm->level = VLV_WM_LEVEL_PM5;
5646
Ville Syrjälä58590c12015-09-08 21:05:12 +03005647 /*
5648 * If DDR DVFS is disabled in the BIOS, Punit
5649 * will never ack the request. So if that happens
5650 * assume we don't have to enable/disable DDR DVFS
5651 * dynamically. To test that just set the REQ_ACK
5652 * bit to poke the Punit, but don't change the
5653 * HIGH/LOW bits so that we don't actually change
5654 * the current state.
5655 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005656 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03005657 val |= FORCE_DDR_FREQ_REQ_ACK;
5658 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
5659
5660 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
5661 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
5662 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
5663 "assuming DDR DVFS is disabled\n");
5664 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
5665 } else {
5666 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
5667 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
5668 wm->level = VLV_WM_LEVEL_DDR_DVFS;
5669 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005670
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01005671 mutex_unlock(&dev_priv->pcu_lock);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005672 }
5673
Ville Syrjäläff32c542017-03-02 19:14:57 +02005674 for_each_intel_crtc(dev, crtc) {
5675 struct intel_crtc_state *crtc_state =
5676 to_intel_crtc_state(crtc->base.state);
5677 struct vlv_wm_state *active = &crtc->wm.active.vlv;
5678 const struct vlv_fifo_state *fifo_state =
5679 &crtc_state->wm.vlv.fifo_state;
5680 enum pipe pipe = crtc->pipe;
5681 enum plane_id plane_id;
5682 int level;
5683
5684 vlv_get_fifo_size(crtc_state);
5685
5686 active->num_levels = wm->level + 1;
5687 active->cxsr = wm->cxsr;
5688
Ville Syrjäläff32c542017-03-02 19:14:57 +02005689 for (level = 0; level < active->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03005690 struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02005691 &crtc_state->wm.vlv.raw[level];
5692
5693 active->sr[level].plane = wm->sr.plane;
5694 active->sr[level].cursor = wm->sr.cursor;
5695
5696 for_each_plane_id_on_crtc(crtc, plane_id) {
5697 active->wm[level].plane[plane_id] =
5698 wm->pipe[pipe].plane[plane_id];
5699
5700 raw->plane[plane_id] =
5701 vlv_invert_wm_value(active->wm[level].plane[plane_id],
5702 fifo_state->plane[plane_id]);
5703 }
5704 }
5705
5706 for_each_plane_id_on_crtc(crtc, plane_id)
5707 vlv_raw_plane_wm_set(crtc_state, level,
5708 plane_id, USHRT_MAX);
5709 vlv_invalidate_wms(crtc, active, level);
5710
5711 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02005712 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02005713
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005714 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 +02005715 pipe_name(pipe),
5716 wm->pipe[pipe].plane[PLANE_PRIMARY],
5717 wm->pipe[pipe].plane[PLANE_CURSOR],
5718 wm->pipe[pipe].plane[PLANE_SPRITE0],
5719 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02005720 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005721
5722 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
5723 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
5724}
5725
Ville Syrjälä602ae832017-03-02 19:15:02 +02005726void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
5727{
5728 struct intel_plane *plane;
5729 struct intel_crtc *crtc;
5730
5731 mutex_lock(&dev_priv->wm.wm_mutex);
5732
5733 for_each_intel_plane(&dev_priv->drm, plane) {
5734 struct intel_crtc *crtc =
5735 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
5736 struct intel_crtc_state *crtc_state =
5737 to_intel_crtc_state(crtc->base.state);
5738 struct intel_plane_state *plane_state =
5739 to_intel_plane_state(plane->base.state);
5740 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
5741 const struct vlv_fifo_state *fifo_state =
5742 &crtc_state->wm.vlv.fifo_state;
5743 enum plane_id plane_id = plane->id;
5744 int level;
5745
5746 if (plane_state->base.visible)
5747 continue;
5748
5749 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03005750 struct g4x_pipe_wm *raw =
Ville Syrjälä602ae832017-03-02 19:15:02 +02005751 &crtc_state->wm.vlv.raw[level];
5752
5753 raw->plane[plane_id] = 0;
5754
5755 wm_state->wm[level].plane[plane_id] =
5756 vlv_invert_wm_value(raw->plane[plane_id],
5757 fifo_state->plane[plane_id]);
5758 }
5759 }
5760
5761 for_each_intel_crtc(&dev_priv->drm, crtc) {
5762 struct intel_crtc_state *crtc_state =
5763 to_intel_crtc_state(crtc->base.state);
5764
5765 crtc_state->wm.vlv.intermediate =
5766 crtc_state->wm.vlv.optimal;
5767 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
5768 }
5769
5770 vlv_program_watermarks(dev_priv);
5771
5772 mutex_unlock(&dev_priv->wm.wm_mutex);
5773}
5774
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02005775/*
5776 * FIXME should probably kill this and improve
5777 * the real watermark readout/sanitation instead
5778 */
5779static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
5780{
5781 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
5782 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
5783 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
5784
5785 /*
5786 * Don't touch WM1S_LP_EN here.
5787 * Doing so could cause underruns.
5788 */
5789}
5790
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005791void ilk_wm_get_hw_state(struct drm_device *dev)
5792{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005793 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02005794 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005795 struct drm_crtc *crtc;
5796
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02005797 ilk_init_lp_watermarks(dev_priv);
5798
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01005799 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005800 ilk_pipe_wm_get_hw_state(crtc);
5801
5802 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
5803 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
5804 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
5805
5806 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00005807 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjäläcfa76982014-03-07 18:32:08 +02005808 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
5809 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
5810 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005811
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005812 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02005813 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
5814 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01005815 else if (IS_IVYBRIDGE(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02005816 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
5817 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005818
5819 hw->enable_fbc_wm =
5820 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
5821}
5822
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005823/**
5824 * intel_update_watermarks - update FIFO watermark values based on current modes
5825 *
5826 * Calculate watermark values for the various WM regs based on current mode
5827 * and plane configuration.
5828 *
5829 * There are several cases to deal with here:
5830 * - normal (i.e. non-self-refresh)
5831 * - self-refresh (SR) mode
5832 * - lines are large relative to FIFO size (buffer can hold up to 2)
5833 * - lines are small relative to FIFO size (buffer can hold more than 2
5834 * lines), so need to account for TLB latency
5835 *
5836 * The normal calculation is:
5837 * watermark = dotclock * bytes per pixel * latency
5838 * where latency is platform & configuration dependent (we assume pessimal
5839 * values here).
5840 *
5841 * The SR calculation is:
5842 * watermark = (trunc(latency/line time)+1) * surface width *
5843 * bytes per pixel
5844 * where
5845 * line time = htotal / dotclock
5846 * surface width = hdisplay for normal plane and 64 for cursor
5847 * and latency is assumed to be high, as above.
5848 *
5849 * The final value programmed to the register should always be rounded up,
5850 * and include an extra 2 entries to account for clock crossings.
5851 *
5852 * We don't use the sprite, so we can ignore that. And on Crestline we have
5853 * to set the non-SR watermarks to 8.
5854 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02005855void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005856{
Ville Syrjälä432081b2016-10-31 22:37:03 +02005857 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005858
5859 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005860 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005861}
5862
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05305863void intel_enable_ipc(struct drm_i915_private *dev_priv)
5864{
5865 u32 val;
5866
Rodrigo Vivi4d6ef0d2017-10-02 23:36:50 -07005867 /* Display WA #0477 WaDisableIPC: skl */
5868 if (IS_SKYLAKE(dev_priv)) {
5869 dev_priv->ipc_enabled = false;
5870 return;
5871 }
5872
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05305873 val = I915_READ(DISP_ARB_CTL2);
5874
5875 if (dev_priv->ipc_enabled)
5876 val |= DISP_IPC_ENABLE;
5877 else
5878 val &= ~DISP_IPC_ENABLE;
5879
5880 I915_WRITE(DISP_ARB_CTL2, val);
5881}
5882
5883void intel_init_ipc(struct drm_i915_private *dev_priv)
5884{
5885 dev_priv->ipc_enabled = false;
5886 if (!HAS_IPC(dev_priv))
5887 return;
5888
5889 dev_priv->ipc_enabled = true;
5890 intel_enable_ipc(dev_priv);
5891}
5892
Jani Nikulae2828912016-01-18 09:19:47 +02005893/*
Daniel Vetter92703882012-08-09 16:46:01 +02005894 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02005895 */
5896DEFINE_SPINLOCK(mchdev_lock);
5897
5898/* Global for IPS driver to get at the current i915 device. Protected by
5899 * mchdev_lock. */
5900static struct drm_i915_private *i915_mch_dev;
5901
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005902bool ironlake_set_drps(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005903{
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005904 u16 rgvswctl;
5905
Chris Wilson67520412017-03-02 13:28:01 +00005906 lockdep_assert_held(&mchdev_lock);
Daniel Vetter92703882012-08-09 16:46:01 +02005907
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005908 rgvswctl = I915_READ16(MEMSWCTL);
5909 if (rgvswctl & MEMCTL_CMD_STS) {
5910 DRM_DEBUG("gpu busy, RCS change rejected\n");
5911 return false; /* still busy with another command */
5912 }
5913
5914 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
5915 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
5916 I915_WRITE16(MEMSWCTL, rgvswctl);
5917 POSTING_READ16(MEMSWCTL);
5918
5919 rgvswctl |= MEMCTL_CMD_STS;
5920 I915_WRITE16(MEMSWCTL, rgvswctl);
5921
5922 return true;
5923}
5924
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005925static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005926{
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00005927 u32 rgvmodectl;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005928 u8 fmax, fmin, fstart, vstart;
5929
Daniel Vetter92703882012-08-09 16:46:01 +02005930 spin_lock_irq(&mchdev_lock);
5931
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00005932 rgvmodectl = I915_READ(MEMMODECTL);
5933
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005934 /* Enable temp reporting */
5935 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
5936 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
5937
5938 /* 100ms RC evaluation intervals */
5939 I915_WRITE(RCUPEI, 100000);
5940 I915_WRITE(RCDNEI, 100000);
5941
5942 /* Set max/min thresholds to 90ms and 80ms respectively */
5943 I915_WRITE(RCBMAXAVG, 90000);
5944 I915_WRITE(RCBMINAVG, 80000);
5945
5946 I915_WRITE(MEMIHYST, 1);
5947
5948 /* Set up min, max, and cur for interrupt handling */
5949 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
5950 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
5951 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
5952 MEMMODE_FSTART_SHIFT;
5953
Ville Syrjälä616847e2015-09-18 20:03:19 +03005954 vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005955 PXVFREQ_PX_SHIFT;
5956
Daniel Vetter20e4d402012-08-08 23:35:39 +02005957 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
5958 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005959
Daniel Vetter20e4d402012-08-08 23:35:39 +02005960 dev_priv->ips.max_delay = fstart;
5961 dev_priv->ips.min_delay = fmin;
5962 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005963
5964 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
5965 fmax, fmin, fstart);
5966
5967 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
5968
5969 /*
5970 * Interrupts will be enabled in ironlake_irq_postinstall
5971 */
5972
5973 I915_WRITE(VIDSTART, vstart);
5974 POSTING_READ(VIDSTART);
5975
5976 rgvmodectl |= MEMMODE_SWMODE_EN;
5977 I915_WRITE(MEMMODECTL, rgvmodectl);
5978
Daniel Vetter92703882012-08-09 16:46:01 +02005979 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005980 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005981 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005982
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005983 ironlake_set_drps(dev_priv, fstart);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005984
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03005985 dev_priv->ips.last_count1 = I915_READ(DMIEC) +
5986 I915_READ(DDREC) + I915_READ(CSIEC);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005987 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03005988 dev_priv->ips.last_count2 = I915_READ(GFXEC);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005989 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02005990
5991 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005992}
5993
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005994static void ironlake_disable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005995{
Daniel Vetter92703882012-08-09 16:46:01 +02005996 u16 rgvswctl;
5997
5998 spin_lock_irq(&mchdev_lock);
5999
6000 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006001
6002 /* Ack interrupts, disable EFC interrupt */
6003 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
6004 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
6005 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
6006 I915_WRITE(DEIIR, DE_PCU_EVENT);
6007 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
6008
6009 /* Go back to the starting frequency */
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006010 ironlake_set_drps(dev_priv, dev_priv->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02006011 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006012 rgvswctl |= MEMCTL_CMD_STS;
6013 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02006014 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006015
Daniel Vetter92703882012-08-09 16:46:01 +02006016 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006017}
6018
Daniel Vetteracbe9472012-07-26 11:50:05 +02006019/* There's a funny hw issue where the hw returns all 0 when reading from
6020 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
6021 * ourselves, instead of doing a rmw cycle (which might result in us clearing
6022 * all limits and the gpu stuck at whatever frequency it is at atm).
6023 */
Akash Goel74ef1172015-03-06 11:07:19 +05306024static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006025{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006026 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006027 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006028
Daniel Vetter20b46e52012-07-26 11:16:14 +02006029 /* Only set the down limit when we've reached the lowest level to avoid
6030 * getting more interrupts, otherwise leave this clear. This prevents a
6031 * race in the hw when coming out of rc6: There's a tiny window where
6032 * the hw runs at the minimal clock before selecting the desired
6033 * frequency, if the down threshold expires in that window we will not
6034 * receive a down interrupt. */
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006035 if (INTEL_GEN(dev_priv) >= 9) {
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006036 limits = (rps->max_freq_softlimit) << 23;
6037 if (val <= rps->min_freq_softlimit)
6038 limits |= (rps->min_freq_softlimit) << 14;
Akash Goel74ef1172015-03-06 11:07:19 +05306039 } else {
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006040 limits = rps->max_freq_softlimit << 24;
6041 if (val <= rps->min_freq_softlimit)
6042 limits |= rps->min_freq_softlimit << 16;
Akash Goel74ef1172015-03-06 11:07:19 +05306043 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02006044
6045 return limits;
6046}
6047
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006048static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
6049{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006050 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006051 int new_power;
Akash Goel8a586432015-03-06 11:07:18 +05306052 u32 threshold_up = 0, threshold_down = 0; /* in % */
6053 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006054
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006055 new_power = rps->power;
6056 switch (rps->power) {
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006057 case LOW_POWER:
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006058 if (val > rps->efficient_freq + 1 &&
6059 val > rps->cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006060 new_power = BETWEEN;
6061 break;
6062
6063 case BETWEEN:
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006064 if (val <= rps->efficient_freq &&
6065 val < rps->cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006066 new_power = LOW_POWER;
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006067 else if (val >= rps->rp0_freq &&
6068 val > rps->cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006069 new_power = HIGH_POWER;
6070 break;
6071
6072 case HIGH_POWER:
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006073 if (val < (rps->rp1_freq + rps->rp0_freq) >> 1 &&
6074 val < rps->cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006075 new_power = BETWEEN;
6076 break;
6077 }
6078 /* Max/min bins are special */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006079 if (val <= rps->min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006080 new_power = LOW_POWER;
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006081 if (val >= rps->max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006082 new_power = HIGH_POWER;
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006083 if (new_power == rps->power)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006084 return;
6085
6086 /* Note the units here are not exactly 1us, but 1280ns. */
6087 switch (new_power) {
6088 case LOW_POWER:
6089 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05306090 ei_up = 16000;
6091 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006092
6093 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306094 ei_down = 32000;
6095 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006096 break;
6097
6098 case BETWEEN:
6099 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05306100 ei_up = 13000;
6101 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006102
6103 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306104 ei_down = 32000;
6105 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006106 break;
6107
6108 case HIGH_POWER:
6109 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05306110 ei_up = 10000;
6111 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006112
6113 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306114 ei_down = 32000;
6115 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006116 break;
6117 }
6118
Mika Kuoppala6067a272017-02-15 15:52:59 +02006119 /* When byt can survive without system hang with dynamic
6120 * sw freq adjustments, this restriction can be lifted.
6121 */
6122 if (IS_VALLEYVIEW(dev_priv))
6123 goto skip_hw_write;
6124
Akash Goel8a586432015-03-06 11:07:18 +05306125 I915_WRITE(GEN6_RP_UP_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01006126 GT_INTERVAL_FROM_US(dev_priv, ei_up));
Akash Goel8a586432015-03-06 11:07:18 +05306127 I915_WRITE(GEN6_RP_UP_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01006128 GT_INTERVAL_FROM_US(dev_priv,
6129 ei_up * threshold_up / 100));
Akash Goel8a586432015-03-06 11:07:18 +05306130
6131 I915_WRITE(GEN6_RP_DOWN_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01006132 GT_INTERVAL_FROM_US(dev_priv, ei_down));
Akash Goel8a586432015-03-06 11:07:18 +05306133 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01006134 GT_INTERVAL_FROM_US(dev_priv,
6135 ei_down * threshold_down / 100));
Akash Goel8a586432015-03-06 11:07:18 +05306136
Chris Wilsona72b5622016-07-02 15:35:59 +01006137 I915_WRITE(GEN6_RP_CONTROL,
6138 GEN6_RP_MEDIA_TURBO |
6139 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6140 GEN6_RP_MEDIA_IS_GFX |
6141 GEN6_RP_ENABLE |
6142 GEN6_RP_UP_BUSY_AVG |
6143 GEN6_RP_DOWN_IDLE_AVG);
Akash Goel8a586432015-03-06 11:07:18 +05306144
Mika Kuoppala6067a272017-02-15 15:52:59 +02006145skip_hw_write:
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006146 rps->power = new_power;
6147 rps->up_threshold = threshold_up;
6148 rps->down_threshold = threshold_down;
6149 rps->last_adj = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006150}
6151
Chris Wilson2876ce72014-03-28 08:03:34 +00006152static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
6153{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006154 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Chris Wilson2876ce72014-03-28 08:03:34 +00006155 u32 mask = 0;
6156
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006157 /* We use UP_EI_EXPIRED interupts for both up/down in manual mode */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006158 if (val > rps->min_freq_softlimit)
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006159 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 +01006160 if (val < rps->max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00006161 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00006162
Chris Wilson7b3c29f2014-07-10 20:31:19 +01006163 mask &= dev_priv->pm_rps_events;
6164
Imre Deak59d02a12014-12-19 19:33:26 +02006165 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00006166}
6167
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006168/* gen6_set_rps is called to update the frequency request, but should also be
6169 * called when the range (min_delay and max_delay) is modified so that we can
6170 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006171static int gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02006172{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006173 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6174
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006175 /* min/max delay may still have been modified so be sure to
6176 * write the limits value.
6177 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006178 if (val != rps->cur_freq) {
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006179 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006180
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006181 if (INTEL_GEN(dev_priv) >= 9)
Akash Goel57041952015-03-06 11:07:17 +05306182 I915_WRITE(GEN6_RPNSWREQ,
6183 GEN9_FREQUENCY(val));
Chris Wilsondc979972016-05-10 14:10:04 +01006184 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006185 I915_WRITE(GEN6_RPNSWREQ,
6186 HSW_FREQUENCY(val));
6187 else
6188 I915_WRITE(GEN6_RPNSWREQ,
6189 GEN6_FREQUENCY(val) |
6190 GEN6_OFFSET(0) |
6191 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006192 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006193
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006194 /* Make sure we continue to get interrupts
6195 * until we hit the minimum or maximum frequencies.
6196 */
Akash Goel74ef1172015-03-06 11:07:19 +05306197 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00006198 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006199
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006200 rps->cur_freq = val;
Mika Kuoppala0f945922015-11-17 18:14:26 +02006201 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006202
6203 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006204}
6205
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006206static int valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006207{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006208 int err;
6209
Chris Wilsondc979972016-05-10 14:10:04 +01006210 if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006211 "Odd GPU freq value\n"))
6212 val &= ~1;
6213
Deepak Scd25dd52015-07-10 18:31:40 +05306214 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
6215
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006216 if (val != dev_priv->gt_pm.rps.cur_freq) {
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006217 err = vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
6218 if (err)
6219 return err;
6220
Chris Wilsondb4c5e02017-02-10 15:03:46 +00006221 gen6_set_rps_thresholds(dev_priv, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01006222 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006223
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006224 dev_priv->gt_pm.rps.cur_freq = val;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006225 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006226
6227 return 0;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006228}
6229
Deepak Sa7f6e232015-05-09 18:04:44 +05306230/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05306231 *
6232 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05306233 * 1. Forcewake Media well.
6234 * 2. Request idle freq.
6235 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05306236*/
6237static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
6238{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006239 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6240 u32 val = rps->idle_freq;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006241 int err;
Deepak S5549d252014-06-28 11:26:11 +05306242
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006243 if (rps->cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05306244 return;
6245
Chris Wilsonc9efef72017-01-02 15:28:45 +00006246 /* The punit delays the write of the frequency and voltage until it
6247 * determines the GPU is awake. During normal usage we don't want to
6248 * waste power changing the frequency if the GPU is sleeping (rc6).
6249 * However, the GPU and driver is now idle and we do not want to delay
6250 * switching to minimum voltage (reducing power whilst idle) as we do
6251 * not expect to be woken in the near future and so must flush the
6252 * change by waking the device.
6253 *
6254 * We choose to take the media powerwell (either would do to trick the
6255 * punit into committing the voltage change) as that takes a lot less
6256 * power than the render powerwell.
6257 */
Deepak Sa7f6e232015-05-09 18:04:44 +05306258 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006259 err = valleyview_set_rps(dev_priv, val);
Deepak Sa7f6e232015-05-09 18:04:44 +05306260 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006261
6262 if (err)
6263 DRM_ERROR("Failed to set RPS for idle\n");
Deepak S76c3552f2014-01-30 23:08:16 +05306264}
6265
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006266void gen6_rps_busy(struct drm_i915_private *dev_priv)
6267{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006268 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6269
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01006270 mutex_lock(&dev_priv->pcu_lock);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006271 if (rps->enabled) {
Chris Wilsonbd648182017-02-10 15:03:48 +00006272 u8 freq;
6273
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006274 if (dev_priv->pm_rps_events & GEN6_PM_RP_UP_EI_EXPIRED)
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006275 gen6_rps_reset_ei(dev_priv);
6276 I915_WRITE(GEN6_PMINTRMSK,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006277 gen6_rps_pm_mask(dev_priv, rps->cur_freq));
Michał Winiarski2b83c4c2016-06-20 11:58:27 +02006278
Chris Wilsonc33d2472016-07-04 08:08:36 +01006279 gen6_enable_rps_interrupts(dev_priv);
6280
Chris Wilsonbd648182017-02-10 15:03:48 +00006281 /* Use the user's desired frequency as a guide, but for better
6282 * performance, jump directly to RPe as our starting frequency.
6283 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006284 freq = max(rps->cur_freq,
6285 rps->efficient_freq);
Chris Wilsonbd648182017-02-10 15:03:48 +00006286
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006287 if (intel_set_rps(dev_priv,
Chris Wilsonbd648182017-02-10 15:03:48 +00006288 clamp(freq,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006289 rps->min_freq_softlimit,
6290 rps->max_freq_softlimit)))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006291 DRM_DEBUG_DRIVER("Failed to set idle frequency\n");
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006292 }
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01006293 mutex_unlock(&dev_priv->pcu_lock);
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006294}
6295
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006296void gen6_rps_idle(struct drm_i915_private *dev_priv)
6297{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006298 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6299
Chris Wilsonc33d2472016-07-04 08:08:36 +01006300 /* Flush our bottom-half so that it does not race with us
6301 * setting the idle frequency and so that it is bounded by
6302 * our rpm wakeref. And then disable the interrupts to stop any
6303 * futher RPS reclocking whilst we are asleep.
6304 */
6305 gen6_disable_rps_interrupts(dev_priv);
6306
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01006307 mutex_lock(&dev_priv->pcu_lock);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006308 if (rps->enabled) {
Chris Wilsondc979972016-05-10 14:10:04 +01006309 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Deepak S76c3552f2014-01-30 23:08:16 +05306310 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02006311 else
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006312 gen6_set_rps(dev_priv, rps->idle_freq);
6313 rps->last_adj = 0;
Ville Syrjälä12c100b2016-05-23 17:42:48 +03006314 I915_WRITE(GEN6_PMINTRMSK,
6315 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Chris Wilsonc0951f02013-10-10 21:58:50 +01006316 }
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01006317 mutex_unlock(&dev_priv->pcu_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006318}
6319
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006320void gen6_rps_boost(struct drm_i915_gem_request *rq,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006321 struct intel_rps_client *rps_client)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006322{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006323 struct intel_rps *rps = &rq->i915->gt_pm.rps;
Chris Wilson74d290f2017-08-17 13:37:06 +01006324 unsigned long flags;
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006325 bool boost;
6326
Chris Wilson8d3afd72015-05-21 21:01:47 +01006327 /* This is intentionally racy! We peek at the state here, then
6328 * validate inside the RPS worker.
6329 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006330 if (!rps->enabled)
Chris Wilson8d3afd72015-05-21 21:01:47 +01006331 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006332
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006333 boost = false;
Chris Wilson74d290f2017-08-17 13:37:06 +01006334 spin_lock_irqsave(&rq->lock, flags);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006335 if (!rq->waitboost && !i915_gem_request_completed(rq)) {
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006336 atomic_inc(&rps->num_waiters);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006337 rq->waitboost = true;
6338 boost = true;
Chris Wilsonc0951f02013-10-10 21:58:50 +01006339 }
Chris Wilson74d290f2017-08-17 13:37:06 +01006340 spin_unlock_irqrestore(&rq->lock, flags);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006341 if (!boost)
6342 return;
6343
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006344 if (READ_ONCE(rps->cur_freq) < rps->boost_freq)
6345 schedule_work(&rps->work);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006346
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006347 atomic_inc(rps_client ? &rps_client->boosts : &rps->boosts);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006348}
6349
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006350int intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006351{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006352 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006353 int err;
6354
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01006355 lockdep_assert_held(&dev_priv->pcu_lock);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006356 GEM_BUG_ON(val > rps->max_freq);
6357 GEM_BUG_ON(val < rps->min_freq);
Chris Wilsoncfd1c482017-02-20 09:47:07 +00006358
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006359 if (!rps->enabled) {
6360 rps->cur_freq = val;
Chris Wilson76e4e4b2017-02-20 09:47:08 +00006361 return 0;
6362 }
6363
Chris Wilsondc979972016-05-10 14:10:04 +01006364 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006365 err = valleyview_set_rps(dev_priv, val);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006366 else
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006367 err = gen6_set_rps(dev_priv, val);
6368
6369 return err;
Jesse Barnes0a073b82013-04-17 15:54:58 -07006370}
6371
Chris Wilsondc979972016-05-10 14:10:04 +01006372static void gen9_disable_rc6(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00006373{
Zhe Wang20e49362014-11-04 17:07:05 +00006374 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00006375 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00006376}
6377
Chris Wilsondc979972016-05-10 14:10:04 +01006378static void gen9_disable_rps(struct drm_i915_private *dev_priv)
Akash Goel2030d682016-04-23 00:05:45 +05306379{
Akash Goel2030d682016-04-23 00:05:45 +05306380 I915_WRITE(GEN6_RP_CONTROL, 0);
6381}
6382
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01006383static void gen6_disable_rc6(struct drm_i915_private *dev_priv)
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006384{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006385 I915_WRITE(GEN6_RC_CONTROL, 0);
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01006386}
6387
6388static void gen6_disable_rps(struct drm_i915_private *dev_priv)
6389{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006390 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Akash Goel2030d682016-04-23 00:05:45 +05306391 I915_WRITE(GEN6_RP_CONTROL, 0);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006392}
6393
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01006394static void cherryview_disable_rc6(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306395{
Deepak S38807742014-05-23 21:00:15 +05306396 I915_WRITE(GEN6_RC_CONTROL, 0);
6397}
6398
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01006399static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
6400{
6401 I915_WRITE(GEN6_RP_CONTROL, 0);
6402}
6403
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01006404static void valleyview_disable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006405{
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01006406 /* We're doing forcewake before Disabling RC6,
Deepak S98a2e5f2014-08-18 10:35:27 -07006407 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02006408 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07006409
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006410 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006411
Mika Kuoppala59bad942015-01-16 11:34:40 +02006412 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006413}
6414
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01006415static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
6416{
6417 I915_WRITE(GEN6_RP_CONTROL, 0);
6418}
6419
Chris Wilsondc979972016-05-10 14:10:04 +01006420static void intel_print_rc6_info(struct drm_i915_private *dev_priv, u32 mode)
Ben Widawskydc39fff2013-10-18 12:32:07 -07006421{
Chris Wilsondc979972016-05-10 14:10:04 +01006422 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Imre Deak91ca6892014-04-14 20:24:25 +03006423 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
6424 mode = GEN6_RC_CTL_RC6_ENABLE;
6425 else
6426 mode = 0;
6427 }
Chris Wilsondc979972016-05-10 14:10:04 +01006428 if (HAS_RC6p(dev_priv))
Imre Deakb99d49c2016-06-29 19:13:54 +03006429 DRM_DEBUG_DRIVER("Enabling RC6 states: "
6430 "RC6 %s RC6p %s RC6pp %s\n",
6431 onoff(mode & GEN6_RC_CTL_RC6_ENABLE),
6432 onoff(mode & GEN6_RC_CTL_RC6p_ENABLE),
6433 onoff(mode & GEN6_RC_CTL_RC6pp_ENABLE));
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07006434
6435 else
Imre Deakb99d49c2016-06-29 19:13:54 +03006436 DRM_DEBUG_DRIVER("Enabling RC6 states: RC6 %s\n",
6437 onoff(mode & GEN6_RC_CTL_RC6_ENABLE));
Ben Widawskydc39fff2013-10-18 12:32:07 -07006438}
6439
Chris Wilsondc979972016-05-10 14:10:04 +01006440static bool bxt_check_bios_rc6_setup(struct drm_i915_private *dev_priv)
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306441{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006442 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306443 bool enable_rc6 = true;
6444 unsigned long rc6_ctx_base;
Imre Deakfc619842016-06-29 19:13:55 +03006445 u32 rc_ctl;
6446 int rc_sw_target;
6447
6448 rc_ctl = I915_READ(GEN6_RC_CONTROL);
6449 rc_sw_target = (I915_READ(GEN6_RC_STATE) & RC_SW_TARGET_STATE_MASK) >>
6450 RC_SW_TARGET_STATE_SHIFT;
6451 DRM_DEBUG_DRIVER("BIOS enabled RC states: "
6452 "HW_CTRL %s HW_RC6 %s SW_TARGET_STATE %x\n",
6453 onoff(rc_ctl & GEN6_RC_CTL_HW_ENABLE),
6454 onoff(rc_ctl & GEN6_RC_CTL_RC6_ENABLE),
6455 rc_sw_target);
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306456
6457 if (!(I915_READ(RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006458 DRM_DEBUG_DRIVER("RC6 Base location not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306459 enable_rc6 = false;
6460 }
6461
6462 /*
6463 * The exact context size is not known for BXT, so assume a page size
6464 * for this check.
6465 */
6466 rc6_ctx_base = I915_READ(RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006467 if (!((rc6_ctx_base >= ggtt->stolen_reserved_base) &&
6468 (rc6_ctx_base + PAGE_SIZE <= ggtt->stolen_reserved_base +
6469 ggtt->stolen_reserved_size))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006470 DRM_DEBUG_DRIVER("RC6 Base address not as expected.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306471 enable_rc6 = false;
6472 }
6473
6474 if (!(((I915_READ(PWRCTX_MAXCNT_RCSUNIT) & IDLE_TIME_MASK) > 1) &&
6475 ((I915_READ(PWRCTX_MAXCNT_VCSUNIT0) & IDLE_TIME_MASK) > 1) &&
6476 ((I915_READ(PWRCTX_MAXCNT_BCSUNIT) & IDLE_TIME_MASK) > 1) &&
6477 ((I915_READ(PWRCTX_MAXCNT_VECSUNIT) & IDLE_TIME_MASK) > 1))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006478 DRM_DEBUG_DRIVER("Engine Idle wait time not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306479 enable_rc6 = false;
6480 }
6481
Imre Deakfc619842016-06-29 19:13:55 +03006482 if (!I915_READ(GEN8_PUSHBUS_CONTROL) ||
6483 !I915_READ(GEN8_PUSHBUS_ENABLE) ||
6484 !I915_READ(GEN8_PUSHBUS_SHIFT)) {
6485 DRM_DEBUG_DRIVER("Pushbus not setup properly.\n");
6486 enable_rc6 = false;
6487 }
6488
6489 if (!I915_READ(GEN6_GFXPAUSE)) {
6490 DRM_DEBUG_DRIVER("GFX pause not setup properly.\n");
6491 enable_rc6 = false;
6492 }
6493
6494 if (!I915_READ(GEN8_MISC_CTRL0)) {
6495 DRM_DEBUG_DRIVER("GPM control not setup properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306496 enable_rc6 = false;
6497 }
6498
6499 return enable_rc6;
6500}
6501
Chris Wilsondc979972016-05-10 14:10:04 +01006502int sanitize_rc6_option(struct drm_i915_private *dev_priv, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006503{
Daniel Vettere7d66d82015-06-15 23:23:54 +02006504 /* No RC6 before Ironlake and code is gone for ilk. */
Chris Wilsondc979972016-05-10 14:10:04 +01006505 if (INTEL_INFO(dev_priv)->gen < 6)
Imre Deake6069ca2014-04-18 16:01:02 +03006506 return 0;
6507
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306508 if (!enable_rc6)
6509 return 0;
6510
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02006511 if (IS_GEN9_LP(dev_priv) && !bxt_check_bios_rc6_setup(dev_priv)) {
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306512 DRM_INFO("RC6 disabled by BIOS\n");
6513 return 0;
6514 }
6515
Daniel Vetter456470e2012-08-08 23:35:40 +02006516 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03006517 if (enable_rc6 >= 0) {
6518 int mask;
6519
Chris Wilsondc979972016-05-10 14:10:04 +01006520 if (HAS_RC6p(dev_priv))
Imre Deake6069ca2014-04-18 16:01:02 +03006521 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
6522 INTEL_RC6pp_ENABLE;
6523 else
6524 mask = INTEL_RC6_ENABLE;
6525
6526 if ((enable_rc6 & mask) != enable_rc6)
Imre Deakb99d49c2016-06-29 19:13:54 +03006527 DRM_DEBUG_DRIVER("Adjusting RC6 mask to %d "
6528 "(requested %d, valid %d)\n",
6529 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03006530
6531 return enable_rc6 & mask;
6532 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006533
Chris Wilsondc979972016-05-10 14:10:04 +01006534 if (IS_IVYBRIDGE(dev_priv))
Ben Widawskycca84a12014-01-28 20:25:38 -08006535 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08006536
6537 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006538}
6539
Chris Wilsondc979972016-05-10 14:10:04 +01006540static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
Imre Deake6069ca2014-04-18 16:01:02 +03006541{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006542 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6543
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006544 /* All of these values are in units of 50MHz */
Chris Wilson773ea9a2016-07-13 09:10:33 +01006545
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006546 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02006547 if (IS_GEN9_LP(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006548 u32 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006549 rps->rp0_freq = (rp_state_cap >> 16) & 0xff;
6550 rps->rp1_freq = (rp_state_cap >> 8) & 0xff;
6551 rps->min_freq = (rp_state_cap >> 0) & 0xff;
Bob Paauwe35040562015-06-25 14:54:07 -07006552 } else {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006553 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006554 rps->rp0_freq = (rp_state_cap >> 0) & 0xff;
6555 rps->rp1_freq = (rp_state_cap >> 8) & 0xff;
6556 rps->min_freq = (rp_state_cap >> 16) & 0xff;
Bob Paauwe35040562015-06-25 14:54:07 -07006557 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006558 /* hw_max = RP0 until we check for overclocking */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006559 rps->max_freq = rps->rp0_freq;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006560
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006561 rps->efficient_freq = rps->rp1_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01006562 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006563 IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006564 u32 ddcc_status = 0;
6565
6566 if (sandybridge_pcode_read(dev_priv,
6567 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
6568 &ddcc_status) == 0)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006569 rps->efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08006570 clamp_t(u8,
6571 ((ddcc_status >> 8) & 0xff),
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006572 rps->min_freq,
6573 rps->max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006574 }
6575
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006576 if (IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Akash Goelc5e06882015-06-29 14:50:19 +05306577 /* Store the frequency values in 16.66 MHZ units, which is
Chris Wilson773ea9a2016-07-13 09:10:33 +01006578 * the natural hardware unit for SKL
6579 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006580 rps->rp0_freq *= GEN9_FREQ_SCALER;
6581 rps->rp1_freq *= GEN9_FREQ_SCALER;
6582 rps->min_freq *= GEN9_FREQ_SCALER;
6583 rps->max_freq *= GEN9_FREQ_SCALER;
6584 rps->efficient_freq *= GEN9_FREQ_SCALER;
Akash Goelc5e06882015-06-29 14:50:19 +05306585 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006586}
6587
Chris Wilson3a45b052016-07-13 09:10:32 +01006588static void reset_rps(struct drm_i915_private *dev_priv,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006589 int (*set)(struct drm_i915_private *, u8))
Chris Wilson3a45b052016-07-13 09:10:32 +01006590{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006591 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6592 u8 freq = rps->cur_freq;
Chris Wilson3a45b052016-07-13 09:10:32 +01006593
6594 /* force a reset */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006595 rps->power = -1;
6596 rps->cur_freq = -1;
Chris Wilson3a45b052016-07-13 09:10:32 +01006597
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006598 if (set(dev_priv, freq))
6599 DRM_ERROR("Failed to reset RPS to initial values\n");
Chris Wilson3a45b052016-07-13 09:10:32 +01006600}
6601
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006602/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Chris Wilsondc979972016-05-10 14:10:04 +01006603static void gen9_enable_rps(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00006604{
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006605 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
6606
David Weinehall36fe7782017-11-17 10:01:46 +02006607 /* Program defaults and thresholds for RPS */
6608 if (IS_GEN9(dev_priv))
6609 I915_WRITE(GEN6_RC_VIDEO_FREQ,
6610 GEN9_FREQUENCY(dev_priv->gt_pm.rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006611
Akash Goel0beb0592015-03-06 11:07:20 +05306612 /* 1 second timeout*/
6613 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
6614 GT_INTERVAL_FROM_US(dev_priv, 1000000));
6615
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006616 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006617
Akash Goel0beb0592015-03-06 11:07:20 +05306618 /* Leaning on the below call to gen6_set_rps to program/setup the
6619 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
6620 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
Chris Wilson3a45b052016-07-13 09:10:32 +01006621 reset_rps(dev_priv, gen6_set_rps);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006622
6623 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
6624}
6625
Chris Wilsondc979972016-05-10 14:10:04 +01006626static void gen9_enable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006627{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006628 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306629 enum intel_engine_id id;
Rodrigo Vivie4ffc832017-08-22 16:58:28 -07006630 u32 rc6_mode, rc6_mask = 0;
Zhe Wang20e49362014-11-04 17:07:05 +00006631
6632 /* 1a: Software RC state - RC0 */
6633 I915_WRITE(GEN6_RC_STATE, 0);
6634
6635 /* 1b: Get forcewake during program sequence. Although the driver
6636 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006637 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00006638
6639 /* 2a: Disable RC states. */
6640 I915_WRITE(GEN6_RC_CONTROL, 0);
6641
6642 /* 2b: Program RC6 thresholds.*/
Rodrigo Vivi0aab2012017-10-23 15:46:12 -07006643 if (INTEL_GEN(dev_priv) >= 10) {
6644 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16 | 85);
6645 I915_WRITE(GEN10_MEDIA_WAKE_RATE_LIMIT, 150);
6646 } else if (IS_SKYLAKE(dev_priv)) {
6647 /*
6648 * WaRsDoubleRc6WrlWithCoarsePowerGating:skl Doubling WRL only
6649 * when CPG is enabled
6650 */
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05306651 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
Rodrigo Vivi0aab2012017-10-23 15:46:12 -07006652 } else {
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05306653 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
Rodrigo Vivi0aab2012017-10-23 15:46:12 -07006654 }
6655
Zhe Wang20e49362014-11-04 17:07:05 +00006656 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6657 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05306658 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006659 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05306660
Dave Gordon1a3d1892016-05-13 15:36:30 +01006661 if (HAS_GUC(dev_priv))
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05306662 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
6663
Zhe Wang20e49362014-11-04 17:07:05 +00006664 I915_WRITE(GEN6_RC_SLEEP, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00006665
Zhe Wang38c23522015-01-20 12:23:04 +00006666 /* 2c: Program Coarse Power Gating Policies. */
6667 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
6668 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
6669
Zhe Wang20e49362014-11-04 17:07:05 +00006670 /* 3a: Enable RC6 */
Sagar Arun Kamble771decb2017-10-10 22:30:07 +01006671 if (intel_rc6_enabled() & INTEL_RC6_ENABLE)
Zhe Wang20e49362014-11-04 17:07:05 +00006672 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Jani Nikula87ad3212016-01-14 12:53:34 +02006673 DRM_INFO("RC6 %s\n", onoff(rc6_mask & GEN6_RC_CTL_RC6_ENABLE));
Chris Wilson1c044f92017-01-25 17:26:01 +00006674 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
Rodrigo Vivie4ffc832017-08-22 16:58:28 -07006675
6676 /* WaRsUseTimeoutMode:cnl (pre-prod) */
6677 if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_C0))
6678 rc6_mode = GEN7_RC_CTL_TO_MODE;
6679 else
6680 rc6_mode = GEN6_RC_CTL_EI_MODE(1);
6681
Chris Wilson1c044f92017-01-25 17:26:01 +00006682 I915_WRITE(GEN6_RC_CONTROL,
Rodrigo Vivie4ffc832017-08-22 16:58:28 -07006683 GEN6_RC_CTL_HW_ENABLE | rc6_mode | rc6_mask);
Zhe Wang20e49362014-11-04 17:07:05 +00006684
Sagar Kamblecb07bae2015-04-12 11:28:14 +05306685 /*
6686 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05306687 * WaRsDisableCoarsePowerGating:skl,bxt - Render/Media PG need to be disabled with RC6.
Sagar Kamblecb07bae2015-04-12 11:28:14 +05306688 */
Chris Wilsondc979972016-05-10 14:10:04 +01006689 if (NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05306690 I915_WRITE(GEN9_PG_ENABLE, 0);
6691 else
6692 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
6693 (GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE) : 0);
Zhe Wang38c23522015-01-20 12:23:04 +00006694
Mika Kuoppala59bad942015-01-16 11:34:40 +02006695 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00006696}
6697
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01006698static void gen8_enable_rc6(struct drm_i915_private *dev_priv)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006699{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006700 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306701 enum intel_engine_id id;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006702 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006703
6704 /* 1a: Software RC state - RC0 */
6705 I915_WRITE(GEN6_RC_STATE, 0);
6706
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01006707 /* 1b: Get forcewake during program sequence. Although the driver
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006708 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006709 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006710
6711 /* 2a: Disable RC states. */
6712 I915_WRITE(GEN6_RC_CONTROL, 0);
6713
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006714 /* 2b: Program RC6 thresholds.*/
6715 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
6716 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6717 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05306718 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006719 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006720 I915_WRITE(GEN6_RC_SLEEP, 0);
Sagar Arun Kamble415544d2017-10-10 22:30:00 +01006721 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006722
6723 /* 3: Enable RC6 */
Sagar Arun Kamble771decb2017-10-10 22:30:07 +01006724 if (intel_rc6_enabled() & INTEL_RC6_ENABLE)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006725 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Chris Wilsondc979972016-05-10 14:10:04 +01006726 intel_print_rc6_info(dev_priv, rc6_mask);
Sagar Arun Kamble415544d2017-10-10 22:30:00 +01006727
6728 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
6729 GEN7_RC_CTL_TO_MODE |
6730 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006731
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01006732 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
6733}
6734
6735static void gen8_enable_rps(struct drm_i915_private *dev_priv)
6736{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006737 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6738
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01006739 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
6740
6741 /* 1 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07006742 I915_WRITE(GEN6_RPNSWREQ,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006743 HSW_FREQUENCY(rps->rp1_freq));
Ben Widawskyf9bdc582014-03-31 17:16:41 -07006744 I915_WRITE(GEN6_RC_VIDEO_FREQ,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006745 HSW_FREQUENCY(rps->rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02006746 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
6747 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006748
Daniel Vetter7526ed72014-09-29 15:07:19 +02006749 /* Docs recommend 900MHz, and 300 MHz respectively */
6750 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006751 rps->max_freq_softlimit << 24 |
6752 rps->min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006753
Daniel Vetter7526ed72014-09-29 15:07:19 +02006754 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
6755 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
6756 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
6757 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006758
Daniel Vetter7526ed72014-09-29 15:07:19 +02006759 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006760
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01006761 /* 2: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02006762 I915_WRITE(GEN6_RP_CONTROL,
6763 GEN6_RP_MEDIA_TURBO |
6764 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6765 GEN6_RP_MEDIA_IS_GFX |
6766 GEN6_RP_ENABLE |
6767 GEN6_RP_UP_BUSY_AVG |
6768 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006769
Chris Wilson3a45b052016-07-13 09:10:32 +01006770 reset_rps(dev_priv, gen6_set_rps);
Daniel Vetter7526ed72014-09-29 15:07:19 +02006771
Mika Kuoppala59bad942015-01-16 11:34:40 +02006772 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006773}
6774
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01006775static void gen6_enable_rc6(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006776{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006777 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306778 enum intel_engine_id id;
Chris Wilson99ac9612016-07-13 09:10:34 +01006779 u32 rc6vids, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006780 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006781 int rc6_mode;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00006782 int ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006783
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006784 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006785
6786 /* Clear the DBG now so we don't confuse earlier errors */
Ville Syrjälä297b32e2016-04-13 21:09:30 +03006787 gtfifodbg = I915_READ(GTFIFODBG);
6788 if (gtfifodbg) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006789 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
6790 I915_WRITE(GTFIFODBG, gtfifodbg);
6791 }
6792
Mika Kuoppala59bad942015-01-16 11:34:40 +02006793 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006794
6795 /* disable the counters and set deterministic thresholds */
6796 I915_WRITE(GEN6_RC_CONTROL, 0);
6797
6798 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
6799 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
6800 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
6801 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
6802 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
6803
Akash Goel3b3f1652016-10-13 22:44:48 +05306804 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006805 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006806
6807 I915_WRITE(GEN6_RC_SLEEP, 0);
6808 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Chris Wilsondc979972016-05-10 14:10:04 +01006809 if (IS_IVYBRIDGE(dev_priv))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07006810 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
6811 else
6812 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08006813 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006814 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
6815
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006816 /* Check if we are enabling RC6 */
Sagar Arun Kamble771decb2017-10-10 22:30:07 +01006817 rc6_mode = intel_rc6_enabled();
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006818 if (rc6_mode & INTEL_RC6_ENABLE)
6819 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
6820
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006821 /* We don't use those on Haswell */
Chris Wilsondc979972016-05-10 14:10:04 +01006822 if (!IS_HASWELL(dev_priv)) {
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006823 if (rc6_mode & INTEL_RC6p_ENABLE)
6824 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006825
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006826 if (rc6_mode & INTEL_RC6pp_ENABLE)
6827 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
6828 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006829
Chris Wilsondc979972016-05-10 14:10:04 +01006830 intel_print_rc6_info(dev_priv, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006831
6832 I915_WRITE(GEN6_RC_CONTROL,
6833 rc6_mask |
6834 GEN6_RC_CTL_EI_MODE(1) |
6835 GEN6_RC_CTL_HW_ENABLE);
6836
Ben Widawsky31643d52012-09-26 10:34:01 -07006837 rc6vids = 0;
6838 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
Chris Wilsondc979972016-05-10 14:10:04 +01006839 if (IS_GEN6(dev_priv) && ret) {
Ben Widawsky31643d52012-09-26 10:34:01 -07006840 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
Chris Wilsondc979972016-05-10 14:10:04 +01006841 } else if (IS_GEN6(dev_priv) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
Ben Widawsky31643d52012-09-26 10:34:01 -07006842 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
6843 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
6844 rc6vids &= 0xffff00;
6845 rc6vids |= GEN6_ENCODE_RC6_VID(450);
6846 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
6847 if (ret)
6848 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
6849 }
6850
Mika Kuoppala59bad942015-01-16 11:34:40 +02006851 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006852}
6853
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01006854static void gen6_enable_rps(struct drm_i915_private *dev_priv)
6855{
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01006856 /* Here begins a magic sequence of register writes to enable
6857 * auto-downclocking.
6858 *
6859 * Perhaps there might be some value in exposing these to
6860 * userspace...
6861 */
6862 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
6863
6864 /* Power down if completely idle for over 50ms */
6865 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
6866 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
6867
6868 reset_rps(dev_priv, gen6_set_rps);
6869
6870 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
6871}
6872
Chris Wilsonfb7404e2016-07-13 09:10:38 +01006873static void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006874{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006875 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006876 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01006877 unsigned int gpu_freq;
6878 unsigned int max_ia_freq, min_ring_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05306879 unsigned int max_gpu_freq, min_gpu_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006880 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03006881 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006882
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01006883 WARN_ON(!mutex_is_locked(&dev_priv->pcu_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02006884
Ben Widawskyeda79642013-10-07 17:15:48 -03006885 policy = cpufreq_cpu_get(0);
6886 if (policy) {
6887 max_ia_freq = policy->cpuinfo.max_freq;
6888 cpufreq_cpu_put(policy);
6889 } else {
6890 /*
6891 * Default to measured freq if none found, PCU will ensure we
6892 * don't go over
6893 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006894 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03006895 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006896
6897 /* Convert from kHz to MHz */
6898 max_ia_freq /= 1000;
6899
Ben Widawsky153b4b952013-10-22 22:05:09 -07006900 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07006901 /* convert DDR frequency from units of 266.6MHz to bandwidth */
6902 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01006903
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006904 if (IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05306905 /* Convert GT frequency to 50 HZ units */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006906 min_gpu_freq = rps->min_freq / GEN9_FREQ_SCALER;
6907 max_gpu_freq = rps->max_freq / GEN9_FREQ_SCALER;
Akash Goel4c8c7742015-06-29 14:50:20 +05306908 } else {
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006909 min_gpu_freq = rps->min_freq;
6910 max_gpu_freq = rps->max_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05306911 }
6912
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006913 /*
6914 * For each potential GPU frequency, load a ring frequency we'd like
6915 * to use for memory access. We do this by specifying the IA frequency
6916 * the PCU should use as a reference to determine the ring frequency.
6917 */
Akash Goel4c8c7742015-06-29 14:50:20 +05306918 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
6919 int diff = max_gpu_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01006920 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006921
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006922 if (IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05306923 /*
6924 * ring_freq = 2 * GT. ring_freq is in 100MHz units
6925 * No floor required for ring frequency on SKL.
6926 */
6927 ring_freq = gpu_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01006928 } else if (INTEL_INFO(dev_priv)->gen >= 8) {
Ben Widawsky46c764d2013-11-02 21:07:49 -07006929 /* max(2 * GT, DDR). NB: GT is 50MHz units */
6930 ring_freq = max(min_ring_freq, gpu_freq);
Chris Wilsondc979972016-05-10 14:10:04 +01006931 } else if (IS_HASWELL(dev_priv)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07006932 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01006933 ring_freq = max(min_ring_freq, ring_freq);
6934 /* leave ia_freq as the default, chosen by cpufreq */
6935 } else {
6936 /* On older processors, there is no separate ring
6937 * clock domain, so in order to boost the bandwidth
6938 * of the ring, we need to upclock the CPU (ia_freq).
6939 *
6940 * For GPU frequencies less than 750MHz,
6941 * just use the lowest ring freq.
6942 */
6943 if (gpu_freq < min_freq)
6944 ia_freq = 800;
6945 else
6946 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
6947 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
6948 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006949
Ben Widawsky42c05262012-09-26 10:34:00 -07006950 sandybridge_pcode_write(dev_priv,
6951 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01006952 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
6953 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
6954 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006955 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006956}
6957
Ville Syrjälä03af2042014-06-28 02:03:53 +03006958static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05306959{
6960 u32 val, rp0;
6961
Jani Nikula5b5929c2015-10-07 11:17:46 +03006962 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05306963
Imre Deak43b67992016-08-31 19:13:02 +03006964 switch (INTEL_INFO(dev_priv)->sseu.eu_total) {
Jani Nikula5b5929c2015-10-07 11:17:46 +03006965 case 8:
6966 /* (2 * 4) config */
6967 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
6968 break;
6969 case 12:
6970 /* (2 * 6) config */
6971 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
6972 break;
6973 case 16:
6974 /* (2 * 8) config */
6975 default:
6976 /* Setting (2 * 8) Min RP0 for any other combination */
6977 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
6978 break;
Deepak S095acd52015-01-17 11:05:59 +05306979 }
Jani Nikula5b5929c2015-10-07 11:17:46 +03006980
6981 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
6982
Deepak S2b6b3a02014-05-27 15:59:30 +05306983 return rp0;
6984}
6985
6986static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
6987{
6988 u32 val, rpe;
6989
6990 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
6991 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
6992
6993 return rpe;
6994}
6995
Deepak S7707df42014-07-12 18:46:14 +05306996static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
6997{
6998 u32 val, rp1;
6999
Jani Nikula5b5929c2015-10-07 11:17:46 +03007000 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
7001 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
7002
Deepak S7707df42014-07-12 18:46:14 +05307003 return rp1;
7004}
7005
Deepak S96676fe2016-08-12 18:46:41 +05307006static u32 cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
7007{
7008 u32 val, rpn;
7009
7010 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
7011 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
7012 FB_GFX_FREQ_FUSE_MASK);
7013
7014 return rpn;
7015}
7016
Deepak Sf8f2b002014-07-10 13:16:21 +05307017static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
7018{
7019 u32 val, rp1;
7020
7021 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
7022
7023 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
7024
7025 return rp1;
7026}
7027
Ville Syrjälä03af2042014-06-28 02:03:53 +03007028static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07007029{
7030 u32 val, rp0;
7031
Jani Nikula64936252013-05-22 15:36:20 +03007032 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007033
7034 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
7035 /* Clamp to max */
7036 rp0 = min_t(u32, rp0, 0xea);
7037
7038 return rp0;
7039}
7040
7041static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
7042{
7043 u32 val, rpe;
7044
Jani Nikula64936252013-05-22 15:36:20 +03007045 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007046 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03007047 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007048 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
7049
7050 return rpe;
7051}
7052
Ville Syrjälä03af2042014-06-28 02:03:53 +03007053static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07007054{
Imre Deak36146032014-12-04 18:39:35 +02007055 u32 val;
7056
7057 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
7058 /*
7059 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
7060 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
7061 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
7062 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
7063 * to make sure it matches what Punit accepts.
7064 */
7065 return max_t(u32, val, 0xc0);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007066}
7067
Imre Deakae484342014-03-31 15:10:44 +03007068/* Check that the pctx buffer wasn't move under us. */
7069static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
7070{
7071 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
7072
7073 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
7074 dev_priv->vlv_pctx->stolen->start);
7075}
7076
Deepak S38807742014-05-23 21:00:15 +05307077
7078/* Check that the pcbr address is not empty. */
7079static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
7080{
7081 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
7082
7083 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
7084}
7085
Chris Wilsondc979972016-05-10 14:10:04 +01007086static void cherryview_setup_pctx(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05307087{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02007088 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03007089 unsigned long pctx_paddr, paddr;
Deepak S38807742014-05-23 21:00:15 +05307090 u32 pcbr;
7091 int pctx_size = 32*1024;
7092
Deepak S38807742014-05-23 21:00:15 +05307093 pcbr = I915_READ(VLV_PCBR);
7094 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02007095 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05307096 paddr = (dev_priv->mm.stolen_base +
Joonas Lahtinen62106b42016-03-18 10:42:57 +02007097 (ggtt->stolen_size - pctx_size));
Deepak S38807742014-05-23 21:00:15 +05307098
7099 pctx_paddr = (paddr & (~4095));
7100 I915_WRITE(VLV_PCBR, pctx_paddr);
7101 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02007102
7103 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05307104}
7105
Chris Wilsondc979972016-05-10 14:10:04 +01007106static void valleyview_setup_pctx(struct drm_i915_private *dev_priv)
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007107{
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007108 struct drm_i915_gem_object *pctx;
7109 unsigned long pctx_paddr;
7110 u32 pcbr;
7111 int pctx_size = 24*1024;
7112
7113 pcbr = I915_READ(VLV_PCBR);
7114 if (pcbr) {
7115 /* BIOS set it up already, grab the pre-alloc'd space */
7116 int pcbr_offset;
7117
7118 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00007119 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007120 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02007121 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007122 pctx_size);
7123 goto out;
7124 }
7125
Ville Syrjäläce611ef2014-11-07 21:33:46 +02007126 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
7127
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007128 /*
7129 * From the Gunit register HAS:
7130 * The Gfx driver is expected to program this register and ensure
7131 * proper allocation within Gfx stolen memory. For example, this
7132 * register should be programmed such than the PCBR range does not
7133 * overlap with other ranges, such as the frame buffer, protected
7134 * memory, or any other relevant ranges.
7135 */
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00007136 pctx = i915_gem_object_create_stolen(dev_priv, pctx_size);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007137 if (!pctx) {
7138 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
Tvrtko Ursulinee504892016-02-11 10:27:30 +00007139 goto out;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007140 }
7141
7142 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
7143 I915_WRITE(VLV_PCBR, pctx_paddr);
7144
7145out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02007146 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007147 dev_priv->vlv_pctx = pctx;
7148}
7149
Chris Wilsondc979972016-05-10 14:10:04 +01007150static void valleyview_cleanup_pctx(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007151{
Imre Deakae484342014-03-31 15:10:44 +03007152 if (WARN_ON(!dev_priv->vlv_pctx))
7153 return;
7154
Chris Wilsonf0cd5182016-10-28 13:58:43 +01007155 i915_gem_object_put(dev_priv->vlv_pctx);
Imre Deakae484342014-03-31 15:10:44 +03007156 dev_priv->vlv_pctx = NULL;
7157}
7158
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007159static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
7160{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007161 dev_priv->gt_pm.rps.gpll_ref_freq =
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007162 vlv_get_cck_clock(dev_priv, "GPLL ref",
7163 CCK_GPLL_CLOCK_CONTROL,
7164 dev_priv->czclk_freq);
7165
7166 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007167 dev_priv->gt_pm.rps.gpll_ref_freq);
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007168}
7169
Chris Wilsondc979972016-05-10 14:10:04 +01007170static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03007171{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007172 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007173 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03007174
Chris Wilsondc979972016-05-10 14:10:04 +01007175 valleyview_setup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007176
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007177 vlv_init_gpll_ref_freq(dev_priv);
7178
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007179 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
7180 switch ((val >> 6) & 3) {
7181 case 0:
7182 case 1:
7183 dev_priv->mem_freq = 800;
7184 break;
7185 case 2:
7186 dev_priv->mem_freq = 1066;
7187 break;
7188 case 3:
7189 dev_priv->mem_freq = 1333;
7190 break;
7191 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02007192 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007193
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007194 rps->max_freq = valleyview_rps_max_freq(dev_priv);
7195 rps->rp0_freq = rps->max_freq;
Imre Deak4e805192014-04-14 20:24:41 +03007196 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007197 intel_gpu_freq(dev_priv, rps->max_freq),
7198 rps->max_freq);
Imre Deak4e805192014-04-14 20:24:41 +03007199
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007200 rps->efficient_freq = valleyview_rps_rpe_freq(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007201 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007202 intel_gpu_freq(dev_priv, rps->efficient_freq),
7203 rps->efficient_freq);
Imre Deak4e805192014-04-14 20:24:41 +03007204
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007205 rps->rp1_freq = valleyview_rps_guar_freq(dev_priv);
Deepak Sf8f2b002014-07-10 13:16:21 +05307206 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007207 intel_gpu_freq(dev_priv, rps->rp1_freq),
7208 rps->rp1_freq);
Deepak Sf8f2b002014-07-10 13:16:21 +05307209
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007210 rps->min_freq = valleyview_rps_min_freq(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007211 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007212 intel_gpu_freq(dev_priv, rps->min_freq),
7213 rps->min_freq);
Imre Deak4e805192014-04-14 20:24:41 +03007214}
7215
Chris Wilsondc979972016-05-10 14:10:04 +01007216static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05307217{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007218 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007219 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05307220
Chris Wilsondc979972016-05-10 14:10:04 +01007221 cherryview_setup_pctx(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307222
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007223 vlv_init_gpll_ref_freq(dev_priv);
7224
Ville Syrjäläa5805162015-05-26 20:42:30 +03007225 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02007226 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007227 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02007228
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007229 switch ((val >> 2) & 0x7) {
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007230 case 3:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007231 dev_priv->mem_freq = 2000;
7232 break;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03007233 default:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007234 dev_priv->mem_freq = 1600;
7235 break;
7236 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02007237 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007238
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007239 rps->max_freq = cherryview_rps_max_freq(dev_priv);
7240 rps->rp0_freq = rps->max_freq;
Deepak S2b6b3a02014-05-27 15:59:30 +05307241 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007242 intel_gpu_freq(dev_priv, rps->max_freq),
7243 rps->max_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05307244
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007245 rps->efficient_freq = cherryview_rps_rpe_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307246 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007247 intel_gpu_freq(dev_priv, rps->efficient_freq),
7248 rps->efficient_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05307249
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007250 rps->rp1_freq = cherryview_rps_guar_freq(dev_priv);
Deepak S7707df42014-07-12 18:46:14 +05307251 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007252 intel_gpu_freq(dev_priv, rps->rp1_freq),
7253 rps->rp1_freq);
Deepak S7707df42014-07-12 18:46:14 +05307254
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007255 rps->min_freq = cherryview_rps_min_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307256 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007257 intel_gpu_freq(dev_priv, rps->min_freq),
7258 rps->min_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05307259
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007260 WARN_ONCE((rps->max_freq | rps->efficient_freq | rps->rp1_freq |
7261 rps->min_freq) & 1,
Ville Syrjälä1c147622014-08-18 14:42:43 +03007262 "Odd GPU freq values\n");
Deepak S38807742014-05-23 21:00:15 +05307263}
7264
Chris Wilsondc979972016-05-10 14:10:04 +01007265static void valleyview_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03007266{
Chris Wilsondc979972016-05-10 14:10:04 +01007267 valleyview_cleanup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007268}
7269
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007270static void cherryview_enable_rc6(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05307271{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007272 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05307273 enum intel_engine_id id;
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007274 u32 gtfifodbg, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05307275
Ville Syrjälä297b32e2016-04-13 21:09:30 +03007276 gtfifodbg = I915_READ(GTFIFODBG) & ~(GT_FIFO_SBDEDICATE_FREE_ENTRY_CHV |
7277 GT_FIFO_FREE_ENTRIES_CHV);
Deepak S38807742014-05-23 21:00:15 +05307278 if (gtfifodbg) {
7279 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
7280 gtfifodbg);
7281 I915_WRITE(GTFIFODBG, gtfifodbg);
7282 }
7283
7284 cherryview_check_pctx(dev_priv);
7285
7286 /* 1a & 1b: Get forcewake during program sequence. Although the driver
7287 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02007288 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05307289
Ville Syrjälä160614a2015-01-19 13:50:47 +02007290 /* Disable RC states. */
7291 I915_WRITE(GEN6_RC_CONTROL, 0);
7292
Deepak S38807742014-05-23 21:00:15 +05307293 /* 2a: Program RC6 thresholds.*/
7294 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
7295 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
7296 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
7297
Akash Goel3b3f1652016-10-13 22:44:48 +05307298 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007299 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Deepak S38807742014-05-23 21:00:15 +05307300 I915_WRITE(GEN6_RC_SLEEP, 0);
7301
Deepak Sf4f71c72015-03-28 15:23:35 +05307302 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
7303 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
Deepak S38807742014-05-23 21:00:15 +05307304
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007305 /* Allows RC6 residency counter to work */
Deepak S38807742014-05-23 21:00:15 +05307306 I915_WRITE(VLV_COUNTER_CONTROL,
7307 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
7308 VLV_MEDIA_RC6_COUNT_EN |
7309 VLV_RENDER_RC6_COUNT_EN));
7310
7311 /* For now we assume BIOS is allocating and populating the PCBR */
7312 pcbr = I915_READ(VLV_PCBR);
7313
Deepak S38807742014-05-23 21:00:15 +05307314 /* 3: Enable RC6 */
Sagar Arun Kamble771decb2017-10-10 22:30:07 +01007315 if ((intel_rc6_enabled() & INTEL_RC6_ENABLE) &&
Chris Wilsondc979972016-05-10 14:10:04 +01007316 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02007317 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05307318
7319 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
7320
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007321 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
7322}
7323
7324static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
7325{
7326 u32 val;
7327
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007328 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
7329
7330 /* 1: Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02007331 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05307332 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
7333 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
7334 I915_WRITE(GEN6_RP_UP_EI, 66000);
7335 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
7336
7337 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7338
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007339 /* 2: Enable RPS */
Deepak S2b6b3a02014-05-27 15:59:30 +05307340 I915_WRITE(GEN6_RP_CONTROL,
7341 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02007342 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05307343 GEN6_RP_ENABLE |
7344 GEN6_RP_UP_BUSY_AVG |
7345 GEN6_RP_DOWN_IDLE_AVG);
7346
Deepak S3ef62342015-04-29 08:36:24 +05307347 /* Setting Fixed Bias */
7348 val = VLV_OVERRIDE_EN |
7349 VLV_SOC_TDP_EN |
7350 CHV_BIAS_CPU_50_SOC_50;
7351 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
7352
Deepak S2b6b3a02014-05-27 15:59:30 +05307353 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
7354
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02007355 /* RPS code assumes GPLL is used */
7356 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
7357
Jani Nikula742f4912015-09-03 11:16:09 +03007358 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05307359 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7360
Chris Wilson3a45b052016-07-13 09:10:32 +01007361 reset_rps(dev_priv, valleyview_set_rps);
Deepak S2b6b3a02014-05-27 15:59:30 +05307362
Mika Kuoppala59bad942015-01-16 11:34:40 +02007363 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05307364}
7365
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01007366static void valleyview_enable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07007367{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007368 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05307369 enum intel_engine_id id;
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01007370 u32 gtfifodbg, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07007371
Imre Deakae484342014-03-31 15:10:44 +03007372 valleyview_check_pctx(dev_priv);
7373
Ville Syrjälä297b32e2016-04-13 21:09:30 +03007374 gtfifodbg = I915_READ(GTFIFODBG);
7375 if (gtfifodbg) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07007376 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
7377 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007378 I915_WRITE(GTFIFODBG, gtfifodbg);
7379 }
7380
Mika Kuoppala59bad942015-01-16 11:34:40 +02007381 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007382
Ville Syrjälä160614a2015-01-19 13:50:47 +02007383 /* Disable RC states. */
7384 I915_WRITE(GEN6_RC_CONTROL, 0);
7385
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01007386 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
7387 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
7388 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
7389
7390 for_each_engine(engine, dev_priv, id)
7391 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
7392
7393 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
7394
7395 /* Allows RC6 residency counter to work */
7396 I915_WRITE(VLV_COUNTER_CONTROL,
7397 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
7398 VLV_MEDIA_RC0_COUNT_EN |
7399 VLV_RENDER_RC0_COUNT_EN |
7400 VLV_MEDIA_RC6_COUNT_EN |
7401 VLV_RENDER_RC6_COUNT_EN));
7402
Sagar Arun Kamble771decb2017-10-10 22:30:07 +01007403 if (intel_rc6_enabled() & INTEL_RC6_ENABLE)
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01007404 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
7405
7406 intel_print_rc6_info(dev_priv, rc6_mode);
7407
7408 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
7409
7410 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
7411}
7412
7413static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
7414{
7415 u32 val;
7416
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01007417 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
7418
Ville Syrjäläcad725f2015-01-19 13:50:48 +02007419 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007420 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
7421 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
7422 I915_WRITE(GEN6_RP_UP_EI, 66000);
7423 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
7424
7425 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7426
7427 I915_WRITE(GEN6_RP_CONTROL,
7428 GEN6_RP_MEDIA_TURBO |
7429 GEN6_RP_MEDIA_HW_NORMAL_MODE |
7430 GEN6_RP_MEDIA_IS_GFX |
7431 GEN6_RP_ENABLE |
7432 GEN6_RP_UP_BUSY_AVG |
7433 GEN6_RP_DOWN_IDLE_CONT);
7434
Deepak S3ef62342015-04-29 08:36:24 +05307435 /* Setting Fixed Bias */
7436 val = VLV_OVERRIDE_EN |
7437 VLV_SOC_TDP_EN |
7438 VLV_BIAS_CPU_125_SOC_875;
7439 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
7440
Jani Nikula64936252013-05-22 15:36:20 +03007441 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007442
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02007443 /* RPS code assumes GPLL is used */
7444 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
7445
Jani Nikula742f4912015-09-03 11:16:09 +03007446 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07007447 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7448
Chris Wilson3a45b052016-07-13 09:10:32 +01007449 reset_rps(dev_priv, valleyview_set_rps);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007450
Mika Kuoppala59bad942015-01-16 11:34:40 +02007451 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007452}
7453
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007454static unsigned long intel_pxfreq(u32 vidfreq)
7455{
7456 unsigned long freq;
7457 int div = (vidfreq & 0x3f0000) >> 16;
7458 int post = (vidfreq & 0x3000) >> 12;
7459 int pre = (vidfreq & 0x7);
7460
7461 if (!pre)
7462 return 0;
7463
7464 freq = ((div * 133333) / ((1<<post) * pre));
7465
7466 return freq;
7467}
7468
Daniel Vettereb48eb02012-04-26 23:28:12 +02007469static const struct cparams {
7470 u16 i;
7471 u16 t;
7472 u16 m;
7473 u16 c;
7474} cparams[] = {
7475 { 1, 1333, 301, 28664 },
7476 { 1, 1066, 294, 24460 },
7477 { 1, 800, 294, 25192 },
7478 { 0, 1333, 276, 27605 },
7479 { 0, 1066, 276, 27605 },
7480 { 0, 800, 231, 23784 },
7481};
7482
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007483static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007484{
7485 u64 total_count, diff, ret;
7486 u32 count1, count2, count3, m = 0, c = 0;
7487 unsigned long now = jiffies_to_msecs(jiffies), diff1;
7488 int i;
7489
Chris Wilson67520412017-03-02 13:28:01 +00007490 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007491
Daniel Vetter20e4d402012-08-08 23:35:39 +02007492 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007493
7494 /* Prevent division-by-zero if we are asking too fast.
7495 * Also, we don't get interesting results if we are polling
7496 * faster than once in 10ms, so just return the saved value
7497 * in such cases.
7498 */
7499 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02007500 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007501
7502 count1 = I915_READ(DMIEC);
7503 count2 = I915_READ(DDREC);
7504 count3 = I915_READ(CSIEC);
7505
7506 total_count = count1 + count2 + count3;
7507
7508 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02007509 if (total_count < dev_priv->ips.last_count1) {
7510 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007511 diff += total_count;
7512 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007513 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007514 }
7515
7516 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007517 if (cparams[i].i == dev_priv->ips.c_m &&
7518 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02007519 m = cparams[i].m;
7520 c = cparams[i].c;
7521 break;
7522 }
7523 }
7524
7525 diff = div_u64(diff, diff1);
7526 ret = ((m * diff) + c);
7527 ret = div_u64(ret, 10);
7528
Daniel Vetter20e4d402012-08-08 23:35:39 +02007529 dev_priv->ips.last_count1 = total_count;
7530 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007531
Daniel Vetter20e4d402012-08-08 23:35:39 +02007532 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007533
7534 return ret;
7535}
7536
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007537unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
7538{
7539 unsigned long val;
7540
Chris Wilsondc979972016-05-10 14:10:04 +01007541 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007542 return 0;
7543
7544 spin_lock_irq(&mchdev_lock);
7545
7546 val = __i915_chipset_val(dev_priv);
7547
7548 spin_unlock_irq(&mchdev_lock);
7549
7550 return val;
7551}
7552
Daniel Vettereb48eb02012-04-26 23:28:12 +02007553unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
7554{
7555 unsigned long m, x, b;
7556 u32 tsfs;
7557
7558 tsfs = I915_READ(TSFS);
7559
7560 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
7561 x = I915_READ8(TR1);
7562
7563 b = tsfs & TSFS_INTR_MASK;
7564
7565 return ((m * x) / 127) - b;
7566}
7567
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007568static int _pxvid_to_vd(u8 pxvid)
7569{
7570 if (pxvid == 0)
7571 return 0;
7572
7573 if (pxvid >= 8 && pxvid < 31)
7574 pxvid = 31;
7575
7576 return (pxvid + 2) * 125;
7577}
7578
7579static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007580{
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007581 const int vd = _pxvid_to_vd(pxvid);
7582 const int vm = vd - 1125;
7583
Chris Wilsondc979972016-05-10 14:10:04 +01007584 if (INTEL_INFO(dev_priv)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007585 return vm > 0 ? vm : 0;
7586
7587 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007588}
7589
Daniel Vetter02d71952012-08-09 16:44:54 +02007590static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007591{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00007592 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007593 u32 count;
7594
Chris Wilson67520412017-03-02 13:28:01 +00007595 lockdep_assert_held(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007596
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00007597 now = ktime_get_raw_ns();
7598 diffms = now - dev_priv->ips.last_time2;
7599 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007600
7601 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02007602 if (!diffms)
7603 return;
7604
7605 count = I915_READ(GFXEC);
7606
Daniel Vetter20e4d402012-08-08 23:35:39 +02007607 if (count < dev_priv->ips.last_count2) {
7608 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007609 diff += count;
7610 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007611 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007612 }
7613
Daniel Vetter20e4d402012-08-08 23:35:39 +02007614 dev_priv->ips.last_count2 = count;
7615 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007616
7617 /* More magic constants... */
7618 diff = diff * 1181;
7619 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02007620 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007621}
7622
Daniel Vetter02d71952012-08-09 16:44:54 +02007623void i915_update_gfx_val(struct drm_i915_private *dev_priv)
7624{
Chris Wilsondc979972016-05-10 14:10:04 +01007625 if (INTEL_INFO(dev_priv)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02007626 return;
7627
Daniel Vetter92703882012-08-09 16:46:01 +02007628 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007629
7630 __i915_update_gfx_val(dev_priv);
7631
Daniel Vetter92703882012-08-09 16:46:01 +02007632 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007633}
7634
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007635static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007636{
7637 unsigned long t, corr, state1, corr2, state2;
7638 u32 pxvid, ext_v;
7639
Chris Wilson67520412017-03-02 13:28:01 +00007640 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007641
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007642 pxvid = I915_READ(PXVFREQ(dev_priv->gt_pm.rps.cur_freq));
Daniel Vettereb48eb02012-04-26 23:28:12 +02007643 pxvid = (pxvid >> 24) & 0x7f;
7644 ext_v = pvid_to_extvid(dev_priv, pxvid);
7645
7646 state1 = ext_v;
7647
7648 t = i915_mch_val(dev_priv);
7649
7650 /* Revel in the empirically derived constants */
7651
7652 /* Correction factor in 1/100000 units */
7653 if (t > 80)
7654 corr = ((t * 2349) + 135940);
7655 else if (t >= 50)
7656 corr = ((t * 964) + 29317);
7657 else /* < 50 */
7658 corr = ((t * 301) + 1004);
7659
7660 corr = corr * ((150142 * state1) / 10000 - 78642);
7661 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02007662 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007663
7664 state2 = (corr2 * state1) / 10000;
7665 state2 /= 100; /* convert to mW */
7666
Daniel Vetter02d71952012-08-09 16:44:54 +02007667 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007668
Daniel Vetter20e4d402012-08-08 23:35:39 +02007669 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007670}
7671
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007672unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
7673{
7674 unsigned long val;
7675
Chris Wilsondc979972016-05-10 14:10:04 +01007676 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007677 return 0;
7678
7679 spin_lock_irq(&mchdev_lock);
7680
7681 val = __i915_gfx_val(dev_priv);
7682
7683 spin_unlock_irq(&mchdev_lock);
7684
7685 return val;
7686}
7687
Daniel Vettereb48eb02012-04-26 23:28:12 +02007688/**
7689 * i915_read_mch_val - return value for IPS use
7690 *
7691 * Calculate and return a value for the IPS driver to use when deciding whether
7692 * we have thermal and power headroom to increase CPU or GPU power budget.
7693 */
7694unsigned long i915_read_mch_val(void)
7695{
7696 struct drm_i915_private *dev_priv;
7697 unsigned long chipset_val, graphics_val, ret = 0;
7698
Daniel Vetter92703882012-08-09 16:46:01 +02007699 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007700 if (!i915_mch_dev)
7701 goto out_unlock;
7702 dev_priv = i915_mch_dev;
7703
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007704 chipset_val = __i915_chipset_val(dev_priv);
7705 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007706
7707 ret = chipset_val + graphics_val;
7708
7709out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007710 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007711
7712 return ret;
7713}
7714EXPORT_SYMBOL_GPL(i915_read_mch_val);
7715
7716/**
7717 * i915_gpu_raise - raise GPU frequency limit
7718 *
7719 * Raise the limit; IPS indicates we have thermal headroom.
7720 */
7721bool i915_gpu_raise(void)
7722{
7723 struct drm_i915_private *dev_priv;
7724 bool ret = true;
7725
Daniel Vetter92703882012-08-09 16:46:01 +02007726 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007727 if (!i915_mch_dev) {
7728 ret = false;
7729 goto out_unlock;
7730 }
7731 dev_priv = i915_mch_dev;
7732
Daniel Vetter20e4d402012-08-08 23:35:39 +02007733 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
7734 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007735
7736out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007737 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007738
7739 return ret;
7740}
7741EXPORT_SYMBOL_GPL(i915_gpu_raise);
7742
7743/**
7744 * i915_gpu_lower - lower GPU frequency limit
7745 *
7746 * IPS indicates we're close to a thermal limit, so throttle back the GPU
7747 * frequency maximum.
7748 */
7749bool i915_gpu_lower(void)
7750{
7751 struct drm_i915_private *dev_priv;
7752 bool ret = true;
7753
Daniel Vetter92703882012-08-09 16:46:01 +02007754 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007755 if (!i915_mch_dev) {
7756 ret = false;
7757 goto out_unlock;
7758 }
7759 dev_priv = i915_mch_dev;
7760
Daniel Vetter20e4d402012-08-08 23:35:39 +02007761 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
7762 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007763
7764out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007765 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007766
7767 return ret;
7768}
7769EXPORT_SYMBOL_GPL(i915_gpu_lower);
7770
7771/**
7772 * i915_gpu_busy - indicate GPU business to IPS
7773 *
7774 * Tell the IPS driver whether or not the GPU is busy.
7775 */
7776bool i915_gpu_busy(void)
7777{
Daniel Vettereb48eb02012-04-26 23:28:12 +02007778 bool ret = false;
7779
Daniel Vetter92703882012-08-09 16:46:01 +02007780 spin_lock_irq(&mchdev_lock);
Chris Wilsondcff85c2016-08-05 10:14:11 +01007781 if (i915_mch_dev)
7782 ret = i915_mch_dev->gt.awake;
Daniel Vetter92703882012-08-09 16:46:01 +02007783 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007784
7785 return ret;
7786}
7787EXPORT_SYMBOL_GPL(i915_gpu_busy);
7788
7789/**
7790 * i915_gpu_turbo_disable - disable graphics turbo
7791 *
7792 * Disable graphics turbo by resetting the max frequency and setting the
7793 * current frequency to the default.
7794 */
7795bool i915_gpu_turbo_disable(void)
7796{
7797 struct drm_i915_private *dev_priv;
7798 bool ret = true;
7799
Daniel Vetter92703882012-08-09 16:46:01 +02007800 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007801 if (!i915_mch_dev) {
7802 ret = false;
7803 goto out_unlock;
7804 }
7805 dev_priv = i915_mch_dev;
7806
Daniel Vetter20e4d402012-08-08 23:35:39 +02007807 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007808
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01007809 if (!ironlake_set_drps(dev_priv, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02007810 ret = false;
7811
7812out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007813 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007814
7815 return ret;
7816}
7817EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
7818
7819/**
7820 * Tells the intel_ips driver that the i915 driver is now loaded, if
7821 * IPS got loaded first.
7822 *
7823 * This awkward dance is so that neither module has to depend on the
7824 * other in order for IPS to do the appropriate communication of
7825 * GPU turbo limits to i915.
7826 */
7827static void
7828ips_ping_for_i915_load(void)
7829{
7830 void (*link)(void);
7831
7832 link = symbol_get(ips_link_to_i915_driver);
7833 if (link) {
7834 link();
7835 symbol_put(ips_link_to_i915_driver);
7836 }
7837}
7838
7839void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
7840{
Daniel Vetter02d71952012-08-09 16:44:54 +02007841 /* We only register the i915 ips part with intel-ips once everything is
7842 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02007843 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007844 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02007845 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007846
7847 ips_ping_for_i915_load();
7848}
7849
7850void intel_gpu_ips_teardown(void)
7851{
Daniel Vetter92703882012-08-09 16:46:01 +02007852 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007853 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02007854 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007855}
Deepak S76c3552f2014-01-30 23:08:16 +05307856
Chris Wilsondc979972016-05-10 14:10:04 +01007857static void intel_init_emon(struct drm_i915_private *dev_priv)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007858{
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007859 u32 lcfuse;
7860 u8 pxw[16];
7861 int i;
7862
7863 /* Disable to program */
7864 I915_WRITE(ECR, 0);
7865 POSTING_READ(ECR);
7866
7867 /* Program energy weights for various events */
7868 I915_WRITE(SDEW, 0x15040d00);
7869 I915_WRITE(CSIEW0, 0x007f0000);
7870 I915_WRITE(CSIEW1, 0x1e220004);
7871 I915_WRITE(CSIEW2, 0x04000004);
7872
7873 for (i = 0; i < 5; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007874 I915_WRITE(PEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007875 for (i = 0; i < 3; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007876 I915_WRITE(DEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007877
7878 /* Program P-state weights to account for frequency power adjustment */
7879 for (i = 0; i < 16; i++) {
Ville Syrjälä616847e2015-09-18 20:03:19 +03007880 u32 pxvidfreq = I915_READ(PXVFREQ(i));
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007881 unsigned long freq = intel_pxfreq(pxvidfreq);
7882 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
7883 PXVFREQ_PX_SHIFT;
7884 unsigned long val;
7885
7886 val = vid * vid;
7887 val *= (freq / 1000);
7888 val *= 255;
7889 val /= (127*127*900);
7890 if (val > 0xff)
7891 DRM_ERROR("bad pxval: %ld\n", val);
7892 pxw[i] = val;
7893 }
7894 /* Render standby states get 0 weight */
7895 pxw[14] = 0;
7896 pxw[15] = 0;
7897
7898 for (i = 0; i < 4; i++) {
7899 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
7900 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
Ville Syrjälä616847e2015-09-18 20:03:19 +03007901 I915_WRITE(PXW(i), val);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007902 }
7903
7904 /* Adjust magic regs to magic values (more experimental results) */
7905 I915_WRITE(OGW0, 0);
7906 I915_WRITE(OGW1, 0);
7907 I915_WRITE(EG0, 0x00007f00);
7908 I915_WRITE(EG1, 0x0000000e);
7909 I915_WRITE(EG2, 0x000e0000);
7910 I915_WRITE(EG3, 0x68000300);
7911 I915_WRITE(EG4, 0x42000000);
7912 I915_WRITE(EG5, 0x00140031);
7913 I915_WRITE(EG6, 0);
7914 I915_WRITE(EG7, 0);
7915
7916 for (i = 0; i < 8; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007917 I915_WRITE(PXWL(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007918
7919 /* Enable PMON + select events */
7920 I915_WRITE(ECR, 0x80000019);
7921
7922 lcfuse = I915_READ(LCFUSE02);
7923
Daniel Vetter20e4d402012-08-08 23:35:39 +02007924 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007925}
7926
Chris Wilsondc979972016-05-10 14:10:04 +01007927void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007928{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007929 struct intel_rps *rps = &dev_priv->gt_pm.rps;
7930
Imre Deakb268c692015-12-15 20:10:31 +02007931 /*
7932 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
7933 * requirement.
7934 */
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00007935 if (!i915_modparams.enable_rc6) {
Imre Deakb268c692015-12-15 20:10:31 +02007936 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
7937 intel_runtime_pm_get(dev_priv);
7938 }
Imre Deake6069ca2014-04-18 16:01:02 +03007939
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01007940 mutex_lock(&dev_priv->pcu_lock);
Chris Wilson773ea9a2016-07-13 09:10:33 +01007941
7942 /* Initialize RPS limits (for userspace) */
Chris Wilsondc979972016-05-10 14:10:04 +01007943 if (IS_CHERRYVIEW(dev_priv))
7944 cherryview_init_gt_powersave(dev_priv);
7945 else if (IS_VALLEYVIEW(dev_priv))
7946 valleyview_init_gt_powersave(dev_priv);
Chris Wilson2a13ae72016-08-02 11:15:27 +01007947 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson773ea9a2016-07-13 09:10:33 +01007948 gen6_init_rps_frequencies(dev_priv);
7949
7950 /* Derive initial user preferences/limits from the hardware limits */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007951 rps->idle_freq = rps->min_freq;
7952 rps->cur_freq = rps->idle_freq;
Chris Wilson773ea9a2016-07-13 09:10:33 +01007953
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007954 rps->max_freq_softlimit = rps->max_freq;
7955 rps->min_freq_softlimit = rps->min_freq;
Chris Wilson773ea9a2016-07-13 09:10:33 +01007956
7957 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007958 rps->min_freq_softlimit =
Chris Wilson773ea9a2016-07-13 09:10:33 +01007959 max_t(int,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007960 rps->efficient_freq,
Chris Wilson773ea9a2016-07-13 09:10:33 +01007961 intel_freq_opcode(dev_priv, 450));
7962
Chris Wilson99ac9612016-07-13 09:10:34 +01007963 /* After setting max-softlimit, find the overclock max freq */
7964 if (IS_GEN6(dev_priv) ||
7965 IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
7966 u32 params = 0;
7967
7968 sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &params);
7969 if (params & BIT(31)) { /* OC supported */
7970 DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007971 (rps->max_freq & 0xff) * 50,
Chris Wilson99ac9612016-07-13 09:10:34 +01007972 (params & 0xff) * 50);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007973 rps->max_freq = params & 0xff;
Chris Wilson99ac9612016-07-13 09:10:34 +01007974 }
7975 }
7976
Chris Wilson29ecd78d2016-07-13 09:10:35 +01007977 /* Finally allow us to boost to max by default */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007978 rps->boost_freq = rps->max_freq;
Chris Wilson29ecd78d2016-07-13 09:10:35 +01007979
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01007980 mutex_unlock(&dev_priv->pcu_lock);
Imre Deakae484342014-03-31 15:10:44 +03007981}
7982
Chris Wilsondc979972016-05-10 14:10:04 +01007983void intel_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007984{
Ville Syrjälä8dac1e12016-08-02 14:07:33 +03007985 if (IS_VALLEYVIEW(dev_priv))
Chris Wilsondc979972016-05-10 14:10:04 +01007986 valleyview_cleanup_gt_powersave(dev_priv);
Imre Deakb268c692015-12-15 20:10:31 +02007987
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00007988 if (!i915_modparams.enable_rc6)
Imre Deakb268c692015-12-15 20:10:31 +02007989 intel_runtime_pm_put(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03007990}
7991
Chris Wilson54b4f682016-07-21 21:16:19 +01007992/**
7993 * intel_suspend_gt_powersave - suspend PM work and helper threads
7994 * @dev_priv: i915 device
7995 *
7996 * We don't want to disable RC6 or other features here, we just want
7997 * to make sure any work we've queued has finished and won't bother
7998 * us while we're suspended.
7999 */
8000void intel_suspend_gt_powersave(struct drm_i915_private *dev_priv)
8001{
8002 if (INTEL_GEN(dev_priv) < 6)
8003 return;
8004
Chris Wilson54b4f682016-07-21 21:16:19 +01008005 /* gen6_rps_idle() will be called later to disable interrupts */
8006}
8007
Chris Wilsonb7137e02016-07-13 09:10:37 +01008008void intel_sanitize_gt_powersave(struct drm_i915_private *dev_priv)
8009{
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008010 dev_priv->gt_pm.rps.enabled = true; /* force RPS disabling */
8011 dev_priv->gt_pm.rc6.enabled = true; /* force RC6 disabling */
Chris Wilsonb7137e02016-07-13 09:10:37 +01008012 intel_disable_gt_powersave(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01008013
8014 gen6_reset_rps_interrupts(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07008015}
8016
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008017static inline void intel_disable_llc_pstate(struct drm_i915_private *i915)
8018{
8019 lockdep_assert_held(&i915->pcu_lock);
8020
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008021 if (!i915->gt_pm.llc_pstate.enabled)
8022 return;
8023
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008024 /* Currently there is no HW configuration to be done to disable. */
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008025
8026 i915->gt_pm.llc_pstate.enabled = false;
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008027}
8028
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008029static void intel_disable_rc6(struct drm_i915_private *dev_priv)
8030{
8031 lockdep_assert_held(&dev_priv->pcu_lock);
8032
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008033 if (!dev_priv->gt_pm.rc6.enabled)
8034 return;
8035
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008036 if (INTEL_GEN(dev_priv) >= 9)
8037 gen9_disable_rc6(dev_priv);
8038 else if (IS_CHERRYVIEW(dev_priv))
8039 cherryview_disable_rc6(dev_priv);
8040 else if (IS_VALLEYVIEW(dev_priv))
8041 valleyview_disable_rc6(dev_priv);
8042 else if (INTEL_GEN(dev_priv) >= 6)
8043 gen6_disable_rc6(dev_priv);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008044
8045 dev_priv->gt_pm.rc6.enabled = false;
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008046}
8047
8048static void intel_disable_rps(struct drm_i915_private *dev_priv)
8049{
8050 lockdep_assert_held(&dev_priv->pcu_lock);
8051
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008052 if (!dev_priv->gt_pm.rps.enabled)
8053 return;
8054
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008055 if (INTEL_GEN(dev_priv) >= 9)
8056 gen9_disable_rps(dev_priv);
8057 else if (IS_CHERRYVIEW(dev_priv))
8058 cherryview_disable_rps(dev_priv);
8059 else if (IS_VALLEYVIEW(dev_priv))
8060 valleyview_disable_rps(dev_priv);
8061 else if (INTEL_GEN(dev_priv) >= 6)
8062 gen6_disable_rps(dev_priv);
8063 else if (IS_IRONLAKE_M(dev_priv))
8064 ironlake_disable_drps(dev_priv);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008065
8066 dev_priv->gt_pm.rps.enabled = false;
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008067}
8068
Chris Wilsondc979972016-05-10 14:10:04 +01008069void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
Daniel Vetter8090c6b2012-06-24 16:42:32 +02008070{
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01008071 mutex_lock(&dev_priv->pcu_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07008072
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008073 intel_disable_rc6(dev_priv);
8074 intel_disable_rps(dev_priv);
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008075 if (HAS_LLC(dev_priv))
8076 intel_disable_llc_pstate(dev_priv);
8077
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01008078 mutex_unlock(&dev_priv->pcu_lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01008079}
8080
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008081static inline void intel_enable_llc_pstate(struct drm_i915_private *i915)
8082{
8083 lockdep_assert_held(&i915->pcu_lock);
8084
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008085 if (i915->gt_pm.llc_pstate.enabled)
8086 return;
8087
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008088 gen6_update_ring_freq(i915);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008089
8090 i915->gt_pm.llc_pstate.enabled = true;
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008091}
8092
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008093static void intel_enable_rc6(struct drm_i915_private *dev_priv)
8094{
8095 lockdep_assert_held(&dev_priv->pcu_lock);
8096
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008097 if (dev_priv->gt_pm.rc6.enabled)
8098 return;
8099
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008100 if (IS_CHERRYVIEW(dev_priv))
8101 cherryview_enable_rc6(dev_priv);
8102 else if (IS_VALLEYVIEW(dev_priv))
8103 valleyview_enable_rc6(dev_priv);
8104 else if (INTEL_GEN(dev_priv) >= 9)
8105 gen9_enable_rc6(dev_priv);
8106 else if (IS_BROADWELL(dev_priv))
8107 gen8_enable_rc6(dev_priv);
8108 else if (INTEL_GEN(dev_priv) >= 6)
8109 gen6_enable_rc6(dev_priv);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008110
8111 dev_priv->gt_pm.rc6.enabled = true;
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008112}
8113
8114static void intel_enable_rps(struct drm_i915_private *dev_priv)
8115{
8116 struct intel_rps *rps = &dev_priv->gt_pm.rps;
8117
8118 lockdep_assert_held(&dev_priv->pcu_lock);
8119
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008120 if (rps->enabled)
8121 return;
8122
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008123 if (IS_CHERRYVIEW(dev_priv)) {
8124 cherryview_enable_rps(dev_priv);
8125 } else if (IS_VALLEYVIEW(dev_priv)) {
8126 valleyview_enable_rps(dev_priv);
8127 } else if (INTEL_GEN(dev_priv) >= 9) {
8128 gen9_enable_rps(dev_priv);
8129 } else if (IS_BROADWELL(dev_priv)) {
8130 gen8_enable_rps(dev_priv);
8131 } else if (INTEL_GEN(dev_priv) >= 6) {
8132 gen6_enable_rps(dev_priv);
8133 } else if (IS_IRONLAKE_M(dev_priv)) {
8134 ironlake_enable_drps(dev_priv);
8135 intel_init_emon(dev_priv);
8136 }
8137
8138 WARN_ON(rps->max_freq < rps->min_freq);
8139 WARN_ON(rps->idle_freq > rps->max_freq);
8140
8141 WARN_ON(rps->efficient_freq < rps->min_freq);
8142 WARN_ON(rps->efficient_freq > rps->max_freq);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008143
8144 rps->enabled = true;
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008145}
8146
Chris Wilsonb7137e02016-07-13 09:10:37 +01008147void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
8148{
Chris Wilsonb7137e02016-07-13 09:10:37 +01008149 /* Powersaving is controlled by the host when inside a VM */
8150 if (intel_vgpu_active(dev_priv))
8151 return;
8152
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01008153 mutex_lock(&dev_priv->pcu_lock);
Imre Deak3cc134e2014-11-19 15:30:03 +02008154
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008155 intel_enable_rc6(dev_priv);
8156 intel_enable_rps(dev_priv);
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008157 if (HAS_LLC(dev_priv))
8158 intel_enable_llc_pstate(dev_priv);
8159
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01008160 mutex_unlock(&dev_priv->pcu_lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01008161}
Imre Deakc6df39b2014-04-14 20:24:29 +03008162
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008163static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01008164{
Daniel Vetter3107bd42012-10-31 22:52:31 +01008165 /*
8166 * On Ibex Peak and Cougar Point, we need to disable clock
8167 * gating for the panel power sequencer or it will fail to
8168 * start up when no ports are active.
8169 */
8170 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
8171}
8172
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008173static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008174{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03008175 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008176
Damien Lespiau055e3932014-08-18 13:49:10 +01008177 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008178 I915_WRITE(DSPCNTR(pipe),
8179 I915_READ(DSPCNTR(pipe)) |
8180 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03008181
8182 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
8183 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008184 }
8185}
8186
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008187static void ilk_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008188{
Damien Lespiau231e54f2012-10-19 17:55:41 +01008189 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008190
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01008191 /*
8192 * Required for FBC
8193 * WaFbcDisableDpfcClockGating:ilk
8194 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008195 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
8196 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
8197 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008198
8199 I915_WRITE(PCH_3DCGDIS0,
8200 MARIUNIT_CLOCK_GATE_DISABLE |
8201 SVSMUNIT_CLOCK_GATE_DISABLE);
8202 I915_WRITE(PCH_3DCGDIS1,
8203 VFMUNIT_CLOCK_GATE_DISABLE);
8204
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008205 /*
8206 * According to the spec the following bits should be set in
8207 * order to enable memory self-refresh
8208 * The bit 22/21 of 0x42004
8209 * The bit 5 of 0x42020
8210 * The bit 15 of 0x45000
8211 */
8212 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8213 (I915_READ(ILK_DISPLAY_CHICKEN2) |
8214 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008215 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008216 I915_WRITE(DISP_ARB_CTL,
8217 (I915_READ(DISP_ARB_CTL) |
8218 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02008219
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008220 /*
8221 * Based on the document from hardware guys the following bits
8222 * should be set unconditionally in order to enable FBC.
8223 * The bit 22 of 0x42000
8224 * The bit 22 of 0x42004
8225 * The bit 7,8,9 of 0x42020.
8226 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008227 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01008228 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008229 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8230 I915_READ(ILK_DISPLAY_CHICKEN1) |
8231 ILK_FBCQ_DIS);
8232 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8233 I915_READ(ILK_DISPLAY_CHICKEN2) |
8234 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008235 }
8236
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008237 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
8238
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008239 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8240 I915_READ(ILK_DISPLAY_CHICKEN2) |
8241 ILK_ELPIN_409_SELECT);
8242 I915_WRITE(_3D_CHICKEN2,
8243 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
8244 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02008245
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008246 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02008247 I915_WRITE(CACHE_MODE_0,
8248 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01008249
Akash Goel4e046322014-04-04 17:14:38 +05308250 /* WaDisable_RenderCache_OperationalFlush:ilk */
8251 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8252
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008253 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03008254
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008255 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008256}
8257
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008258static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01008259{
Daniel Vetter3107bd42012-10-31 22:52:31 +01008260 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008261 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01008262
8263 /*
8264 * On Ibex Peak and Cougar Point, we need to disable clock
8265 * gating for the panel power sequencer or it will fail to
8266 * start up when no ports are active.
8267 */
Jesse Barnescd664072013-10-02 10:34:19 -07008268 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
8269 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
8270 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008271 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
8272 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01008273 /* The below fixes the weird display corruption, a few pixels shifted
8274 * downward, on (only) LVDS of some HP laptops with IVY.
8275 */
Damien Lespiau055e3932014-08-18 13:49:10 +01008276 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03008277 val = I915_READ(TRANS_CHICKEN2(pipe));
8278 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
8279 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008280 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008281 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03008282 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
8283 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
8284 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008285 I915_WRITE(TRANS_CHICKEN2(pipe), val);
8286 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01008287 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01008288 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01008289 I915_WRITE(TRANS_CHICKEN1(pipe),
8290 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
8291 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008292}
8293
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008294static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008295{
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008296 uint32_t tmp;
8297
8298 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02008299 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
8300 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
8301 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008302}
8303
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008304static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008305{
Damien Lespiau231e54f2012-10-19 17:55:41 +01008306 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008307
Damien Lespiau231e54f2012-10-19 17:55:41 +01008308 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008309
8310 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8311 I915_READ(ILK_DISPLAY_CHICKEN2) |
8312 ILK_ELPIN_409_SELECT);
8313
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008314 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01008315 I915_WRITE(_3D_CHICKEN,
8316 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
8317
Akash Goel4e046322014-04-04 17:14:38 +05308318 /* WaDisable_RenderCache_OperationalFlush:snb */
8319 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8320
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008321 /*
8322 * BSpec recoomends 8x4 when MSAA is used,
8323 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008324 *
8325 * Note that PS/WM thread counts depend on the WIZ hashing
8326 * disable bit, which we don't touch here, but it's good
8327 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008328 */
8329 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008330 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008331
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008332 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02008333 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008334
8335 I915_WRITE(GEN6_UCGCTL1,
8336 I915_READ(GEN6_UCGCTL1) |
8337 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
8338 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
8339
8340 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
8341 * gating disable must be set. Failure to set it results in
8342 * flickering pixels due to Z write ordering failures after
8343 * some amount of runtime in the Mesa "fire" demo, and Unigine
8344 * Sanctuary and Tropics, and apparently anything else with
8345 * alpha test or pixel discard.
8346 *
8347 * According to the spec, bit 11 (RCCUNIT) must also be set,
8348 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008349 *
Ville Syrjäläef593182014-01-22 21:32:47 +02008350 * WaDisableRCCUnitClockGating:snb
8351 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008352 */
8353 I915_WRITE(GEN6_UCGCTL2,
8354 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
8355 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
8356
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02008357 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02008358 I915_WRITE(_3D_CHICKEN3,
8359 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008360
8361 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02008362 * Bspec says:
8363 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
8364 * 3DSTATE_SF number of SF output attributes is more than 16."
8365 */
8366 I915_WRITE(_3D_CHICKEN3,
8367 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
8368
8369 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008370 * According to the spec the following bits should be
8371 * set in order to enable memory self-refresh and fbc:
8372 * The bit21 and bit22 of 0x42000
8373 * The bit21 and bit22 of 0x42004
8374 * The bit5 and bit7 of 0x42020
8375 * The bit14 of 0x70180
8376 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01008377 *
8378 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008379 */
8380 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8381 I915_READ(ILK_DISPLAY_CHICKEN1) |
8382 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
8383 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8384 I915_READ(ILK_DISPLAY_CHICKEN2) |
8385 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01008386 I915_WRITE(ILK_DSPCLK_GATE_D,
8387 I915_READ(ILK_DSPCLK_GATE_D) |
8388 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
8389 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008390
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008391 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07008392
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008393 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008394
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008395 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008396}
8397
8398static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
8399{
8400 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
8401
Ville Syrjälä3aad9052014-01-22 21:32:59 +02008402 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02008403 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02008404 *
8405 * This actually overrides the dispatch
8406 * mode for all thread types.
8407 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008408 reg &= ~GEN7_FF_SCHED_MASK;
8409 reg |= GEN7_FF_TS_SCHED_HW;
8410 reg |= GEN7_FF_VS_SCHED_HW;
8411 reg |= GEN7_FF_DS_SCHED_HW;
8412
8413 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
8414}
8415
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008416static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008417{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008418 /*
8419 * TODO: this bit should only be enabled when really needed, then
8420 * disabled when not needed anymore in order to save power.
8421 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008422 if (HAS_PCH_LPT_LP(dev_priv))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008423 I915_WRITE(SOUTH_DSPCLK_GATE_D,
8424 I915_READ(SOUTH_DSPCLK_GATE_D) |
8425 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03008426
8427 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03008428 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
8429 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03008430 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008431}
8432
Ville Syrjälä712bf362016-10-31 22:37:23 +02008433static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03008434{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008435 if (HAS_PCH_LPT_LP(dev_priv)) {
Imre Deak7d708ee2013-04-17 14:04:50 +03008436 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
8437
8438 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8439 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8440 }
8441}
8442
Imre Deak450174f2016-05-03 15:54:21 +03008443static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
8444 int general_prio_credits,
8445 int high_prio_credits)
8446{
8447 u32 misccpctl;
Oscar Mateo930a7842017-10-17 13:25:45 -07008448 u32 val;
Imre Deak450174f2016-05-03 15:54:21 +03008449
8450 /* WaTempDisableDOPClkGating:bdw */
8451 misccpctl = I915_READ(GEN7_MISCCPCTL);
8452 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
8453
Oscar Mateo930a7842017-10-17 13:25:45 -07008454 val = I915_READ(GEN8_L3SQCREG1);
8455 val &= ~L3_PRIO_CREDITS_MASK;
8456 val |= L3_GENERAL_PRIO_CREDITS(general_prio_credits);
8457 val |= L3_HIGH_PRIO_CREDITS(high_prio_credits);
8458 I915_WRITE(GEN8_L3SQCREG1, val);
Imre Deak450174f2016-05-03 15:54:21 +03008459
8460 /*
8461 * Wait at least 100 clocks before re-enabling clock gating.
8462 * See the definition of L3SQCREG1 in BSpec.
8463 */
8464 POSTING_READ(GEN8_L3SQCREG1);
8465 udelay(1);
8466 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
8467}
8468
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008469static void cnp_init_clock_gating(struct drm_i915_private *dev_priv)
8470{
8471 if (!HAS_PCH_CNP(dev_priv))
8472 return;
8473
8474 /* Wa #1181 */
Rodrigo Vivi4cc6feb2017-09-08 16:45:33 -07008475 I915_WRITE(SOUTH_DSPCLK_GATE_D, I915_READ(SOUTH_DSPCLK_GATE_D) |
8476 CNP_PWM_CGE_GATING_DISABLE);
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008477}
8478
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008479static void cnl_init_clock_gating(struct drm_i915_private *dev_priv)
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008480{
Rodrigo Vivi8f067832017-09-05 12:30:13 -07008481 u32 val;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008482 cnp_init_clock_gating(dev_priv);
8483
Rodrigo Vivi1a25db62017-08-15 16:16:51 -07008484 /* This is not an Wa. Enable for better image quality */
8485 I915_WRITE(_3D_CHICKEN3,
8486 _MASKED_BIT_ENABLE(_3D_CHICKEN3_AA_LINE_QUALITY_FIX_ENABLE));
8487
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008488 /* WaEnableChickenDCPR:cnl */
8489 I915_WRITE(GEN8_CHICKEN_DCPR_1,
8490 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
8491
8492 /* WaFbcWakeMemOn:cnl */
8493 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
8494 DISP_FBC_MEMORY_WAKE);
8495
Chris Wilson34991bd2017-11-11 10:03:36 +00008496 val = I915_READ(SLICE_UNIT_LEVEL_CLKGATE);
8497 /* ReadHitWriteOnlyDisable:cnl */
8498 val |= RCCUNIT_CLKGATE_DIS;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008499 /* WaSarbUnitClockGatingDisable:cnl (pre-prod) */
8500 if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_B0))
Chris Wilson34991bd2017-11-11 10:03:36 +00008501 val |= SARBUNIT_CLKGATE_DIS;
8502 I915_WRITE(SLICE_UNIT_LEVEL_CLKGATE, val);
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008503}
8504
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008505static void cfl_init_clock_gating(struct drm_i915_private *dev_priv)
8506{
8507 cnp_init_clock_gating(dev_priv);
8508 gen9_init_clock_gating(dev_priv);
8509
8510 /* WaFbcNukeOnHostModify:cfl */
8511 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8512 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
8513}
8514
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008515static void kbl_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008516{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008517 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008518
8519 /* WaDisableSDEUnitClockGating:kbl */
8520 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
8521 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8522 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03008523
8524 /* WaDisableGamClockGating:kbl */
8525 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
8526 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
8527 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008528
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008529 /* WaFbcNukeOnHostModify:kbl */
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008530 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8531 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008532}
8533
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008534static void skl_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008535{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008536 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03008537
8538 /* WAC6entrylatency:skl */
8539 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
8540 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008541
8542 /* WaFbcNukeOnHostModify:skl */
8543 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8544 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008545}
8546
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008547static void bdw_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008548{
Matthew Auld8cb09832017-10-06 23:18:23 +01008549 /* The GTT cache must be disabled if the system is using 2M pages. */
8550 bool can_use_gtt_cache = !HAS_PAGE_SIZES(dev_priv,
8551 I915_GTT_PAGE_SIZE_2M);
Damien Lespiau07d27e22014-03-03 17:31:46 +00008552 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008553
Ben Widawskyab57fff2013-12-12 15:28:04 -08008554 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07008555 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008556
Ben Widawskyab57fff2013-12-12 15:28:04 -08008557 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008558 I915_WRITE(CHICKEN_PAR1_1,
8559 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
8560
Ben Widawskyab57fff2013-12-12 15:28:04 -08008561 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01008562 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00008563 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02008564 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02008565 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008566 }
Ben Widawsky63801f22013-12-12 17:26:03 -08008567
Ben Widawskyab57fff2013-12-12 15:28:04 -08008568 /* WaVSRefCountFullforceMissDisable:bdw */
8569 /* WaDSRefCountFullforceMissDisable:bdw */
8570 I915_WRITE(GEN7_FF_THREAD_MODE,
8571 I915_READ(GEN7_FF_THREAD_MODE) &
8572 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02008573
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02008574 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
8575 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02008576
8577 /* WaDisableSDEUnitClockGating:bdw */
8578 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8579 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00008580
Imre Deak450174f2016-05-03 15:54:21 +03008581 /* WaProgramL3SqcReg1Default:bdw */
8582 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03008583
Matthew Auld8cb09832017-10-06 23:18:23 +01008584 /* WaGttCachingOffByDefault:bdw */
8585 I915_WRITE(HSW_GTT_CACHE_EN, can_use_gtt_cache ? GTT_CACHE_EN_ALL : 0);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008586
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03008587 /* WaKVMNotificationOnConfigChange:bdw */
8588 I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
8589 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
8590
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008591 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00008592
8593 /* WaDisableDopClockGating:bdw
8594 *
8595 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
8596 * clock gating.
8597 */
8598 I915_WRITE(GEN6_UCGCTL1,
8599 I915_READ(GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008600}
8601
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008602static void hsw_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008603{
Francisco Jerezf3fc4882013-10-02 15:53:16 -07008604 /* L3 caching of data atomics doesn't work -- disable it. */
8605 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
8606 I915_WRITE(HSW_ROW_CHICKEN3,
8607 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
8608
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008609 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008610 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8611 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8612 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8613
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02008614 /* WaVSRefCountFullforceMissDisable:hsw */
8615 I915_WRITE(GEN7_FF_THREAD_MODE,
8616 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008617
Akash Goel4e046322014-04-04 17:14:38 +05308618 /* WaDisable_RenderCache_OperationalFlush:hsw */
8619 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8620
Chia-I Wufe27c602014-01-28 13:29:33 +08008621 /* enable HiZ Raw Stall Optimization */
8622 I915_WRITE(CACHE_MODE_0_GEN7,
8623 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
8624
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008625 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008626 I915_WRITE(CACHE_MODE_1,
8627 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03008628
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008629 /*
8630 * BSpec recommends 8x4 when MSAA is used,
8631 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008632 *
8633 * Note that PS/WM thread counts depend on the WIZ hashing
8634 * disable bit, which we don't touch here, but it's good
8635 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008636 */
8637 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008638 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008639
Kenneth Graunke94411592014-12-31 16:23:00 -08008640 /* WaSampleCChickenBitEnable:hsw */
8641 I915_WRITE(HALF_SLICE_CHICKEN3,
8642 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
8643
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008644 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07008645 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
8646
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008647 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008648}
8649
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008650static void ivb_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008651{
Ben Widawsky20848222012-05-04 18:58:59 -07008652 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008653
Damien Lespiau231e54f2012-10-19 17:55:41 +01008654 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008655
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008656 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05008657 I915_WRITE(_3D_CHICKEN3,
8658 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
8659
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008660 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008661 I915_WRITE(IVB_CHICKEN3,
8662 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8663 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8664
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008665 /* WaDisablePSDDualDispatchEnable:ivb */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008666 if (IS_IVB_GT1(dev_priv))
Jesse Barnes12f33822012-10-25 12:15:45 -07008667 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
8668 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07008669
Akash Goel4e046322014-04-04 17:14:38 +05308670 /* WaDisable_RenderCache_OperationalFlush:ivb */
8671 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8672
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008673 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008674 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
8675 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
8676
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008677 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008678 I915_WRITE(GEN7_L3CNTLREG1,
8679 GEN7_WA_FOR_GEN7_L3_CONTROL);
8680 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07008681 GEN7_WA_L3_CHICKEN_MODE);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008682 if (IS_IVB_GT1(dev_priv))
Jesse Barnes8ab43972012-10-25 12:15:42 -07008683 I915_WRITE(GEN7_ROW_CHICKEN2,
8684 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02008685 else {
8686 /* must write both registers */
8687 I915_WRITE(GEN7_ROW_CHICKEN2,
8688 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07008689 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
8690 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02008691 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008692
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008693 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05008694 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
8695 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
8696
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02008697 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07008698 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008699 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008700 */
8701 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02008702 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07008703
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008704 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008705 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8706 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8707 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8708
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008709 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008710
8711 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02008712
Chris Wilson22721342014-03-04 09:41:43 +00008713 if (0) { /* causes HiZ corruption on ivb:gt1 */
8714 /* enable HiZ Raw Stall Optimization */
8715 I915_WRITE(CACHE_MODE_0_GEN7,
8716 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
8717 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08008718
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008719 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02008720 I915_WRITE(CACHE_MODE_1,
8721 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07008722
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008723 /*
8724 * BSpec recommends 8x4 when MSAA is used,
8725 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008726 *
8727 * Note that PS/WM thread counts depend on the WIZ hashing
8728 * disable bit, which we don't touch here, but it's good
8729 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008730 */
8731 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008732 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008733
Ben Widawsky20848222012-05-04 18:58:59 -07008734 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
8735 snpcr &= ~GEN6_MBC_SNPCR_MASK;
8736 snpcr |= GEN6_MBC_SNPCR_MED;
8737 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008738
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008739 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008740 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008741
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008742 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008743}
8744
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008745static void vlv_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008746{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008747 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05008748 I915_WRITE(_3D_CHICKEN3,
8749 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
8750
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008751 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008752 I915_WRITE(IVB_CHICKEN3,
8753 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8754 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8755
Ville Syrjäläfad7d362014-01-22 21:32:39 +02008756 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008757 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07008758 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08008759 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
8760 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07008761
Akash Goel4e046322014-04-04 17:14:38 +05308762 /* WaDisable_RenderCache_OperationalFlush:vlv */
8763 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8764
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008765 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05008766 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
8767 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
8768
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008769 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07008770 I915_WRITE(GEN7_ROW_CHICKEN2,
8771 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
8772
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008773 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008774 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8775 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8776 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8777
Ville Syrjälä46680e02014-01-22 21:33:01 +02008778 gen7_setup_fixed_func_scheduler(dev_priv);
8779
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02008780 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07008781 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008782 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008783 */
8784 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02008785 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07008786
Akash Goelc98f5062014-03-24 23:00:07 +05308787 /* WaDisableL3Bank2xClockGate:vlv
8788 * Disabling L3 clock gating- MMIO 940c[25] = 1
8789 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
8790 I915_WRITE(GEN7_UCGCTL4,
8791 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07008792
Ville Syrjäläafd58e72014-01-22 21:33:03 +02008793 /*
8794 * BSpec says this must be set, even though
8795 * WaDisable4x2SubspanOptimization isn't listed for VLV.
8796 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02008797 I915_WRITE(CACHE_MODE_1,
8798 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07008799
8800 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02008801 * BSpec recommends 8x4 when MSAA is used,
8802 * however in practice 16x4 seems fastest.
8803 *
8804 * Note that PS/WM thread counts depend on the WIZ hashing
8805 * disable bit, which we don't touch here, but it's good
8806 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
8807 */
8808 I915_WRITE(GEN7_GT_MODE,
8809 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
8810
8811 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02008812 * WaIncreaseL3CreditsForVLVB0:vlv
8813 * This is the hardware default actually.
8814 */
8815 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
8816
8817 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008818 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07008819 * Disable clock gating on th GCFG unit to prevent a delay
8820 * in the reporting of vblank events.
8821 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02008822 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008823}
8824
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008825static void chv_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03008826{
Ville Syrjälä232ce332014-04-09 13:28:35 +03008827 /* WaVSRefCountFullforceMissDisable:chv */
8828 /* WaDSRefCountFullforceMissDisable:chv */
8829 I915_WRITE(GEN7_FF_THREAD_MODE,
8830 I915_READ(GEN7_FF_THREAD_MODE) &
8831 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03008832
8833 /* WaDisableSemaphoreAndSyncFlipWait:chv */
8834 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
8835 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03008836
8837 /* WaDisableCSUnitClockGating:chv */
8838 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
8839 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03008840
8841 /* WaDisableSDEUnitClockGating:chv */
8842 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8843 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008844
8845 /*
Imre Deak450174f2016-05-03 15:54:21 +03008846 * WaProgramL3SqcReg1Default:chv
8847 * See gfxspecs/Related Documents/Performance Guide/
8848 * LSQC Setting Recommendations.
8849 */
8850 gen8_set_l3sqc_credits(dev_priv, 38, 2);
8851
8852 /*
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008853 * GTT cache may not work with big pages, so if those
8854 * are ever enabled GTT cache may need to be disabled.
8855 */
8856 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03008857}
8858
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008859static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008860{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008861 uint32_t dspclk_gate;
8862
8863 I915_WRITE(RENCLK_GATE_D1, 0);
8864 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
8865 GS_UNIT_CLOCK_GATE_DISABLE |
8866 CL_UNIT_CLOCK_GATE_DISABLE);
8867 I915_WRITE(RAMCLK_GATE_D, 0);
8868 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
8869 OVRUNIT_CLOCK_GATE_DISABLE |
8870 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008871 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008872 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
8873 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02008874
8875 /* WaDisableRenderCachePipelinedFlush */
8876 I915_WRITE(CACHE_MODE_0,
8877 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03008878
Akash Goel4e046322014-04-04 17:14:38 +05308879 /* WaDisable_RenderCache_OperationalFlush:g4x */
8880 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8881
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008882 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008883}
8884
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008885static void i965gm_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008886{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008887 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
8888 I915_WRITE(RENCLK_GATE_D2, 0);
8889 I915_WRITE(DSPCLK_GATE_D, 0);
8890 I915_WRITE(RAMCLK_GATE_D, 0);
8891 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03008892 I915_WRITE(MI_ARB_STATE,
8893 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05308894
8895 /* WaDisable_RenderCache_OperationalFlush:gen4 */
8896 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008897}
8898
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008899static void i965g_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008900{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008901 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
8902 I965_RCC_CLOCK_GATE_DISABLE |
8903 I965_RCPB_CLOCK_GATE_DISABLE |
8904 I965_ISC_CLOCK_GATE_DISABLE |
8905 I965_FBC_CLOCK_GATE_DISABLE);
8906 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03008907 I915_WRITE(MI_ARB_STATE,
8908 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05308909
8910 /* WaDisable_RenderCache_OperationalFlush:gen4 */
8911 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008912}
8913
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008914static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008915{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008916 u32 dstate = I915_READ(D_STATE);
8917
8918 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
8919 DSTATE_DOT_CLOCK_GATING;
8920 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01008921
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008922 if (IS_PINEVIEW(dev_priv))
Chris Wilson13a86b82012-04-24 14:51:43 +01008923 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02008924
8925 /* IIR "flip pending" means done if this bit is set */
8926 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02008927
8928 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02008929 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02008930
8931 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
8932 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03008933
8934 I915_WRITE(MI_ARB_STATE,
8935 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008936}
8937
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008938static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008939{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008940 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02008941
8942 /* interrupts should cause a wake up from C3 */
8943 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
8944 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03008945
8946 I915_WRITE(MEM_MODE,
8947 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008948}
8949
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008950static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008951{
Ville Syrjälä10383922014-08-15 01:21:54 +03008952 I915_WRITE(MEM_MODE,
8953 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
8954 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008955}
8956
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008957void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008958{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008959 dev_priv->display.init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008960}
8961
Ville Syrjälä712bf362016-10-31 22:37:23 +02008962void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03008963{
Ville Syrjälä712bf362016-10-31 22:37:23 +02008964 if (HAS_PCH_LPT(dev_priv))
8965 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03008966}
8967
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008968static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02008969{
8970 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
8971}
8972
8973/**
8974 * intel_init_clock_gating_hooks - setup the clock gating hooks
8975 * @dev_priv: device private
8976 *
8977 * Setup the hooks that configure which clocks of a given platform can be
8978 * gated and also apply various GT and display specific workarounds for these
8979 * platforms. Note that some GT specific workarounds are applied separately
8980 * when GPU contexts or batchbuffers start their execution.
8981 */
8982void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
8983{
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008984 if (IS_CANNONLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008985 dev_priv->display.init_clock_gating = cnl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008986 else if (IS_COFFEELAKE(dev_priv))
8987 dev_priv->display.init_clock_gating = cfl_init_clock_gating;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008988 else if (IS_SKYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008989 dev_priv->display.init_clock_gating = skl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008990 else if (IS_KABYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008991 dev_priv->display.init_clock_gating = kbl_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02008992 else if (IS_BROXTON(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02008993 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02008994 else if (IS_GEMINILAKE(dev_priv))
8995 dev_priv->display.init_clock_gating = glk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008996 else if (IS_BROADWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008997 dev_priv->display.init_clock_gating = bdw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008998 else if (IS_CHERRYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008999 dev_priv->display.init_clock_gating = chv_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02009000 else if (IS_HASWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009001 dev_priv->display.init_clock_gating = hsw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02009002 else if (IS_IVYBRIDGE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009003 dev_priv->display.init_clock_gating = ivb_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02009004 else if (IS_VALLEYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009005 dev_priv->display.init_clock_gating = vlv_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02009006 else if (IS_GEN6(dev_priv))
9007 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
9008 else if (IS_GEN5(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009009 dev_priv->display.init_clock_gating = ilk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02009010 else if (IS_G4X(dev_priv))
9011 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02009012 else if (IS_I965GM(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009013 dev_priv->display.init_clock_gating = i965gm_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02009014 else if (IS_I965G(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009015 dev_priv->display.init_clock_gating = i965g_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02009016 else if (IS_GEN3(dev_priv))
9017 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
9018 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
9019 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
9020 else if (IS_GEN2(dev_priv))
9021 dev_priv->display.init_clock_gating = i830_init_clock_gating;
9022 else {
9023 MISSING_CASE(INTEL_DEVID(dev_priv));
9024 dev_priv->display.init_clock_gating = nop_init_clock_gating;
9025 }
9026}
9027
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009028/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02009029void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009030{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02009031 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009032
Daniel Vetterc921aba2012-04-26 23:28:17 +02009033 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02009034 if (IS_PINEVIEW(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02009035 i915_pineview_get_mem_freq(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01009036 else if (IS_GEN5(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02009037 i915_ironlake_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02009038
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009039 /* For FIFO watermark updates */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02009040 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02009041 skl_setup_wm_latency(dev_priv);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01009042 dev_priv->display.initial_watermarks = skl_initial_wm;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01009043 dev_priv->display.atomic_update_watermarks = skl_atomic_update_crtc_wm;
Matt Roper98d39492016-05-12 07:06:03 -07009044 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01009045 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02009046 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03009047
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01009048 if ((IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02009049 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01009050 (!IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02009051 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07009052 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08009053 dev_priv->display.compute_intermediate_wm =
9054 ilk_compute_intermediate_wm;
9055 dev_priv->display.initial_watermarks =
9056 ilk_initial_watermarks;
9057 dev_priv->display.optimize_watermarks =
9058 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02009059 } else {
9060 DRM_DEBUG_KMS("Failed to read display plane latency. "
9061 "Disable CxSR\n");
9062 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02009063 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02009064 vlv_setup_wm_latency(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02009065 dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
Ville Syrjälä4841da52017-03-02 19:14:59 +02009066 dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02009067 dev_priv->display.initial_watermarks = vlv_initial_watermarks;
Ville Syrjälä4841da52017-03-02 19:14:59 +02009068 dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
Ville Syrjäläff32c542017-03-02 19:14:57 +02009069 dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03009070 } else if (IS_G4X(dev_priv)) {
9071 g4x_setup_wm_latency(dev_priv);
9072 dev_priv->display.compute_pipe_wm = g4x_compute_pipe_wm;
9073 dev_priv->display.compute_intermediate_wm = g4x_compute_intermediate_wm;
9074 dev_priv->display.initial_watermarks = g4x_initial_watermarks;
9075 dev_priv->display.optimize_watermarks = g4x_optimize_watermarks;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02009076 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01009077 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009078 dev_priv->is_ddr3,
9079 dev_priv->fsb_freq,
9080 dev_priv->mem_freq)) {
9081 DRM_INFO("failed to find known CxSR latency "
9082 "(found ddr%s fsb freq %d, mem freq %d), "
9083 "disabling CxSR\n",
9084 (dev_priv->is_ddr3 == 1) ? "3" : "2",
9085 dev_priv->fsb_freq, dev_priv->mem_freq);
9086 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03009087 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009088 dev_priv->display.update_wm = NULL;
9089 } else
9090 dev_priv->display.update_wm = pineview_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01009091 } else if (IS_GEN4(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009092 dev_priv->display.update_wm = i965_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01009093 } else if (IS_GEN3(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009094 dev_priv->display.update_wm = i9xx_update_wm;
9095 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01009096 } else if (IS_GEN2(dev_priv)) {
Ville Syrjälä62d75df2016-10-31 22:37:25 +02009097 if (INTEL_INFO(dev_priv)->num_pipes == 1) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009098 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009099 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009100 } else {
9101 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009102 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009103 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009104 } else {
9105 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009106 }
9107}
9108
Lyude87660502016-08-17 15:55:53 -04009109static inline int gen6_check_mailbox_status(struct drm_i915_private *dev_priv)
9110{
9111 uint32_t flags =
9112 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
9113
9114 switch (flags) {
9115 case GEN6_PCODE_SUCCESS:
9116 return 0;
9117 case GEN6_PCODE_UNIMPLEMENTED_CMD:
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009118 return -ENODEV;
Lyude87660502016-08-17 15:55:53 -04009119 case GEN6_PCODE_ILLEGAL_CMD:
9120 return -ENXIO;
9121 case GEN6_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Chris Wilson7850d1c2016-08-26 11:59:26 +01009122 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Lyude87660502016-08-17 15:55:53 -04009123 return -EOVERFLOW;
9124 case GEN6_PCODE_TIMEOUT:
9125 return -ETIMEDOUT;
9126 default:
Michal Wajdeczkof0d66152017-03-28 08:45:12 +00009127 MISSING_CASE(flags);
Lyude87660502016-08-17 15:55:53 -04009128 return 0;
9129 }
9130}
9131
9132static inline int gen7_check_mailbox_status(struct drm_i915_private *dev_priv)
9133{
9134 uint32_t flags =
9135 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
9136
9137 switch (flags) {
9138 case GEN6_PCODE_SUCCESS:
9139 return 0;
9140 case GEN6_PCODE_ILLEGAL_CMD:
9141 return -ENXIO;
9142 case GEN7_PCODE_TIMEOUT:
9143 return -ETIMEDOUT;
9144 case GEN7_PCODE_ILLEGAL_DATA:
9145 return -EINVAL;
9146 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
9147 return -EOVERFLOW;
9148 default:
9149 MISSING_CASE(flags);
9150 return 0;
9151 }
9152}
9153
Tom O'Rourke151a49d2014-11-13 18:50:10 -08009154int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07009155{
Lyude87660502016-08-17 15:55:53 -04009156 int status;
9157
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01009158 WARN_ON(!mutex_is_locked(&dev_priv->pcu_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07009159
Chris Wilson3f5582d2016-06-30 15:32:45 +01009160 /* GEN6_PCODE_* are outside of the forcewake domain, we can
9161 * use te fw I915_READ variants to reduce the amount of work
9162 * required when reading/writing.
9163 */
9164
9165 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009166 DRM_DEBUG_DRIVER("warning: pcode (read from mbox %x) mailbox access failed for %ps\n",
9167 mbox, __builtin_return_address(0));
Ben Widawsky42c05262012-09-26 10:34:00 -07009168 return -EAGAIN;
9169 }
9170
Chris Wilson3f5582d2016-06-30 15:32:45 +01009171 I915_WRITE_FW(GEN6_PCODE_DATA, *val);
9172 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
9173 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07009174
Chris Wilsone09a3032017-04-11 11:13:39 +01009175 if (__intel_wait_for_register_fw(dev_priv,
9176 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
9177 500, 0, NULL)) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009178 DRM_ERROR("timeout waiting for pcode read (from mbox %x) to finish for %ps\n",
9179 mbox, __builtin_return_address(0));
Ben Widawsky42c05262012-09-26 10:34:00 -07009180 return -ETIMEDOUT;
9181 }
9182
Chris Wilson3f5582d2016-06-30 15:32:45 +01009183 *val = I915_READ_FW(GEN6_PCODE_DATA);
9184 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07009185
Lyude87660502016-08-17 15:55:53 -04009186 if (INTEL_GEN(dev_priv) > 6)
9187 status = gen7_check_mailbox_status(dev_priv);
9188 else
9189 status = gen6_check_mailbox_status(dev_priv);
9190
9191 if (status) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009192 DRM_DEBUG_DRIVER("warning: pcode (read from mbox %x) mailbox access failed for %ps: %d\n",
9193 mbox, __builtin_return_address(0), status);
Lyude87660502016-08-17 15:55:53 -04009194 return status;
9195 }
9196
Ben Widawsky42c05262012-09-26 10:34:00 -07009197 return 0;
9198}
9199
Chris Wilson3f5582d2016-06-30 15:32:45 +01009200int sandybridge_pcode_write(struct drm_i915_private *dev_priv,
Lyude87660502016-08-17 15:55:53 -04009201 u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07009202{
Lyude87660502016-08-17 15:55:53 -04009203 int status;
9204
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01009205 WARN_ON(!mutex_is_locked(&dev_priv->pcu_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07009206
Chris Wilson3f5582d2016-06-30 15:32:45 +01009207 /* GEN6_PCODE_* are outside of the forcewake domain, we can
9208 * use te fw I915_READ variants to reduce the amount of work
9209 * required when reading/writing.
9210 */
9211
9212 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009213 DRM_DEBUG_DRIVER("warning: pcode (write of 0x%08x to mbox %x) mailbox access failed for %ps\n",
9214 val, mbox, __builtin_return_address(0));
Ben Widawsky42c05262012-09-26 10:34:00 -07009215 return -EAGAIN;
9216 }
9217
Chris Wilson3f5582d2016-06-30 15:32:45 +01009218 I915_WRITE_FW(GEN6_PCODE_DATA, val);
Imre Deak8bf41b72016-11-28 17:29:27 +02009219 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
Chris Wilson3f5582d2016-06-30 15:32:45 +01009220 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07009221
Chris Wilsone09a3032017-04-11 11:13:39 +01009222 if (__intel_wait_for_register_fw(dev_priv,
9223 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
9224 500, 0, NULL)) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009225 DRM_ERROR("timeout waiting for pcode write of 0x%08x to mbox %x to finish for %ps\n",
9226 val, mbox, __builtin_return_address(0));
Ben Widawsky42c05262012-09-26 10:34:00 -07009227 return -ETIMEDOUT;
9228 }
9229
Chris Wilson3f5582d2016-06-30 15:32:45 +01009230 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07009231
Lyude87660502016-08-17 15:55:53 -04009232 if (INTEL_GEN(dev_priv) > 6)
9233 status = gen7_check_mailbox_status(dev_priv);
9234 else
9235 status = gen6_check_mailbox_status(dev_priv);
9236
9237 if (status) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009238 DRM_DEBUG_DRIVER("warning: pcode (write of 0x%08x to mbox %x) mailbox access failed for %ps: %d\n",
9239 val, mbox, __builtin_return_address(0), status);
Lyude87660502016-08-17 15:55:53 -04009240 return status;
9241 }
9242
Ben Widawsky42c05262012-09-26 10:34:00 -07009243 return 0;
9244}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07009245
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009246static bool skl_pcode_try_request(struct drm_i915_private *dev_priv, u32 mbox,
9247 u32 request, u32 reply_mask, u32 reply,
9248 u32 *status)
9249{
9250 u32 val = request;
9251
9252 *status = sandybridge_pcode_read(dev_priv, mbox, &val);
9253
9254 return *status || ((val & reply_mask) == reply);
9255}
9256
9257/**
9258 * skl_pcode_request - send PCODE request until acknowledgment
9259 * @dev_priv: device private
9260 * @mbox: PCODE mailbox ID the request is targeted for
9261 * @request: request ID
9262 * @reply_mask: mask used to check for request acknowledgment
9263 * @reply: value used to check for request acknowledgment
9264 * @timeout_base_ms: timeout for polling with preemption enabled
9265 *
9266 * Keep resending the @request to @mbox until PCODE acknowledges it, PCODE
Imre Deak01299362017-02-24 16:32:10 +02009267 * reports an error or an overall timeout of @timeout_base_ms+50 ms expires.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009268 * The request is acknowledged once the PCODE reply dword equals @reply after
9269 * applying @reply_mask. Polling is first attempted with preemption enabled
Imre Deak01299362017-02-24 16:32:10 +02009270 * for @timeout_base_ms and if this times out for another 50 ms with
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009271 * preemption disabled.
9272 *
9273 * Returns 0 on success, %-ETIMEDOUT in case of a timeout, <0 in case of some
9274 * other error as reported by PCODE.
9275 */
9276int skl_pcode_request(struct drm_i915_private *dev_priv, u32 mbox, u32 request,
9277 u32 reply_mask, u32 reply, int timeout_base_ms)
9278{
9279 u32 status;
9280 int ret;
9281
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01009282 WARN_ON(!mutex_is_locked(&dev_priv->pcu_lock));
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009283
9284#define COND skl_pcode_try_request(dev_priv, mbox, request, reply_mask, reply, \
9285 &status)
9286
9287 /*
9288 * Prime the PCODE by doing a request first. Normally it guarantees
9289 * that a subsequent request, at most @timeout_base_ms later, succeeds.
9290 * _wait_for() doesn't guarantee when its passed condition is evaluated
9291 * first, so send the first request explicitly.
9292 */
9293 if (COND) {
9294 ret = 0;
9295 goto out;
9296 }
Chris Wilsona54b1872017-11-24 13:00:30 +00009297 ret = _wait_for(COND, timeout_base_ms * 1000, 10, 10);
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009298 if (!ret)
9299 goto out;
9300
9301 /*
9302 * The above can time out if the number of requests was low (2 in the
9303 * worst case) _and_ PCODE was busy for some reason even after a
9304 * (queued) request and @timeout_base_ms delay. As a workaround retry
9305 * the poll with preemption disabled to maximize the number of
Imre Deak01299362017-02-24 16:32:10 +02009306 * requests. Increase the timeout from @timeout_base_ms to 50ms to
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009307 * account for interrupts that could reduce the number of these
Imre Deak01299362017-02-24 16:32:10 +02009308 * requests, and for any quirks of the PCODE firmware that delays
9309 * the request completion.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009310 */
9311 DRM_DEBUG_KMS("PCODE timeout, retrying with preemption disabled\n");
9312 WARN_ON_ONCE(timeout_base_ms > 3);
9313 preempt_disable();
Imre Deak01299362017-02-24 16:32:10 +02009314 ret = wait_for_atomic(COND, 50);
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009315 preempt_enable();
9316
9317out:
9318 return ret ? ret : status;
9319#undef COND
9320}
9321
Ville Syrjälädd06f882014-11-10 22:55:12 +02009322static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
9323{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009324 struct intel_rps *rps = &dev_priv->gt_pm.rps;
9325
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009326 /*
9327 * N = val - 0xb7
9328 * Slow = Fast = GPLL ref * N
9329 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009330 return DIV_ROUND_CLOSEST(rps->gpll_ref_freq * (val - 0xb7), 1000);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009331}
9332
Fengguang Wub55dd642014-07-12 11:21:39 +02009333static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009334{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009335 struct intel_rps *rps = &dev_priv->gt_pm.rps;
9336
9337 return DIV_ROUND_CLOSEST(1000 * val, rps->gpll_ref_freq) + 0xb7;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009338}
9339
Fengguang Wub55dd642014-07-12 11:21:39 +02009340static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05309341{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009342 struct intel_rps *rps = &dev_priv->gt_pm.rps;
9343
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009344 /*
9345 * N = val / 2
9346 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
9347 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009348 return DIV_ROUND_CLOSEST(rps->gpll_ref_freq * val, 2 * 2 * 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05309349}
9350
Fengguang Wub55dd642014-07-12 11:21:39 +02009351static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05309352{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009353 struct intel_rps *rps = &dev_priv->gt_pm.rps;
9354
Ville Syrjälä1c147622014-08-18 14:42:43 +03009355 /* CHV needs even values */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009356 return DIV_ROUND_CLOSEST(2 * 1000 * val, rps->gpll_ref_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05309357}
9358
Ville Syrjälä616bc822015-01-23 21:04:25 +02009359int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
9360{
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07009361 if (INTEL_GEN(dev_priv) >= 9)
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009362 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
9363 GEN9_FREQ_SCALER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009364 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009365 return chv_gpu_freq(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009366 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009367 return byt_gpu_freq(dev_priv, val);
9368 else
9369 return val * GT_FREQUENCY_MULTIPLIER;
9370}
9371
Ville Syrjälä616bc822015-01-23 21:04:25 +02009372int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
9373{
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07009374 if (INTEL_GEN(dev_priv) >= 9)
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009375 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
9376 GT_FREQUENCY_MULTIPLIER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009377 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009378 return chv_freq_opcode(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009379 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009380 return byt_freq_opcode(dev_priv, val);
9381 else
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009382 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
Deepak S22b1b2f2014-07-12 14:54:33 +05309383}
9384
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00009385void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01009386{
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01009387 mutex_init(&dev_priv->pcu_lock);
Daniel Vetterf742a552013-12-06 10:17:53 +01009388
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009389 atomic_set(&dev_priv->gt_pm.rps.num_waiters, 0);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03009390
Sagar Arun Kamblead1443f2017-10-10 22:30:04 +01009391 dev_priv->runtime_pm.suspended = false;
9392 atomic_set(&dev_priv->runtime_pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01009393}
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009394
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009395static u64 vlv_residency_raw(struct drm_i915_private *dev_priv,
9396 const i915_reg_t reg)
9397{
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009398 u32 lower, upper, tmp;
Chris Wilsonb4e3c932017-11-22 22:25:10 +00009399 unsigned long flags;
Chris Wilson71cc2b12017-03-24 16:54:18 +00009400 int loop = 2;
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009401
9402 /* The register accessed do not need forcewake. We borrow
9403 * uncore lock to prevent concurrent access to range reg.
9404 */
Chris Wilsonb4e3c932017-11-22 22:25:10 +00009405 spin_lock_irqsave(&dev_priv->uncore.lock, flags);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009406
9407 /* vlv and chv residency counters are 40 bits in width.
9408 * With a control bit, we can choose between upper or lower
9409 * 32bit window into this counter.
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009410 *
9411 * Although we always use the counter in high-range mode elsewhere,
9412 * userspace may attempt to read the value before rc6 is initialised,
9413 * before we have set the default VLV_COUNTER_CONTROL value. So always
9414 * set the high bit to be safe.
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009415 */
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009416 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9417 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009418 upper = I915_READ_FW(reg);
9419 do {
9420 tmp = upper;
9421
9422 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9423 _MASKED_BIT_DISABLE(VLV_COUNT_RANGE_HIGH));
9424 lower = I915_READ_FW(reg);
9425
9426 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9427 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
9428 upper = I915_READ_FW(reg);
Chris Wilson71cc2b12017-03-24 16:54:18 +00009429 } while (upper != tmp && --loop);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009430
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009431 /* Everywhere else we always use VLV_COUNTER_CONTROL with the
9432 * VLV_COUNT_RANGE_HIGH bit set - so it is safe to leave it set
9433 * now.
9434 */
9435
Chris Wilsonb4e3c932017-11-22 22:25:10 +00009436 spin_unlock_irqrestore(&dev_priv->uncore.lock, flags);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009437
9438 return lower | (u64)upper << 8;
9439}
9440
Tvrtko Ursulin36cc8b92017-11-21 18:18:51 +00009441u64 intel_rc6_residency_ns(struct drm_i915_private *dev_priv,
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009442 const i915_reg_t reg)
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009443{
Tvrtko Ursulin36cc8b92017-11-21 18:18:51 +00009444 u64 time_hw;
9445 u32 mul, div;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009446
Sagar Arun Kamble771decb2017-10-10 22:30:07 +01009447 if (!intel_rc6_enabled())
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009448 return 0;
9449
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009450 /* On VLV and CHV, residency time is in CZ units rather than 1.28us */
9451 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Tvrtko Ursulin36cc8b92017-11-21 18:18:51 +00009452 mul = 1000000;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009453 div = dev_priv->czclk_freq;
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009454 time_hw = vlv_residency_raw(dev_priv, reg);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009455 } else {
Tvrtko Ursulin36cc8b92017-11-21 18:18:51 +00009456 /* 833.33ns units on Gen9LP, 1.28us elsewhere. */
9457 if (IS_GEN9_LP(dev_priv)) {
9458 mul = 10000;
9459 div = 12;
9460 } else {
9461 mul = 1280;
9462 div = 1;
9463 }
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009464
9465 time_hw = I915_READ(reg);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009466 }
9467
Tvrtko Ursulin36cc8b92017-11-21 18:18:51 +00009468 return DIV_ROUND_UP_ULL(time_hw * mul, div);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009469}
Tvrtko Ursulinc84b2702017-11-21 18:18:44 +00009470
9471u32 intel_get_cagf(struct drm_i915_private *dev_priv, u32 rpstat)
9472{
9473 u32 cagf;
9474
9475 if (INTEL_GEN(dev_priv) >= 9)
9476 cagf = (rpstat & GEN9_CAGF_MASK) >> GEN9_CAGF_SHIFT;
9477 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
9478 cagf = (rpstat & HSW_CAGF_MASK) >> HSW_CAGF_SHIFT;
9479 else
9480 cagf = (rpstat & GEN6_CAGF_MASK) >> GEN6_CAGF_SHIFT;
9481
9482 return cagf;
9483}