blob: 957ef10b156918e1874ec03ed1fa132115ec5495 [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{
Mika Kuoppalab033bb62016-06-07 17:19:04 +030061 /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl */
62 I915_WRITE(CHICKEN_PAR1_1,
63 I915_READ(CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
64
65 I915_WRITE(GEN8_CONFIG0,
66 I915_READ(GEN8_CONFIG0) | GEN9_DEFAULT_FIXES);
Mika Kuoppala590e8ff2016-06-07 17:19:13 +030067
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +020068 /* WaEnableChickenDCPR:skl,bxt,kbl,glk */
Mika Kuoppala590e8ff2016-06-07 17:19:13 +030069 I915_WRITE(GEN8_CHICKEN_DCPR_1,
70 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
Mika Kuoppala0f78dee2016-06-07 17:19:16 +030071
72 /* WaFbcTurnOffFbcWatermark:skl,bxt,kbl */
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +020073 /* WaFbcWakeMemOn:skl,bxt,kbl,glk */
Mika Kuoppala303d4ea2016-06-07 17:19:17 +030074 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
75 DISP_FBC_WM_DIS |
76 DISP_FBC_MEMORY_WAKE);
Mika Kuoppalad1b4eef2016-06-07 17:19:19 +030077
78 /* WaFbcHighMemBwCorruptionAvoidance:skl,bxt,kbl */
79 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
80 ILK_DPFC_DISABLE_DUMMY0);
Mika Kuoppalab033bb62016-06-07 17:19:04 +030081}
82
Ville Syrjälä46f16e62016-10-31 22:37:22 +020083static void bxt_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deaka82abe42015-03-27 14:00:04 +020084{
Ville Syrjälä46f16e62016-10-31 22:37:22 +020085 gen9_init_clock_gating(dev_priv);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +020086
Nick Hoatha7546152015-06-29 14:07:32 +010087 /* WaDisableSDEUnitClockGating:bxt */
88 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
89 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
90
Imre Deak32608ca2015-03-11 11:10:27 +020091 /*
92 * FIXME:
Ben Widawsky868434c2015-03-11 10:49:32 +020093 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
Imre Deak32608ca2015-03-11 11:10:27 +020094 */
Imre Deak32608ca2015-03-11 11:10:27 +020095 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
Ben Widawsky868434c2015-03-11 10:49:32 +020096 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
Imre Deakd965e7ac2015-12-01 10:23:52 +020097
98 /*
99 * Wa: Backlight PWM may stop in the asserted state, causing backlight
100 * to stay fully on.
101 */
Jani Nikula8aeaf642017-02-15 17:21:37 +0200102 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
103 PWM1_GATING_DIS | PWM2_GATING_DIS);
Imre Deaka82abe42015-03-27 14:00:04 +0200104}
105
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200106static void glk_init_clock_gating(struct drm_i915_private *dev_priv)
107{
108 gen9_init_clock_gating(dev_priv);
109
110 /*
111 * WaDisablePWMClockGating:glk
112 * Backlight PWM may stop in the asserted state, causing backlight
113 * to stay fully on.
114 */
115 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
116 PWM1_GATING_DIS | PWM2_GATING_DIS);
Ander Conselvan de Oliveiraf4f4b592017-02-22 08:34:29 +0200117
118 /* WaDDIIOTimeout:glk */
119 if (IS_GLK_REVID(dev_priv, 0, GLK_REVID_A1)) {
120 u32 val = I915_READ(CHICKEN_MISC_2);
121 val &= ~(GLK_CL0_PWR_DOWN |
122 GLK_CL1_PWR_DOWN |
123 GLK_CL2_PWR_DOWN);
124 I915_WRITE(CHICKEN_MISC_2, val);
125 }
126
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200127}
128
Ville Syrjälä148ac1f2016-10-31 22:37:16 +0200129static void i915_pineview_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200130{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200131 u32 tmp;
132
133 tmp = I915_READ(CLKCFG);
134
135 switch (tmp & CLKCFG_FSB_MASK) {
136 case CLKCFG_FSB_533:
137 dev_priv->fsb_freq = 533; /* 133*4 */
138 break;
139 case CLKCFG_FSB_800:
140 dev_priv->fsb_freq = 800; /* 200*4 */
141 break;
142 case CLKCFG_FSB_667:
143 dev_priv->fsb_freq = 667; /* 167*4 */
144 break;
145 case CLKCFG_FSB_400:
146 dev_priv->fsb_freq = 400; /* 100*4 */
147 break;
148 }
149
150 switch (tmp & CLKCFG_MEM_MASK) {
151 case CLKCFG_MEM_533:
152 dev_priv->mem_freq = 533;
153 break;
154 case CLKCFG_MEM_667:
155 dev_priv->mem_freq = 667;
156 break;
157 case CLKCFG_MEM_800:
158 dev_priv->mem_freq = 800;
159 break;
160 }
161
162 /* detect pineview DDR3 setting */
163 tmp = I915_READ(CSHRDDR3CTL);
164 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
165}
166
Ville Syrjälä148ac1f2016-10-31 22:37:16 +0200167static void i915_ironlake_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200168{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200169 u16 ddrpll, csipll;
170
171 ddrpll = I915_READ16(DDRMPLL1);
172 csipll = I915_READ16(CSIPLL0);
173
174 switch (ddrpll & 0xff) {
175 case 0xc:
176 dev_priv->mem_freq = 800;
177 break;
178 case 0x10:
179 dev_priv->mem_freq = 1066;
180 break;
181 case 0x14:
182 dev_priv->mem_freq = 1333;
183 break;
184 case 0x18:
185 dev_priv->mem_freq = 1600;
186 break;
187 default:
188 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
189 ddrpll & 0xff);
190 dev_priv->mem_freq = 0;
191 break;
192 }
193
Daniel Vetter20e4d402012-08-08 23:35:39 +0200194 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200195
196 switch (csipll & 0x3ff) {
197 case 0x00c:
198 dev_priv->fsb_freq = 3200;
199 break;
200 case 0x00e:
201 dev_priv->fsb_freq = 3733;
202 break;
203 case 0x010:
204 dev_priv->fsb_freq = 4266;
205 break;
206 case 0x012:
207 dev_priv->fsb_freq = 4800;
208 break;
209 case 0x014:
210 dev_priv->fsb_freq = 5333;
211 break;
212 case 0x016:
213 dev_priv->fsb_freq = 5866;
214 break;
215 case 0x018:
216 dev_priv->fsb_freq = 6400;
217 break;
218 default:
219 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
220 csipll & 0x3ff);
221 dev_priv->fsb_freq = 0;
222 break;
223 }
224
225 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200226 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200227 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200228 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200229 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200230 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200231 }
232}
233
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300234static const struct cxsr_latency cxsr_latency_table[] = {
235 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
236 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
237 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
238 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
239 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
240
241 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
242 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
243 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
244 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
245 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
246
247 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
248 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
249 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
250 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
251 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
252
253 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
254 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
255 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
256 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
257 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
258
259 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
260 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
261 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
262 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
263 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
264
265 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
266 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
267 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
268 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
269 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
270};
271
Tvrtko Ursulin44a655c2016-10-13 11:09:23 +0100272static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
273 bool is_ddr3,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300274 int fsb,
275 int mem)
276{
277 const struct cxsr_latency *latency;
278 int i;
279
280 if (fsb == 0 || mem == 0)
281 return NULL;
282
283 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
284 latency = &cxsr_latency_table[i];
285 if (is_desktop == latency->is_desktop &&
286 is_ddr3 == latency->is_ddr3 &&
287 fsb == latency->fsb_freq && mem == latency->mem_freq)
288 return latency;
289 }
290
291 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
292
293 return NULL;
294}
295
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200296static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
297{
298 u32 val;
299
300 mutex_lock(&dev_priv->rps.hw_lock);
301
302 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
303 if (enable)
304 val &= ~FORCE_DDR_HIGH_FREQ;
305 else
306 val |= FORCE_DDR_HIGH_FREQ;
307 val &= ~FORCE_DDR_LOW_FREQ;
308 val |= FORCE_DDR_FREQ_REQ_ACK;
309 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
310
311 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
312 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
313 DRM_ERROR("timed out waiting for Punit DDR DVFS request\n");
314
315 mutex_unlock(&dev_priv->rps.hw_lock);
316}
317
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200318static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
319{
320 u32 val;
321
322 mutex_lock(&dev_priv->rps.hw_lock);
323
324 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
325 if (enable)
326 val |= DSP_MAXFIFO_PM5_ENABLE;
327 else
328 val &= ~DSP_MAXFIFO_PM5_ENABLE;
329 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
330
331 mutex_unlock(&dev_priv->rps.hw_lock);
332}
333
Ville Syrjäläf4998962015-03-10 17:02:21 +0200334#define FW_WM(value, plane) \
335 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
336
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200337static bool _intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300338{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200339 bool was_enabled;
Imre Deak5209b1f2014-07-01 12:36:17 +0300340 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300341
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100342 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200343 was_enabled = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300344 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300345 POSTING_READ(FW_BLC_SELF_VLV);
Jani Nikulac0f86832016-12-07 12:13:04 +0200346 } else if (IS_G4X(dev_priv) || IS_I965GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200347 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300348 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300349 POSTING_READ(FW_BLC_SELF);
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +0200350 } else if (IS_PINEVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200351 val = I915_READ(DSPFW3);
352 was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
353 if (enable)
354 val |= PINEVIEW_SELF_REFRESH_EN;
355 else
356 val &= ~PINEVIEW_SELF_REFRESH_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300357 I915_WRITE(DSPFW3, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300358 POSTING_READ(DSPFW3);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100359 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200360 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300361 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
362 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
363 I915_WRITE(FW_BLC_SELF, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300364 POSTING_READ(FW_BLC_SELF);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100365 } else if (IS_I915GM(dev_priv)) {
Ville Syrjäläacb91352016-07-29 17:57:02 +0300366 /*
367 * FIXME can't find a bit like this for 915G, and
368 * and yet it does have the related watermark in
369 * FW_BLC_SELF. What's going on?
370 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200371 was_enabled = I915_READ(INSTPM) & INSTPM_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300372 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
373 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
374 I915_WRITE(INSTPM, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300375 POSTING_READ(INSTPM);
Imre Deak5209b1f2014-07-01 12:36:17 +0300376 } else {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200377 return false;
Imre Deak5209b1f2014-07-01 12:36:17 +0300378 }
379
Ville Syrjälä1489bba2017-03-02 19:15:07 +0200380 trace_intel_memory_cxsr(dev_priv, was_enabled, enable);
381
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200382 DRM_DEBUG_KMS("memory self-refresh is %s (was %s)\n",
383 enableddisabled(enable),
384 enableddisabled(was_enabled));
385
386 return was_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300387}
388
Ville Syrjälä62571fc2017-04-21 21:14:23 +0300389/**
390 * intel_set_memory_cxsr - Configure CxSR state
391 * @dev_priv: i915 device
392 * @enable: Allow vs. disallow CxSR
393 *
394 * Allow or disallow the system to enter a special CxSR
395 * (C-state self refresh) state. What typically happens in CxSR mode
396 * is that several display FIFOs may get combined into a single larger
397 * FIFO for a particular plane (so called max FIFO mode) to allow the
398 * system to defer memory fetches longer, and the memory will enter
399 * self refresh.
400 *
401 * Note that enabling CxSR does not guarantee that the system enter
402 * this special mode, nor does it guarantee that the system stays
403 * in that mode once entered. So this just allows/disallows the system
404 * to autonomously utilize the CxSR mode. Other factors such as core
405 * C-states will affect when/if the system actually enters/exits the
406 * CxSR mode.
407 *
408 * Note that on VLV/CHV this actually only controls the max FIFO mode,
409 * and the system is free to enter/exit memory self refresh at any time
410 * even when the use of CxSR has been disallowed.
411 *
412 * While the system is actually in the CxSR/max FIFO mode, some plane
413 * control registers will not get latched on vblank. Thus in order to
414 * guarantee the system will respond to changes in the plane registers
415 * we must always disallow CxSR prior to making changes to those registers.
416 * Unfortunately the system will re-evaluate the CxSR conditions at
417 * frame start which happens after vblank start (which is when the plane
418 * registers would get latched), so we can't proceed with the plane update
419 * during the same frame where we disallowed CxSR.
420 *
421 * Certain platforms also have a deeper HPLL SR mode. Fortunately the
422 * HPLL SR mode depends on CxSR itself, so we don't have to hand hold
423 * the hardware w.r.t. HPLL SR when writing to plane registers.
424 * Disallowing just CxSR is sufficient.
425 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200426bool intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200427{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200428 bool ret;
429
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200430 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200431 ret = _intel_set_memory_cxsr(dev_priv, enable);
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300432 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
433 dev_priv->wm.vlv.cxsr = enable;
434 else if (IS_G4X(dev_priv))
435 dev_priv->wm.g4x.cxsr = enable;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200436 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200437
438 return ret;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200439}
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200440
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300441/*
442 * Latency for FIFO fetches is dependent on several factors:
443 * - memory configuration (speed, channels)
444 * - chipset
445 * - current MCH state
446 * It can be fairly high in some situations, so here we assume a fairly
447 * pessimal value. It's a tradeoff between extra memory fetches (if we
448 * set this value too high, the FIFO will fetch frequently to stay full)
449 * and power consumption (set it too low to save power and we might see
450 * FIFO underruns and display "flicker").
451 *
452 * A value of 5us seems to be a good balance; safe for very low end
453 * platforms but not overly aggressive on lower latency configs.
454 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100455static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300456
Ville Syrjäläb5004722015-03-05 21:19:47 +0200457#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
458 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
459
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200460static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
Ville Syrjäläb5004722015-03-05 21:19:47 +0200461{
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200462 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200463 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200464 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200465 enum pipe pipe = crtc->pipe;
466 int sprite0_start, sprite1_start;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200467
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200468 switch (pipe) {
Ville Syrjäläb5004722015-03-05 21:19:47 +0200469 uint32_t dsparb, dsparb2, dsparb3;
470 case PIPE_A:
471 dsparb = I915_READ(DSPARB);
472 dsparb2 = I915_READ(DSPARB2);
473 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
474 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
475 break;
476 case PIPE_B:
477 dsparb = I915_READ(DSPARB);
478 dsparb2 = I915_READ(DSPARB2);
479 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
480 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
481 break;
482 case PIPE_C:
483 dsparb2 = I915_READ(DSPARB2);
484 dsparb3 = I915_READ(DSPARB3);
485 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
486 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
487 break;
488 default:
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200489 MISSING_CASE(pipe);
490 return;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200491 }
492
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200493 fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
494 fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
495 fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
496 fifo_state->plane[PLANE_CURSOR] = 63;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200497}
498
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200499static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300500{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300501 uint32_t dsparb = I915_READ(DSPARB);
502 int size;
503
504 size = dsparb & 0x7f;
505 if (plane)
506 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
507
508 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
509 plane ? "B" : "A", size);
510
511 return size;
512}
513
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200514static int i830_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300515{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300516 uint32_t dsparb = I915_READ(DSPARB);
517 int size;
518
519 size = dsparb & 0x1ff;
520 if (plane)
521 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
522 size >>= 1; /* Convert to cachelines */
523
524 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
525 plane ? "B" : "A", size);
526
527 return size;
528}
529
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200530static int i845_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300531{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300532 uint32_t dsparb = I915_READ(DSPARB);
533 int size;
534
535 size = dsparb & 0x7f;
536 size >>= 2; /* Convert to cachelines */
537
538 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
539 plane ? "B" : "A",
540 size);
541
542 return size;
543}
544
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300545/* Pineview has different values for various configs */
546static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300547 .fifo_size = PINEVIEW_DISPLAY_FIFO,
548 .max_wm = PINEVIEW_MAX_WM,
549 .default_wm = PINEVIEW_DFT_WM,
550 .guard_size = PINEVIEW_GUARD_WM,
551 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300552};
553static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300554 .fifo_size = PINEVIEW_DISPLAY_FIFO,
555 .max_wm = PINEVIEW_MAX_WM,
556 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
557 .guard_size = PINEVIEW_GUARD_WM,
558 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300559};
560static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300561 .fifo_size = PINEVIEW_CURSOR_FIFO,
562 .max_wm = PINEVIEW_CURSOR_MAX_WM,
563 .default_wm = PINEVIEW_CURSOR_DFT_WM,
564 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
565 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300566};
567static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300568 .fifo_size = PINEVIEW_CURSOR_FIFO,
569 .max_wm = PINEVIEW_CURSOR_MAX_WM,
570 .default_wm = PINEVIEW_CURSOR_DFT_WM,
571 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
572 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300573};
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300574static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300575 .fifo_size = I965_CURSOR_FIFO,
576 .max_wm = I965_CURSOR_MAX_WM,
577 .default_wm = I965_CURSOR_DFT_WM,
578 .guard_size = 2,
579 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300580};
581static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300582 .fifo_size = I945_FIFO_SIZE,
583 .max_wm = I915_MAX_WM,
584 .default_wm = 1,
585 .guard_size = 2,
586 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300587};
588static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300589 .fifo_size = I915_FIFO_SIZE,
590 .max_wm = I915_MAX_WM,
591 .default_wm = 1,
592 .guard_size = 2,
593 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300594};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300595static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300596 .fifo_size = I855GM_FIFO_SIZE,
597 .max_wm = I915_MAX_WM,
598 .default_wm = 1,
599 .guard_size = 2,
600 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300601};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300602static const struct intel_watermark_params i830_bc_wm_info = {
603 .fifo_size = I855GM_FIFO_SIZE,
604 .max_wm = I915_MAX_WM/2,
605 .default_wm = 1,
606 .guard_size = 2,
607 .cacheline_size = I830_FIFO_LINE_SIZE,
608};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200609static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300610 .fifo_size = I830_FIFO_SIZE,
611 .max_wm = I915_MAX_WM,
612 .default_wm = 1,
613 .guard_size = 2,
614 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300615};
616
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300617/**
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300618 * intel_wm_method1 - Method 1 / "small buffer" watermark formula
619 * @pixel_rate: Pipe pixel rate in kHz
620 * @cpp: Plane bytes per pixel
621 * @latency: Memory wakeup latency in 0.1us units
622 *
623 * Compute the watermark using the method 1 or "small buffer"
624 * formula. The caller may additonally add extra cachelines
625 * to account for TLB misses and clock crossings.
626 *
627 * This method is concerned with the short term drain rate
628 * of the FIFO, ie. it does not account for blanking periods
629 * which would effectively reduce the average drain rate across
630 * a longer period. The name "small" refers to the fact the
631 * FIFO is relatively small compared to the amount of data
632 * fetched.
633 *
634 * The FIFO level vs. time graph might look something like:
635 *
636 * |\ |\
637 * | \ | \
638 * __---__---__ (- plane active, _ blanking)
639 * -> time
640 *
641 * or perhaps like this:
642 *
643 * |\|\ |\|\
644 * __----__----__ (- plane active, _ blanking)
645 * -> time
646 *
647 * Returns:
648 * The watermark in bytes
649 */
650static unsigned int intel_wm_method1(unsigned int pixel_rate,
651 unsigned int cpp,
652 unsigned int latency)
653{
654 uint64_t ret;
655
656 ret = (uint64_t) pixel_rate * cpp * latency;
657 ret = DIV_ROUND_UP_ULL(ret, 10000);
658
659 return ret;
660}
661
662/**
663 * intel_wm_method2 - Method 2 / "large buffer" watermark formula
664 * @pixel_rate: Pipe pixel rate in kHz
665 * @htotal: Pipe horizontal total
666 * @width: Plane width in pixels
667 * @cpp: Plane bytes per pixel
668 * @latency: Memory wakeup latency in 0.1us units
669 *
670 * Compute the watermark using the method 2 or "large buffer"
671 * formula. The caller may additonally add extra cachelines
672 * to account for TLB misses and clock crossings.
673 *
674 * This method is concerned with the long term drain rate
675 * of the FIFO, ie. it does account for blanking periods
676 * which effectively reduce the average drain rate across
677 * a longer period. The name "large" refers to the fact the
678 * FIFO is relatively large compared to the amount of data
679 * fetched.
680 *
681 * The FIFO level vs. time graph might look something like:
682 *
683 * |\___ |\___
684 * | \___ | \___
685 * | \ | \
686 * __ --__--__--__--__--__--__ (- plane active, _ blanking)
687 * -> time
688 *
689 * Returns:
690 * The watermark in bytes
691 */
692static unsigned int intel_wm_method2(unsigned int pixel_rate,
693 unsigned int htotal,
694 unsigned int width,
695 unsigned int cpp,
696 unsigned int latency)
697{
698 unsigned int ret;
699
700 /*
701 * FIXME remove once all users are computing
702 * watermarks in the correct place.
703 */
704 if (WARN_ON_ONCE(htotal == 0))
705 htotal = 1;
706
707 ret = (latency * pixel_rate) / (htotal * 10000);
708 ret = (ret + 1) * width * cpp;
709
710 return ret;
711}
712
713/**
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300714 * intel_calculate_wm - calculate watermark level
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300715 * @pixel_rate: pixel clock
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300716 * @wm: chip FIFO params
Ville Syrjäläac484962016-01-20 21:05:26 +0200717 * @cpp: bytes per pixel
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300718 * @latency_ns: memory latency for the platform
719 *
720 * Calculate the watermark level (the level at which the display plane will
721 * start fetching from memory again). Each chip has a different display
722 * FIFO size and allocation, so the caller needs to figure that out and pass
723 * in the correct intel_watermark_params structure.
724 *
725 * As the pixel clock runs, the FIFO will be drained at a rate that depends
726 * on the pixel size. When it reaches the watermark level, it'll start
727 * fetching FIFO line sized based chunks from memory until the FIFO fills
728 * past the watermark point. If the FIFO drains completely, a FIFO underrun
729 * will occur, and a display engine hang could result.
730 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300731static unsigned int intel_calculate_wm(int pixel_rate,
732 const struct intel_watermark_params *wm,
733 int fifo_size, int cpp,
734 unsigned int latency_ns)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300735{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300736 int entries, wm_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300737
738 /*
739 * Note: we need to make sure we don't overflow for various clock &
740 * latency values.
741 * clocks go from a few thousand to several hundred thousand.
742 * latency is usually a few thousand
743 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300744 entries = intel_wm_method1(pixel_rate, cpp,
745 latency_ns / 100);
746 entries = DIV_ROUND_UP(entries, wm->cacheline_size) +
747 wm->guard_size;
748 DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300749
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300750 wm_size = fifo_size - entries;
751 DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300752
753 /* Don't promote wm_size to unsigned... */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300754 if (wm_size > wm->max_wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300755 wm_size = wm->max_wm;
756 if (wm_size <= 0)
757 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300758
759 /*
760 * Bspec seems to indicate that the value shouldn't be lower than
761 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
762 * Lets go for 8 which is the burst size since certain platforms
763 * already use a hardcoded 8 (which is what the spec says should be
764 * done).
765 */
766 if (wm_size <= 8)
767 wm_size = 8;
768
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300769 return wm_size;
770}
771
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300772static bool is_disabling(int old, int new, int threshold)
773{
774 return old >= threshold && new < threshold;
775}
776
777static bool is_enabling(int old, int new, int threshold)
778{
779 return old < threshold && new >= threshold;
780}
781
Ville Syrjälä6d5019b2017-04-21 21:14:20 +0300782static int intel_wm_num_levels(struct drm_i915_private *dev_priv)
783{
784 return dev_priv->wm.max_level + 1;
785}
786
Ville Syrjälä24304d812017-03-14 17:10:49 +0200787static bool intel_wm_plane_visible(const struct intel_crtc_state *crtc_state,
788 const struct intel_plane_state *plane_state)
789{
790 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
791
792 /* FIXME check the 'enable' instead */
793 if (!crtc_state->base.active)
794 return false;
795
796 /*
797 * Treat cursor with fb as always visible since cursor updates
798 * can happen faster than the vrefresh rate, and the current
799 * watermark code doesn't handle that correctly. Cursor updates
800 * which set/clear the fb or change the cursor size are going
801 * to get throttled by intel_legacy_cursor_update() to work
802 * around this problem with the watermark code.
803 */
804 if (plane->id == PLANE_CURSOR)
805 return plane_state->base.fb != NULL;
806 else
807 return plane_state->base.visible;
808}
809
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200810static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300811{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200812 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300813
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200814 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200815 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300816 if (enabled)
817 return NULL;
818 enabled = crtc;
819 }
820 }
821
822 return enabled;
823}
824
Ville Syrjälä432081b2016-10-31 22:37:03 +0200825static void pineview_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300826{
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200827 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200828 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300829 const struct cxsr_latency *latency;
830 u32 reg;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300831 unsigned int wm;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300832
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100833 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
834 dev_priv->is_ddr3,
835 dev_priv->fsb_freq,
836 dev_priv->mem_freq);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300837 if (!latency) {
838 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300839 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300840 return;
841 }
842
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200843 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300844 if (crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200845 const struct drm_display_mode *adjusted_mode =
846 &crtc->config->base.adjusted_mode;
847 const struct drm_framebuffer *fb =
848 crtc->base.primary->state->fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200849 int cpp = fb->format->cpp[0];
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300850 int clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300851
852 /* Display SR */
853 wm = intel_calculate_wm(clock, &pineview_display_wm,
854 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200855 cpp, latency->display_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300856 reg = I915_READ(DSPFW1);
857 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200858 reg |= FW_WM(wm, SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300859 I915_WRITE(DSPFW1, reg);
860 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
861
862 /* cursor SR */
863 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
864 pineview_display_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300865 4, latency->cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300866 reg = I915_READ(DSPFW3);
867 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200868 reg |= FW_WM(wm, CURSOR_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300869 I915_WRITE(DSPFW3, reg);
870
871 /* Display HPLL off SR */
872 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
873 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200874 cpp, latency->display_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300875 reg = I915_READ(DSPFW3);
876 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200877 reg |= FW_WM(wm, HPLL_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300878 I915_WRITE(DSPFW3, reg);
879
880 /* cursor HPLL off SR */
881 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
882 pineview_display_hplloff_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300883 4, latency->cursor_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300884 reg = I915_READ(DSPFW3);
885 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200886 reg |= FW_WM(wm, HPLL_CURSOR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300887 I915_WRITE(DSPFW3, reg);
888 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
889
Imre Deak5209b1f2014-07-01 12:36:17 +0300890 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300891 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300892 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300893 }
894}
895
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300896/*
897 * Documentation says:
898 * "If the line size is small, the TLB fetches can get in the way of the
899 * data fetches, causing some lag in the pixel data return which is not
900 * accounted for in the above formulas. The following adjustment only
901 * needs to be applied if eight whole lines fit in the buffer at once.
902 * The WM is adjusted upwards by the difference between the FIFO size
903 * and the size of 8 whole lines. This adjustment is always performed
904 * in the actual pixel depth regardless of whether FBC is enabled or not."
905 */
906static int g4x_tlb_miss_wa(int fifo_size, int width, int cpp)
907{
908 int tlb_miss = fifo_size * 64 - width * cpp * 8;
909
910 return max(0, tlb_miss);
911}
912
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300913static void g4x_write_wm_values(struct drm_i915_private *dev_priv,
914 const struct g4x_wm_values *wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300915{
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300916 I915_WRITE(DSPFW1,
917 FW_WM(wm->sr.plane, SR) |
918 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
919 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
920 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
921 I915_WRITE(DSPFW2,
922 (wm->fbc_en ? DSPFW_FBC_SR_EN : 0) |
923 FW_WM(wm->sr.fbc, FBC_SR) |
924 FW_WM(wm->hpll.fbc, FBC_HPLL_SR) |
925 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEB) |
926 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
927 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
928 I915_WRITE(DSPFW3,
929 (wm->hpll_en ? DSPFW_HPLL_SR_EN : 0) |
930 FW_WM(wm->sr.cursor, CURSOR_SR) |
931 FW_WM(wm->hpll.cursor, HPLL_CURSOR) |
932 FW_WM(wm->hpll.plane, HPLL_SR));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300933
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300934 POSTING_READ(DSPFW1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300935}
936
Ville Syrjälä15665972015-03-10 16:16:28 +0200937#define FW_WM_VLV(value, plane) \
938 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
939
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200940static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200941 const struct vlv_wm_values *wm)
942{
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200943 enum pipe pipe;
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200944
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200945 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläc137d662017-03-02 19:15:06 +0200946 trace_vlv_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
947
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200948 I915_WRITE(VLV_DDL(pipe),
949 (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
950 (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
951 (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
952 (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
953 }
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200954
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +0200955 /*
956 * Zero the (unused) WM1 watermarks, and also clear all the
957 * high order bits so that there are no out of bounds values
958 * present in the registers during the reprogramming.
959 */
960 I915_WRITE(DSPHOWM, 0);
961 I915_WRITE(DSPHOWM1, 0);
962 I915_WRITE(DSPFW4, 0);
963 I915_WRITE(DSPFW5, 0);
964 I915_WRITE(DSPFW6, 0);
965
Ville Syrjäläae801522015-03-05 21:19:49 +0200966 I915_WRITE(DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +0200967 FW_WM(wm->sr.plane, SR) |
Ville Syrjälä1b313892016-11-28 19:37:08 +0200968 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
969 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
970 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200971 I915_WRITE(DSPFW2,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200972 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
973 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
974 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200975 I915_WRITE(DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +0200976 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +0200977
978 if (IS_CHERRYVIEW(dev_priv)) {
979 I915_WRITE(DSPFW7_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200980 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
981 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200982 I915_WRITE(DSPFW8_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200983 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
984 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
Ville Syrjäläae801522015-03-05 21:19:49 +0200985 I915_WRITE(DSPFW9_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200986 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
987 FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200988 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +0200989 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +0200990 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
991 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
992 FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
993 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
994 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
995 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
996 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
997 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
998 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +0200999 } else {
1000 I915_WRITE(DSPFW7,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001001 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1002 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001003 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001004 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001005 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1006 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1007 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1008 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1009 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1010 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001011 }
1012
1013 POSTING_READ(DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001014}
1015
Ville Syrjälä15665972015-03-10 16:16:28 +02001016#undef FW_WM_VLV
1017
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001018static void g4x_setup_wm_latency(struct drm_i915_private *dev_priv)
1019{
1020 /* all latencies in usec */
1021 dev_priv->wm.pri_latency[G4X_WM_LEVEL_NORMAL] = 5;
1022 dev_priv->wm.pri_latency[G4X_WM_LEVEL_SR] = 12;
Ville Syrjälä79d94302017-04-21 21:14:30 +03001023 dev_priv->wm.pri_latency[G4X_WM_LEVEL_HPLL] = 35;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001024
Ville Syrjälä79d94302017-04-21 21:14:30 +03001025 dev_priv->wm.max_level = G4X_WM_LEVEL_HPLL;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001026}
1027
1028static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
1029{
1030 /*
1031 * DSPCNTR[13] supposedly controls whether the
1032 * primary plane can use the FIFO space otherwise
1033 * reserved for the sprite plane. It's not 100% clear
1034 * what the actual FIFO size is, but it looks like we
1035 * can happily set both primary and sprite watermarks
1036 * up to 127 cachelines. So that would seem to mean
1037 * that either DSPCNTR[13] doesn't do anything, or that
1038 * the total FIFO is >= 256 cachelines in size. Either
1039 * way, we don't seem to have to worry about this
1040 * repartitioning as the maximum watermark value the
1041 * register can hold for each plane is lower than the
1042 * minimum FIFO size.
1043 */
1044 switch (plane_id) {
1045 case PLANE_CURSOR:
1046 return 63;
1047 case PLANE_PRIMARY:
1048 return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
1049 case PLANE_SPRITE0:
1050 return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
1051 default:
1052 MISSING_CASE(plane_id);
1053 return 0;
1054 }
1055}
1056
1057static int g4x_fbc_fifo_size(int level)
1058{
1059 switch (level) {
1060 case G4X_WM_LEVEL_SR:
1061 return 7;
1062 case G4X_WM_LEVEL_HPLL:
1063 return 15;
1064 default:
1065 MISSING_CASE(level);
1066 return 0;
1067 }
1068}
1069
1070static uint16_t g4x_compute_wm(const struct intel_crtc_state *crtc_state,
1071 const struct intel_plane_state *plane_state,
1072 int level)
1073{
1074 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1075 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1076 const struct drm_display_mode *adjusted_mode =
1077 &crtc_state->base.adjusted_mode;
1078 int clock, htotal, cpp, width, wm;
1079 int latency = dev_priv->wm.pri_latency[level] * 10;
1080
1081 if (latency == 0)
1082 return USHRT_MAX;
1083
1084 if (!intel_wm_plane_visible(crtc_state, plane_state))
1085 return 0;
1086
1087 /*
1088 * Not 100% sure which way ELK should go here as the
1089 * spec only says CL/CTG should assume 32bpp and BW
1090 * doesn't need to. But as these things followed the
1091 * mobile vs. desktop lines on gen3 as well, let's
1092 * assume ELK doesn't need this.
1093 *
1094 * The spec also fails to list such a restriction for
1095 * the HPLL watermark, which seems a little strange.
1096 * Let's use 32bpp for the HPLL watermark as well.
1097 */
1098 if (IS_GM45(dev_priv) && plane->id == PLANE_PRIMARY &&
1099 level != G4X_WM_LEVEL_NORMAL)
1100 cpp = 4;
1101 else
1102 cpp = plane_state->base.fb->format->cpp[0];
1103
1104 clock = adjusted_mode->crtc_clock;
1105 htotal = adjusted_mode->crtc_htotal;
1106
1107 if (plane->id == PLANE_CURSOR)
1108 width = plane_state->base.crtc_w;
1109 else
1110 width = drm_rect_width(&plane_state->base.dst);
1111
1112 if (plane->id == PLANE_CURSOR) {
1113 wm = intel_wm_method2(clock, htotal, width, cpp, latency);
1114 } else if (plane->id == PLANE_PRIMARY &&
1115 level == G4X_WM_LEVEL_NORMAL) {
1116 wm = intel_wm_method1(clock, cpp, latency);
1117 } else {
1118 int small, large;
1119
1120 small = intel_wm_method1(clock, cpp, latency);
1121 large = intel_wm_method2(clock, htotal, width, cpp, latency);
1122
1123 wm = min(small, large);
1124 }
1125
1126 wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
1127 width, cpp);
1128
1129 wm = DIV_ROUND_UP(wm, 64) + 2;
1130
1131 return min_t(int, wm, USHRT_MAX);
1132}
1133
1134static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1135 int level, enum plane_id plane_id, u16 value)
1136{
1137 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1138 bool dirty = false;
1139
1140 for (; level < intel_wm_num_levels(dev_priv); level++) {
1141 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1142
1143 dirty |= raw->plane[plane_id] != value;
1144 raw->plane[plane_id] = value;
1145 }
1146
1147 return dirty;
1148}
1149
1150static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
1151 int level, u16 value)
1152{
1153 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1154 bool dirty = false;
1155
1156 /* NORMAL level doesn't have an FBC watermark */
1157 level = max(level, G4X_WM_LEVEL_SR);
1158
1159 for (; level < intel_wm_num_levels(dev_priv); level++) {
1160 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1161
1162 dirty |= raw->fbc != value;
1163 raw->fbc = value;
1164 }
1165
1166 return dirty;
1167}
1168
1169static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
1170 const struct intel_plane_state *pstate,
1171 uint32_t pri_val);
1172
1173static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1174 const struct intel_plane_state *plane_state)
1175{
1176 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1177 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
1178 enum plane_id plane_id = plane->id;
1179 bool dirty = false;
1180 int level;
1181
1182 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1183 dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1184 if (plane_id == PLANE_PRIMARY)
1185 dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
1186 goto out;
1187 }
1188
1189 for (level = 0; level < num_levels; level++) {
1190 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1191 int wm, max_wm;
1192
1193 wm = g4x_compute_wm(crtc_state, plane_state, level);
1194 max_wm = g4x_plane_fifo_size(plane_id, level);
1195
1196 if (wm > max_wm)
1197 break;
1198
1199 dirty |= raw->plane[plane_id] != wm;
1200 raw->plane[plane_id] = wm;
1201
1202 if (plane_id != PLANE_PRIMARY ||
1203 level == G4X_WM_LEVEL_NORMAL)
1204 continue;
1205
1206 wm = ilk_compute_fbc_wm(crtc_state, plane_state,
1207 raw->plane[plane_id]);
1208 max_wm = g4x_fbc_fifo_size(level);
1209
1210 /*
1211 * FBC wm is not mandatory as we
1212 * can always just disable its use.
1213 */
1214 if (wm > max_wm)
1215 wm = USHRT_MAX;
1216
1217 dirty |= raw->fbc != wm;
1218 raw->fbc = wm;
1219 }
1220
1221 /* mark watermarks as invalid */
1222 dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1223
1224 if (plane_id == PLANE_PRIMARY)
1225 dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
1226
1227 out:
1228 if (dirty) {
1229 DRM_DEBUG_KMS("%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
1230 plane->base.name,
1231 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
1232 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
1233 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
1234
1235 if (plane_id == PLANE_PRIMARY)
1236 DRM_DEBUG_KMS("FBC watermarks: SR=%d, HPLL=%d\n",
1237 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
1238 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
1239 }
1240
1241 return dirty;
1242}
1243
1244static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1245 enum plane_id plane_id, int level)
1246{
1247 const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1248
1249 return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
1250}
1251
1252static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
1253 int level)
1254{
1255 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1256
1257 if (level > dev_priv->wm.max_level)
1258 return false;
1259
1260 return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1261 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1262 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1263}
1264
1265/* mark all levels starting from 'level' as invalid */
1266static void g4x_invalidate_wms(struct intel_crtc *crtc,
1267 struct g4x_wm_state *wm_state, int level)
1268{
1269 if (level <= G4X_WM_LEVEL_NORMAL) {
1270 enum plane_id plane_id;
1271
1272 for_each_plane_id_on_crtc(crtc, plane_id)
1273 wm_state->wm.plane[plane_id] = USHRT_MAX;
1274 }
1275
1276 if (level <= G4X_WM_LEVEL_SR) {
1277 wm_state->cxsr = false;
1278 wm_state->sr.cursor = USHRT_MAX;
1279 wm_state->sr.plane = USHRT_MAX;
1280 wm_state->sr.fbc = USHRT_MAX;
1281 }
1282
1283 if (level <= G4X_WM_LEVEL_HPLL) {
1284 wm_state->hpll_en = false;
1285 wm_state->hpll.cursor = USHRT_MAX;
1286 wm_state->hpll.plane = USHRT_MAX;
1287 wm_state->hpll.fbc = USHRT_MAX;
1288 }
1289}
1290
1291static int g4x_compute_pipe_wm(struct intel_crtc_state *crtc_state)
1292{
1293 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1294 struct intel_atomic_state *state =
1295 to_intel_atomic_state(crtc_state->base.state);
1296 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
1297 int num_active_planes = hweight32(crtc_state->active_planes &
1298 ~BIT(PLANE_CURSOR));
1299 const struct g4x_pipe_wm *raw;
1300 struct intel_plane_state *plane_state;
1301 struct intel_plane *plane;
1302 enum plane_id plane_id;
1303 int i, level;
1304 unsigned int dirty = 0;
1305
1306 for_each_intel_plane_in_state(state, plane, plane_state, i) {
1307 const struct intel_plane_state *old_plane_state =
1308 to_intel_plane_state(plane->base.state);
1309
1310 if (plane_state->base.crtc != &crtc->base &&
1311 old_plane_state->base.crtc != &crtc->base)
1312 continue;
1313
1314 if (g4x_raw_plane_wm_compute(crtc_state, plane_state))
1315 dirty |= BIT(plane->id);
1316 }
1317
1318 if (!dirty)
1319 return 0;
1320
1321 level = G4X_WM_LEVEL_NORMAL;
1322 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1323 goto out;
1324
1325 raw = &crtc_state->wm.g4x.raw[level];
1326 for_each_plane_id_on_crtc(crtc, plane_id)
1327 wm_state->wm.plane[plane_id] = raw->plane[plane_id];
1328
1329 level = G4X_WM_LEVEL_SR;
1330
1331 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1332 goto out;
1333
1334 raw = &crtc_state->wm.g4x.raw[level];
1335 wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
1336 wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
1337 wm_state->sr.fbc = raw->fbc;
1338
1339 wm_state->cxsr = num_active_planes == BIT(PLANE_PRIMARY);
1340
1341 level = G4X_WM_LEVEL_HPLL;
1342
1343 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1344 goto out;
1345
1346 raw = &crtc_state->wm.g4x.raw[level];
1347 wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
1348 wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
1349 wm_state->hpll.fbc = raw->fbc;
1350
1351 wm_state->hpll_en = wm_state->cxsr;
1352
1353 level++;
1354
1355 out:
1356 if (level == G4X_WM_LEVEL_NORMAL)
1357 return -EINVAL;
1358
1359 /* invalidate the higher levels */
1360 g4x_invalidate_wms(crtc, wm_state, level);
1361
1362 /*
1363 * Determine if the FBC watermark(s) can be used. IF
1364 * this isn't the case we prefer to disable the FBC
1365 ( watermark(s) rather than disable the SR/HPLL
1366 * level(s) entirely.
1367 */
1368 wm_state->fbc_en = level > G4X_WM_LEVEL_NORMAL;
1369
1370 if (level >= G4X_WM_LEVEL_SR &&
1371 wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
1372 wm_state->fbc_en = false;
1373 else if (level >= G4X_WM_LEVEL_HPLL &&
1374 wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
1375 wm_state->fbc_en = false;
1376
1377 return 0;
1378}
1379
1380static int g4x_compute_intermediate_wm(struct drm_device *dev,
1381 struct intel_crtc *crtc,
1382 struct intel_crtc_state *crtc_state)
1383{
1384 struct g4x_wm_state *intermediate = &crtc_state->wm.g4x.intermediate;
1385 const struct g4x_wm_state *optimal = &crtc_state->wm.g4x.optimal;
1386 const struct g4x_wm_state *active = &crtc->wm.active.g4x;
1387 enum plane_id plane_id;
1388
1389 intermediate->cxsr = optimal->cxsr && active->cxsr &&
1390 !crtc_state->disable_cxsr;
1391 intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
1392 !crtc_state->disable_cxsr;
1393 intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
1394
1395 for_each_plane_id_on_crtc(crtc, plane_id) {
1396 intermediate->wm.plane[plane_id] =
1397 max(optimal->wm.plane[plane_id],
1398 active->wm.plane[plane_id]);
1399
1400 WARN_ON(intermediate->wm.plane[plane_id] >
1401 g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
1402 }
1403
1404 intermediate->sr.plane = max(optimal->sr.plane,
1405 active->sr.plane);
1406 intermediate->sr.cursor = max(optimal->sr.cursor,
1407 active->sr.cursor);
1408 intermediate->sr.fbc = max(optimal->sr.fbc,
1409 active->sr.fbc);
1410
1411 intermediate->hpll.plane = max(optimal->hpll.plane,
1412 active->hpll.plane);
1413 intermediate->hpll.cursor = max(optimal->hpll.cursor,
1414 active->hpll.cursor);
1415 intermediate->hpll.fbc = max(optimal->hpll.fbc,
1416 active->hpll.fbc);
1417
1418 WARN_ON((intermediate->sr.plane >
1419 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
1420 intermediate->sr.cursor >
1421 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
1422 intermediate->cxsr);
1423 WARN_ON((intermediate->sr.plane >
1424 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
1425 intermediate->sr.cursor >
1426 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
1427 intermediate->hpll_en);
1428
1429 WARN_ON(intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
1430 intermediate->fbc_en && intermediate->cxsr);
1431 WARN_ON(intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
1432 intermediate->fbc_en && intermediate->hpll_en);
1433
1434 /*
1435 * If our intermediate WM are identical to the final WM, then we can
1436 * omit the post-vblank programming; only update if it's different.
1437 */
1438 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
1439 crtc_state->wm.need_postvbl_update = true;
1440
1441 return 0;
1442}
1443
1444static void g4x_merge_wm(struct drm_i915_private *dev_priv,
1445 struct g4x_wm_values *wm)
1446{
1447 struct intel_crtc *crtc;
1448 int num_active_crtcs = 0;
1449
1450 wm->cxsr = true;
1451 wm->hpll_en = true;
1452 wm->fbc_en = true;
1453
1454 for_each_intel_crtc(&dev_priv->drm, crtc) {
1455 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1456
1457 if (!crtc->active)
1458 continue;
1459
1460 if (!wm_state->cxsr)
1461 wm->cxsr = false;
1462 if (!wm_state->hpll_en)
1463 wm->hpll_en = false;
1464 if (!wm_state->fbc_en)
1465 wm->fbc_en = false;
1466
1467 num_active_crtcs++;
1468 }
1469
1470 if (num_active_crtcs != 1) {
1471 wm->cxsr = false;
1472 wm->hpll_en = false;
1473 wm->fbc_en = false;
1474 }
1475
1476 for_each_intel_crtc(&dev_priv->drm, crtc) {
1477 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1478 enum pipe pipe = crtc->pipe;
1479
1480 wm->pipe[pipe] = wm_state->wm;
1481 if (crtc->active && wm->cxsr)
1482 wm->sr = wm_state->sr;
1483 if (crtc->active && wm->hpll_en)
1484 wm->hpll = wm_state->hpll;
1485 }
1486}
1487
1488static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
1489{
1490 struct g4x_wm_values *old_wm = &dev_priv->wm.g4x;
1491 struct g4x_wm_values new_wm = {};
1492
1493 g4x_merge_wm(dev_priv, &new_wm);
1494
1495 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
1496 return;
1497
1498 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
1499 _intel_set_memory_cxsr(dev_priv, false);
1500
1501 g4x_write_wm_values(dev_priv, &new_wm);
1502
1503 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
1504 _intel_set_memory_cxsr(dev_priv, true);
1505
1506 *old_wm = new_wm;
1507}
1508
1509static void g4x_initial_watermarks(struct intel_atomic_state *state,
1510 struct intel_crtc_state *crtc_state)
1511{
1512 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1513 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1514
1515 mutex_lock(&dev_priv->wm.wm_mutex);
1516 crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
1517 g4x_program_watermarks(dev_priv);
1518 mutex_unlock(&dev_priv->wm.wm_mutex);
1519}
1520
1521static void g4x_optimize_watermarks(struct intel_atomic_state *state,
1522 struct intel_crtc_state *crtc_state)
1523{
1524 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1525 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
1526
1527 if (!crtc_state->wm.need_postvbl_update)
1528 return;
1529
1530 mutex_lock(&dev_priv->wm.wm_mutex);
1531 intel_crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
1532 g4x_program_watermarks(dev_priv);
1533 mutex_unlock(&dev_priv->wm.wm_mutex);
1534}
1535
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001536/* latency must be in 0.1us units. */
1537static unsigned int vlv_wm_method2(unsigned int pixel_rate,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001538 unsigned int htotal,
1539 unsigned int width,
Ville Syrjäläac484962016-01-20 21:05:26 +02001540 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001541 unsigned int latency)
1542{
1543 unsigned int ret;
1544
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001545 ret = intel_wm_method2(pixel_rate, htotal,
1546 width, cpp, latency);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001547 ret = DIV_ROUND_UP(ret, 64);
1548
1549 return ret;
1550}
1551
Ville Syrjäläbb726512016-10-31 22:37:24 +02001552static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001553{
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001554 /* all latencies in usec */
1555 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1556
Ville Syrjälä58590c12015-09-08 21:05:12 +03001557 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
1558
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001559 if (IS_CHERRYVIEW(dev_priv)) {
1560 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1561 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001562
1563 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001564 }
1565}
1566
Ville Syrjäläe339d672016-11-28 19:37:17 +02001567static uint16_t vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1568 const struct intel_plane_state *plane_state,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001569 int level)
1570{
Ville Syrjäläe339d672016-11-28 19:37:17 +02001571 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001572 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläe339d672016-11-28 19:37:17 +02001573 const struct drm_display_mode *adjusted_mode =
1574 &crtc_state->base.adjusted_mode;
Ville Syrjäläac484962016-01-20 21:05:26 +02001575 int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001576
1577 if (dev_priv->wm.pri_latency[level] == 0)
1578 return USHRT_MAX;
1579
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001580 if (!intel_wm_plane_visible(crtc_state, plane_state))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001581 return 0;
1582
Daniel Vetteref426c12017-01-04 11:41:10 +01001583 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjäläe339d672016-11-28 19:37:17 +02001584 clock = adjusted_mode->crtc_clock;
1585 htotal = adjusted_mode->crtc_htotal;
1586 width = crtc_state->pipe_src_w;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001587
Ville Syrjälä709f3fc2017-03-03 17:19:26 +02001588 if (plane->id == PLANE_CURSOR) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001589 /*
1590 * FIXME the formula gives values that are
1591 * too big for the cursor FIFO, and hence we
1592 * would never be able to use cursors. For
1593 * now just hardcode the watermark.
1594 */
1595 wm = 63;
1596 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +02001597 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001598 dev_priv->wm.pri_latency[level] * 10);
1599 }
1600
1601 return min_t(int, wm, USHRT_MAX);
1602}
1603
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001604static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1605{
1606 return (active_planes & (BIT(PLANE_SPRITE0) |
1607 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1608}
1609
Ville Syrjälä5012e602017-03-02 19:14:56 +02001610static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001611{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001612 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001613 const struct g4x_pipe_wm *raw =
Ville Syrjälä5012e602017-03-02 19:14:56 +02001614 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001615 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001616 unsigned int active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1617 int num_active_planes = hweight32(active_planes);
1618 const int fifo_size = 511;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001619 int fifo_extra, fifo_left = fifo_size;
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001620 int sprite0_fifo_extra = 0;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001621 unsigned int total_rate;
1622 enum plane_id plane_id;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001623
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001624 /*
1625 * When enabling sprite0 after sprite1 has already been enabled
1626 * we tend to get an underrun unless sprite0 already has some
1627 * FIFO space allcoated. Hence we always allocate at least one
1628 * cacheline for sprite0 whenever sprite1 is enabled.
1629 *
1630 * All other plane enable sequences appear immune to this problem.
1631 */
1632 if (vlv_need_sprite0_fifo_workaround(active_planes))
1633 sprite0_fifo_extra = 1;
1634
Ville Syrjälä5012e602017-03-02 19:14:56 +02001635 total_rate = raw->plane[PLANE_PRIMARY] +
1636 raw->plane[PLANE_SPRITE0] +
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001637 raw->plane[PLANE_SPRITE1] +
1638 sprite0_fifo_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001639
Ville Syrjälä5012e602017-03-02 19:14:56 +02001640 if (total_rate > fifo_size)
1641 return -EINVAL;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001642
Ville Syrjälä5012e602017-03-02 19:14:56 +02001643 if (total_rate == 0)
1644 total_rate = 1;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001645
Ville Syrjälä5012e602017-03-02 19:14:56 +02001646 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001647 unsigned int rate;
1648
Ville Syrjälä5012e602017-03-02 19:14:56 +02001649 if ((active_planes & BIT(plane_id)) == 0) {
1650 fifo_state->plane[plane_id] = 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001651 continue;
1652 }
1653
Ville Syrjälä5012e602017-03-02 19:14:56 +02001654 rate = raw->plane[plane_id];
1655 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1656 fifo_left -= fifo_state->plane[plane_id];
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001657 }
1658
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001659 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1660 fifo_left -= sprite0_fifo_extra;
1661
Ville Syrjälä5012e602017-03-02 19:14:56 +02001662 fifo_state->plane[PLANE_CURSOR] = 63;
1663
1664 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001665
1666 /* spread the remainder evenly */
Ville Syrjälä5012e602017-03-02 19:14:56 +02001667 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001668 int plane_extra;
1669
1670 if (fifo_left == 0)
1671 break;
1672
Ville Syrjälä5012e602017-03-02 19:14:56 +02001673 if ((active_planes & BIT(plane_id)) == 0)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001674 continue;
1675
1676 plane_extra = min(fifo_extra, fifo_left);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001677 fifo_state->plane[plane_id] += plane_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001678 fifo_left -= plane_extra;
1679 }
1680
Ville Syrjälä5012e602017-03-02 19:14:56 +02001681 WARN_ON(active_planes != 0 && fifo_left != 0);
1682
1683 /* give it all to the first plane if none are active */
1684 if (active_planes == 0) {
1685 WARN_ON(fifo_left != fifo_size);
1686 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1687 }
1688
1689 return 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001690}
1691
Ville Syrjäläff32c542017-03-02 19:14:57 +02001692/* mark all levels starting from 'level' as invalid */
1693static void vlv_invalidate_wms(struct intel_crtc *crtc,
1694 struct vlv_wm_state *wm_state, int level)
1695{
1696 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1697
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001698 for (; level < intel_wm_num_levels(dev_priv); level++) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02001699 enum plane_id plane_id;
1700
1701 for_each_plane_id_on_crtc(crtc, plane_id)
1702 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1703
1704 wm_state->sr[level].cursor = USHRT_MAX;
1705 wm_state->sr[level].plane = USHRT_MAX;
1706 }
1707}
1708
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001709static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1710{
1711 if (wm > fifo_size)
1712 return USHRT_MAX;
1713 else
1714 return fifo_size - wm;
1715}
1716
Ville Syrjäläff32c542017-03-02 19:14:57 +02001717/*
1718 * Starting from 'level' set all higher
1719 * levels to 'value' in the "raw" watermarks.
1720 */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001721static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001722 int level, enum plane_id plane_id, u16 value)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001723{
Ville Syrjäläff32c542017-03-02 19:14:57 +02001724 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001725 int num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001726 bool dirty = false;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001727
Ville Syrjäläff32c542017-03-02 19:14:57 +02001728 for (; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001729 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001730
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001731 dirty |= raw->plane[plane_id] != value;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001732 raw->plane[plane_id] = value;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001733 }
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001734
1735 return dirty;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001736}
1737
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001738static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1739 const struct intel_plane_state *plane_state)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001740{
1741 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1742 enum plane_id plane_id = plane->id;
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001743 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
Ville Syrjäläff32c542017-03-02 19:14:57 +02001744 int level;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001745 bool dirty = false;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001746
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001747 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001748 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1749 goto out;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001750 }
1751
1752 for (level = 0; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001753 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001754 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1755 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1756
Ville Syrjäläff32c542017-03-02 19:14:57 +02001757 if (wm > max_wm)
1758 break;
1759
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001760 dirty |= raw->plane[plane_id] != wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001761 raw->plane[plane_id] = wm;
1762 }
1763
1764 /* mark all higher levels as invalid */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001765 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001766
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001767out:
1768 if (dirty)
Ville Syrjälä57a65282017-04-21 21:14:22 +03001769 DRM_DEBUG_KMS("%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001770 plane->base.name,
1771 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1772 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1773 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
1774
1775 return dirty;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001776}
1777
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001778static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1779 enum plane_id plane_id, int level)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001780{
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001781 const struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02001782 &crtc_state->wm.vlv.raw[level];
1783 const struct vlv_fifo_state *fifo_state =
1784 &crtc_state->wm.vlv.fifo_state;
1785
1786 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1787}
1788
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001789static 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 +02001790{
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001791 return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1792 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1793 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1794 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001795}
1796
1797static int vlv_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001798{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001799 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001800 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001801 struct intel_atomic_state *state =
1802 to_intel_atomic_state(crtc_state->base.state);
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001803 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001804 const struct vlv_fifo_state *fifo_state =
1805 &crtc_state->wm.vlv.fifo_state;
1806 int num_active_planes = hweight32(crtc_state->active_planes &
1807 ~BIT(PLANE_CURSOR));
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001808 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->base);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001809 struct intel_plane_state *plane_state;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001810 struct intel_plane *plane;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001811 enum plane_id plane_id;
1812 int level, ret, i;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001813 unsigned int dirty = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001814
Ville Syrjäläff32c542017-03-02 19:14:57 +02001815 for_each_intel_plane_in_state(state, plane, plane_state, i) {
1816 const struct intel_plane_state *old_plane_state =
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001817 to_intel_plane_state(plane->base.state);
1818
Ville Syrjäläff32c542017-03-02 19:14:57 +02001819 if (plane_state->base.crtc != &crtc->base &&
1820 old_plane_state->base.crtc != &crtc->base)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001821 continue;
1822
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001823 if (vlv_raw_plane_wm_compute(crtc_state, plane_state))
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001824 dirty |= BIT(plane->id);
1825 }
1826
1827 /*
1828 * DSPARB registers may have been reset due to the
1829 * power well being turned off. Make sure we restore
1830 * them to a consistent state even if no primary/sprite
1831 * planes are initially active.
1832 */
1833 if (needs_modeset)
1834 crtc_state->fifo_changed = true;
1835
1836 if (!dirty)
1837 return 0;
1838
1839 /* cursor changes don't warrant a FIFO recompute */
1840 if (dirty & ~BIT(PLANE_CURSOR)) {
1841 const struct intel_crtc_state *old_crtc_state =
1842 to_intel_crtc_state(crtc->base.state);
1843 const struct vlv_fifo_state *old_fifo_state =
1844 &old_crtc_state->wm.vlv.fifo_state;
1845
1846 ret = vlv_compute_fifo(crtc_state);
1847 if (ret)
1848 return ret;
1849
1850 if (needs_modeset ||
1851 memcmp(old_fifo_state, fifo_state,
1852 sizeof(*fifo_state)) != 0)
1853 crtc_state->fifo_changed = true;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001854 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001855
Ville Syrjäläff32c542017-03-02 19:14:57 +02001856 /* initially allow all levels */
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001857 wm_state->num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001858 /*
1859 * Note that enabling cxsr with no primary/sprite planes
1860 * enabled can wedge the pipe. Hence we only allow cxsr
1861 * with exactly one enabled primary/sprite plane.
1862 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001863 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001864
Ville Syrjälä5012e602017-03-02 19:14:56 +02001865 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001866 const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001867 const int sr_fifo_size = INTEL_INFO(dev_priv)->num_pipes * 512 - 1;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001868
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001869 if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
Ville Syrjäläff32c542017-03-02 19:14:57 +02001870 break;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001871
Ville Syrjäläff32c542017-03-02 19:14:57 +02001872 for_each_plane_id_on_crtc(crtc, plane_id) {
1873 wm_state->wm[level].plane[plane_id] =
1874 vlv_invert_wm_value(raw->plane[plane_id],
1875 fifo_state->plane[plane_id]);
1876 }
1877
1878 wm_state->sr[level].plane =
1879 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
Ville Syrjälä5012e602017-03-02 19:14:56 +02001880 raw->plane[PLANE_SPRITE0],
Ville Syrjäläff32c542017-03-02 19:14:57 +02001881 raw->plane[PLANE_SPRITE1]),
1882 sr_fifo_size);
1883
1884 wm_state->sr[level].cursor =
1885 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1886 63);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001887 }
1888
Ville Syrjäläff32c542017-03-02 19:14:57 +02001889 if (level == 0)
1890 return -EINVAL;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001891
Ville Syrjäläff32c542017-03-02 19:14:57 +02001892 /* limit to only levels we can actually handle */
1893 wm_state->num_levels = level;
1894
1895 /* invalidate the higher levels */
1896 vlv_invalidate_wms(crtc, wm_state, level);
1897
1898 return 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001899}
1900
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001901#define VLV_FIFO(plane, value) \
1902 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1903
Ville Syrjäläff32c542017-03-02 19:14:57 +02001904static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
1905 struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001906{
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001907 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001908 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001909 const struct vlv_fifo_state *fifo_state =
1910 &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001911 int sprite0_start, sprite1_start, fifo_size;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001912
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001913 if (!crtc_state->fifo_changed)
1914 return;
1915
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001916 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
1917 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
1918 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001919
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001920 WARN_ON(fifo_state->plane[PLANE_CURSOR] != 63);
1921 WARN_ON(fifo_size != 511);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001922
Ville Syrjäläc137d662017-03-02 19:15:06 +02001923 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
1924
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001925 /*
1926 * uncore.lock serves a double purpose here. It allows us to
1927 * use the less expensive I915_{READ,WRITE}_FW() functions, and
1928 * it protects the DSPARB registers from getting clobbered by
1929 * parallel updates from multiple pipes.
1930 *
1931 * intel_pipe_update_start() has already disabled interrupts
1932 * for us, so a plain spin_lock() is sufficient here.
1933 */
1934 spin_lock(&dev_priv->uncore.lock);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001935
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001936 switch (crtc->pipe) {
1937 uint32_t dsparb, dsparb2, dsparb3;
1938 case PIPE_A:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001939 dsparb = I915_READ_FW(DSPARB);
1940 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001941
1942 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1943 VLV_FIFO(SPRITEB, 0xff));
1944 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1945 VLV_FIFO(SPRITEB, sprite1_start));
1946
1947 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1948 VLV_FIFO(SPRITEB_HI, 0x1));
1949 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1950 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1951
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001952 I915_WRITE_FW(DSPARB, dsparb);
1953 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001954 break;
1955 case PIPE_B:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001956 dsparb = I915_READ_FW(DSPARB);
1957 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001958
1959 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1960 VLV_FIFO(SPRITED, 0xff));
1961 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1962 VLV_FIFO(SPRITED, sprite1_start));
1963
1964 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
1965 VLV_FIFO(SPRITED_HI, 0xff));
1966 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
1967 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
1968
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001969 I915_WRITE_FW(DSPARB, dsparb);
1970 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001971 break;
1972 case PIPE_C:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001973 dsparb3 = I915_READ_FW(DSPARB3);
1974 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001975
1976 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
1977 VLV_FIFO(SPRITEF, 0xff));
1978 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
1979 VLV_FIFO(SPRITEF, sprite1_start));
1980
1981 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
1982 VLV_FIFO(SPRITEF_HI, 0xff));
1983 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
1984 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
1985
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001986 I915_WRITE_FW(DSPARB3, dsparb3);
1987 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001988 break;
1989 default:
1990 break;
1991 }
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001992
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001993 POSTING_READ_FW(DSPARB);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001994
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001995 spin_unlock(&dev_priv->uncore.lock);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001996}
1997
1998#undef VLV_FIFO
1999
Ville Syrjälä4841da52017-03-02 19:14:59 +02002000static int vlv_compute_intermediate_wm(struct drm_device *dev,
2001 struct intel_crtc *crtc,
2002 struct intel_crtc_state *crtc_state)
2003{
2004 struct vlv_wm_state *intermediate = &crtc_state->wm.vlv.intermediate;
2005 const struct vlv_wm_state *optimal = &crtc_state->wm.vlv.optimal;
2006 const struct vlv_wm_state *active = &crtc->wm.active.vlv;
2007 int level;
2008
2009 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002010 intermediate->cxsr = optimal->cxsr && active->cxsr &&
2011 !crtc_state->disable_cxsr;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002012
2013 for (level = 0; level < intermediate->num_levels; level++) {
2014 enum plane_id plane_id;
2015
2016 for_each_plane_id_on_crtc(crtc, plane_id) {
2017 intermediate->wm[level].plane[plane_id] =
2018 min(optimal->wm[level].plane[plane_id],
2019 active->wm[level].plane[plane_id]);
2020 }
2021
2022 intermediate->sr[level].plane = min(optimal->sr[level].plane,
2023 active->sr[level].plane);
2024 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
2025 active->sr[level].cursor);
2026 }
2027
2028 vlv_invalidate_wms(crtc, intermediate, level);
2029
2030 /*
2031 * If our intermediate WM are identical to the final WM, then we can
2032 * omit the post-vblank programming; only update if it's different.
2033 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002034 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
2035 crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002036
2037 return 0;
2038}
2039
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002040static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002041 struct vlv_wm_values *wm)
2042{
2043 struct intel_crtc *crtc;
2044 int num_active_crtcs = 0;
2045
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002046 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002047 wm->cxsr = true;
2048
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002049 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002050 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002051
2052 if (!crtc->active)
2053 continue;
2054
2055 if (!wm_state->cxsr)
2056 wm->cxsr = false;
2057
2058 num_active_crtcs++;
2059 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
2060 }
2061
2062 if (num_active_crtcs != 1)
2063 wm->cxsr = false;
2064
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03002065 if (num_active_crtcs > 1)
2066 wm->level = VLV_WM_LEVEL_PM2;
2067
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002068 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002069 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002070 enum pipe pipe = crtc->pipe;
2071
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002072 wm->pipe[pipe] = wm_state->wm[wm->level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02002073 if (crtc->active && wm->cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002074 wm->sr = wm_state->sr[wm->level];
2075
Ville Syrjälä1b313892016-11-28 19:37:08 +02002076 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
2077 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
2078 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
2079 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002080 }
2081}
2082
Ville Syrjäläff32c542017-03-02 19:14:57 +02002083static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002084{
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002085 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
2086 struct vlv_wm_values new_wm = {};
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002087
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002088 vlv_merge_wm(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002089
Ville Syrjäläff32c542017-03-02 19:14:57 +02002090 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002091 return;
2092
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002093 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002094 chv_set_memory_dvfs(dev_priv, false);
2095
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002096 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002097 chv_set_memory_pm5(dev_priv, false);
2098
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002099 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002100 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002101
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002102 vlv_write_wm_values(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002103
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002104 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002105 _intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002106
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002107 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002108 chv_set_memory_pm5(dev_priv, true);
2109
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002110 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002111 chv_set_memory_dvfs(dev_priv, true);
2112
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002113 *old_wm = new_wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03002114}
2115
Ville Syrjäläff32c542017-03-02 19:14:57 +02002116static void vlv_initial_watermarks(struct intel_atomic_state *state,
2117 struct intel_crtc_state *crtc_state)
2118{
2119 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2120 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
2121
2122 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002123 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
2124 vlv_program_watermarks(dev_priv);
2125 mutex_unlock(&dev_priv->wm.wm_mutex);
2126}
2127
2128static void vlv_optimize_watermarks(struct intel_atomic_state *state,
2129 struct intel_crtc_state *crtc_state)
2130{
2131 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2132 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2133
2134 if (!crtc_state->wm.need_postvbl_update)
2135 return;
2136
2137 mutex_lock(&dev_priv->wm.wm_mutex);
2138 intel_crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02002139 vlv_program_watermarks(dev_priv);
2140 mutex_unlock(&dev_priv->wm.wm_mutex);
2141}
2142
Ville Syrjälä432081b2016-10-31 22:37:03 +02002143static void i965_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002144{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002145 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002146 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002147 int srwm = 1;
2148 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03002149 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002150
2151 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002152 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002153 if (crtc) {
2154 /* self-refresh has much higher latency */
2155 static const int sr_latency_ns = 12000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002156 const struct drm_display_mode *adjusted_mode =
2157 &crtc->config->base.adjusted_mode;
2158 const struct drm_framebuffer *fb =
2159 crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002160 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002161 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002162 int hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002163 int cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002164 int entries;
2165
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002166 entries = intel_wm_method2(clock, htotal,
2167 hdisplay, cpp, sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002168 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
2169 srwm = I965_FIFO_SIZE - entries;
2170 if (srwm < 0)
2171 srwm = 1;
2172 srwm &= 0x1ff;
2173 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
2174 entries, srwm);
2175
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002176 entries = intel_wm_method2(clock, htotal,
2177 crtc->base.cursor->state->crtc_w, 4,
2178 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002179 entries = DIV_ROUND_UP(entries,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002180 i965_cursor_wm_info.cacheline_size) +
2181 i965_cursor_wm_info.guard_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002182
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002183 cursor_sr = i965_cursor_wm_info.fifo_size - entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002184 if (cursor_sr > i965_cursor_wm_info.max_wm)
2185 cursor_sr = i965_cursor_wm_info.max_wm;
2186
2187 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
2188 "cursor %d\n", srwm, cursor_sr);
2189
Imre Deak98584252014-06-13 14:54:20 +03002190 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002191 } else {
Imre Deak98584252014-06-13 14:54:20 +03002192 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002193 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03002194 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002195 }
2196
2197 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2198 srwm);
2199
2200 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002201 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
2202 FW_WM(8, CURSORB) |
2203 FW_WM(8, PLANEB) |
2204 FW_WM(8, PLANEA));
2205 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
2206 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002207 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002208 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03002209
2210 if (cxsr_enabled)
2211 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002212}
2213
Ville Syrjäläf4998962015-03-10 17:02:21 +02002214#undef FW_WM
2215
Ville Syrjälä432081b2016-10-31 22:37:03 +02002216static void i9xx_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002217{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002218 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002219 const struct intel_watermark_params *wm_info;
2220 uint32_t fwater_lo;
2221 uint32_t fwater_hi;
2222 int cwm, srwm = 1;
2223 int fifo_size;
2224 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002225 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002226
Ville Syrjäläa9097be2016-10-31 22:37:20 +02002227 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002228 wm_info = &i945_wm_info;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002229 else if (!IS_GEN2(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002230 wm_info = &i915_wm_info;
2231 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03002232 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002233
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02002234 fifo_size = dev_priv->display.get_fifo_size(dev_priv, 0);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02002235 crtc = intel_get_crtc_for_plane(dev_priv, 0);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002236 if (intel_crtc_active(crtc)) {
2237 const struct drm_display_mode *adjusted_mode =
2238 &crtc->config->base.adjusted_mode;
2239 const struct drm_framebuffer *fb =
2240 crtc->base.primary->state->fb;
2241 int cpp;
2242
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002243 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002244 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002245 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002246 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002247
Damien Lespiau241bfc32013-09-25 16:45:37 +01002248 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002249 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002250 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002251 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002252 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002253 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002254 if (planea_wm > (long)wm_info->max_wm)
2255 planea_wm = wm_info->max_wm;
2256 }
2257
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002258 if (IS_GEN2(dev_priv))
Ville Syrjälä9d539102014-08-15 01:21:53 +03002259 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002260
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02002261 fifo_size = dev_priv->display.get_fifo_size(dev_priv, 1);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02002262 crtc = intel_get_crtc_for_plane(dev_priv, 1);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002263 if (intel_crtc_active(crtc)) {
2264 const struct drm_display_mode *adjusted_mode =
2265 &crtc->config->base.adjusted_mode;
2266 const struct drm_framebuffer *fb =
2267 crtc->base.primary->state->fb;
2268 int cpp;
2269
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002270 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002271 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002272 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002273 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002274
Damien Lespiau241bfc32013-09-25 16:45:37 +01002275 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002276 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002277 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002278 if (enabled == NULL)
2279 enabled = crtc;
2280 else
2281 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002282 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002283 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002284 if (planeb_wm > (long)wm_info->max_wm)
2285 planeb_wm = wm_info->max_wm;
2286 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002287
2288 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
2289
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002290 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07002291 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002292
Ville Syrjäläefc26112016-10-31 22:37:04 +02002293 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002294
2295 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01002296 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002297 enabled = NULL;
2298 }
2299
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002300 /*
2301 * Overlay gets an aggressive default since video jitter is bad.
2302 */
2303 cwm = 2;
2304
2305 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03002306 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002307
2308 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02002309 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002310 /* self-refresh has much higher latency */
2311 static const int sr_latency_ns = 6000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002312 const struct drm_display_mode *adjusted_mode =
2313 &enabled->config->base.adjusted_mode;
2314 const struct drm_framebuffer *fb =
2315 enabled->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002316 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002317 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002318 int hdisplay = enabled->config->pipe_src_w;
2319 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002320 int entries;
2321
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002322 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002323 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002324 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002325 cpp = fb->format->cpp[0];
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002326
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002327 entries = intel_wm_method2(clock, htotal, hdisplay, cpp,
2328 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002329 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
2330 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
2331 srwm = wm_info->fifo_size - entries;
2332 if (srwm < 0)
2333 srwm = 1;
2334
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002335 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002336 I915_WRITE(FW_BLC_SELF,
2337 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03002338 else
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002339 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
2340 }
2341
2342 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
2343 planea_wm, planeb_wm, cwm, srwm);
2344
2345 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2346 fwater_hi = (cwm & 0x1f);
2347
2348 /* Set request length to 8 cachelines per fetch */
2349 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2350 fwater_hi = fwater_hi | (1 << 8);
2351
2352 I915_WRITE(FW_BLC, fwater_lo);
2353 I915_WRITE(FW_BLC2, fwater_hi);
2354
Imre Deak5209b1f2014-07-01 12:36:17 +03002355 if (enabled)
2356 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002357}
2358
Ville Syrjälä432081b2016-10-31 22:37:03 +02002359static void i845_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002360{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002361 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002362 struct intel_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002363 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002364 uint32_t fwater_lo;
2365 int planea_wm;
2366
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002367 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002368 if (crtc == NULL)
2369 return;
2370
Ville Syrjäläefc26112016-10-31 22:37:04 +02002371 adjusted_mode = &crtc->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002372 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02002373 &i845_wm_info,
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02002374 dev_priv->display.get_fifo_size(dev_priv, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01002375 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002376 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
2377 fwater_lo |= (3<<8) | planea_wm;
2378
2379 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
2380
2381 I915_WRITE(FW_BLC, fwater_lo);
2382}
2383
Ville Syrjälä37126462013-08-01 16:18:55 +03002384/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002385static unsigned int ilk_wm_method1(unsigned int pixel_rate,
2386 unsigned int cpp,
2387 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002388{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002389 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002390
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002391 ret = intel_wm_method1(pixel_rate, cpp, latency);
2392 ret = DIV_ROUND_UP(ret, 64) + 2;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002393
2394 return ret;
2395}
2396
Ville Syrjälä37126462013-08-01 16:18:55 +03002397/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002398static unsigned int ilk_wm_method2(unsigned int pixel_rate,
2399 unsigned int htotal,
2400 unsigned int width,
2401 unsigned int cpp,
2402 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002403{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002404 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002405
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002406 ret = intel_wm_method2(pixel_rate, htotal,
2407 width, cpp, latency);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002408 ret = DIV_ROUND_UP(ret, 64) + 2;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002409
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002410 return ret;
2411}
2412
Ville Syrjälä23297042013-07-05 11:57:17 +03002413static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Ville Syrjäläac484962016-01-20 21:05:26 +02002414 uint8_t cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002415{
Matt Roper15126882015-12-03 11:37:40 -08002416 /*
2417 * Neither of these should be possible since this function shouldn't be
2418 * called if the CRTC is off or the plane is invisible. But let's be
2419 * extra paranoid to avoid a potential divide-by-zero if we screw up
2420 * elsewhere in the driver.
2421 */
Ville Syrjäläac484962016-01-20 21:05:26 +02002422 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08002423 return 0;
2424 if (WARN_ON(!horiz_pixels))
2425 return 0;
2426
Ville Syrjäläac484962016-01-20 21:05:26 +02002427 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002428}
2429
Imre Deak820c1982013-12-17 14:46:36 +02002430struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002431 uint16_t pri;
2432 uint16_t spr;
2433 uint16_t cur;
2434 uint16_t fbc;
2435};
2436
Ville Syrjälä37126462013-08-01 16:18:55 +03002437/*
2438 * For both WM_PIPE and WM_LP.
2439 * mem_value must be in 0.1us units.
2440 */
Matt Roper7221fc32015-09-24 15:53:08 -07002441static uint32_t ilk_compute_pri_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002442 const struct intel_plane_state *pstate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03002443 uint32_t mem_value,
2444 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002445{
Paulo Zanonicca32e92013-05-31 11:45:06 -03002446 uint32_t method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002447 int cpp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002448
Ville Syrjälä24304d812017-03-14 17:10:49 +02002449 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002450 return 0;
2451
Ville Syrjälä353c8592016-12-14 23:30:57 +02002452 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002453
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002454 method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002455
2456 if (!is_lp)
2457 return method1;
2458
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002459 method2 = ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002460 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002461 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002462 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002463
2464 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002465}
2466
Ville Syrjälä37126462013-08-01 16:18:55 +03002467/*
2468 * For both WM_PIPE and WM_LP.
2469 * mem_value must be in 0.1us units.
2470 */
Matt Roper7221fc32015-09-24 15:53:08 -07002471static uint32_t ilk_compute_spr_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002472 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002473 uint32_t mem_value)
2474{
2475 uint32_t method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002476 int cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002477
Ville Syrjälä24304d812017-03-14 17:10:49 +02002478 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002479 return 0;
2480
Ville Syrjälä353c8592016-12-14 23:30:57 +02002481 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002482
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002483 method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
2484 method2 = ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002485 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002486 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002487 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002488 return min(method1, method2);
2489}
2490
Ville Syrjälä37126462013-08-01 16:18:55 +03002491/*
2492 * For both WM_PIPE and WM_LP.
2493 * mem_value must be in 0.1us units.
2494 */
Matt Roper7221fc32015-09-24 15:53:08 -07002495static uint32_t ilk_compute_cur_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002496 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002497 uint32_t mem_value)
2498{
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002499 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002500
Ville Syrjälä24304d812017-03-14 17:10:49 +02002501 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002502 return 0;
2503
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002504 cpp = pstate->base.fb->format->cpp[0];
2505
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002506 return ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002507 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002508 pstate->base.crtc_w, cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002509}
2510
Paulo Zanonicca32e92013-05-31 11:45:06 -03002511/* Only for WM_LP. */
Matt Roper7221fc32015-09-24 15:53:08 -07002512static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002513 const struct intel_plane_state *pstate,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03002514 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002515{
Ville Syrjälä83054942016-11-18 21:53:00 +02002516 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002517
Ville Syrjälä24304d812017-03-14 17:10:49 +02002518 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanonicca32e92013-05-31 11:45:06 -03002519 return 0;
2520
Ville Syrjälä353c8592016-12-14 23:30:57 +02002521 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002522
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002523 return ilk_wm_fbc(pri_val, drm_rect_width(&pstate->base.dst), cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002524}
2525
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002526static unsigned int
2527ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002528{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002529 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002530 return 3072;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002531 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002532 return 768;
2533 else
2534 return 512;
2535}
2536
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002537static unsigned int
2538ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2539 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002540{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002541 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002542 /* BDW primary/sprite plane watermarks */
2543 return level == 0 ? 255 : 2047;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002544 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002545 /* IVB/HSW primary/sprite plane watermarks */
2546 return level == 0 ? 127 : 1023;
2547 else if (!is_sprite)
2548 /* ILK/SNB primary plane watermarks */
2549 return level == 0 ? 127 : 511;
2550 else
2551 /* ILK/SNB sprite plane watermarks */
2552 return level == 0 ? 63 : 255;
2553}
2554
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002555static unsigned int
2556ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002557{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002558 if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002559 return level == 0 ? 63 : 255;
2560 else
2561 return level == 0 ? 31 : 63;
2562}
2563
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002564static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002565{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002566 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002567 return 31;
2568 else
2569 return 15;
2570}
2571
Ville Syrjälä158ae642013-08-07 13:28:19 +03002572/* Calculate the maximum primary/sprite plane watermark */
2573static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
2574 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002575 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002576 enum intel_ddb_partitioning ddb_partitioning,
2577 bool is_sprite)
2578{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002579 struct drm_i915_private *dev_priv = to_i915(dev);
2580 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002581
2582 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002583 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002584 return 0;
2585
2586 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002587 if (level == 0 || config->num_pipes_active > 1) {
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002588 fifo_size /= INTEL_INFO(dev_priv)->num_pipes;
Ville Syrjälä158ae642013-08-07 13:28:19 +03002589
2590 /*
2591 * For some reason the non self refresh
2592 * FIFO size is only half of the self
2593 * refresh FIFO size on ILK/SNB.
2594 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002595 if (INTEL_GEN(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002596 fifo_size /= 2;
2597 }
2598
Ville Syrjälä240264f2013-08-07 13:29:12 +03002599 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002600 /* level 0 is always calculated with 1:1 split */
2601 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2602 if (is_sprite)
2603 fifo_size *= 5;
2604 fifo_size /= 6;
2605 } else {
2606 fifo_size /= 2;
2607 }
2608 }
2609
2610 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002611 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002612}
2613
2614/* Calculate the maximum cursor plane watermark */
2615static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002616 int level,
2617 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002618{
2619 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002620 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002621 return 64;
2622
2623 /* otherwise just report max that registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002624 return ilk_cursor_wm_reg_max(to_i915(dev), level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002625}
2626
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002627static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002628 int level,
2629 const struct intel_wm_config *config,
2630 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002631 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002632{
Ville Syrjälä240264f2013-08-07 13:29:12 +03002633 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
2634 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
2635 max->cur = ilk_cursor_wm_max(dev, level, config);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002636 max->fbc = ilk_fbc_wm_reg_max(to_i915(dev));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002637}
2638
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002639static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002640 int level,
2641 struct ilk_wm_maximums *max)
2642{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002643 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2644 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2645 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2646 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002647}
2648
Ville Syrjäläd9395652013-10-09 19:18:10 +03002649static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002650 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002651 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002652{
2653 bool ret;
2654
2655 /* already determined to be invalid? */
2656 if (!result->enable)
2657 return false;
2658
2659 result->enable = result->pri_val <= max->pri &&
2660 result->spr_val <= max->spr &&
2661 result->cur_val <= max->cur;
2662
2663 ret = result->enable;
2664
2665 /*
2666 * HACK until we can pre-compute everything,
2667 * and thus fail gracefully if LP0 watermarks
2668 * are exceeded...
2669 */
2670 if (level == 0 && !result->enable) {
2671 if (result->pri_val > max->pri)
2672 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2673 level, result->pri_val, max->pri);
2674 if (result->spr_val > max->spr)
2675 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2676 level, result->spr_val, max->spr);
2677 if (result->cur_val > max->cur)
2678 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2679 level, result->cur_val, max->cur);
2680
2681 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
2682 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
2683 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
2684 result->enable = true;
2685 }
2686
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002687 return ret;
2688}
2689
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002690static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Matt Roper43d59ed2015-09-24 15:53:07 -07002691 const struct intel_crtc *intel_crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002692 int level,
Matt Roper7221fc32015-09-24 15:53:08 -07002693 struct intel_crtc_state *cstate,
Matt Roper86c8bbb2015-09-24 15:53:16 -07002694 struct intel_plane_state *pristate,
2695 struct intel_plane_state *sprstate,
2696 struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002697 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002698{
2699 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2700 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2701 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2702
2703 /* WM1+ latency values stored in 0.5us units */
2704 if (level > 0) {
2705 pri_latency *= 5;
2706 spr_latency *= 5;
2707 cur_latency *= 5;
2708 }
2709
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002710 if (pristate) {
2711 result->pri_val = ilk_compute_pri_wm(cstate, pristate,
2712 pri_latency, level);
2713 result->fbc_val = ilk_compute_fbc_wm(cstate, pristate, result->pri_val);
2714 }
2715
2716 if (sprstate)
2717 result->spr_val = ilk_compute_spr_wm(cstate, sprstate, spr_latency);
2718
2719 if (curstate)
2720 result->cur_val = ilk_compute_cur_wm(cstate, curstate, cur_latency);
2721
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002722 result->enable = true;
2723}
2724
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002725static uint32_t
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002726hsw_compute_linetime_wm(const struct intel_crtc_state *cstate)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002727{
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002728 const struct intel_atomic_state *intel_state =
2729 to_intel_atomic_state(cstate->base.state);
Matt Roperee91a152015-12-03 11:37:39 -08002730 const struct drm_display_mode *adjusted_mode =
2731 &cstate->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002732 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002733
Matt Roperee91a152015-12-03 11:37:39 -08002734 if (!cstate->base.active)
2735 return 0;
2736 if (WARN_ON(adjusted_mode->crtc_clock == 0))
2737 return 0;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002738 if (WARN_ON(intel_state->cdclk.logical.cdclk == 0))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002739 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002740
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002741 /* The WM are computed with base on how long it takes to fill a single
2742 * row at the given clock rate, multiplied by 8.
2743 * */
Ville Syrjälä124abe02015-09-08 13:40:45 +03002744 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2745 adjusted_mode->crtc_clock);
2746 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002747 intel_state->cdclk.logical.cdclk);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002748
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002749 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2750 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002751}
2752
Ville Syrjäläbb726512016-10-31 22:37:24 +02002753static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
2754 uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002755{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002756 if (IS_GEN9(dev_priv)) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002757 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002758 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002759 int level, max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002760
2761 /* read the first set of memory latencies[0:3] */
2762 val = 0; /* data0 to be programmed to 0 for first set */
2763 mutex_lock(&dev_priv->rps.hw_lock);
2764 ret = sandybridge_pcode_read(dev_priv,
2765 GEN9_PCODE_READ_MEM_LATENCY,
2766 &val);
2767 mutex_unlock(&dev_priv->rps.hw_lock);
2768
2769 if (ret) {
2770 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2771 return;
2772 }
2773
2774 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2775 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2776 GEN9_MEM_LATENCY_LEVEL_MASK;
2777 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2778 GEN9_MEM_LATENCY_LEVEL_MASK;
2779 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2780 GEN9_MEM_LATENCY_LEVEL_MASK;
2781
2782 /* read the second set of memory latencies[4:7] */
2783 val = 1; /* data0 to be programmed to 1 for second set */
2784 mutex_lock(&dev_priv->rps.hw_lock);
2785 ret = sandybridge_pcode_read(dev_priv,
2786 GEN9_PCODE_READ_MEM_LATENCY,
2787 &val);
2788 mutex_unlock(&dev_priv->rps.hw_lock);
2789 if (ret) {
2790 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2791 return;
2792 }
2793
2794 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2795 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2796 GEN9_MEM_LATENCY_LEVEL_MASK;
2797 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2798 GEN9_MEM_LATENCY_LEVEL_MASK;
2799 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2800 GEN9_MEM_LATENCY_LEVEL_MASK;
2801
Vandana Kannan367294b2014-11-04 17:06:46 +00002802 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002803 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2804 * need to be disabled. We make sure to sanitize the values out
2805 * of the punit to satisfy this requirement.
2806 */
2807 for (level = 1; level <= max_level; level++) {
2808 if (wm[level] == 0) {
2809 for (i = level + 1; i <= max_level; i++)
2810 wm[i] = 0;
2811 break;
2812 }
2813 }
2814
2815 /*
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02002816 * WaWmMemoryReadLatency:skl,glk
Damien Lespiau6f972352015-02-09 19:33:07 +00002817 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002818 * punit doesn't take into account the read latency so we need
Paulo Zanoni0727e402016-09-22 18:00:30 -03002819 * to add 2us to the various latency levels we retrieve from the
2820 * punit when level 0 response data us 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002821 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002822 if (wm[0] == 0) {
2823 wm[0] += 2;
2824 for (level = 1; level <= max_level; level++) {
2825 if (wm[level] == 0)
2826 break;
Vandana Kannan367294b2014-11-04 17:06:46 +00002827 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002828 }
Paulo Zanoni0727e402016-09-22 18:00:30 -03002829 }
2830
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002831 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002832 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2833
2834 wm[0] = (sskpd >> 56) & 0xFF;
2835 if (wm[0] == 0)
2836 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002837 wm[1] = (sskpd >> 4) & 0xFF;
2838 wm[2] = (sskpd >> 12) & 0xFF;
2839 wm[3] = (sskpd >> 20) & 0x1FF;
2840 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002841 } else if (INTEL_GEN(dev_priv) >= 6) {
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002842 uint32_t sskpd = I915_READ(MCH_SSKPD);
2843
2844 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2845 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2846 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2847 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002848 } else if (INTEL_GEN(dev_priv) >= 5) {
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002849 uint32_t mltr = I915_READ(MLTR_ILK);
2850
2851 /* ILK primary LP0 latency is 700 ns */
2852 wm[0] = 7;
2853 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2854 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002855 }
2856}
2857
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002858static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
2859 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002860{
2861 /* ILK sprite LP0 latency is 1300 ns */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002862 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002863 wm[0] = 13;
2864}
2865
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002866static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
2867 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002868{
2869 /* ILK cursor LP0 latency is 1300 ns */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002870 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002871 wm[0] = 13;
2872
2873 /* WaDoubleCursorLP3Latency:ivb */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002874 if (IS_IVYBRIDGE(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002875 wm[3] *= 2;
2876}
2877
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002878int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002879{
2880 /* how many WM levels are we expecting */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002881 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002882 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002883 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002884 return 4;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002885 else if (INTEL_GEN(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002886 return 3;
2887 else
2888 return 2;
2889}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002890
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002891static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002892 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002893 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002894{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002895 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002896
2897 for (level = 0; level <= max_level; level++) {
2898 unsigned int latency = wm[level];
2899
2900 if (latency == 0) {
2901 DRM_ERROR("%s WM%d latency not provided\n",
2902 name, level);
2903 continue;
2904 }
2905
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002906 /*
2907 * - latencies are in us on gen9.
2908 * - before then, WM1+ latency values are in 0.5us units
2909 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002910 if (IS_GEN9(dev_priv))
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002911 latency *= 10;
2912 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002913 latency *= 5;
2914
2915 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2916 name, level, wm[level],
2917 latency / 10, latency % 10);
2918 }
2919}
2920
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002921static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2922 uint16_t wm[5], uint16_t min)
2923{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002924 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002925
2926 if (wm[0] >= min)
2927 return false;
2928
2929 wm[0] = max(wm[0], min);
2930 for (level = 1; level <= max_level; level++)
2931 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2932
2933 return true;
2934}
2935
Ville Syrjäläbb726512016-10-31 22:37:24 +02002936static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002937{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002938 bool changed;
2939
2940 /*
2941 * The BIOS provided WM memory latency values are often
2942 * inadequate for high resolution displays. Adjust them.
2943 */
2944 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2945 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2946 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2947
2948 if (!changed)
2949 return;
2950
2951 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002952 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
2953 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
2954 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002955}
2956
Ville Syrjäläbb726512016-10-31 22:37:24 +02002957static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002958{
Ville Syrjäläbb726512016-10-31 22:37:24 +02002959 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002960
2961 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2962 sizeof(dev_priv->wm.pri_latency));
2963 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2964 sizeof(dev_priv->wm.pri_latency));
2965
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002966 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002967 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002968
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002969 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
2970 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
2971 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002972
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002973 if (IS_GEN6(dev_priv))
Ville Syrjäläbb726512016-10-31 22:37:24 +02002974 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002975}
2976
Ville Syrjäläbb726512016-10-31 22:37:24 +02002977static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002978{
Ville Syrjäläbb726512016-10-31 22:37:24 +02002979 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002980 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002981}
2982
Matt Ropered4a6a72016-02-23 17:20:13 -08002983static bool ilk_validate_pipe_wm(struct drm_device *dev,
2984 struct intel_pipe_wm *pipe_wm)
2985{
2986 /* LP0 watermark maximums depend on this pipe alone */
2987 const struct intel_wm_config config = {
2988 .num_pipes_active = 1,
2989 .sprites_enabled = pipe_wm->sprites_enabled,
2990 .sprites_scaled = pipe_wm->sprites_scaled,
2991 };
2992 struct ilk_wm_maximums max;
2993
2994 /* LP0 watermarks always use 1/2 DDB partitioning */
2995 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2996
2997 /* At least LP0 must be valid */
2998 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
2999 DRM_DEBUG_KMS("LP0 watermark invalid\n");
3000 return false;
3001 }
3002
3003 return true;
3004}
3005
Matt Roper261a27d2015-10-08 15:28:25 -07003006/* Compute new watermarks for the pipe */
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003007static int ilk_compute_pipe_wm(struct intel_crtc_state *cstate)
Matt Roper261a27d2015-10-08 15:28:25 -07003008{
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003009 struct drm_atomic_state *state = cstate->base.state;
3010 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07003011 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003012 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003013 const struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper43d59ed2015-09-24 15:53:07 -07003014 struct intel_plane *intel_plane;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003015 struct intel_plane_state *pristate = NULL;
Matt Roper43d59ed2015-09-24 15:53:07 -07003016 struct intel_plane_state *sprstate = NULL;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003017 struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003018 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02003019 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003020
Matt Ropere8f1f022016-05-12 07:05:55 -07003021 pipe_wm = &cstate->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003022
Matt Roper43d59ed2015-09-24 15:53:07 -07003023 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003024 struct intel_plane_state *ps;
3025
3026 ps = intel_atomic_get_existing_plane_state(state,
3027 intel_plane);
3028 if (!ps)
3029 continue;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003030
3031 if (intel_plane->base.type == DRM_PLANE_TYPE_PRIMARY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003032 pristate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003033 else if (intel_plane->base.type == DRM_PLANE_TYPE_OVERLAY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003034 sprstate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003035 else if (intel_plane->base.type == DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003036 curstate = ps;
Matt Roper43d59ed2015-09-24 15:53:07 -07003037 }
3038
Matt Ropered4a6a72016-02-23 17:20:13 -08003039 pipe_wm->pipe_enabled = cstate->base.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003040 if (sprstate) {
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003041 pipe_wm->sprites_enabled = sprstate->base.visible;
3042 pipe_wm->sprites_scaled = sprstate->base.visible &&
3043 (drm_rect_width(&sprstate->base.dst) != drm_rect_width(&sprstate->base.src) >> 16 ||
3044 drm_rect_height(&sprstate->base.dst) != drm_rect_height(&sprstate->base.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003045 }
3046
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003047 usable_level = max_level;
3048
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003049 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003050 if (INTEL_GEN(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003051 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003052
3053 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08003054 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003055 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003056
Matt Roper86c8bbb2015-09-24 15:53:16 -07003057 ilk_compute_wm_level(dev_priv, intel_crtc, 0, cstate,
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003058 pristate, sprstate, curstate, &pipe_wm->raw_wm[0]);
3059
3060 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
3061 pipe_wm->wm[0] = pipe_wm->raw_wm[0];
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003062
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003063 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjälä532f7a72016-04-29 17:31:17 +03003064 pipe_wm->linetime = hsw_compute_linetime_wm(cstate);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003065
Matt Ropered4a6a72016-02-23 17:20:13 -08003066 if (!ilk_validate_pipe_wm(dev, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01003067 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003068
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003069 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003070
3071 for (level = 1; level <= max_level; level++) {
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003072 struct intel_wm_level *wm = &pipe_wm->raw_wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003073
Matt Roper86c8bbb2015-09-24 15:53:16 -07003074 ilk_compute_wm_level(dev_priv, intel_crtc, level, cstate,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003075 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003076
3077 /*
3078 * Disable any watermark level that exceeds the
3079 * register maximums since such watermarks are
3080 * always invalid.
3081 */
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003082 if (level > usable_level)
3083 continue;
3084
3085 if (ilk_validate_wm_level(level, &max, wm))
3086 pipe_wm->wm[level] = *wm;
3087 else
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003088 usable_level = level;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003089 }
3090
Matt Roper86c8bbb2015-09-24 15:53:16 -07003091 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003092}
3093
3094/*
Matt Ropered4a6a72016-02-23 17:20:13 -08003095 * Build a set of 'intermediate' watermark values that satisfy both the old
3096 * state and the new state. These can be programmed to the hardware
3097 * immediately.
3098 */
3099static int ilk_compute_intermediate_wm(struct drm_device *dev,
3100 struct intel_crtc *intel_crtc,
3101 struct intel_crtc_state *newstate)
3102{
Matt Ropere8f1f022016-05-12 07:05:55 -07003103 struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08003104 struct intel_pipe_wm *b = &intel_crtc->wm.active.ilk;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003105 int level, max_level = ilk_wm_max_level(to_i915(dev));
Matt Ropered4a6a72016-02-23 17:20:13 -08003106
3107 /*
3108 * Start with the final, target watermarks, then combine with the
3109 * currently active watermarks to get values that are safe both before
3110 * and after the vblank.
3111 */
Matt Ropere8f1f022016-05-12 07:05:55 -07003112 *a = newstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08003113 a->pipe_enabled |= b->pipe_enabled;
3114 a->sprites_enabled |= b->sprites_enabled;
3115 a->sprites_scaled |= b->sprites_scaled;
3116
3117 for (level = 0; level <= max_level; level++) {
3118 struct intel_wm_level *a_wm = &a->wm[level];
3119 const struct intel_wm_level *b_wm = &b->wm[level];
3120
3121 a_wm->enable &= b_wm->enable;
3122 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
3123 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
3124 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
3125 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
3126 }
3127
3128 /*
3129 * We need to make sure that these merged watermark values are
3130 * actually a valid configuration themselves. If they're not,
3131 * there's no safe way to transition from the old state to
3132 * the new state, so we need to fail the atomic transaction.
3133 */
3134 if (!ilk_validate_pipe_wm(dev, a))
3135 return -EINVAL;
3136
3137 /*
3138 * If our intermediate WM are identical to the final WM, then we can
3139 * omit the post-vblank programming; only update if it's different.
3140 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02003141 if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) != 0)
3142 newstate->wm.need_postvbl_update = true;
Matt Ropered4a6a72016-02-23 17:20:13 -08003143
3144 return 0;
3145}
3146
3147/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003148 * Merge the watermarks from all active pipes for a specific level.
3149 */
3150static void ilk_merge_wm_level(struct drm_device *dev,
3151 int level,
3152 struct intel_wm_level *ret_wm)
3153{
3154 const struct intel_crtc *intel_crtc;
3155
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003156 ret_wm->enable = true;
3157
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003158 for_each_intel_crtc(dev, intel_crtc) {
Matt Ropered4a6a72016-02-23 17:20:13 -08003159 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02003160 const struct intel_wm_level *wm = &active->wm[level];
3161
3162 if (!active->pipe_enabled)
3163 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003164
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003165 /*
3166 * The watermark values may have been used in the past,
3167 * so we must maintain them in the registers for some
3168 * time even if the level is now disabled.
3169 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003170 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003171 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003172
3173 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
3174 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
3175 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
3176 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
3177 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003178}
3179
3180/*
3181 * Merge all low power watermarks for all active pipes.
3182 */
3183static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003184 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02003185 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003186 struct intel_pipe_wm *merged)
3187{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003188 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003189 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003190 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003191
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003192 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003193 if ((INTEL_GEN(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003194 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03003195 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003196
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003197 /* ILK: FBC WM must be disabled always */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003198 merged->fbc_wm_enabled = INTEL_GEN(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003199
3200 /* merge each WM1+ level */
3201 for (level = 1; level <= max_level; level++) {
3202 struct intel_wm_level *wm = &merged->wm[level];
3203
3204 ilk_merge_wm_level(dev, level, wm);
3205
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003206 if (level > last_enabled_level)
3207 wm->enable = false;
3208 else if (!ilk_validate_wm_level(level, max, wm))
3209 /* make sure all following levels get disabled */
3210 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003211
3212 /*
3213 * The spec says it is preferred to disable
3214 * FBC WMs instead of disabling a WM level.
3215 */
3216 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003217 if (wm->enable)
3218 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003219 wm->fbc_val = 0;
3220 }
3221 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003222
3223 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
3224 /*
3225 * FIXME this is racy. FBC might get enabled later.
3226 * What we should check here is whether FBC can be
3227 * enabled sometime later.
3228 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003229 if (IS_GEN5(dev_priv) && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03003230 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003231 for (level = 2; level <= max_level; level++) {
3232 struct intel_wm_level *wm = &merged->wm[level];
3233
3234 wm->enable = false;
3235 }
3236 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003237}
3238
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003239static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
3240{
3241 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
3242 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
3243}
3244
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003245/* The value we need to program into the WM_LPx latency field */
3246static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
3247{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003248 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003249
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003250 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003251 return 2 * level;
3252 else
3253 return dev_priv->wm.pri_latency[level];
3254}
3255
Imre Deak820c1982013-12-17 14:46:36 +02003256static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03003257 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03003258 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02003259 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003260{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003261 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003262 struct intel_crtc *intel_crtc;
3263 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003264
Ville Syrjälä0362c782013-10-09 19:17:57 +03003265 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03003266 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003267
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003268 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03003269 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03003270 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003271
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003272 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003273
Ville Syrjälä0362c782013-10-09 19:17:57 +03003274 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03003275
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003276 /*
3277 * Maintain the watermark values even if the level is
3278 * disabled. Doing otherwise could cause underruns.
3279 */
3280 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003281 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07003282 (r->pri_val << WM1_LP_SR_SHIFT) |
3283 r->cur_val;
3284
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003285 if (r->enable)
3286 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
3287
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003288 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07003289 results->wm_lp[wm_lp - 1] |=
3290 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
3291 else
3292 results->wm_lp[wm_lp - 1] |=
3293 r->fbc_val << WM1_LP_FBC_SHIFT;
3294
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003295 /*
3296 * Always set WM1S_LP_EN when spr_val != 0, even if the
3297 * level is disabled. Doing otherwise could cause underruns.
3298 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003299 if (INTEL_GEN(dev_priv) <= 6 && r->spr_val) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003300 WARN_ON(wm_lp != 1);
3301 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
3302 } else
3303 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003304 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003305
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003306 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003307 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003308 enum pipe pipe = intel_crtc->pipe;
Matt Ropered4a6a72016-02-23 17:20:13 -08003309 const struct intel_wm_level *r =
3310 &intel_crtc->wm.active.ilk.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003311
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003312 if (WARN_ON(!r->enable))
3313 continue;
3314
Matt Ropered4a6a72016-02-23 17:20:13 -08003315 results->wm_linetime[pipe] = intel_crtc->wm.active.ilk.linetime;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003316
3317 results->wm_pipe[pipe] =
3318 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
3319 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
3320 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003321 }
3322}
3323
Paulo Zanoni861f3382013-05-31 10:19:21 -03003324/* Find the result with the highest level enabled. Check for enable_fbc_wm in
3325 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02003326static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003327 struct intel_pipe_wm *r1,
3328 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003329{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003330 int level, max_level = ilk_wm_max_level(to_i915(dev));
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003331 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003332
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003333 for (level = 1; level <= max_level; level++) {
3334 if (r1->wm[level].enable)
3335 level1 = level;
3336 if (r2->wm[level].enable)
3337 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003338 }
3339
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003340 if (level1 == level2) {
3341 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003342 return r2;
3343 else
3344 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003345 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03003346 return r1;
3347 } else {
3348 return r2;
3349 }
3350}
3351
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003352/* dirty bits used to track which watermarks need changes */
3353#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
3354#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
3355#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
3356#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
3357#define WM_DIRTY_FBC (1 << 24)
3358#define WM_DIRTY_DDB (1 << 25)
3359
Damien Lespiau055e3932014-08-18 13:49:10 +01003360static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02003361 const struct ilk_wm_values *old,
3362 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003363{
3364 unsigned int dirty = 0;
3365 enum pipe pipe;
3366 int wm_lp;
3367
Damien Lespiau055e3932014-08-18 13:49:10 +01003368 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003369 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
3370 dirty |= WM_DIRTY_LINETIME(pipe);
3371 /* Must disable LP1+ watermarks too */
3372 dirty |= WM_DIRTY_LP_ALL;
3373 }
3374
3375 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
3376 dirty |= WM_DIRTY_PIPE(pipe);
3377 /* Must disable LP1+ watermarks too */
3378 dirty |= WM_DIRTY_LP_ALL;
3379 }
3380 }
3381
3382 if (old->enable_fbc_wm != new->enable_fbc_wm) {
3383 dirty |= WM_DIRTY_FBC;
3384 /* Must disable LP1+ watermarks too */
3385 dirty |= WM_DIRTY_LP_ALL;
3386 }
3387
3388 if (old->partitioning != new->partitioning) {
3389 dirty |= WM_DIRTY_DDB;
3390 /* Must disable LP1+ watermarks too */
3391 dirty |= WM_DIRTY_LP_ALL;
3392 }
3393
3394 /* LP1+ watermarks already deemed dirty, no need to continue */
3395 if (dirty & WM_DIRTY_LP_ALL)
3396 return dirty;
3397
3398 /* Find the lowest numbered LP1+ watermark in need of an update... */
3399 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3400 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
3401 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
3402 break;
3403 }
3404
3405 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
3406 for (; wm_lp <= 3; wm_lp++)
3407 dirty |= WM_DIRTY_LP(wm_lp);
3408
3409 return dirty;
3410}
3411
Ville Syrjälä8553c182013-12-05 15:51:39 +02003412static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
3413 unsigned int dirty)
3414{
Imre Deak820c1982013-12-17 14:46:36 +02003415 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02003416 bool changed = false;
3417
3418 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
3419 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
3420 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
3421 changed = true;
3422 }
3423 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
3424 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
3425 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
3426 changed = true;
3427 }
3428 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
3429 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
3430 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
3431 changed = true;
3432 }
3433
3434 /*
3435 * Don't touch WM1S_LP_EN here.
3436 * Doing so could cause underruns.
3437 */
3438
3439 return changed;
3440}
3441
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003442/*
3443 * The spec says we shouldn't write when we don't need, because every write
3444 * causes WMs to be re-evaluated, expending some power.
3445 */
Imre Deak820c1982013-12-17 14:46:36 +02003446static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
3447 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003448{
Imre Deak820c1982013-12-17 14:46:36 +02003449 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003450 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003451 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003452
Damien Lespiau055e3932014-08-18 13:49:10 +01003453 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003454 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003455 return;
3456
Ville Syrjälä8553c182013-12-05 15:51:39 +02003457 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003458
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003459 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003460 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003461 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003462 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003463 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003464 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
3465
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003466 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003467 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003468 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003469 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003470 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003471 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
3472
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003473 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003474 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003475 val = I915_READ(WM_MISC);
3476 if (results->partitioning == INTEL_DDB_PART_1_2)
3477 val &= ~WM_MISC_DATA_PARTITION_5_6;
3478 else
3479 val |= WM_MISC_DATA_PARTITION_5_6;
3480 I915_WRITE(WM_MISC, val);
3481 } else {
3482 val = I915_READ(DISP_ARB_CTL2);
3483 if (results->partitioning == INTEL_DDB_PART_1_2)
3484 val &= ~DISP_DATA_PARTITION_5_6;
3485 else
3486 val |= DISP_DATA_PARTITION_5_6;
3487 I915_WRITE(DISP_ARB_CTL2, val);
3488 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003489 }
3490
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003491 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03003492 val = I915_READ(DISP_ARB_CTL);
3493 if (results->enable_fbc_wm)
3494 val &= ~DISP_FBC_WM_DIS;
3495 else
3496 val |= DISP_FBC_WM_DIS;
3497 I915_WRITE(DISP_ARB_CTL, val);
3498 }
3499
Imre Deak954911e2013-12-17 14:46:34 +02003500 if (dirty & WM_DIRTY_LP(1) &&
3501 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
3502 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
3503
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003504 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003505 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
3506 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
3507 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
3508 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
3509 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003510
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003511 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003512 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003513 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003514 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003515 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003516 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003517
3518 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003519}
3520
Matt Ropered4a6a72016-02-23 17:20:13 -08003521bool ilk_disable_lp_wm(struct drm_device *dev)
Ville Syrjälä8553c182013-12-05 15:51:39 +02003522{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003523 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003524
3525 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3526}
3527
Lyude656d1b82016-08-17 15:55:54 -04003528#define SKL_SAGV_BLOCK_TIME 30 /* µs */
Damien Lespiaub9cec072014-11-04 17:06:43 +00003529
Matt Roper024c9042015-09-24 15:53:11 -07003530/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003531 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3532 * so assume we'll always need it in order to avoid underruns.
3533 */
3534static bool skl_needs_memory_bw_wa(struct intel_atomic_state *state)
3535{
3536 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3537
Rodrigo Vivib976dc52017-01-23 10:32:37 -08003538 if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv))
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003539 return true;
3540
3541 return false;
3542}
3543
Paulo Zanoni56feca92016-09-22 18:00:28 -03003544static bool
3545intel_has_sagv(struct drm_i915_private *dev_priv)
3546{
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003547 if (IS_KABYLAKE(dev_priv))
3548 return true;
3549
3550 if (IS_SKYLAKE(dev_priv) &&
3551 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED)
3552 return true;
3553
3554 return false;
Paulo Zanoni56feca92016-09-22 18:00:28 -03003555}
3556
Lyude656d1b82016-08-17 15:55:54 -04003557/*
3558 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3559 * depending on power and performance requirements. The display engine access
3560 * to system memory is blocked during the adjustment time. Because of the
3561 * blocking time, having this enabled can cause full system hangs and/or pipe
3562 * underruns if we don't meet all of the following requirements:
3563 *
3564 * - <= 1 pipe enabled
3565 * - All planes can enable watermarks for latencies >= SAGV engine block time
3566 * - We're not using an interlaced display configuration
3567 */
3568int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003569intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003570{
3571 int ret;
3572
Paulo Zanoni56feca92016-09-22 18:00:28 -03003573 if (!intel_has_sagv(dev_priv))
3574 return 0;
3575
3576 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003577 return 0;
3578
3579 DRM_DEBUG_KMS("Enabling the SAGV\n");
3580 mutex_lock(&dev_priv->rps.hw_lock);
3581
3582 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3583 GEN9_SAGV_ENABLE);
3584
3585 /* We don't need to wait for the SAGV when enabling */
3586 mutex_unlock(&dev_priv->rps.hw_lock);
3587
3588 /*
3589 * Some skl systems, pre-release machines in particular,
3590 * don't actually have an SAGV.
3591 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003592 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003593 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003594 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003595 return 0;
3596 } else if (ret < 0) {
3597 DRM_ERROR("Failed to enable the SAGV\n");
3598 return ret;
3599 }
3600
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003601 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003602 return 0;
3603}
3604
Lyude656d1b82016-08-17 15:55:54 -04003605int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003606intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003607{
Imre Deakb3b8e992016-12-05 18:27:38 +02003608 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003609
Paulo Zanoni56feca92016-09-22 18:00:28 -03003610 if (!intel_has_sagv(dev_priv))
3611 return 0;
3612
3613 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003614 return 0;
3615
3616 DRM_DEBUG_KMS("Disabling the SAGV\n");
3617 mutex_lock(&dev_priv->rps.hw_lock);
3618
3619 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003620 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3621 GEN9_SAGV_DISABLE,
3622 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3623 1);
Lyude656d1b82016-08-17 15:55:54 -04003624 mutex_unlock(&dev_priv->rps.hw_lock);
3625
Lyude656d1b82016-08-17 15:55:54 -04003626 /*
3627 * Some skl systems, pre-release machines in particular,
3628 * don't actually have an SAGV.
3629 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003630 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003631 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003632 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003633 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003634 } else if (ret < 0) {
3635 DRM_ERROR("Failed to disable the SAGV (%d)\n", ret);
3636 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003637 }
3638
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003639 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003640 return 0;
3641}
3642
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003643bool intel_can_enable_sagv(struct drm_atomic_state *state)
Lyude656d1b82016-08-17 15:55:54 -04003644{
3645 struct drm_device *dev = state->dev;
3646 struct drm_i915_private *dev_priv = to_i915(dev);
3647 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003648 struct intel_crtc *crtc;
3649 struct intel_plane *plane;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003650 struct intel_crtc_state *cstate;
Lyude656d1b82016-08-17 15:55:54 -04003651 enum pipe pipe;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003652 int level, latency;
Lyude656d1b82016-08-17 15:55:54 -04003653
Paulo Zanoni56feca92016-09-22 18:00:28 -03003654 if (!intel_has_sagv(dev_priv))
3655 return false;
3656
Lyude656d1b82016-08-17 15:55:54 -04003657 /*
3658 * SKL workaround: bspec recommends we disable the SAGV when we have
3659 * more then one pipe enabled
3660 *
3661 * If there are no active CRTCs, no additional checks need be performed
3662 */
3663 if (hweight32(intel_state->active_crtcs) == 0)
3664 return true;
3665 else if (hweight32(intel_state->active_crtcs) > 1)
3666 return false;
3667
3668 /* Since we're now guaranteed to only have one active CRTC... */
3669 pipe = ffs(intel_state->active_crtcs) - 1;
Ville Syrjälä98187832016-10-31 22:37:10 +02003670 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003671 cstate = to_intel_crtc_state(crtc->base.state);
Lyude656d1b82016-08-17 15:55:54 -04003672
Paulo Zanonic89cadd2016-10-10 17:30:59 -03003673 if (crtc->base.state->adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003674 return false;
3675
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003676 for_each_intel_plane_on_crtc(dev, crtc, plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003677 struct skl_plane_wm *wm =
3678 &cstate->wm.skl.optimal.planes[plane->id];
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003679
Lyude656d1b82016-08-17 15:55:54 -04003680 /* Skip this plane if it's not enabled */
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003681 if (!wm->wm[0].plane_en)
Lyude656d1b82016-08-17 15:55:54 -04003682 continue;
3683
3684 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003685 for (level = ilk_wm_max_level(dev_priv);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003686 !wm->wm[level].plane_en; --level)
Lyude656d1b82016-08-17 15:55:54 -04003687 { }
3688
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003689 latency = dev_priv->wm.skl_latency[level];
3690
3691 if (skl_needs_memory_bw_wa(intel_state) &&
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003692 plane->base.state->fb->modifier ==
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003693 I915_FORMAT_MOD_X_TILED)
3694 latency += 15;
3695
Lyude656d1b82016-08-17 15:55:54 -04003696 /*
3697 * If any of the planes on this pipe don't enable wm levels
3698 * that incur memory latencies higher then 30µs we can't enable
3699 * the SAGV
3700 */
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003701 if (latency < SKL_SAGV_BLOCK_TIME)
Lyude656d1b82016-08-17 15:55:54 -04003702 return false;
3703 }
3704
3705 return true;
3706}
3707
Damien Lespiaub9cec072014-11-04 17:06:43 +00003708static void
3709skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
Matt Roper024c9042015-09-24 15:53:11 -07003710 const struct intel_crtc_state *cstate,
Matt Roperc107acf2016-05-12 07:06:01 -07003711 struct skl_ddb_entry *alloc, /* out */
3712 int *num_active /* out */)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003713{
Matt Roperc107acf2016-05-12 07:06:01 -07003714 struct drm_atomic_state *state = cstate->base.state;
3715 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
3716 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper024c9042015-09-24 15:53:11 -07003717 struct drm_crtc *for_crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003718 unsigned int pipe_size, ddb_size;
3719 int nth_active_pipe;
Matt Roperc107acf2016-05-12 07:06:01 -07003720
Matt Ropera6d3460e2016-05-12 07:06:04 -07003721 if (WARN_ON(!state) || !cstate->base.active) {
Damien Lespiaub9cec072014-11-04 17:06:43 +00003722 alloc->start = 0;
3723 alloc->end = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003724 *num_active = hweight32(dev_priv->active_crtcs);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003725 return;
3726 }
3727
Matt Ropera6d3460e2016-05-12 07:06:04 -07003728 if (intel_state->active_pipe_changes)
3729 *num_active = hweight32(intel_state->active_crtcs);
3730 else
3731 *num_active = hweight32(dev_priv->active_crtcs);
3732
Deepak M6f3fff62016-09-15 15:01:10 +05303733 ddb_size = INTEL_INFO(dev_priv)->ddb_size;
3734 WARN_ON(ddb_size == 0);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003735
3736 ddb_size -= 4; /* 4 blocks for bypass path allocation */
3737
Matt Roperc107acf2016-05-12 07:06:01 -07003738 /*
Matt Ropera6d3460e2016-05-12 07:06:04 -07003739 * If the state doesn't change the active CRTC's, then there's
3740 * no need to recalculate; the existing pipe allocation limits
3741 * should remain unchanged. Note that we're safe from racing
3742 * commits since any racing commit that changes the active CRTC
3743 * list would need to grab _all_ crtc locks, including the one
3744 * we currently hold.
Matt Roperc107acf2016-05-12 07:06:01 -07003745 */
Matt Ropera6d3460e2016-05-12 07:06:04 -07003746 if (!intel_state->active_pipe_changes) {
Maarten Lankhorst512b5522016-11-08 13:55:34 +01003747 /*
3748 * alloc may be cleared by clear_intel_crtc_state,
3749 * copy from old state to be sure
3750 */
3751 *alloc = to_intel_crtc_state(for_crtc->state)->wm.skl.ddb;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003752 return;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003753 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07003754
3755 nth_active_pipe = hweight32(intel_state->active_crtcs &
3756 (drm_crtc_mask(for_crtc) - 1));
3757 pipe_size = ddb_size / hweight32(intel_state->active_crtcs);
3758 alloc->start = nth_active_pipe * ddb_size / *num_active;
3759 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003760}
3761
Matt Roperc107acf2016-05-12 07:06:01 -07003762static unsigned int skl_cursor_allocation(int num_active)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003763{
Matt Roperc107acf2016-05-12 07:06:01 -07003764 if (num_active == 1)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003765 return 32;
3766
3767 return 8;
3768}
3769
Damien Lespiaua269c582014-11-04 17:06:49 +00003770static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
3771{
3772 entry->start = reg & 0x3ff;
3773 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00003774 if (entry->end)
3775 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00003776}
3777
Damien Lespiau08db6652014-11-04 17:06:52 +00003778void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
3779 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00003780{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003781 struct intel_crtc *crtc;
Damien Lespiaua269c582014-11-04 17:06:49 +00003782
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003783 memset(ddb, 0, sizeof(*ddb));
3784
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003785 for_each_intel_crtc(&dev_priv->drm, crtc) {
Imre Deak4d800032016-02-17 16:31:29 +02003786 enum intel_display_power_domain power_domain;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003787 enum plane_id plane_id;
3788 enum pipe pipe = crtc->pipe;
Imre Deak4d800032016-02-17 16:31:29 +02003789
3790 power_domain = POWER_DOMAIN_PIPE(pipe);
3791 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003792 continue;
3793
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003794 for_each_plane_id_on_crtc(crtc, plane_id) {
3795 u32 val;
Damien Lespiaua269c582014-11-04 17:06:49 +00003796
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003797 if (plane_id != PLANE_CURSOR)
3798 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
3799 else
3800 val = I915_READ(CUR_BUF_CFG(pipe));
3801
3802 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane_id], val);
3803 }
Imre Deak4d800032016-02-17 16:31:29 +02003804
3805 intel_display_power_put(dev_priv, power_domain);
Damien Lespiaua269c582014-11-04 17:06:49 +00003806 }
3807}
3808
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003809/*
3810 * Determines the downscale amount of a plane for the purposes of watermark calculations.
3811 * The bspec defines downscale amount as:
3812 *
3813 * """
3814 * Horizontal down scale amount = maximum[1, Horizontal source size /
3815 * Horizontal destination size]
3816 * Vertical down scale amount = maximum[1, Vertical source size /
3817 * Vertical destination size]
3818 * Total down scale amount = Horizontal down scale amount *
3819 * Vertical down scale amount
3820 * """
3821 *
3822 * Return value is provided in 16.16 fixed point form to retain fractional part.
3823 * Caller should take care of dividing & rounding off the value.
3824 */
3825static uint32_t
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003826skl_plane_downscale_amount(const struct intel_crtc_state *cstate,
3827 const struct intel_plane_state *pstate)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003828{
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003829 struct intel_plane *plane = to_intel_plane(pstate->base.plane);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003830 uint32_t downscale_h, downscale_w;
3831 uint32_t src_w, src_h, dst_w, dst_h;
3832
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003833 if (WARN_ON(!intel_wm_plane_visible(cstate, pstate)))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003834 return DRM_PLANE_HELPER_NO_SCALING;
3835
3836 /* n.b., src is 16.16 fixed point, dst is whole integer */
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003837 if (plane->id == PLANE_CURSOR) {
3838 src_w = pstate->base.src_w;
3839 src_h = pstate->base.src_h;
3840 dst_w = pstate->base.crtc_w;
3841 dst_h = pstate->base.crtc_h;
3842 } else {
3843 src_w = drm_rect_width(&pstate->base.src);
3844 src_h = drm_rect_height(&pstate->base.src);
3845 dst_w = drm_rect_width(&pstate->base.dst);
3846 dst_h = drm_rect_height(&pstate->base.dst);
3847 }
3848
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003849 if (drm_rotation_90_or_270(pstate->base.rotation))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003850 swap(dst_w, dst_h);
3851
3852 downscale_h = max(src_h / dst_h, (uint32_t)DRM_PLANE_HELPER_NO_SCALING);
3853 downscale_w = max(src_w / dst_w, (uint32_t)DRM_PLANE_HELPER_NO_SCALING);
3854
3855 /* Provide result in 16.16 fixed point */
3856 return (uint64_t)downscale_w * downscale_h >> 16;
3857}
3858
Damien Lespiaub9cec072014-11-04 17:06:43 +00003859static unsigned int
Matt Roper024c9042015-09-24 15:53:11 -07003860skl_plane_relative_data_rate(const struct intel_crtc_state *cstate,
3861 const struct drm_plane_state *pstate,
3862 int y)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003863{
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003864 struct intel_plane *plane = to_intel_plane(pstate->plane);
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003865 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003866 uint32_t down_scale_amount, data_rate;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003867 uint32_t width = 0, height = 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02003868 struct drm_framebuffer *fb;
3869 u32 format;
Matt Ropera1de91e2016-05-12 07:05:57 -07003870
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003871 if (!intel_pstate->base.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07003872 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02003873
3874 fb = pstate->fb;
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003875 format = fb->format->format;
Ville Syrjälä83054942016-11-18 21:53:00 +02003876
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003877 if (plane->id == PLANE_CURSOR)
Matt Ropera1de91e2016-05-12 07:05:57 -07003878 return 0;
3879 if (y && format != DRM_FORMAT_NV12)
3880 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003881
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003882 width = drm_rect_width(&intel_pstate->base.src) >> 16;
3883 height = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003884
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003885 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003886 swap(width, height);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003887
3888 /* for planar format */
Matt Ropera1de91e2016-05-12 07:05:57 -07003889 if (format == DRM_FORMAT_NV12) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003890 if (y) /* y-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003891 data_rate = width * height *
Ville Syrjälä353c8592016-12-14 23:30:57 +02003892 fb->format->cpp[0];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003893 else /* uv-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003894 data_rate = (width / 2) * (height / 2) *
Ville Syrjälä353c8592016-12-14 23:30:57 +02003895 fb->format->cpp[1];
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003896 } else {
3897 /* for packed formats */
Ville Syrjälä353c8592016-12-14 23:30:57 +02003898 data_rate = width * height * fb->format->cpp[0];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003899 }
3900
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003901 down_scale_amount = skl_plane_downscale_amount(cstate, intel_pstate);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003902
3903 return (uint64_t)data_rate * down_scale_amount >> 16;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003904}
3905
3906/*
3907 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
3908 * a 8192x4096@32bpp framebuffer:
3909 * 3 * 4096 * 8192 * 4 < 2^32
3910 */
3911static unsigned int
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003912skl_get_total_relative_data_rate(struct intel_crtc_state *intel_cstate,
3913 unsigned *plane_data_rate,
3914 unsigned *plane_y_data_rate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003915{
Matt Roper9c74d822016-05-12 07:05:58 -07003916 struct drm_crtc_state *cstate = &intel_cstate->base;
3917 struct drm_atomic_state *state = cstate->state;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003918 struct drm_plane *plane;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003919 const struct drm_plane_state *pstate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003920 unsigned int total_data_rate = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003921
3922 if (WARN_ON(!state))
3923 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003924
Matt Ropera1de91e2016-05-12 07:05:57 -07003925 /* Calculate and cache data rate for each plane */
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003926 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, cstate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003927 enum plane_id plane_id = to_intel_plane(plane)->id;
3928 unsigned int rate;
Matt Roper024c9042015-09-24 15:53:11 -07003929
Matt Ropera6d3460e2016-05-12 07:06:04 -07003930 /* packed/uv */
3931 rate = skl_plane_relative_data_rate(intel_cstate,
3932 pstate, 0);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003933 plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003934
3935 total_data_rate += rate;
Matt Roper9c74d822016-05-12 07:05:58 -07003936
Matt Ropera6d3460e2016-05-12 07:06:04 -07003937 /* y-plane */
3938 rate = skl_plane_relative_data_rate(intel_cstate,
3939 pstate, 1);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003940 plane_y_data_rate[plane_id] = rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07003941
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003942 total_data_rate += rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003943 }
3944
3945 return total_data_rate;
3946}
3947
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003948static uint16_t
3949skl_ddb_min_alloc(const struct drm_plane_state *pstate,
3950 const int y)
3951{
3952 struct drm_framebuffer *fb = pstate->fb;
3953 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
3954 uint32_t src_w, src_h;
3955 uint32_t min_scanlines = 8;
3956 uint8_t plane_bpp;
3957
3958 if (WARN_ON(!fb))
3959 return 0;
3960
3961 /* For packed formats, no y-plane, return 0 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003962 if (y && fb->format->format != DRM_FORMAT_NV12)
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003963 return 0;
3964
3965 /* For Non Y-tile return 8-blocks */
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003966 if (fb->modifier != I915_FORMAT_MOD_Y_TILED &&
3967 fb->modifier != I915_FORMAT_MOD_Yf_TILED)
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003968 return 8;
3969
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003970 src_w = drm_rect_width(&intel_pstate->base.src) >> 16;
3971 src_h = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003972
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003973 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003974 swap(src_w, src_h);
3975
3976 /* Halve UV plane width and height for NV12 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003977 if (fb->format->format == DRM_FORMAT_NV12 && !y) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003978 src_w /= 2;
3979 src_h /= 2;
3980 }
3981
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003982 if (fb->format->format == DRM_FORMAT_NV12 && !y)
Ville Syrjälä353c8592016-12-14 23:30:57 +02003983 plane_bpp = fb->format->cpp[1];
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003984 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02003985 plane_bpp = fb->format->cpp[0];
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003986
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003987 if (drm_rotation_90_or_270(pstate->rotation)) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003988 switch (plane_bpp) {
3989 case 1:
3990 min_scanlines = 32;
3991 break;
3992 case 2:
3993 min_scanlines = 16;
3994 break;
3995 case 4:
3996 min_scanlines = 8;
3997 break;
3998 case 8:
3999 min_scanlines = 4;
4000 break;
4001 default:
4002 WARN(1, "Unsupported pixel depth %u for rotation",
4003 plane_bpp);
4004 min_scanlines = 32;
4005 }
4006 }
4007
4008 return DIV_ROUND_UP((4 * src_w * plane_bpp), 512) * min_scanlines/4 + 3;
4009}
4010
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004011static void
4012skl_ddb_calc_min(const struct intel_crtc_state *cstate, int num_active,
4013 uint16_t *minimum, uint16_t *y_minimum)
4014{
4015 const struct drm_plane_state *pstate;
4016 struct drm_plane *plane;
4017
4018 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, &cstate->base) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004019 enum plane_id plane_id = to_intel_plane(plane)->id;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004020
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004021 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004022 continue;
4023
4024 if (!pstate->visible)
4025 continue;
4026
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004027 minimum[plane_id] = skl_ddb_min_alloc(pstate, 0);
4028 y_minimum[plane_id] = skl_ddb_min_alloc(pstate, 1);
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004029 }
4030
4031 minimum[PLANE_CURSOR] = skl_cursor_allocation(num_active);
4032}
4033
Matt Roperc107acf2016-05-12 07:06:01 -07004034static int
Matt Roper024c9042015-09-24 15:53:11 -07004035skl_allocate_pipe_ddb(struct intel_crtc_state *cstate,
Damien Lespiaub9cec072014-11-04 17:06:43 +00004036 struct skl_ddb_allocation *ddb /* out */)
4037{
Matt Roperc107acf2016-05-12 07:06:01 -07004038 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07004039 struct drm_crtc *crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004040 struct drm_device *dev = crtc->dev;
4041 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4042 enum pipe pipe = intel_crtc->pipe;
Lyudece0ba282016-09-15 10:46:35 -04004043 struct skl_ddb_entry *alloc = &cstate->wm.skl.ddb;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004044 uint16_t alloc_size, start;
Maarten Lankhorstfefdd812016-10-26 15:41:33 +02004045 uint16_t minimum[I915_MAX_PLANES] = {};
4046 uint16_t y_minimum[I915_MAX_PLANES] = {};
Damien Lespiaub9cec072014-11-04 17:06:43 +00004047 unsigned int total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004048 enum plane_id plane_id;
Matt Roperc107acf2016-05-12 07:06:01 -07004049 int num_active;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004050 unsigned plane_data_rate[I915_MAX_PLANES] = {};
4051 unsigned plane_y_data_rate[I915_MAX_PLANES] = {};
Damien Lespiaub9cec072014-11-04 17:06:43 +00004052
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004053 /* Clear the partitioning for disabled planes. */
4054 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
4055 memset(ddb->y_plane[pipe], 0, sizeof(ddb->y_plane[pipe]));
4056
Matt Ropera6d3460e2016-05-12 07:06:04 -07004057 if (WARN_ON(!state))
4058 return 0;
4059
Matt Roperc107acf2016-05-12 07:06:01 -07004060 if (!cstate->base.active) {
Lyudece0ba282016-09-15 10:46:35 -04004061 alloc->start = alloc->end = 0;
Matt Roperc107acf2016-05-12 07:06:01 -07004062 return 0;
4063 }
4064
Matt Ropera6d3460e2016-05-12 07:06:04 -07004065 skl_ddb_get_pipe_allocation_limits(dev, cstate, alloc, &num_active);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00004066 alloc_size = skl_ddb_entry_size(alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004067 if (alloc_size == 0) {
4068 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
Matt Roperc107acf2016-05-12 07:06:01 -07004069 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004070 }
4071
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004072 skl_ddb_calc_min(cstate, num_active, minimum, y_minimum);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004073
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004074 /*
4075 * 1. Allocate the mininum required blocks for each active plane
4076 * and allocate the cursor, it doesn't require extra allocation
4077 * proportional to the data rate.
4078 */
Damien Lespiaub9cec072014-11-04 17:06:43 +00004079
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004080 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
4081 alloc_size -= minimum[plane_id];
4082 alloc_size -= y_minimum[plane_id];
Damien Lespiau80958152015-02-09 13:35:10 +00004083 }
4084
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004085 ddb->plane[pipe][PLANE_CURSOR].start = alloc->end - minimum[PLANE_CURSOR];
4086 ddb->plane[pipe][PLANE_CURSOR].end = alloc->end;
4087
Damien Lespiaub9cec072014-11-04 17:06:43 +00004088 /*
Damien Lespiau80958152015-02-09 13:35:10 +00004089 * 2. Distribute the remaining space in proportion to the amount of
4090 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00004091 *
4092 * FIXME: we may not allocate every single block here.
4093 */
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004094 total_data_rate = skl_get_total_relative_data_rate(cstate,
4095 plane_data_rate,
4096 plane_y_data_rate);
Matt Ropera1de91e2016-05-12 07:05:57 -07004097 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004098 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004099
Damien Lespiau34bb56a2014-11-04 17:07:01 +00004100 start = alloc->start;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004101 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004102 unsigned int data_rate, y_data_rate;
4103 uint16_t plane_blocks, y_plane_blocks = 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004104
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004105 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004106 continue;
4107
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004108 data_rate = plane_data_rate[plane_id];
Damien Lespiaub9cec072014-11-04 17:06:43 +00004109
4110 /*
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004111 * allocation for (packed formats) or (uv-plane part of planar format):
Damien Lespiaub9cec072014-11-04 17:06:43 +00004112 * promote the expression to 64 bits to avoid overflowing, the
4113 * result is < available as data_rate / total_data_rate < 1
4114 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004115 plane_blocks = minimum[plane_id];
Damien Lespiau80958152015-02-09 13:35:10 +00004116 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
4117 total_data_rate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004118
Matt Roperc107acf2016-05-12 07:06:01 -07004119 /* Leave disabled planes at (0,0) */
4120 if (data_rate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004121 ddb->plane[pipe][plane_id].start = start;
4122 ddb->plane[pipe][plane_id].end = start + plane_blocks;
Matt Roperc107acf2016-05-12 07:06:01 -07004123 }
Damien Lespiaub9cec072014-11-04 17:06:43 +00004124
4125 start += plane_blocks;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004126
4127 /*
4128 * allocation for y_plane part of planar format:
4129 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004130 y_data_rate = plane_y_data_rate[plane_id];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004131
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004132 y_plane_blocks = y_minimum[plane_id];
Matt Ropera1de91e2016-05-12 07:05:57 -07004133 y_plane_blocks += div_u64((uint64_t)alloc_size * y_data_rate,
4134 total_data_rate);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004135
Matt Roperc107acf2016-05-12 07:06:01 -07004136 if (y_data_rate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004137 ddb->y_plane[pipe][plane_id].start = start;
4138 ddb->y_plane[pipe][plane_id].end = start + y_plane_blocks;
Matt Roperc107acf2016-05-12 07:06:01 -07004139 }
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004140
Matt Ropera1de91e2016-05-12 07:05:57 -07004141 start += y_plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004142 }
4143
Matt Roperc107acf2016-05-12 07:06:01 -07004144 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004145}
4146
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004147/*
4148 * 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 +02004149 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004150 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
4151 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
4152*/
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304153static uint_fixed_16_16_t skl_wm_method1(uint32_t pixel_rate, uint8_t cpp,
4154 uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004155{
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304156 uint32_t wm_intermediate_val;
4157 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004158
4159 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304160 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004161
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304162 wm_intermediate_val = latency * pixel_rate * cpp;
4163 ret = fixed_16_16_div_round_up_u64(wm_intermediate_val, 1000 * 512);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004164 return ret;
4165}
4166
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304167static uint_fixed_16_16_t skl_wm_method2(uint32_t pixel_rate,
4168 uint32_t pipe_htotal,
4169 uint32_t latency,
4170 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004171{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004172 uint32_t wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304173 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004174
4175 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304176 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004177
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004178 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304179 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
4180 pipe_htotal * 1000);
4181 ret = mul_u32_fixed_16_16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004182 return ret;
4183}
4184
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004185static uint32_t skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *cstate,
4186 struct intel_plane_state *pstate)
4187{
4188 uint64_t adjusted_pixel_rate;
4189 uint64_t downscale_amount;
4190 uint64_t pixel_rate;
4191
4192 /* Shouldn't reach here on disabled planes... */
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004193 if (WARN_ON(!intel_wm_plane_visible(cstate, pstate)))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004194 return 0;
4195
4196 /*
4197 * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
4198 * with additional adjustments for plane-specific scaling.
4199 */
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02004200 adjusted_pixel_rate = cstate->pixel_rate;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004201 downscale_amount = skl_plane_downscale_amount(cstate, pstate);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004202
4203 pixel_rate = adjusted_pixel_rate * downscale_amount >> 16;
4204 WARN_ON(pixel_rate != clamp_t(uint32_t, pixel_rate, 0, ~0));
4205
4206 return pixel_rate;
4207}
4208
Matt Roper55994c22016-05-12 07:06:08 -07004209static int skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
4210 struct intel_crtc_state *cstate,
4211 struct intel_plane_state *intel_pstate,
4212 uint16_t ddb_allocation,
4213 int level,
4214 uint16_t *out_blocks, /* out */
4215 uint8_t *out_lines, /* out */
4216 bool *enabled /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004217{
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004218 struct intel_plane *plane = to_intel_plane(intel_pstate->base.plane);
Matt Roper33815fa2016-05-12 07:06:05 -07004219 struct drm_plane_state *pstate = &intel_pstate->base;
4220 struct drm_framebuffer *fb = pstate->fb;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004221 uint32_t latency = dev_priv->wm.skl_latency[level];
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304222 uint_fixed_16_16_t method1, method2;
4223 uint_fixed_16_16_t plane_blocks_per_line;
4224 uint_fixed_16_16_t selected_result;
4225 uint32_t interm_pbpl;
4226 uint32_t plane_bytes_per_line;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004227 uint32_t res_blocks, res_lines;
Ville Syrjäläac484962016-01-20 21:05:26 +02004228 uint8_t cpp;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004229 uint32_t width = 0, height = 0;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004230 uint32_t plane_pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304231 uint_fixed_16_16_t y_tile_minimum;
4232 uint32_t y_min_scanlines;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03004233 struct intel_atomic_state *state =
4234 to_intel_atomic_state(cstate->base.state);
4235 bool apply_memory_bw_wa = skl_needs_memory_bw_wa(state);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304236 bool y_tiled, x_tiled;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004237
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004238 if (latency == 0 ||
4239 !intel_wm_plane_visible(cstate, intel_pstate)) {
Matt Roper55994c22016-05-12 07:06:08 -07004240 *enabled = false;
4241 return 0;
4242 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004243
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304244 y_tiled = fb->modifier == I915_FORMAT_MOD_Y_TILED ||
4245 fb->modifier == I915_FORMAT_MOD_Yf_TILED;
4246 x_tiled = fb->modifier == I915_FORMAT_MOD_X_TILED;
4247
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05304248 /* Display WA #1141: kbl. */
4249 if (IS_KABYLAKE(dev_priv) && dev_priv->ipc_enabled)
4250 latency += 4;
4251
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304252 if (apply_memory_bw_wa && x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03004253 latency += 15;
4254
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004255 if (plane->id == PLANE_CURSOR) {
4256 width = intel_pstate->base.crtc_w;
4257 height = intel_pstate->base.crtc_h;
4258 } else {
4259 width = drm_rect_width(&intel_pstate->base.src) >> 16;
4260 height = drm_rect_height(&intel_pstate->base.src) >> 16;
4261 }
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004262
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03004263 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004264 swap(width, height);
4265
Ville Syrjälä353c8592016-12-14 23:30:57 +02004266 cpp = fb->format->cpp[0];
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004267 plane_pixel_rate = skl_adjusted_plane_pixel_rate(cstate, intel_pstate);
4268
Dave Airlie61d0a042016-10-25 16:35:20 +10004269 if (drm_rotation_90_or_270(pstate->rotation)) {
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004270 int cpp = (fb->format->format == DRM_FORMAT_NV12) ?
Ville Syrjälä353c8592016-12-14 23:30:57 +02004271 fb->format->cpp[1] :
4272 fb->format->cpp[0];
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004273
4274 switch (cpp) {
4275 case 1:
4276 y_min_scanlines = 16;
4277 break;
4278 case 2:
4279 y_min_scanlines = 8;
4280 break;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004281 case 4:
4282 y_min_scanlines = 4;
4283 break;
Paulo Zanoni86a462b2016-09-22 18:00:35 -03004284 default:
4285 MISSING_CASE(cpp);
4286 return -EINVAL;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004287 }
4288 } else {
4289 y_min_scanlines = 4;
4290 }
4291
Paulo Zanoni2ef32de2016-11-08 18:22:11 -02004292 if (apply_memory_bw_wa)
4293 y_min_scanlines *= 2;
4294
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03004295 plane_bytes_per_line = width * cpp;
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304296 if (y_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304297 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line *
4298 y_min_scanlines, 512);
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03004299 plane_blocks_per_line =
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304300 fixed_16_16_div_round_up(interm_pbpl, y_min_scanlines);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304301 } else if (x_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304302 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line, 512);
4303 plane_blocks_per_line = u32_to_fixed_16_16(interm_pbpl);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304304 } else {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304305 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line, 512) + 1;
4306 plane_blocks_per_line = u32_to_fixed_16_16(interm_pbpl);
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03004307 }
4308
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004309 method1 = skl_wm_method1(plane_pixel_rate, cpp, latency);
4310 method2 = skl_wm_method2(plane_pixel_rate,
Matt Roper024c9042015-09-24 15:53:11 -07004311 cstate->base.adjusted_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004312 latency,
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03004313 plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004314
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304315 y_tile_minimum = mul_u32_fixed_16_16(y_min_scanlines,
4316 plane_blocks_per_line);
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004317
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304318 if (y_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304319 selected_result = max_fixed_16_16(method2, y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004320 } else {
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03004321 if ((cpp * cstate->base.adjusted_mode.crtc_htotal / 512 < 1) &&
4322 (plane_bytes_per_line / 512 < 1))
4323 selected_result = method2;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304324 else if ((ddb_allocation /
4325 fixed_16_16_to_u32_round_up(plane_blocks_per_line)) >= 1)
4326 selected_result = min_fixed_16_16(method1, method2);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004327 else
4328 selected_result = method1;
4329 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004330
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304331 res_blocks = fixed_16_16_to_u32_round_up(selected_result) + 1;
4332 res_lines = DIV_ROUND_UP(selected_result.val,
4333 plane_blocks_per_line.val);
Damien Lespiaue6d66172014-11-04 17:06:55 +00004334
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004335 if (level >= 1 && level <= 7) {
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304336 if (y_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304337 res_blocks += fixed_16_16_to_u32_round_up(y_tile_minimum);
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004338 res_lines += y_min_scanlines;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004339 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004340 res_blocks++;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004341 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004342 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004343
Matt Roper55994c22016-05-12 07:06:08 -07004344 if (res_blocks >= ddb_allocation || res_lines > 31) {
4345 *enabled = false;
Matt Roper6b6bada2016-05-12 07:06:10 -07004346
4347 /*
4348 * If there are no valid level 0 watermarks, then we can't
4349 * support this display configuration.
4350 */
4351 if (level) {
4352 return 0;
4353 } else {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004354 struct drm_plane *plane = pstate->plane;
Matt Roper6b6bada2016-05-12 07:06:10 -07004355
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004356 DRM_DEBUG_KMS("Requested display configuration exceeds system watermark limitations\n");
4357 DRM_DEBUG_KMS("[PLANE:%d:%s] blocks required = %u/%u, lines required = %u/31\n",
4358 plane->base.id, plane->name,
4359 res_blocks, ddb_allocation, res_lines);
Matt Roper6b6bada2016-05-12 07:06:10 -07004360 return -EINVAL;
4361 }
Matt Roper55994c22016-05-12 07:06:08 -07004362 }
Damien Lespiaue6d66172014-11-04 17:06:55 +00004363
4364 *out_blocks = res_blocks;
4365 *out_lines = res_lines;
Matt Roper55994c22016-05-12 07:06:08 -07004366 *enabled = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004367
Matt Roper55994c22016-05-12 07:06:08 -07004368 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004369}
4370
Matt Roperf4a96752016-05-12 07:06:06 -07004371static int
4372skl_compute_wm_level(const struct drm_i915_private *dev_priv,
4373 struct skl_ddb_allocation *ddb,
4374 struct intel_crtc_state *cstate,
Lyudea62163e2016-10-04 14:28:20 -04004375 struct intel_plane *intel_plane,
Matt Roperf4a96752016-05-12 07:06:06 -07004376 int level,
4377 struct skl_wm_level *result)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004378{
Matt Roperf4a96752016-05-12 07:06:06 -07004379 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07004380 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Lyudea62163e2016-10-04 14:28:20 -04004381 struct drm_plane *plane = &intel_plane->base;
4382 struct intel_plane_state *intel_pstate = NULL;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004383 uint16_t ddb_blocks;
Matt Roper024c9042015-09-24 15:53:11 -07004384 enum pipe pipe = intel_crtc->pipe;
Matt Roper55994c22016-05-12 07:06:08 -07004385 int ret;
Lyudea62163e2016-10-04 14:28:20 -04004386
4387 if (state)
4388 intel_pstate =
4389 intel_atomic_get_existing_plane_state(state,
4390 intel_plane);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004391
Matt Roperf4a96752016-05-12 07:06:06 -07004392 /*
Lyudea62163e2016-10-04 14:28:20 -04004393 * Note: If we start supporting multiple pending atomic commits against
4394 * the same planes/CRTC's in the future, plane->state will no longer be
4395 * the correct pre-state to use for the calculations here and we'll
4396 * need to change where we get the 'unchanged' plane data from.
4397 *
4398 * For now this is fine because we only allow one queued commit against
4399 * a CRTC. Even if the plane isn't modified by this transaction and we
4400 * don't have a plane lock, we still have the CRTC's lock, so we know
4401 * that no other transactions are racing with us to update it.
Matt Roperf4a96752016-05-12 07:06:06 -07004402 */
Lyudea62163e2016-10-04 14:28:20 -04004403 if (!intel_pstate)
4404 intel_pstate = to_intel_plane_state(plane->state);
Matt Roperf4a96752016-05-12 07:06:06 -07004405
Lyudea62163e2016-10-04 14:28:20 -04004406 WARN_ON(!intel_pstate->base.fb);
Matt Roper024c9042015-09-24 15:53:11 -07004407
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004408 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][intel_plane->id]);
Matt Roperf4a96752016-05-12 07:06:06 -07004409
Lyudea62163e2016-10-04 14:28:20 -04004410 ret = skl_compute_plane_wm(dev_priv,
4411 cstate,
4412 intel_pstate,
4413 ddb_blocks,
4414 level,
4415 &result->plane_res_b,
4416 &result->plane_res_l,
4417 &result->plane_en);
4418 if (ret)
4419 return ret;
Matt Roperf4a96752016-05-12 07:06:06 -07004420
4421 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004422}
4423
Damien Lespiau407b50f2014-11-04 17:06:57 +00004424static uint32_t
Matt Roper024c9042015-09-24 15:53:11 -07004425skl_compute_linetime_wm(struct intel_crtc_state *cstate)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004426{
Mahesh Kumara3a89862016-12-01 21:19:34 +05304427 struct drm_atomic_state *state = cstate->base.state;
4428 struct drm_i915_private *dev_priv = to_i915(state->dev);
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03004429 uint32_t pixel_rate;
Mahesh Kumara3a89862016-12-01 21:19:34 +05304430 uint32_t linetime_wm;
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03004431
Matt Roper024c9042015-09-24 15:53:11 -07004432 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004433 return 0;
4434
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02004435 pixel_rate = cstate->pixel_rate;
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03004436
4437 if (WARN_ON(pixel_rate == 0))
Mika Kuoppala661abfc2015-07-16 19:36:51 +03004438 return 0;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004439
Mahesh Kumara3a89862016-12-01 21:19:34 +05304440 linetime_wm = DIV_ROUND_UP(8 * cstate->base.adjusted_mode.crtc_htotal *
4441 1000, pixel_rate);
4442
4443 /* Display WA #1135: bxt. */
4444 if (IS_BROXTON(dev_priv) && dev_priv->ipc_enabled)
4445 linetime_wm = DIV_ROUND_UP(linetime_wm, 2);
4446
4447 return linetime_wm;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004448}
4449
Matt Roper024c9042015-09-24 15:53:11 -07004450static void skl_compute_transition_wm(struct intel_crtc_state *cstate,
Damien Lespiau9414f562014-11-04 17:06:58 +00004451 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004452{
Matt Roper024c9042015-09-24 15:53:11 -07004453 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004454 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00004455
4456 /* Until we know more, just disable transition WMs */
Lyudea62163e2016-10-04 14:28:20 -04004457 trans_wm->plane_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004458}
4459
Matt Roper55994c22016-05-12 07:06:08 -07004460static int skl_build_pipe_wm(struct intel_crtc_state *cstate,
4461 struct skl_ddb_allocation *ddb,
4462 struct skl_pipe_wm *pipe_wm)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004463{
Matt Roper024c9042015-09-24 15:53:11 -07004464 struct drm_device *dev = cstate->base.crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004465 const struct drm_i915_private *dev_priv = to_i915(dev);
Lyudea62163e2016-10-04 14:28:20 -04004466 struct intel_plane *intel_plane;
4467 struct skl_plane_wm *wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004468 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Roper55994c22016-05-12 07:06:08 -07004469 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004470
Lyudea62163e2016-10-04 14:28:20 -04004471 /*
4472 * We'll only calculate watermarks for planes that are actually
4473 * enabled, so make sure all other planes are set as disabled.
4474 */
4475 memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
4476
4477 for_each_intel_plane_mask(&dev_priv->drm,
4478 intel_plane,
4479 cstate->base.plane_mask) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004480 wm = &pipe_wm->planes[intel_plane->id];
Lyudea62163e2016-10-04 14:28:20 -04004481
4482 for (level = 0; level <= max_level; level++) {
4483 ret = skl_compute_wm_level(dev_priv, ddb, cstate,
4484 intel_plane, level,
4485 &wm->wm[level]);
4486 if (ret)
4487 return ret;
4488 }
4489 skl_compute_transition_wm(cstate, &wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004490 }
Matt Roper024c9042015-09-24 15:53:11 -07004491 pipe_wm->linetime = skl_compute_linetime_wm(cstate);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004492
Matt Roper55994c22016-05-12 07:06:08 -07004493 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004494}
4495
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004496static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
4497 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00004498 const struct skl_ddb_entry *entry)
4499{
4500 if (entry->end)
4501 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
4502 else
4503 I915_WRITE(reg, 0);
4504}
4505
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004506static void skl_write_wm_level(struct drm_i915_private *dev_priv,
4507 i915_reg_t reg,
4508 const struct skl_wm_level *level)
4509{
4510 uint32_t val = 0;
4511
4512 if (level->plane_en) {
4513 val |= PLANE_WM_EN;
4514 val |= level->plane_res_b;
4515 val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
4516 }
4517
4518 I915_WRITE(reg, val);
4519}
4520
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004521static void skl_write_plane_wm(struct intel_crtc *intel_crtc,
4522 const struct skl_plane_wm *wm,
4523 const struct skl_ddb_allocation *ddb,
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004524 enum plane_id plane_id)
Lyude62e0fb82016-08-22 12:50:08 -04004525{
4526 struct drm_crtc *crtc = &intel_crtc->base;
4527 struct drm_device *dev = crtc->dev;
4528 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004529 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004530 enum pipe pipe = intel_crtc->pipe;
4531
4532 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004533 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004534 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004535 }
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004536 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004537 &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004538
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004539 skl_ddb_entry_write(dev_priv, PLANE_BUF_CFG(pipe, plane_id),
4540 &ddb->plane[pipe][plane_id]);
4541 skl_ddb_entry_write(dev_priv, PLANE_NV12_BUF_CFG(pipe, plane_id),
4542 &ddb->y_plane[pipe][plane_id]);
Lyude62e0fb82016-08-22 12:50:08 -04004543}
4544
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004545static void skl_write_cursor_wm(struct intel_crtc *intel_crtc,
4546 const struct skl_plane_wm *wm,
4547 const struct skl_ddb_allocation *ddb)
Lyude62e0fb82016-08-22 12:50:08 -04004548{
4549 struct drm_crtc *crtc = &intel_crtc->base;
4550 struct drm_device *dev = crtc->dev;
4551 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004552 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004553 enum pipe pipe = intel_crtc->pipe;
4554
4555 for (level = 0; level <= max_level; level++) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004556 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
4557 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004558 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004559 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004560
4561 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004562 &ddb->plane[pipe][PLANE_CURSOR]);
Lyude62e0fb82016-08-22 12:50:08 -04004563}
4564
cpaul@redhat.com45ece232016-10-14 17:31:56 -04004565bool skl_wm_level_equals(const struct skl_wm_level *l1,
4566 const struct skl_wm_level *l2)
4567{
4568 if (l1->plane_en != l2->plane_en)
4569 return false;
4570
4571 /* If both planes aren't enabled, the rest shouldn't matter */
4572 if (!l1->plane_en)
4573 return true;
4574
4575 return (l1->plane_res_l == l2->plane_res_l &&
4576 l1->plane_res_b == l2->plane_res_b);
4577}
4578
Lyude27082492016-08-24 07:48:10 +02004579static inline bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
4580 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004581{
Lyude27082492016-08-24 07:48:10 +02004582 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004583}
4584
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01004585bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry **entries,
4586 const struct skl_ddb_entry *ddb,
4587 int ignore)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004588{
Lyudece0ba282016-09-15 10:46:35 -04004589 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004590
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01004591 for (i = 0; i < I915_MAX_PIPES; i++)
4592 if (i != ignore && entries[i] &&
4593 skl_ddb_entries_overlap(ddb, entries[i]))
Lyude27082492016-08-24 07:48:10 +02004594 return true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004595
Lyude27082492016-08-24 07:48:10 +02004596 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004597}
4598
Matt Roper55994c22016-05-12 07:06:08 -07004599static int skl_update_pipe_wm(struct drm_crtc_state *cstate,
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004600 const struct skl_pipe_wm *old_pipe_wm,
Matt Roper55994c22016-05-12 07:06:08 -07004601 struct skl_pipe_wm *pipe_wm, /* out */
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004602 struct skl_ddb_allocation *ddb, /* out */
Matt Roper55994c22016-05-12 07:06:08 -07004603 bool *changed /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004604{
Matt Roperf4a96752016-05-12 07:06:06 -07004605 struct intel_crtc_state *intel_cstate = to_intel_crtc_state(cstate);
Matt Roper55994c22016-05-12 07:06:08 -07004606 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004607
Matt Roper55994c22016-05-12 07:06:08 -07004608 ret = skl_build_pipe_wm(intel_cstate, ddb, pipe_wm);
4609 if (ret)
4610 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004611
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004612 if (!memcmp(old_pipe_wm, pipe_wm, sizeof(*pipe_wm)))
Matt Roper55994c22016-05-12 07:06:08 -07004613 *changed = false;
4614 else
4615 *changed = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004616
Matt Roper55994c22016-05-12 07:06:08 -07004617 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004618}
4619
Matt Roper9b613022016-06-27 16:42:44 -07004620static uint32_t
4621pipes_modified(struct drm_atomic_state *state)
4622{
4623 struct drm_crtc *crtc;
4624 struct drm_crtc_state *cstate;
4625 uint32_t i, ret = 0;
4626
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004627 for_each_new_crtc_in_state(state, crtc, cstate, i)
Matt Roper9b613022016-06-27 16:42:44 -07004628 ret |= drm_crtc_mask(crtc);
4629
4630 return ret;
4631}
4632
Jani Nikulabb7791b2016-10-04 12:29:17 +03004633static int
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004634skl_ddb_add_affected_planes(struct intel_crtc_state *cstate)
4635{
4636 struct drm_atomic_state *state = cstate->base.state;
4637 struct drm_device *dev = state->dev;
4638 struct drm_crtc *crtc = cstate->base.crtc;
4639 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4640 struct drm_i915_private *dev_priv = to_i915(dev);
4641 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4642 struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
4643 struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
4644 struct drm_plane_state *plane_state;
4645 struct drm_plane *plane;
4646 enum pipe pipe = intel_crtc->pipe;
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004647
4648 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
4649
Maarten Lankhorst220b0962016-10-26 15:41:30 +02004650 drm_for_each_plane_mask(plane, dev, cstate->base.plane_mask) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004651 enum plane_id plane_id = to_intel_plane(plane)->id;
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004652
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004653 if (skl_ddb_entry_equal(&cur_ddb->plane[pipe][plane_id],
4654 &new_ddb->plane[pipe][plane_id]) &&
4655 skl_ddb_entry_equal(&cur_ddb->y_plane[pipe][plane_id],
4656 &new_ddb->y_plane[pipe][plane_id]))
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004657 continue;
4658
4659 plane_state = drm_atomic_get_plane_state(state, plane);
4660 if (IS_ERR(plane_state))
4661 return PTR_ERR(plane_state);
4662 }
4663
4664 return 0;
4665}
4666
Matt Roper98d39492016-05-12 07:06:03 -07004667static int
4668skl_compute_ddb(struct drm_atomic_state *state)
4669{
4670 struct drm_device *dev = state->dev;
4671 struct drm_i915_private *dev_priv = to_i915(dev);
4672 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4673 struct intel_crtc *intel_crtc;
Matt Roper734fa012016-05-12 15:11:40 -07004674 struct skl_ddb_allocation *ddb = &intel_state->wm_results.ddb;
Matt Roper9b613022016-06-27 16:42:44 -07004675 uint32_t realloc_pipes = pipes_modified(state);
Matt Roper98d39492016-05-12 07:06:03 -07004676 int ret;
4677
4678 /*
4679 * If this is our first atomic update following hardware readout,
4680 * we can't trust the DDB that the BIOS programmed for us. Let's
4681 * pretend that all pipes switched active status so that we'll
4682 * ensure a full DDB recompute.
4683 */
Matt Roper1b54a882016-06-17 13:42:18 -07004684 if (dev_priv->wm.distrust_bios_wm) {
4685 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
4686 state->acquire_ctx);
4687 if (ret)
4688 return ret;
4689
Matt Roper98d39492016-05-12 07:06:03 -07004690 intel_state->active_pipe_changes = ~0;
4691
Matt Roper1b54a882016-06-17 13:42:18 -07004692 /*
4693 * We usually only initialize intel_state->active_crtcs if we
4694 * we're doing a modeset; make sure this field is always
4695 * initialized during the sanitization process that happens
4696 * on the first commit too.
4697 */
4698 if (!intel_state->modeset)
4699 intel_state->active_crtcs = dev_priv->active_crtcs;
4700 }
4701
Matt Roper98d39492016-05-12 07:06:03 -07004702 /*
4703 * If the modeset changes which CRTC's are active, we need to
4704 * recompute the DDB allocation for *all* active pipes, even
4705 * those that weren't otherwise being modified in any way by this
4706 * atomic commit. Due to the shrinking of the per-pipe allocations
4707 * when new active CRTC's are added, it's possible for a pipe that
4708 * we were already using and aren't changing at all here to suddenly
4709 * become invalid if its DDB needs exceeds its new allocation.
4710 *
4711 * Note that if we wind up doing a full DDB recompute, we can't let
4712 * any other display updates race with this transaction, so we need
4713 * to grab the lock on *all* CRTC's.
4714 */
Matt Roper734fa012016-05-12 15:11:40 -07004715 if (intel_state->active_pipe_changes) {
Matt Roper98d39492016-05-12 07:06:03 -07004716 realloc_pipes = ~0;
Matt Roper734fa012016-05-12 15:11:40 -07004717 intel_state->wm_results.dirty_pipes = ~0;
4718 }
Matt Roper98d39492016-05-12 07:06:03 -07004719
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004720 /*
4721 * We're not recomputing for the pipes not included in the commit, so
4722 * make sure we start with the current state.
4723 */
4724 memcpy(ddb, &dev_priv->wm.skl_hw.ddb, sizeof(*ddb));
4725
Matt Roper98d39492016-05-12 07:06:03 -07004726 for_each_intel_crtc_mask(dev, intel_crtc, realloc_pipes) {
4727 struct intel_crtc_state *cstate;
4728
4729 cstate = intel_atomic_get_crtc_state(state, intel_crtc);
4730 if (IS_ERR(cstate))
4731 return PTR_ERR(cstate);
4732
Matt Roper734fa012016-05-12 15:11:40 -07004733 ret = skl_allocate_pipe_ddb(cstate, ddb);
Matt Roper98d39492016-05-12 07:06:03 -07004734 if (ret)
4735 return ret;
Lyude05a76d32016-08-17 15:55:57 -04004736
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004737 ret = skl_ddb_add_affected_planes(cstate);
Lyude05a76d32016-08-17 15:55:57 -04004738 if (ret)
4739 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07004740 }
4741
4742 return 0;
4743}
4744
Matt Roper2722efb2016-08-17 15:55:55 -04004745static void
4746skl_copy_wm_for_pipe(struct skl_wm_values *dst,
4747 struct skl_wm_values *src,
4748 enum pipe pipe)
4749{
Matt Roper2722efb2016-08-17 15:55:55 -04004750 memcpy(dst->ddb.y_plane[pipe], src->ddb.y_plane[pipe],
4751 sizeof(dst->ddb.y_plane[pipe]));
4752 memcpy(dst->ddb.plane[pipe], src->ddb.plane[pipe],
4753 sizeof(dst->ddb.plane[pipe]));
4754}
4755
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004756static void
4757skl_print_wm_changes(const struct drm_atomic_state *state)
4758{
4759 const struct drm_device *dev = state->dev;
4760 const struct drm_i915_private *dev_priv = to_i915(dev);
4761 const struct intel_atomic_state *intel_state =
4762 to_intel_atomic_state(state);
4763 const struct drm_crtc *crtc;
4764 const struct drm_crtc_state *cstate;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004765 const struct intel_plane *intel_plane;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004766 const struct skl_ddb_allocation *old_ddb = &dev_priv->wm.skl_hw.ddb;
4767 const struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
Maarten Lankhorst75704982016-11-01 12:04:10 +01004768 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004769
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004770 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst75704982016-11-01 12:04:10 +01004771 const struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4772 enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004773
Maarten Lankhorst75704982016-11-01 12:04:10 +01004774 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004775 enum plane_id plane_id = intel_plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004776 const struct skl_ddb_entry *old, *new;
4777
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004778 old = &old_ddb->plane[pipe][plane_id];
4779 new = &new_ddb->plane[pipe][plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004780
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004781 if (skl_ddb_entry_equal(old, new))
4782 continue;
4783
Maarten Lankhorst75704982016-11-01 12:04:10 +01004784 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] ddb (%d - %d) -> (%d - %d)\n",
4785 intel_plane->base.base.id,
4786 intel_plane->base.name,
4787 old->start, old->end,
4788 new->start, new->end);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004789 }
4790 }
4791}
4792
Matt Roper98d39492016-05-12 07:06:03 -07004793static int
4794skl_compute_wm(struct drm_atomic_state *state)
4795{
4796 struct drm_crtc *crtc;
4797 struct drm_crtc_state *cstate;
Matt Roper734fa012016-05-12 15:11:40 -07004798 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4799 struct skl_wm_values *results = &intel_state->wm_results;
4800 struct skl_pipe_wm *pipe_wm;
Matt Roper98d39492016-05-12 07:06:03 -07004801 bool changed = false;
Matt Roper734fa012016-05-12 15:11:40 -07004802 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07004803
4804 /*
4805 * If this transaction isn't actually touching any CRTC's, don't
4806 * bother with watermark calculation. Note that if we pass this
4807 * test, we're guaranteed to hold at least one CRTC state mutex,
4808 * which means we can safely use values like dev_priv->active_crtcs
4809 * since any racing commits that want to update them would need to
4810 * hold _all_ CRTC state mutexes.
4811 */
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004812 for_each_new_crtc_in_state(state, crtc, cstate, i)
Matt Roper98d39492016-05-12 07:06:03 -07004813 changed = true;
4814 if (!changed)
4815 return 0;
4816
Matt Roper734fa012016-05-12 15:11:40 -07004817 /* Clear all dirty flags */
4818 results->dirty_pipes = 0;
4819
Matt Roper98d39492016-05-12 07:06:03 -07004820 ret = skl_compute_ddb(state);
4821 if (ret)
4822 return ret;
4823
Matt Roper734fa012016-05-12 15:11:40 -07004824 /*
4825 * Calculate WM's for all pipes that are part of this transaction.
4826 * Note that the DDB allocation above may have added more CRTC's that
4827 * weren't otherwise being modified (and set bits in dirty_pipes) if
4828 * pipe allocations had to change.
4829 *
4830 * FIXME: Now that we're doing this in the atomic check phase, we
4831 * should allow skl_update_pipe_wm() to return failure in cases where
4832 * no suitable watermark values can be found.
4833 */
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004834 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Matt Roper734fa012016-05-12 15:11:40 -07004835 struct intel_crtc_state *intel_cstate =
4836 to_intel_crtc_state(cstate);
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004837 const struct skl_pipe_wm *old_pipe_wm =
4838 &to_intel_crtc_state(crtc->state)->wm.skl.optimal;
Matt Roper734fa012016-05-12 15:11:40 -07004839
4840 pipe_wm = &intel_cstate->wm.skl.optimal;
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004841 ret = skl_update_pipe_wm(cstate, old_pipe_wm, pipe_wm,
4842 &results->ddb, &changed);
Matt Roper734fa012016-05-12 15:11:40 -07004843 if (ret)
4844 return ret;
4845
4846 if (changed)
4847 results->dirty_pipes |= drm_crtc_mask(crtc);
4848
4849 if ((results->dirty_pipes & drm_crtc_mask(crtc)) == 0)
4850 /* This pipe's WM's did not change */
4851 continue;
4852
4853 intel_cstate->update_wm_pre = true;
Matt Roper734fa012016-05-12 15:11:40 -07004854 }
4855
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004856 skl_print_wm_changes(state);
4857
Matt Roper98d39492016-05-12 07:06:03 -07004858 return 0;
4859}
4860
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004861static void skl_atomic_update_crtc_wm(struct intel_atomic_state *state,
4862 struct intel_crtc_state *cstate)
4863{
4864 struct intel_crtc *crtc = to_intel_crtc(cstate->base.crtc);
4865 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
4866 struct skl_pipe_wm *pipe_wm = &cstate->wm.skl.optimal;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004867 const struct skl_ddb_allocation *ddb = &state->wm_results.ddb;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004868 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004869 enum plane_id plane_id;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004870
4871 if (!(state->wm_results.dirty_pipes & drm_crtc_mask(&crtc->base)))
4872 return;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004873
4874 I915_WRITE(PIPE_WM_LINETIME(pipe), pipe_wm->linetime);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004875
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004876 for_each_plane_id_on_crtc(crtc, plane_id) {
4877 if (plane_id != PLANE_CURSOR)
4878 skl_write_plane_wm(crtc, &pipe_wm->planes[plane_id],
4879 ddb, plane_id);
4880 else
4881 skl_write_cursor_wm(crtc, &pipe_wm->planes[plane_id],
4882 ddb);
4883 }
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004884}
4885
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004886static void skl_initial_wm(struct intel_atomic_state *state,
4887 struct intel_crtc_state *cstate)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004888{
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004889 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02004890 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004891 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004892 struct skl_wm_values *results = &state->wm_results;
Matt Roper2722efb2016-08-17 15:55:55 -04004893 struct skl_wm_values *hw_vals = &dev_priv->wm.skl_hw;
Lyude27082492016-08-24 07:48:10 +02004894 enum pipe pipe = intel_crtc->pipe;
Bob Paauweadda50b2015-07-21 10:42:53 -07004895
Ville Syrjälä432081b2016-10-31 22:37:03 +02004896 if ((results->dirty_pipes & drm_crtc_mask(&intel_crtc->base)) == 0)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004897 return;
4898
Matt Roper734fa012016-05-12 15:11:40 -07004899 mutex_lock(&dev_priv->wm.wm_mutex);
4900
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004901 if (cstate->base.active_changed)
4902 skl_atomic_update_crtc_wm(state, cstate);
Lyude27082492016-08-24 07:48:10 +02004903
4904 skl_copy_wm_for_pipe(hw_vals, results, pipe);
Matt Roper734fa012016-05-12 15:11:40 -07004905
4906 mutex_unlock(&dev_priv->wm.wm_mutex);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004907}
4908
Ville Syrjäläd8905652016-01-14 14:53:35 +02004909static void ilk_compute_wm_config(struct drm_device *dev,
4910 struct intel_wm_config *config)
4911{
4912 struct intel_crtc *crtc;
4913
4914 /* Compute the currently _active_ config */
4915 for_each_intel_crtc(dev, crtc) {
4916 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
4917
4918 if (!wm->pipe_enabled)
4919 continue;
4920
4921 config->sprites_enabled |= wm->sprites_enabled;
4922 config->sprites_scaled |= wm->sprites_scaled;
4923 config->num_pipes_active++;
4924 }
4925}
4926
Matt Ropered4a6a72016-02-23 17:20:13 -08004927static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03004928{
Chris Wilson91c8a322016-07-05 10:40:23 +01004929 struct drm_device *dev = &dev_priv->drm;
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004930 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02004931 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02004932 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02004933 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03004934 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07004935
Ville Syrjäläd8905652016-01-14 14:53:35 +02004936 ilk_compute_wm_config(dev, &config);
4937
4938 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
4939 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03004940
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03004941 /* 5/6 split only in single pipe config on IVB+ */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00004942 if (INTEL_GEN(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02004943 config.num_pipes_active == 1 && config.sprites_enabled) {
4944 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
4945 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03004946
Imre Deak820c1982013-12-17 14:46:36 +02004947 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03004948 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03004949 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03004950 }
4951
Ville Syrjälä198a1e92013-10-09 19:17:58 +03004952 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03004953 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03004954
Imre Deak820c1982013-12-17 14:46:36 +02004955 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03004956
Imre Deak820c1982013-12-17 14:46:36 +02004957 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03004958}
4959
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004960static void ilk_initial_watermarks(struct intel_atomic_state *state,
4961 struct intel_crtc_state *cstate)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004962{
Matt Ropered4a6a72016-02-23 17:20:13 -08004963 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
4964 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004965
Matt Ropered4a6a72016-02-23 17:20:13 -08004966 mutex_lock(&dev_priv->wm.wm_mutex);
Matt Ropere8f1f022016-05-12 07:05:55 -07004967 intel_crtc->wm.active.ilk = cstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08004968 ilk_program_watermarks(dev_priv);
4969 mutex_unlock(&dev_priv->wm.wm_mutex);
4970}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004971
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004972static void ilk_optimize_watermarks(struct intel_atomic_state *state,
4973 struct intel_crtc_state *cstate)
Matt Ropered4a6a72016-02-23 17:20:13 -08004974{
4975 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
4976 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
4977
4978 mutex_lock(&dev_priv->wm.wm_mutex);
4979 if (cstate->wm.need_postvbl_update) {
Matt Ropere8f1f022016-05-12 07:05:55 -07004980 intel_crtc->wm.active.ilk = cstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08004981 ilk_program_watermarks(dev_priv);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004982 }
Matt Ropered4a6a72016-02-23 17:20:13 -08004983 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004984}
4985
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004986static inline void skl_wm_level_from_reg_val(uint32_t val,
4987 struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00004988{
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004989 level->plane_en = val & PLANE_WM_EN;
4990 level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
4991 level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
4992 PLANE_WM_LINES_MASK;
Pradeep Bhat30789992014-11-04 17:06:45 +00004993}
4994
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004995void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc,
4996 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00004997{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004998 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Pradeep Bhat30789992014-11-04 17:06:45 +00004999 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Pradeep Bhat30789992014-11-04 17:06:45 +00005000 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005001 int level, max_level;
5002 enum plane_id plane_id;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005003 uint32_t val;
Pradeep Bhat30789992014-11-04 17:06:45 +00005004
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005005 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00005006
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005007 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
5008 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00005009
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005010 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005011 if (plane_id != PLANE_CURSOR)
5012 val = I915_READ(PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005013 else
5014 val = I915_READ(CUR_WM(pipe, level));
5015
5016 skl_wm_level_from_reg_val(val, &wm->wm[level]);
5017 }
5018
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005019 if (plane_id != PLANE_CURSOR)
5020 val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005021 else
5022 val = I915_READ(CUR_WM_TRANS(pipe));
5023
5024 skl_wm_level_from_reg_val(val, &wm->trans_wm);
5025 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005026
Matt Roper3ef00282015-03-09 10:19:24 -07005027 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00005028 return;
5029
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005030 out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00005031}
5032
5033void skl_wm_get_hw_state(struct drm_device *dev)
5034{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005035 struct drm_i915_private *dev_priv = to_i915(dev);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005036 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
Damien Lespiaua269c582014-11-04 17:06:49 +00005037 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00005038 struct drm_crtc *crtc;
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005039 struct intel_crtc *intel_crtc;
5040 struct intel_crtc_state *cstate;
Pradeep Bhat30789992014-11-04 17:06:45 +00005041
Damien Lespiaua269c582014-11-04 17:06:49 +00005042 skl_ddb_get_hw_state(dev_priv, ddb);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005043 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5044 intel_crtc = to_intel_crtc(crtc);
5045 cstate = to_intel_crtc_state(crtc->state);
5046
5047 skl_pipe_wm_get_hw_state(crtc, &cstate->wm.skl.optimal);
5048
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02005049 if (intel_crtc->active)
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005050 hw->dirty_pipes |= drm_crtc_mask(crtc);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005051 }
Matt Ropera1de91e2016-05-12 07:05:57 -07005052
Matt Roper279e99d2016-05-12 07:06:02 -07005053 if (dev_priv->active_crtcs) {
5054 /* Fully recompute DDB on first atomic commit */
5055 dev_priv->wm.distrust_bios_wm = true;
5056 } else {
5057 /* Easy/common case; just sanitize DDB now if everything off */
5058 memset(ddb, 0, sizeof(*ddb));
5059 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005060}
5061
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005062static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
5063{
5064 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005065 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02005066 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005067 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper4e0963c2015-09-24 15:53:15 -07005068 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Matt Ropere8f1f022016-05-12 07:05:55 -07005069 struct intel_pipe_wm *active = &cstate->wm.ilk.optimal;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005070 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005071 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005072 [PIPE_A] = WM0_PIPEA_ILK,
5073 [PIPE_B] = WM0_PIPEB_ILK,
5074 [PIPE_C] = WM0_PIPEC_IVB,
5075 };
5076
5077 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005078 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02005079 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005080
Ville Syrjälä15606532016-05-13 17:55:17 +03005081 memset(active, 0, sizeof(*active));
5082
Matt Roper3ef00282015-03-09 10:19:24 -07005083 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02005084
5085 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005086 u32 tmp = hw->wm_pipe[pipe];
5087
5088 /*
5089 * For active pipes LP0 watermark is marked as
5090 * enabled, and LP1+ watermaks as disabled since
5091 * we can't really reverse compute them in case
5092 * multiple pipes are active.
5093 */
5094 active->wm[0].enable = true;
5095 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
5096 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
5097 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
5098 active->linetime = hw->wm_linetime[pipe];
5099 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005100 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005101
5102 /*
5103 * For inactive pipes, all watermark levels
5104 * should be marked as enabled but zeroed,
5105 * which is what we'd compute them to.
5106 */
5107 for (level = 0; level <= max_level; level++)
5108 active->wm[level].enable = true;
5109 }
Matt Roper4e0963c2015-09-24 15:53:15 -07005110
5111 intel_crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005112}
5113
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005114#define _FW_WM(value, plane) \
5115 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
5116#define _FW_WM_VLV(value, plane) \
5117 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
5118
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005119static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
5120 struct g4x_wm_values *wm)
5121{
5122 uint32_t tmp;
5123
5124 tmp = I915_READ(DSPFW1);
5125 wm->sr.plane = _FW_WM(tmp, SR);
5126 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5127 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
5128 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
5129
5130 tmp = I915_READ(DSPFW2);
5131 wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
5132 wm->sr.fbc = _FW_WM(tmp, FBC_SR);
5133 wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
5134 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
5135 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5136 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
5137
5138 tmp = I915_READ(DSPFW3);
5139 wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
5140 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5141 wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
5142 wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
5143}
5144
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005145static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
5146 struct vlv_wm_values *wm)
5147{
5148 enum pipe pipe;
5149 uint32_t tmp;
5150
5151 for_each_pipe(dev_priv, pipe) {
5152 tmp = I915_READ(VLV_DDL(pipe));
5153
Ville Syrjälä1b313892016-11-28 19:37:08 +02005154 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005155 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005156 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005157 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005158 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005159 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005160 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005161 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
5162 }
5163
5164 tmp = I915_READ(DSPFW1);
5165 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005166 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5167 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
5168 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005169
5170 tmp = I915_READ(DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005171 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
5172 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5173 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005174
5175 tmp = I915_READ(DSPFW3);
5176 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5177
5178 if (IS_CHERRYVIEW(dev_priv)) {
5179 tmp = I915_READ(DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005180 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5181 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005182
5183 tmp = I915_READ(DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005184 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
5185 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005186
5187 tmp = I915_READ(DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005188 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
5189 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005190
5191 tmp = I915_READ(DSPHOWM);
5192 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005193 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
5194 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
5195 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
5196 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5197 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5198 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5199 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5200 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5201 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005202 } else {
5203 tmp = I915_READ(DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005204 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5205 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005206
5207 tmp = I915_READ(DSPHOWM);
5208 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005209 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5210 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5211 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5212 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5213 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5214 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005215 }
5216}
5217
5218#undef _FW_WM
5219#undef _FW_WM_VLV
5220
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005221void g4x_wm_get_hw_state(struct drm_device *dev)
5222{
5223 struct drm_i915_private *dev_priv = to_i915(dev);
5224 struct g4x_wm_values *wm = &dev_priv->wm.g4x;
5225 struct intel_crtc *crtc;
5226
5227 g4x_read_wm_values(dev_priv, wm);
5228
5229 wm->cxsr = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
5230
5231 for_each_intel_crtc(dev, crtc) {
5232 struct intel_crtc_state *crtc_state =
5233 to_intel_crtc_state(crtc->base.state);
5234 struct g4x_wm_state *active = &crtc->wm.active.g4x;
5235 struct g4x_pipe_wm *raw;
5236 enum pipe pipe = crtc->pipe;
5237 enum plane_id plane_id;
5238 int level, max_level;
5239
5240 active->cxsr = wm->cxsr;
5241 active->hpll_en = wm->hpll_en;
5242 active->fbc_en = wm->fbc_en;
5243
5244 active->sr = wm->sr;
5245 active->hpll = wm->hpll;
5246
5247 for_each_plane_id_on_crtc(crtc, plane_id) {
5248 active->wm.plane[plane_id] =
5249 wm->pipe[pipe].plane[plane_id];
5250 }
5251
5252 if (wm->cxsr && wm->hpll_en)
5253 max_level = G4X_WM_LEVEL_HPLL;
5254 else if (wm->cxsr)
5255 max_level = G4X_WM_LEVEL_SR;
5256 else
5257 max_level = G4X_WM_LEVEL_NORMAL;
5258
5259 level = G4X_WM_LEVEL_NORMAL;
5260 raw = &crtc_state->wm.g4x.raw[level];
5261 for_each_plane_id_on_crtc(crtc, plane_id)
5262 raw->plane[plane_id] = active->wm.plane[plane_id];
5263
5264 if (++level > max_level)
5265 goto out;
5266
5267 raw = &crtc_state->wm.g4x.raw[level];
5268 raw->plane[PLANE_PRIMARY] = active->sr.plane;
5269 raw->plane[PLANE_CURSOR] = active->sr.cursor;
5270 raw->plane[PLANE_SPRITE0] = 0;
5271 raw->fbc = active->sr.fbc;
5272
5273 if (++level > max_level)
5274 goto out;
5275
5276 raw = &crtc_state->wm.g4x.raw[level];
5277 raw->plane[PLANE_PRIMARY] = active->hpll.plane;
5278 raw->plane[PLANE_CURSOR] = active->hpll.cursor;
5279 raw->plane[PLANE_SPRITE0] = 0;
5280 raw->fbc = active->hpll.fbc;
5281
5282 out:
5283 for_each_plane_id_on_crtc(crtc, plane_id)
5284 g4x_raw_plane_wm_set(crtc_state, level,
5285 plane_id, USHRT_MAX);
5286 g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
5287
5288 crtc_state->wm.g4x.optimal = *active;
5289 crtc_state->wm.g4x.intermediate = *active;
5290
5291 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
5292 pipe_name(pipe),
5293 wm->pipe[pipe].plane[PLANE_PRIMARY],
5294 wm->pipe[pipe].plane[PLANE_CURSOR],
5295 wm->pipe[pipe].plane[PLANE_SPRITE0]);
5296 }
5297
5298 DRM_DEBUG_KMS("Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
5299 wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
5300 DRM_DEBUG_KMS("Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
5301 wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
5302 DRM_DEBUG_KMS("Initial SR=%s HPLL=%s FBC=%s\n",
5303 yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
5304}
5305
5306void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
5307{
5308 struct intel_plane *plane;
5309 struct intel_crtc *crtc;
5310
5311 mutex_lock(&dev_priv->wm.wm_mutex);
5312
5313 for_each_intel_plane(&dev_priv->drm, plane) {
5314 struct intel_crtc *crtc =
5315 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
5316 struct intel_crtc_state *crtc_state =
5317 to_intel_crtc_state(crtc->base.state);
5318 struct intel_plane_state *plane_state =
5319 to_intel_plane_state(plane->base.state);
5320 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
5321 enum plane_id plane_id = plane->id;
5322 int level;
5323
5324 if (plane_state->base.visible)
5325 continue;
5326
5327 for (level = 0; level < 3; level++) {
5328 struct g4x_pipe_wm *raw =
5329 &crtc_state->wm.g4x.raw[level];
5330
5331 raw->plane[plane_id] = 0;
5332 wm_state->wm.plane[plane_id] = 0;
5333 }
5334
5335 if (plane_id == PLANE_PRIMARY) {
5336 for (level = 0; level < 3; level++) {
5337 struct g4x_pipe_wm *raw =
5338 &crtc_state->wm.g4x.raw[level];
5339 raw->fbc = 0;
5340 }
5341
5342 wm_state->sr.fbc = 0;
5343 wm_state->hpll.fbc = 0;
5344 wm_state->fbc_en = false;
5345 }
5346 }
5347
5348 for_each_intel_crtc(&dev_priv->drm, crtc) {
5349 struct intel_crtc_state *crtc_state =
5350 to_intel_crtc_state(crtc->base.state);
5351
5352 crtc_state->wm.g4x.intermediate =
5353 crtc_state->wm.g4x.optimal;
5354 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
5355 }
5356
5357 g4x_program_watermarks(dev_priv);
5358
5359 mutex_unlock(&dev_priv->wm.wm_mutex);
5360}
5361
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005362void vlv_wm_get_hw_state(struct drm_device *dev)
5363{
5364 struct drm_i915_private *dev_priv = to_i915(dev);
5365 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02005366 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005367 u32 val;
5368
5369 vlv_read_wm_values(dev_priv, wm);
5370
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005371 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
5372 wm->level = VLV_WM_LEVEL_PM2;
5373
5374 if (IS_CHERRYVIEW(dev_priv)) {
5375 mutex_lock(&dev_priv->rps.hw_lock);
5376
5377 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5378 if (val & DSP_MAXFIFO_PM5_ENABLE)
5379 wm->level = VLV_WM_LEVEL_PM5;
5380
Ville Syrjälä58590c12015-09-08 21:05:12 +03005381 /*
5382 * If DDR DVFS is disabled in the BIOS, Punit
5383 * will never ack the request. So if that happens
5384 * assume we don't have to enable/disable DDR DVFS
5385 * dynamically. To test that just set the REQ_ACK
5386 * bit to poke the Punit, but don't change the
5387 * HIGH/LOW bits so that we don't actually change
5388 * the current state.
5389 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005390 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03005391 val |= FORCE_DDR_FREQ_REQ_ACK;
5392 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
5393
5394 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
5395 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
5396 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
5397 "assuming DDR DVFS is disabled\n");
5398 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
5399 } else {
5400 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
5401 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
5402 wm->level = VLV_WM_LEVEL_DDR_DVFS;
5403 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005404
5405 mutex_unlock(&dev_priv->rps.hw_lock);
5406 }
5407
Ville Syrjäläff32c542017-03-02 19:14:57 +02005408 for_each_intel_crtc(dev, crtc) {
5409 struct intel_crtc_state *crtc_state =
5410 to_intel_crtc_state(crtc->base.state);
5411 struct vlv_wm_state *active = &crtc->wm.active.vlv;
5412 const struct vlv_fifo_state *fifo_state =
5413 &crtc_state->wm.vlv.fifo_state;
5414 enum pipe pipe = crtc->pipe;
5415 enum plane_id plane_id;
5416 int level;
5417
5418 vlv_get_fifo_size(crtc_state);
5419
5420 active->num_levels = wm->level + 1;
5421 active->cxsr = wm->cxsr;
5422
Ville Syrjäläff32c542017-03-02 19:14:57 +02005423 for (level = 0; level < active->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03005424 struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02005425 &crtc_state->wm.vlv.raw[level];
5426
5427 active->sr[level].plane = wm->sr.plane;
5428 active->sr[level].cursor = wm->sr.cursor;
5429
5430 for_each_plane_id_on_crtc(crtc, plane_id) {
5431 active->wm[level].plane[plane_id] =
5432 wm->pipe[pipe].plane[plane_id];
5433
5434 raw->plane[plane_id] =
5435 vlv_invert_wm_value(active->wm[level].plane[plane_id],
5436 fifo_state->plane[plane_id]);
5437 }
5438 }
5439
5440 for_each_plane_id_on_crtc(crtc, plane_id)
5441 vlv_raw_plane_wm_set(crtc_state, level,
5442 plane_id, USHRT_MAX);
5443 vlv_invalidate_wms(crtc, active, level);
5444
5445 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02005446 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02005447
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005448 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 +02005449 pipe_name(pipe),
5450 wm->pipe[pipe].plane[PLANE_PRIMARY],
5451 wm->pipe[pipe].plane[PLANE_CURSOR],
5452 wm->pipe[pipe].plane[PLANE_SPRITE0],
5453 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02005454 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005455
5456 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
5457 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
5458}
5459
Ville Syrjälä602ae832017-03-02 19:15:02 +02005460void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
5461{
5462 struct intel_plane *plane;
5463 struct intel_crtc *crtc;
5464
5465 mutex_lock(&dev_priv->wm.wm_mutex);
5466
5467 for_each_intel_plane(&dev_priv->drm, plane) {
5468 struct intel_crtc *crtc =
5469 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
5470 struct intel_crtc_state *crtc_state =
5471 to_intel_crtc_state(crtc->base.state);
5472 struct intel_plane_state *plane_state =
5473 to_intel_plane_state(plane->base.state);
5474 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
5475 const struct vlv_fifo_state *fifo_state =
5476 &crtc_state->wm.vlv.fifo_state;
5477 enum plane_id plane_id = plane->id;
5478 int level;
5479
5480 if (plane_state->base.visible)
5481 continue;
5482
5483 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03005484 struct g4x_pipe_wm *raw =
Ville Syrjälä602ae832017-03-02 19:15:02 +02005485 &crtc_state->wm.vlv.raw[level];
5486
5487 raw->plane[plane_id] = 0;
5488
5489 wm_state->wm[level].plane[plane_id] =
5490 vlv_invert_wm_value(raw->plane[plane_id],
5491 fifo_state->plane[plane_id]);
5492 }
5493 }
5494
5495 for_each_intel_crtc(&dev_priv->drm, crtc) {
5496 struct intel_crtc_state *crtc_state =
5497 to_intel_crtc_state(crtc->base.state);
5498
5499 crtc_state->wm.vlv.intermediate =
5500 crtc_state->wm.vlv.optimal;
5501 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
5502 }
5503
5504 vlv_program_watermarks(dev_priv);
5505
5506 mutex_unlock(&dev_priv->wm.wm_mutex);
5507}
5508
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005509void ilk_wm_get_hw_state(struct drm_device *dev)
5510{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005511 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02005512 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005513 struct drm_crtc *crtc;
5514
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01005515 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005516 ilk_pipe_wm_get_hw_state(crtc);
5517
5518 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
5519 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
5520 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
5521
5522 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00005523 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjäläcfa76982014-03-07 18:32:08 +02005524 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
5525 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
5526 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005527
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005528 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02005529 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
5530 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01005531 else if (IS_IVYBRIDGE(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02005532 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
5533 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005534
5535 hw->enable_fbc_wm =
5536 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
5537}
5538
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005539/**
5540 * intel_update_watermarks - update FIFO watermark values based on current modes
5541 *
5542 * Calculate watermark values for the various WM regs based on current mode
5543 * and plane configuration.
5544 *
5545 * There are several cases to deal with here:
5546 * - normal (i.e. non-self-refresh)
5547 * - self-refresh (SR) mode
5548 * - lines are large relative to FIFO size (buffer can hold up to 2)
5549 * - lines are small relative to FIFO size (buffer can hold more than 2
5550 * lines), so need to account for TLB latency
5551 *
5552 * The normal calculation is:
5553 * watermark = dotclock * bytes per pixel * latency
5554 * where latency is platform & configuration dependent (we assume pessimal
5555 * values here).
5556 *
5557 * The SR calculation is:
5558 * watermark = (trunc(latency/line time)+1) * surface width *
5559 * bytes per pixel
5560 * where
5561 * line time = htotal / dotclock
5562 * surface width = hdisplay for normal plane and 64 for cursor
5563 * and latency is assumed to be high, as above.
5564 *
5565 * The final value programmed to the register should always be rounded up,
5566 * and include an extra 2 entries to account for clock crossings.
5567 *
5568 * We don't use the sprite, so we can ignore that. And on Crestline we have
5569 * to set the non-SR watermarks to 8.
5570 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02005571void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005572{
Ville Syrjälä432081b2016-10-31 22:37:03 +02005573 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005574
5575 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005576 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005577}
5578
Jani Nikulae2828912016-01-18 09:19:47 +02005579/*
Daniel Vetter92703882012-08-09 16:46:01 +02005580 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02005581 */
5582DEFINE_SPINLOCK(mchdev_lock);
5583
5584/* Global for IPS driver to get at the current i915 device. Protected by
5585 * mchdev_lock. */
5586static struct drm_i915_private *i915_mch_dev;
5587
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005588bool ironlake_set_drps(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005589{
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005590 u16 rgvswctl;
5591
Chris Wilson67520412017-03-02 13:28:01 +00005592 lockdep_assert_held(&mchdev_lock);
Daniel Vetter92703882012-08-09 16:46:01 +02005593
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005594 rgvswctl = I915_READ16(MEMSWCTL);
5595 if (rgvswctl & MEMCTL_CMD_STS) {
5596 DRM_DEBUG("gpu busy, RCS change rejected\n");
5597 return false; /* still busy with another command */
5598 }
5599
5600 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
5601 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
5602 I915_WRITE16(MEMSWCTL, rgvswctl);
5603 POSTING_READ16(MEMSWCTL);
5604
5605 rgvswctl |= MEMCTL_CMD_STS;
5606 I915_WRITE16(MEMSWCTL, rgvswctl);
5607
5608 return true;
5609}
5610
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005611static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005612{
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00005613 u32 rgvmodectl;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005614 u8 fmax, fmin, fstart, vstart;
5615
Daniel Vetter92703882012-08-09 16:46:01 +02005616 spin_lock_irq(&mchdev_lock);
5617
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00005618 rgvmodectl = I915_READ(MEMMODECTL);
5619
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005620 /* Enable temp reporting */
5621 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
5622 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
5623
5624 /* 100ms RC evaluation intervals */
5625 I915_WRITE(RCUPEI, 100000);
5626 I915_WRITE(RCDNEI, 100000);
5627
5628 /* Set max/min thresholds to 90ms and 80ms respectively */
5629 I915_WRITE(RCBMAXAVG, 90000);
5630 I915_WRITE(RCBMINAVG, 80000);
5631
5632 I915_WRITE(MEMIHYST, 1);
5633
5634 /* Set up min, max, and cur for interrupt handling */
5635 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
5636 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
5637 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
5638 MEMMODE_FSTART_SHIFT;
5639
Ville Syrjälä616847e2015-09-18 20:03:19 +03005640 vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005641 PXVFREQ_PX_SHIFT;
5642
Daniel Vetter20e4d402012-08-08 23:35:39 +02005643 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
5644 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005645
Daniel Vetter20e4d402012-08-08 23:35:39 +02005646 dev_priv->ips.max_delay = fstart;
5647 dev_priv->ips.min_delay = fmin;
5648 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005649
5650 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
5651 fmax, fmin, fstart);
5652
5653 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
5654
5655 /*
5656 * Interrupts will be enabled in ironlake_irq_postinstall
5657 */
5658
5659 I915_WRITE(VIDSTART, vstart);
5660 POSTING_READ(VIDSTART);
5661
5662 rgvmodectl |= MEMMODE_SWMODE_EN;
5663 I915_WRITE(MEMMODECTL, rgvmodectl);
5664
Daniel Vetter92703882012-08-09 16:46:01 +02005665 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005666 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005667 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005668
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005669 ironlake_set_drps(dev_priv, fstart);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005670
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03005671 dev_priv->ips.last_count1 = I915_READ(DMIEC) +
5672 I915_READ(DDREC) + I915_READ(CSIEC);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005673 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03005674 dev_priv->ips.last_count2 = I915_READ(GFXEC);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005675 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02005676
5677 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005678}
5679
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005680static void ironlake_disable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005681{
Daniel Vetter92703882012-08-09 16:46:01 +02005682 u16 rgvswctl;
5683
5684 spin_lock_irq(&mchdev_lock);
5685
5686 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005687
5688 /* Ack interrupts, disable EFC interrupt */
5689 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
5690 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
5691 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
5692 I915_WRITE(DEIIR, DE_PCU_EVENT);
5693 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
5694
5695 /* Go back to the starting frequency */
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005696 ironlake_set_drps(dev_priv, dev_priv->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005697 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005698 rgvswctl |= MEMCTL_CMD_STS;
5699 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005700 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005701
Daniel Vetter92703882012-08-09 16:46:01 +02005702 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005703}
5704
Daniel Vetteracbe9472012-07-26 11:50:05 +02005705/* There's a funny hw issue where the hw returns all 0 when reading from
5706 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
5707 * ourselves, instead of doing a rmw cycle (which might result in us clearing
5708 * all limits and the gpu stuck at whatever frequency it is at atm).
5709 */
Akash Goel74ef1172015-03-06 11:07:19 +05305710static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005711{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005712 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005713
Daniel Vetter20b46e52012-07-26 11:16:14 +02005714 /* Only set the down limit when we've reached the lowest level to avoid
5715 * getting more interrupts, otherwise leave this clear. This prevents a
5716 * race in the hw when coming out of rc6: There's a tiny window where
5717 * the hw runs at the minimal clock before selecting the desired
5718 * frequency, if the down threshold expires in that window we will not
5719 * receive a down interrupt. */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03005720 if (IS_GEN9(dev_priv)) {
Akash Goel74ef1172015-03-06 11:07:19 +05305721 limits = (dev_priv->rps.max_freq_softlimit) << 23;
5722 if (val <= dev_priv->rps.min_freq_softlimit)
5723 limits |= (dev_priv->rps.min_freq_softlimit) << 14;
5724 } else {
5725 limits = dev_priv->rps.max_freq_softlimit << 24;
5726 if (val <= dev_priv->rps.min_freq_softlimit)
5727 limits |= dev_priv->rps.min_freq_softlimit << 16;
5728 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02005729
5730 return limits;
5731}
5732
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005733static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
5734{
5735 int new_power;
Akash Goel8a586432015-03-06 11:07:18 +05305736 u32 threshold_up = 0, threshold_down = 0; /* in % */
5737 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005738
5739 new_power = dev_priv->rps.power;
5740 switch (dev_priv->rps.power) {
5741 case LOW_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01005742 if (val > dev_priv->rps.efficient_freq + 1 &&
5743 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005744 new_power = BETWEEN;
5745 break;
5746
5747 case BETWEEN:
Chris Wilsona72b5622016-07-02 15:35:59 +01005748 if (val <= dev_priv->rps.efficient_freq &&
5749 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005750 new_power = LOW_POWER;
Chris Wilsona72b5622016-07-02 15:35:59 +01005751 else if (val >= dev_priv->rps.rp0_freq &&
5752 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005753 new_power = HIGH_POWER;
5754 break;
5755
5756 case HIGH_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01005757 if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 &&
5758 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005759 new_power = BETWEEN;
5760 break;
5761 }
5762 /* Max/min bins are special */
Chris Wilsonaed242f2015-03-18 09:48:21 +00005763 if (val <= dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005764 new_power = LOW_POWER;
Chris Wilsonaed242f2015-03-18 09:48:21 +00005765 if (val >= dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005766 new_power = HIGH_POWER;
5767 if (new_power == dev_priv->rps.power)
5768 return;
5769
5770 /* Note the units here are not exactly 1us, but 1280ns. */
5771 switch (new_power) {
5772 case LOW_POWER:
5773 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05305774 ei_up = 16000;
5775 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005776
5777 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305778 ei_down = 32000;
5779 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005780 break;
5781
5782 case BETWEEN:
5783 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05305784 ei_up = 13000;
5785 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005786
5787 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305788 ei_down = 32000;
5789 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005790 break;
5791
5792 case HIGH_POWER:
5793 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05305794 ei_up = 10000;
5795 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005796
5797 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305798 ei_down = 32000;
5799 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005800 break;
5801 }
5802
Mika Kuoppala6067a272017-02-15 15:52:59 +02005803 /* When byt can survive without system hang with dynamic
5804 * sw freq adjustments, this restriction can be lifted.
5805 */
5806 if (IS_VALLEYVIEW(dev_priv))
5807 goto skip_hw_write;
5808
Akash Goel8a586432015-03-06 11:07:18 +05305809 I915_WRITE(GEN6_RP_UP_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01005810 GT_INTERVAL_FROM_US(dev_priv, ei_up));
Akash Goel8a586432015-03-06 11:07:18 +05305811 I915_WRITE(GEN6_RP_UP_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01005812 GT_INTERVAL_FROM_US(dev_priv,
5813 ei_up * threshold_up / 100));
Akash Goel8a586432015-03-06 11:07:18 +05305814
5815 I915_WRITE(GEN6_RP_DOWN_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01005816 GT_INTERVAL_FROM_US(dev_priv, ei_down));
Akash Goel8a586432015-03-06 11:07:18 +05305817 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01005818 GT_INTERVAL_FROM_US(dev_priv,
5819 ei_down * threshold_down / 100));
Akash Goel8a586432015-03-06 11:07:18 +05305820
Chris Wilsona72b5622016-07-02 15:35:59 +01005821 I915_WRITE(GEN6_RP_CONTROL,
5822 GEN6_RP_MEDIA_TURBO |
5823 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5824 GEN6_RP_MEDIA_IS_GFX |
5825 GEN6_RP_ENABLE |
5826 GEN6_RP_UP_BUSY_AVG |
5827 GEN6_RP_DOWN_IDLE_AVG);
Akash Goel8a586432015-03-06 11:07:18 +05305828
Mika Kuoppala6067a272017-02-15 15:52:59 +02005829skip_hw_write:
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005830 dev_priv->rps.power = new_power;
Chris Wilson8fb55192015-04-07 16:20:28 +01005831 dev_priv->rps.up_threshold = threshold_up;
5832 dev_priv->rps.down_threshold = threshold_down;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005833 dev_priv->rps.last_adj = 0;
5834}
5835
Chris Wilson2876ce72014-03-28 08:03:34 +00005836static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
5837{
5838 u32 mask = 0;
5839
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00005840 /* We use UP_EI_EXPIRED interupts for both up/down in manual mode */
Chris Wilson2876ce72014-03-28 08:03:34 +00005841 if (val > dev_priv->rps.min_freq_softlimit)
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00005842 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
Chris Wilson2876ce72014-03-28 08:03:34 +00005843 if (val < dev_priv->rps.max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00005844 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00005845
Chris Wilson7b3c29f2014-07-10 20:31:19 +01005846 mask &= dev_priv->pm_rps_events;
5847
Imre Deak59d02a12014-12-19 19:33:26 +02005848 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00005849}
5850
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06005851/* gen6_set_rps is called to update the frequency request, but should also be
5852 * called when the range (min_delay and max_delay) is modified so that we can
5853 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005854static int gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02005855{
Chris Wilsoneb64cad2014-03-27 08:24:20 +00005856 /* min/max delay may still have been modified so be sure to
5857 * write the limits value.
5858 */
5859 if (val != dev_priv->rps.cur_freq) {
5860 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06005861
Chris Wilsondc979972016-05-10 14:10:04 +01005862 if (IS_GEN9(dev_priv))
Akash Goel57041952015-03-06 11:07:17 +05305863 I915_WRITE(GEN6_RPNSWREQ,
5864 GEN9_FREQUENCY(val));
Chris Wilsondc979972016-05-10 14:10:04 +01005865 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00005866 I915_WRITE(GEN6_RPNSWREQ,
5867 HSW_FREQUENCY(val));
5868 else
5869 I915_WRITE(GEN6_RPNSWREQ,
5870 GEN6_FREQUENCY(val) |
5871 GEN6_OFFSET(0) |
5872 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06005873 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005874
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005875 /* Make sure we continue to get interrupts
5876 * until we hit the minimum or maximum frequencies.
5877 */
Akash Goel74ef1172015-03-06 11:07:19 +05305878 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00005879 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005880
Ben Widawskyb39fb292014-03-19 18:31:11 -07005881 dev_priv->rps.cur_freq = val;
Mika Kuoppala0f945922015-11-17 18:14:26 +02005882 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005883
5884 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005885}
5886
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005887static int valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005888{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005889 int err;
5890
Chris Wilsondc979972016-05-10 14:10:04 +01005891 if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005892 "Odd GPU freq value\n"))
5893 val &= ~1;
5894
Deepak Scd25dd52015-07-10 18:31:40 +05305895 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
5896
Chris Wilson8fb55192015-04-07 16:20:28 +01005897 if (val != dev_priv->rps.cur_freq) {
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005898 err = vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
5899 if (err)
5900 return err;
5901
Chris Wilsondb4c5e02017-02-10 15:03:46 +00005902 gen6_set_rps_thresholds(dev_priv, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01005903 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005904
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005905 dev_priv->rps.cur_freq = val;
5906 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005907
5908 return 0;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005909}
5910
Deepak Sa7f6e232015-05-09 18:04:44 +05305911/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05305912 *
5913 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05305914 * 1. Forcewake Media well.
5915 * 2. Request idle freq.
5916 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05305917*/
5918static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
5919{
Chris Wilsonaed242f2015-03-18 09:48:21 +00005920 u32 val = dev_priv->rps.idle_freq;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005921 int err;
Deepak S5549d252014-06-28 11:26:11 +05305922
Chris Wilsonaed242f2015-03-18 09:48:21 +00005923 if (dev_priv->rps.cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05305924 return;
5925
Chris Wilsonc9efef72017-01-02 15:28:45 +00005926 /* The punit delays the write of the frequency and voltage until it
5927 * determines the GPU is awake. During normal usage we don't want to
5928 * waste power changing the frequency if the GPU is sleeping (rc6).
5929 * However, the GPU and driver is now idle and we do not want to delay
5930 * switching to minimum voltage (reducing power whilst idle) as we do
5931 * not expect to be woken in the near future and so must flush the
5932 * change by waking the device.
5933 *
5934 * We choose to take the media powerwell (either would do to trick the
5935 * punit into committing the voltage change) as that takes a lot less
5936 * power than the render powerwell.
5937 */
Deepak Sa7f6e232015-05-09 18:04:44 +05305938 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005939 err = valleyview_set_rps(dev_priv, val);
Deepak Sa7f6e232015-05-09 18:04:44 +05305940 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005941
5942 if (err)
5943 DRM_ERROR("Failed to set RPS for idle\n");
Deepak S76c3552f2014-01-30 23:08:16 +05305944}
5945
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005946void gen6_rps_busy(struct drm_i915_private *dev_priv)
5947{
5948 mutex_lock(&dev_priv->rps.hw_lock);
5949 if (dev_priv->rps.enabled) {
Chris Wilsonbd648182017-02-10 15:03:48 +00005950 u8 freq;
5951
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00005952 if (dev_priv->pm_rps_events & GEN6_PM_RP_UP_EI_EXPIRED)
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005953 gen6_rps_reset_ei(dev_priv);
5954 I915_WRITE(GEN6_PMINTRMSK,
5955 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Michał Winiarski2b83c4c2016-06-20 11:58:27 +02005956
Chris Wilsonc33d2472016-07-04 08:08:36 +01005957 gen6_enable_rps_interrupts(dev_priv);
5958
Chris Wilsonbd648182017-02-10 15:03:48 +00005959 /* Use the user's desired frequency as a guide, but for better
5960 * performance, jump directly to RPe as our starting frequency.
5961 */
5962 freq = max(dev_priv->rps.cur_freq,
5963 dev_priv->rps.efficient_freq);
5964
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005965 if (intel_set_rps(dev_priv,
Chris Wilsonbd648182017-02-10 15:03:48 +00005966 clamp(freq,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005967 dev_priv->rps.min_freq_softlimit,
5968 dev_priv->rps.max_freq_softlimit)))
5969 DRM_DEBUG_DRIVER("Failed to set idle frequency\n");
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005970 }
5971 mutex_unlock(&dev_priv->rps.hw_lock);
5972}
5973
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005974void gen6_rps_idle(struct drm_i915_private *dev_priv)
5975{
Chris Wilsonc33d2472016-07-04 08:08:36 +01005976 /* Flush our bottom-half so that it does not race with us
5977 * setting the idle frequency and so that it is bounded by
5978 * our rpm wakeref. And then disable the interrupts to stop any
5979 * futher RPS reclocking whilst we are asleep.
5980 */
5981 gen6_disable_rps_interrupts(dev_priv);
5982
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005983 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01005984 if (dev_priv->rps.enabled) {
Chris Wilsondc979972016-05-10 14:10:04 +01005985 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Deepak S76c3552f2014-01-30 23:08:16 +05305986 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02005987 else
Chris Wilsondc979972016-05-10 14:10:04 +01005988 gen6_set_rps(dev_priv, dev_priv->rps.idle_freq);
Chris Wilsonc0951f02013-10-10 21:58:50 +01005989 dev_priv->rps.last_adj = 0;
Ville Syrjälä12c100b2016-05-23 17:42:48 +03005990 I915_WRITE(GEN6_PMINTRMSK,
5991 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Chris Wilsonc0951f02013-10-10 21:58:50 +01005992 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01005993 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005994
Chris Wilson8d3afd72015-05-21 21:01:47 +01005995 spin_lock(&dev_priv->rps.client_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005996 while (!list_empty(&dev_priv->rps.clients))
5997 list_del_init(dev_priv->rps.clients.next);
Chris Wilson8d3afd72015-05-21 21:01:47 +01005998 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005999}
6000
Chris Wilson1854d5c2015-04-07 16:20:32 +01006001void gen6_rps_boost(struct drm_i915_private *dev_priv,
Chris Wilsone61b9952015-04-27 13:41:24 +01006002 struct intel_rps_client *rps,
6003 unsigned long submitted)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006004{
Chris Wilson8d3afd72015-05-21 21:01:47 +01006005 /* This is intentionally racy! We peek at the state here, then
6006 * validate inside the RPS worker.
6007 */
Chris Wilson67d97da2016-07-04 08:08:31 +01006008 if (!(dev_priv->gt.awake &&
Chris Wilson8d3afd72015-05-21 21:01:47 +01006009 dev_priv->rps.enabled &&
Chris Wilson29ecd78d2016-07-13 09:10:35 +01006010 dev_priv->rps.cur_freq < dev_priv->rps.boost_freq))
Chris Wilson8d3afd72015-05-21 21:01:47 +01006011 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006012
Chris Wilsone61b9952015-04-27 13:41:24 +01006013 /* Force a RPS boost (and don't count it against the client) if
6014 * the GPU is severely congested.
6015 */
Chris Wilsond0bc54f2015-05-21 21:01:48 +01006016 if (rps && time_after(jiffies, submitted + DRM_I915_THROTTLE_JIFFIES))
Chris Wilsone61b9952015-04-27 13:41:24 +01006017 rps = NULL;
6018
Chris Wilson8d3afd72015-05-21 21:01:47 +01006019 spin_lock(&dev_priv->rps.client_lock);
6020 if (rps == NULL || list_empty(&rps->link)) {
6021 spin_lock_irq(&dev_priv->irq_lock);
6022 if (dev_priv->rps.interrupts_enabled) {
6023 dev_priv->rps.client_boost = true;
Chris Wilsonc33d2472016-07-04 08:08:36 +01006024 schedule_work(&dev_priv->rps.work);
Chris Wilson8d3afd72015-05-21 21:01:47 +01006025 }
6026 spin_unlock_irq(&dev_priv->irq_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01006027
Chris Wilson2e1b8732015-04-27 13:41:22 +01006028 if (rps != NULL) {
6029 list_add(&rps->link, &dev_priv->rps.clients);
6030 rps->boosts++;
Chris Wilson1854d5c2015-04-07 16:20:32 +01006031 } else
6032 dev_priv->rps.boosts++;
Chris Wilsonc0951f02013-10-10 21:58:50 +01006033 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01006034 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006035}
6036
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006037int intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006038{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006039 int err;
6040
Chris Wilsoncfd1c482017-02-20 09:47:07 +00006041 lockdep_assert_held(&dev_priv->rps.hw_lock);
6042 GEM_BUG_ON(val > dev_priv->rps.max_freq);
6043 GEM_BUG_ON(val < dev_priv->rps.min_freq);
6044
Chris Wilson76e4e4b2017-02-20 09:47:08 +00006045 if (!dev_priv->rps.enabled) {
6046 dev_priv->rps.cur_freq = val;
6047 return 0;
6048 }
6049
Chris Wilsondc979972016-05-10 14:10:04 +01006050 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006051 err = valleyview_set_rps(dev_priv, val);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006052 else
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006053 err = gen6_set_rps(dev_priv, val);
6054
6055 return err;
Jesse Barnes0a073b82013-04-17 15:54:58 -07006056}
6057
Chris Wilsondc979972016-05-10 14:10:04 +01006058static void gen9_disable_rc6(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00006059{
Zhe Wang20e49362014-11-04 17:07:05 +00006060 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00006061 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00006062}
6063
Chris Wilsondc979972016-05-10 14:10:04 +01006064static void gen9_disable_rps(struct drm_i915_private *dev_priv)
Akash Goel2030d682016-04-23 00:05:45 +05306065{
Akash Goel2030d682016-04-23 00:05:45 +05306066 I915_WRITE(GEN6_RP_CONTROL, 0);
6067}
6068
Chris Wilsondc979972016-05-10 14:10:04 +01006069static void gen6_disable_rps(struct drm_i915_private *dev_priv)
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006070{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006071 I915_WRITE(GEN6_RC_CONTROL, 0);
6072 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Akash Goel2030d682016-04-23 00:05:45 +05306073 I915_WRITE(GEN6_RP_CONTROL, 0);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006074}
6075
Chris Wilsondc979972016-05-10 14:10:04 +01006076static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306077{
Deepak S38807742014-05-23 21:00:15 +05306078 I915_WRITE(GEN6_RC_CONTROL, 0);
6079}
6080
Chris Wilsondc979972016-05-10 14:10:04 +01006081static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006082{
Deepak S98a2e5f2014-08-18 10:35:27 -07006083 /* we're doing forcewake before Disabling RC6,
6084 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02006085 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07006086
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006087 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006088
Mika Kuoppala59bad942015-01-16 11:34:40 +02006089 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006090}
6091
Chris Wilsondc979972016-05-10 14:10:04 +01006092static void intel_print_rc6_info(struct drm_i915_private *dev_priv, u32 mode)
Ben Widawskydc39fff2013-10-18 12:32:07 -07006093{
Chris Wilsondc979972016-05-10 14:10:04 +01006094 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Imre Deak91ca6892014-04-14 20:24:25 +03006095 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
6096 mode = GEN6_RC_CTL_RC6_ENABLE;
6097 else
6098 mode = 0;
6099 }
Chris Wilsondc979972016-05-10 14:10:04 +01006100 if (HAS_RC6p(dev_priv))
Imre Deakb99d49c2016-06-29 19:13:54 +03006101 DRM_DEBUG_DRIVER("Enabling RC6 states: "
6102 "RC6 %s RC6p %s RC6pp %s\n",
6103 onoff(mode & GEN6_RC_CTL_RC6_ENABLE),
6104 onoff(mode & GEN6_RC_CTL_RC6p_ENABLE),
6105 onoff(mode & GEN6_RC_CTL_RC6pp_ENABLE));
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07006106
6107 else
Imre Deakb99d49c2016-06-29 19:13:54 +03006108 DRM_DEBUG_DRIVER("Enabling RC6 states: RC6 %s\n",
6109 onoff(mode & GEN6_RC_CTL_RC6_ENABLE));
Ben Widawskydc39fff2013-10-18 12:32:07 -07006110}
6111
Chris Wilsondc979972016-05-10 14:10:04 +01006112static bool bxt_check_bios_rc6_setup(struct drm_i915_private *dev_priv)
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306113{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006114 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306115 bool enable_rc6 = true;
6116 unsigned long rc6_ctx_base;
Imre Deakfc619842016-06-29 19:13:55 +03006117 u32 rc_ctl;
6118 int rc_sw_target;
6119
6120 rc_ctl = I915_READ(GEN6_RC_CONTROL);
6121 rc_sw_target = (I915_READ(GEN6_RC_STATE) & RC_SW_TARGET_STATE_MASK) >>
6122 RC_SW_TARGET_STATE_SHIFT;
6123 DRM_DEBUG_DRIVER("BIOS enabled RC states: "
6124 "HW_CTRL %s HW_RC6 %s SW_TARGET_STATE %x\n",
6125 onoff(rc_ctl & GEN6_RC_CTL_HW_ENABLE),
6126 onoff(rc_ctl & GEN6_RC_CTL_RC6_ENABLE),
6127 rc_sw_target);
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306128
6129 if (!(I915_READ(RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006130 DRM_DEBUG_DRIVER("RC6 Base location not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306131 enable_rc6 = false;
6132 }
6133
6134 /*
6135 * The exact context size is not known for BXT, so assume a page size
6136 * for this check.
6137 */
6138 rc6_ctx_base = I915_READ(RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006139 if (!((rc6_ctx_base >= ggtt->stolen_reserved_base) &&
6140 (rc6_ctx_base + PAGE_SIZE <= ggtt->stolen_reserved_base +
6141 ggtt->stolen_reserved_size))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006142 DRM_DEBUG_DRIVER("RC6 Base address not as expected.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306143 enable_rc6 = false;
6144 }
6145
6146 if (!(((I915_READ(PWRCTX_MAXCNT_RCSUNIT) & IDLE_TIME_MASK) > 1) &&
6147 ((I915_READ(PWRCTX_MAXCNT_VCSUNIT0) & IDLE_TIME_MASK) > 1) &&
6148 ((I915_READ(PWRCTX_MAXCNT_BCSUNIT) & IDLE_TIME_MASK) > 1) &&
6149 ((I915_READ(PWRCTX_MAXCNT_VECSUNIT) & IDLE_TIME_MASK) > 1))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006150 DRM_DEBUG_DRIVER("Engine Idle wait time not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306151 enable_rc6 = false;
6152 }
6153
Imre Deakfc619842016-06-29 19:13:55 +03006154 if (!I915_READ(GEN8_PUSHBUS_CONTROL) ||
6155 !I915_READ(GEN8_PUSHBUS_ENABLE) ||
6156 !I915_READ(GEN8_PUSHBUS_SHIFT)) {
6157 DRM_DEBUG_DRIVER("Pushbus not setup properly.\n");
6158 enable_rc6 = false;
6159 }
6160
6161 if (!I915_READ(GEN6_GFXPAUSE)) {
6162 DRM_DEBUG_DRIVER("GFX pause not setup properly.\n");
6163 enable_rc6 = false;
6164 }
6165
6166 if (!I915_READ(GEN8_MISC_CTRL0)) {
6167 DRM_DEBUG_DRIVER("GPM control not setup properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306168 enable_rc6 = false;
6169 }
6170
6171 return enable_rc6;
6172}
6173
Chris Wilsondc979972016-05-10 14:10:04 +01006174int sanitize_rc6_option(struct drm_i915_private *dev_priv, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006175{
Daniel Vettere7d66d82015-06-15 23:23:54 +02006176 /* No RC6 before Ironlake and code is gone for ilk. */
Chris Wilsondc979972016-05-10 14:10:04 +01006177 if (INTEL_INFO(dev_priv)->gen < 6)
Imre Deake6069ca2014-04-18 16:01:02 +03006178 return 0;
6179
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306180 if (!enable_rc6)
6181 return 0;
6182
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02006183 if (IS_GEN9_LP(dev_priv) && !bxt_check_bios_rc6_setup(dev_priv)) {
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306184 DRM_INFO("RC6 disabled by BIOS\n");
6185 return 0;
6186 }
6187
Daniel Vetter456470e2012-08-08 23:35:40 +02006188 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03006189 if (enable_rc6 >= 0) {
6190 int mask;
6191
Chris Wilsondc979972016-05-10 14:10:04 +01006192 if (HAS_RC6p(dev_priv))
Imre Deake6069ca2014-04-18 16:01:02 +03006193 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
6194 INTEL_RC6pp_ENABLE;
6195 else
6196 mask = INTEL_RC6_ENABLE;
6197
6198 if ((enable_rc6 & mask) != enable_rc6)
Imre Deakb99d49c2016-06-29 19:13:54 +03006199 DRM_DEBUG_DRIVER("Adjusting RC6 mask to %d "
6200 "(requested %d, valid %d)\n",
6201 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03006202
6203 return enable_rc6 & mask;
6204 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006205
Chris Wilsondc979972016-05-10 14:10:04 +01006206 if (IS_IVYBRIDGE(dev_priv))
Ben Widawskycca84a12014-01-28 20:25:38 -08006207 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08006208
6209 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006210}
6211
Chris Wilsondc979972016-05-10 14:10:04 +01006212static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
Imre Deake6069ca2014-04-18 16:01:02 +03006213{
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006214 /* All of these values are in units of 50MHz */
Chris Wilson773ea9a2016-07-13 09:10:33 +01006215
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006216 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02006217 if (IS_GEN9_LP(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006218 u32 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07006219 dev_priv->rps.rp0_freq = (rp_state_cap >> 16) & 0xff;
6220 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
6221 dev_priv->rps.min_freq = (rp_state_cap >> 0) & 0xff;
6222 } else {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006223 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07006224 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
6225 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
6226 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
6227 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006228 /* hw_max = RP0 until we check for overclocking */
Chris Wilson773ea9a2016-07-13 09:10:33 +01006229 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006230
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006231 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01006232 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
Rodrigo Vivib976dc52017-01-23 10:32:37 -08006233 IS_GEN9_BC(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006234 u32 ddcc_status = 0;
6235
6236 if (sandybridge_pcode_read(dev_priv,
6237 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
6238 &ddcc_status) == 0)
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006239 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08006240 clamp_t(u8,
6241 ((ddcc_status >> 8) & 0xff),
6242 dev_priv->rps.min_freq,
6243 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006244 }
6245
Rodrigo Vivib976dc52017-01-23 10:32:37 -08006246 if (IS_GEN9_BC(dev_priv)) {
Akash Goelc5e06882015-06-29 14:50:19 +05306247 /* Store the frequency values in 16.66 MHZ units, which is
Chris Wilson773ea9a2016-07-13 09:10:33 +01006248 * the natural hardware unit for SKL
6249 */
Akash Goelc5e06882015-06-29 14:50:19 +05306250 dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
6251 dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
6252 dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
6253 dev_priv->rps.max_freq *= GEN9_FREQ_SCALER;
6254 dev_priv->rps.efficient_freq *= GEN9_FREQ_SCALER;
6255 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006256}
6257
Chris Wilson3a45b052016-07-13 09:10:32 +01006258static void reset_rps(struct drm_i915_private *dev_priv,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006259 int (*set)(struct drm_i915_private *, u8))
Chris Wilson3a45b052016-07-13 09:10:32 +01006260{
6261 u8 freq = dev_priv->rps.cur_freq;
6262
6263 /* force a reset */
6264 dev_priv->rps.power = -1;
6265 dev_priv->rps.cur_freq = -1;
6266
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006267 if (set(dev_priv, freq))
6268 DRM_ERROR("Failed to reset RPS to initial values\n");
Chris Wilson3a45b052016-07-13 09:10:32 +01006269}
6270
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006271/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Chris Wilsondc979972016-05-10 14:10:04 +01006272static void gen9_enable_rps(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00006273{
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006274 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
6275
Akash Goel0beb0592015-03-06 11:07:20 +05306276 /* Program defaults and thresholds for RPS*/
6277 I915_WRITE(GEN6_RC_VIDEO_FREQ,
6278 GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006279
Akash Goel0beb0592015-03-06 11:07:20 +05306280 /* 1 second timeout*/
6281 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
6282 GT_INTERVAL_FROM_US(dev_priv, 1000000));
6283
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006284 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006285
Akash Goel0beb0592015-03-06 11:07:20 +05306286 /* Leaning on the below call to gen6_set_rps to program/setup the
6287 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
6288 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
Chris Wilson3a45b052016-07-13 09:10:32 +01006289 reset_rps(dev_priv, gen6_set_rps);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006290
6291 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
6292}
6293
Chris Wilsondc979972016-05-10 14:10:04 +01006294static void gen9_enable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006295{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006296 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306297 enum intel_engine_id id;
Zhe Wang20e49362014-11-04 17:07:05 +00006298 uint32_t rc6_mask = 0;
Zhe Wang20e49362014-11-04 17:07:05 +00006299
6300 /* 1a: Software RC state - RC0 */
6301 I915_WRITE(GEN6_RC_STATE, 0);
6302
6303 /* 1b: Get forcewake during program sequence. Although the driver
6304 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006305 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00006306
6307 /* 2a: Disable RC states. */
6308 I915_WRITE(GEN6_RC_CONTROL, 0);
6309
6310 /* 2b: Program RC6 thresholds.*/
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05306311
6312 /* WaRsDoubleRc6WrlWithCoarsePowerGating: Doubling WRL only when CPG is enabled */
Chris Wilsondc979972016-05-10 14:10:04 +01006313 if (IS_SKYLAKE(dev_priv))
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05306314 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
6315 else
6316 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
Zhe Wang20e49362014-11-04 17:07:05 +00006317 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6318 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05306319 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006320 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05306321
Dave Gordon1a3d1892016-05-13 15:36:30 +01006322 if (HAS_GUC(dev_priv))
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05306323 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
6324
Zhe Wang20e49362014-11-04 17:07:05 +00006325 I915_WRITE(GEN6_RC_SLEEP, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00006326
Zhe Wang38c23522015-01-20 12:23:04 +00006327 /* 2c: Program Coarse Power Gating Policies. */
6328 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
6329 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
6330
Zhe Wang20e49362014-11-04 17:07:05 +00006331 /* 3a: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006332 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Zhe Wang20e49362014-11-04 17:07:05 +00006333 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Jani Nikula87ad3212016-01-14 12:53:34 +02006334 DRM_INFO("RC6 %s\n", onoff(rc6_mask & GEN6_RC_CTL_RC6_ENABLE));
Chris Wilson1c044f92017-01-25 17:26:01 +00006335 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
6336 I915_WRITE(GEN6_RC_CONTROL,
6337 GEN6_RC_CTL_HW_ENABLE | GEN6_RC_CTL_EI_MODE(1) | rc6_mask);
Zhe Wang20e49362014-11-04 17:07:05 +00006338
Sagar Kamblecb07bae2015-04-12 11:28:14 +05306339 /*
6340 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05306341 * WaRsDisableCoarsePowerGating:skl,bxt - Render/Media PG need to be disabled with RC6.
Sagar Kamblecb07bae2015-04-12 11:28:14 +05306342 */
Chris Wilsondc979972016-05-10 14:10:04 +01006343 if (NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05306344 I915_WRITE(GEN9_PG_ENABLE, 0);
6345 else
6346 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
6347 (GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE) : 0);
Zhe Wang38c23522015-01-20 12:23:04 +00006348
Mika Kuoppala59bad942015-01-16 11:34:40 +02006349 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00006350}
6351
Chris Wilsondc979972016-05-10 14:10:04 +01006352static void gen8_enable_rps(struct drm_i915_private *dev_priv)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006353{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006354 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306355 enum intel_engine_id id;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006356 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006357
6358 /* 1a: Software RC state - RC0 */
6359 I915_WRITE(GEN6_RC_STATE, 0);
6360
6361 /* 1c & 1d: Get forcewake during program sequence. Although the driver
6362 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006363 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006364
6365 /* 2a: Disable RC states. */
6366 I915_WRITE(GEN6_RC_CONTROL, 0);
6367
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006368 /* 2b: Program RC6 thresholds.*/
6369 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
6370 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6371 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05306372 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006373 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006374 I915_WRITE(GEN6_RC_SLEEP, 0);
Chris Wilsondc979972016-05-10 14:10:04 +01006375 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07006376 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
6377 else
6378 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006379
6380 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006381 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006382 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Chris Wilsondc979972016-05-10 14:10:04 +01006383 intel_print_rc6_info(dev_priv, rc6_mask);
6384 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07006385 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
6386 GEN7_RC_CTL_TO_MODE |
6387 rc6_mask);
6388 else
6389 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
6390 GEN6_RC_CTL_EI_MODE(1) |
6391 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006392
6393 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07006394 I915_WRITE(GEN6_RPNSWREQ,
6395 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
6396 I915_WRITE(GEN6_RC_VIDEO_FREQ,
6397 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02006398 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
6399 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006400
Daniel Vetter7526ed72014-09-29 15:07:19 +02006401 /* Docs recommend 900MHz, and 300 MHz respectively */
6402 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
6403 dev_priv->rps.max_freq_softlimit << 24 |
6404 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006405
Daniel Vetter7526ed72014-09-29 15:07:19 +02006406 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
6407 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
6408 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
6409 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006410
Daniel Vetter7526ed72014-09-29 15:07:19 +02006411 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006412
6413 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02006414 I915_WRITE(GEN6_RP_CONTROL,
6415 GEN6_RP_MEDIA_TURBO |
6416 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6417 GEN6_RP_MEDIA_IS_GFX |
6418 GEN6_RP_ENABLE |
6419 GEN6_RP_UP_BUSY_AVG |
6420 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006421
Daniel Vetter7526ed72014-09-29 15:07:19 +02006422 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006423
Chris Wilson3a45b052016-07-13 09:10:32 +01006424 reset_rps(dev_priv, gen6_set_rps);
Daniel Vetter7526ed72014-09-29 15:07:19 +02006425
Mika Kuoppala59bad942015-01-16 11:34:40 +02006426 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006427}
6428
Chris Wilsondc979972016-05-10 14:10:04 +01006429static void gen6_enable_rps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006430{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006431 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306432 enum intel_engine_id id;
Chris Wilson99ac9612016-07-13 09:10:34 +01006433 u32 rc6vids, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006434 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006435 int rc6_mode;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00006436 int ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006437
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006438 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02006439
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006440 /* Here begins a magic sequence of register writes to enable
6441 * auto-downclocking.
6442 *
6443 * Perhaps there might be some value in exposing these to
6444 * userspace...
6445 */
6446 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006447
6448 /* Clear the DBG now so we don't confuse earlier errors */
Ville Syrjälä297b32e2016-04-13 21:09:30 +03006449 gtfifodbg = I915_READ(GTFIFODBG);
6450 if (gtfifodbg) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006451 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
6452 I915_WRITE(GTFIFODBG, gtfifodbg);
6453 }
6454
Mika Kuoppala59bad942015-01-16 11:34:40 +02006455 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006456
6457 /* disable the counters and set deterministic thresholds */
6458 I915_WRITE(GEN6_RC_CONTROL, 0);
6459
6460 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
6461 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
6462 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
6463 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
6464 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
6465
Akash Goel3b3f1652016-10-13 22:44:48 +05306466 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006467 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006468
6469 I915_WRITE(GEN6_RC_SLEEP, 0);
6470 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Chris Wilsondc979972016-05-10 14:10:04 +01006471 if (IS_IVYBRIDGE(dev_priv))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07006472 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
6473 else
6474 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08006475 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006476 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
6477
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006478 /* Check if we are enabling RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006479 rc6_mode = intel_enable_rc6();
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006480 if (rc6_mode & INTEL_RC6_ENABLE)
6481 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
6482
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006483 /* We don't use those on Haswell */
Chris Wilsondc979972016-05-10 14:10:04 +01006484 if (!IS_HASWELL(dev_priv)) {
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006485 if (rc6_mode & INTEL_RC6p_ENABLE)
6486 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006487
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006488 if (rc6_mode & INTEL_RC6pp_ENABLE)
6489 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
6490 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006491
Chris Wilsondc979972016-05-10 14:10:04 +01006492 intel_print_rc6_info(dev_priv, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006493
6494 I915_WRITE(GEN6_RC_CONTROL,
6495 rc6_mask |
6496 GEN6_RC_CTL_EI_MODE(1) |
6497 GEN6_RC_CTL_HW_ENABLE);
6498
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006499 /* Power down if completely idle for over 50ms */
6500 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006501 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006502
Chris Wilson3a45b052016-07-13 09:10:32 +01006503 reset_rps(dev_priv, gen6_set_rps);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006504
Ben Widawsky31643d52012-09-26 10:34:01 -07006505 rc6vids = 0;
6506 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
Chris Wilsondc979972016-05-10 14:10:04 +01006507 if (IS_GEN6(dev_priv) && ret) {
Ben Widawsky31643d52012-09-26 10:34:01 -07006508 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
Chris Wilsondc979972016-05-10 14:10:04 +01006509 } else if (IS_GEN6(dev_priv) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
Ben Widawsky31643d52012-09-26 10:34:01 -07006510 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
6511 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
6512 rc6vids &= 0xffff00;
6513 rc6vids |= GEN6_ENCODE_RC6_VID(450);
6514 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
6515 if (ret)
6516 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
6517 }
6518
Mika Kuoppala59bad942015-01-16 11:34:40 +02006519 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006520}
6521
Chris Wilsonfb7404e2016-07-13 09:10:38 +01006522static void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006523{
6524 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01006525 unsigned int gpu_freq;
6526 unsigned int max_ia_freq, min_ring_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05306527 unsigned int max_gpu_freq, min_gpu_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006528 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03006529 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006530
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006531 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02006532
Ben Widawskyeda79642013-10-07 17:15:48 -03006533 policy = cpufreq_cpu_get(0);
6534 if (policy) {
6535 max_ia_freq = policy->cpuinfo.max_freq;
6536 cpufreq_cpu_put(policy);
6537 } else {
6538 /*
6539 * Default to measured freq if none found, PCU will ensure we
6540 * don't go over
6541 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006542 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03006543 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006544
6545 /* Convert from kHz to MHz */
6546 max_ia_freq /= 1000;
6547
Ben Widawsky153b4b952013-10-22 22:05:09 -07006548 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07006549 /* convert DDR frequency from units of 266.6MHz to bandwidth */
6550 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01006551
Rodrigo Vivib976dc52017-01-23 10:32:37 -08006552 if (IS_GEN9_BC(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05306553 /* Convert GT frequency to 50 HZ units */
6554 min_gpu_freq = dev_priv->rps.min_freq / GEN9_FREQ_SCALER;
6555 max_gpu_freq = dev_priv->rps.max_freq / GEN9_FREQ_SCALER;
6556 } else {
6557 min_gpu_freq = dev_priv->rps.min_freq;
6558 max_gpu_freq = dev_priv->rps.max_freq;
6559 }
6560
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006561 /*
6562 * For each potential GPU frequency, load a ring frequency we'd like
6563 * to use for memory access. We do this by specifying the IA frequency
6564 * the PCU should use as a reference to determine the ring frequency.
6565 */
Akash Goel4c8c7742015-06-29 14:50:20 +05306566 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
6567 int diff = max_gpu_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01006568 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006569
Rodrigo Vivib976dc52017-01-23 10:32:37 -08006570 if (IS_GEN9_BC(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05306571 /*
6572 * ring_freq = 2 * GT. ring_freq is in 100MHz units
6573 * No floor required for ring frequency on SKL.
6574 */
6575 ring_freq = gpu_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01006576 } else if (INTEL_INFO(dev_priv)->gen >= 8) {
Ben Widawsky46c764d2013-11-02 21:07:49 -07006577 /* max(2 * GT, DDR). NB: GT is 50MHz units */
6578 ring_freq = max(min_ring_freq, gpu_freq);
Chris Wilsondc979972016-05-10 14:10:04 +01006579 } else if (IS_HASWELL(dev_priv)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07006580 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01006581 ring_freq = max(min_ring_freq, ring_freq);
6582 /* leave ia_freq as the default, chosen by cpufreq */
6583 } else {
6584 /* On older processors, there is no separate ring
6585 * clock domain, so in order to boost the bandwidth
6586 * of the ring, we need to upclock the CPU (ia_freq).
6587 *
6588 * For GPU frequencies less than 750MHz,
6589 * just use the lowest ring freq.
6590 */
6591 if (gpu_freq < min_freq)
6592 ia_freq = 800;
6593 else
6594 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
6595 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
6596 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006597
Ben Widawsky42c05262012-09-26 10:34:00 -07006598 sandybridge_pcode_write(dev_priv,
6599 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01006600 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
6601 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
6602 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006603 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006604}
6605
Ville Syrjälä03af2042014-06-28 02:03:53 +03006606static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05306607{
6608 u32 val, rp0;
6609
Jani Nikula5b5929c2015-10-07 11:17:46 +03006610 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05306611
Imre Deak43b67992016-08-31 19:13:02 +03006612 switch (INTEL_INFO(dev_priv)->sseu.eu_total) {
Jani Nikula5b5929c2015-10-07 11:17:46 +03006613 case 8:
6614 /* (2 * 4) config */
6615 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
6616 break;
6617 case 12:
6618 /* (2 * 6) config */
6619 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
6620 break;
6621 case 16:
6622 /* (2 * 8) config */
6623 default:
6624 /* Setting (2 * 8) Min RP0 for any other combination */
6625 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
6626 break;
Deepak S095acd52015-01-17 11:05:59 +05306627 }
Jani Nikula5b5929c2015-10-07 11:17:46 +03006628
6629 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
6630
Deepak S2b6b3a02014-05-27 15:59:30 +05306631 return rp0;
6632}
6633
6634static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
6635{
6636 u32 val, rpe;
6637
6638 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
6639 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
6640
6641 return rpe;
6642}
6643
Deepak S7707df42014-07-12 18:46:14 +05306644static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
6645{
6646 u32 val, rp1;
6647
Jani Nikula5b5929c2015-10-07 11:17:46 +03006648 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
6649 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
6650
Deepak S7707df42014-07-12 18:46:14 +05306651 return rp1;
6652}
6653
Deepak S96676fe2016-08-12 18:46:41 +05306654static u32 cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
6655{
6656 u32 val, rpn;
6657
6658 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
6659 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
6660 FB_GFX_FREQ_FUSE_MASK);
6661
6662 return rpn;
6663}
6664
Deepak Sf8f2b002014-07-10 13:16:21 +05306665static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
6666{
6667 u32 val, rp1;
6668
6669 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
6670
6671 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
6672
6673 return rp1;
6674}
6675
Ville Syrjälä03af2042014-06-28 02:03:53 +03006676static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006677{
6678 u32 val, rp0;
6679
Jani Nikula64936252013-05-22 15:36:20 +03006680 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006681
6682 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
6683 /* Clamp to max */
6684 rp0 = min_t(u32, rp0, 0xea);
6685
6686 return rp0;
6687}
6688
6689static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
6690{
6691 u32 val, rpe;
6692
Jani Nikula64936252013-05-22 15:36:20 +03006693 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006694 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03006695 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006696 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
6697
6698 return rpe;
6699}
6700
Ville Syrjälä03af2042014-06-28 02:03:53 +03006701static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006702{
Imre Deak36146032014-12-04 18:39:35 +02006703 u32 val;
6704
6705 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
6706 /*
6707 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
6708 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
6709 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
6710 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
6711 * to make sure it matches what Punit accepts.
6712 */
6713 return max_t(u32, val, 0xc0);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006714}
6715
Imre Deakae484342014-03-31 15:10:44 +03006716/* Check that the pctx buffer wasn't move under us. */
6717static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
6718{
6719 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
6720
6721 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
6722 dev_priv->vlv_pctx->stolen->start);
6723}
6724
Deepak S38807742014-05-23 21:00:15 +05306725
6726/* Check that the pcbr address is not empty. */
6727static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
6728{
6729 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
6730
6731 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
6732}
6733
Chris Wilsondc979972016-05-10 14:10:04 +01006734static void cherryview_setup_pctx(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306735{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02006736 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006737 unsigned long pctx_paddr, paddr;
Deepak S38807742014-05-23 21:00:15 +05306738 u32 pcbr;
6739 int pctx_size = 32*1024;
6740
Deepak S38807742014-05-23 21:00:15 +05306741 pcbr = I915_READ(VLV_PCBR);
6742 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006743 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05306744 paddr = (dev_priv->mm.stolen_base +
Joonas Lahtinen62106b42016-03-18 10:42:57 +02006745 (ggtt->stolen_size - pctx_size));
Deepak S38807742014-05-23 21:00:15 +05306746
6747 pctx_paddr = (paddr & (~4095));
6748 I915_WRITE(VLV_PCBR, pctx_paddr);
6749 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006750
6751 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05306752}
6753
Chris Wilsondc979972016-05-10 14:10:04 +01006754static void valleyview_setup_pctx(struct drm_i915_private *dev_priv)
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006755{
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006756 struct drm_i915_gem_object *pctx;
6757 unsigned long pctx_paddr;
6758 u32 pcbr;
6759 int pctx_size = 24*1024;
6760
6761 pcbr = I915_READ(VLV_PCBR);
6762 if (pcbr) {
6763 /* BIOS set it up already, grab the pre-alloc'd space */
6764 int pcbr_offset;
6765
6766 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00006767 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006768 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02006769 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006770 pctx_size);
6771 goto out;
6772 }
6773
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006774 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
6775
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006776 /*
6777 * From the Gunit register HAS:
6778 * The Gfx driver is expected to program this register and ensure
6779 * proper allocation within Gfx stolen memory. For example, this
6780 * register should be programmed such than the PCBR range does not
6781 * overlap with other ranges, such as the frame buffer, protected
6782 * memory, or any other relevant ranges.
6783 */
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00006784 pctx = i915_gem_object_create_stolen(dev_priv, pctx_size);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006785 if (!pctx) {
6786 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
Tvrtko Ursulinee504892016-02-11 10:27:30 +00006787 goto out;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006788 }
6789
6790 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
6791 I915_WRITE(VLV_PCBR, pctx_paddr);
6792
6793out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006794 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006795 dev_priv->vlv_pctx = pctx;
6796}
6797
Chris Wilsondc979972016-05-10 14:10:04 +01006798static void valleyview_cleanup_pctx(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006799{
Imre Deakae484342014-03-31 15:10:44 +03006800 if (WARN_ON(!dev_priv->vlv_pctx))
6801 return;
6802
Chris Wilsonf0cd5182016-10-28 13:58:43 +01006803 i915_gem_object_put(dev_priv->vlv_pctx);
Imre Deakae484342014-03-31 15:10:44 +03006804 dev_priv->vlv_pctx = NULL;
6805}
6806
Ville Syrjäläc30fec62016-03-04 21:43:02 +02006807static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
6808{
6809 dev_priv->rps.gpll_ref_freq =
6810 vlv_get_cck_clock(dev_priv, "GPLL ref",
6811 CCK_GPLL_CLOCK_CONTROL,
6812 dev_priv->czclk_freq);
6813
6814 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
6815 dev_priv->rps.gpll_ref_freq);
6816}
6817
Chris Wilsondc979972016-05-10 14:10:04 +01006818static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03006819{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006820 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03006821
Chris Wilsondc979972016-05-10 14:10:04 +01006822 valleyview_setup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03006823
Ville Syrjäläc30fec62016-03-04 21:43:02 +02006824 vlv_init_gpll_ref_freq(dev_priv);
6825
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006826 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
6827 switch ((val >> 6) & 3) {
6828 case 0:
6829 case 1:
6830 dev_priv->mem_freq = 800;
6831 break;
6832 case 2:
6833 dev_priv->mem_freq = 1066;
6834 break;
6835 case 3:
6836 dev_priv->mem_freq = 1333;
6837 break;
6838 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02006839 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006840
Imre Deak4e805192014-04-14 20:24:41 +03006841 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
6842 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
6843 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006844 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03006845 dev_priv->rps.max_freq);
6846
6847 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
6848 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006849 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03006850 dev_priv->rps.efficient_freq);
6851
Deepak Sf8f2b002014-07-10 13:16:21 +05306852 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
6853 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006854 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05306855 dev_priv->rps.rp1_freq);
6856
Imre Deak4e805192014-04-14 20:24:41 +03006857 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
6858 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006859 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03006860 dev_priv->rps.min_freq);
Imre Deak4e805192014-04-14 20:24:41 +03006861}
6862
Chris Wilsondc979972016-05-10 14:10:04 +01006863static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306864{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006865 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05306866
Chris Wilsondc979972016-05-10 14:10:04 +01006867 cherryview_setup_pctx(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05306868
Ville Syrjäläc30fec62016-03-04 21:43:02 +02006869 vlv_init_gpll_ref_freq(dev_priv);
6870
Ville Syrjäläa5805162015-05-26 20:42:30 +03006871 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02006872 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006873 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02006874
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006875 switch ((val >> 2) & 0x7) {
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006876 case 3:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006877 dev_priv->mem_freq = 2000;
6878 break;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006879 default:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006880 dev_priv->mem_freq = 1600;
6881 break;
6882 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02006883 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006884
Deepak S2b6b3a02014-05-27 15:59:30 +05306885 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
6886 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
6887 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006888 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05306889 dev_priv->rps.max_freq);
6890
6891 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
6892 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006893 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05306894 dev_priv->rps.efficient_freq);
6895
Deepak S7707df42014-07-12 18:46:14 +05306896 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
6897 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006898 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05306899 dev_priv->rps.rp1_freq);
6900
Deepak S96676fe2016-08-12 18:46:41 +05306901 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05306902 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006903 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05306904 dev_priv->rps.min_freq);
6905
Ville Syrjälä1c147622014-08-18 14:42:43 +03006906 WARN_ONCE((dev_priv->rps.max_freq |
6907 dev_priv->rps.efficient_freq |
6908 dev_priv->rps.rp1_freq |
6909 dev_priv->rps.min_freq) & 1,
6910 "Odd GPU freq values\n");
Deepak S38807742014-05-23 21:00:15 +05306911}
6912
Chris Wilsondc979972016-05-10 14:10:04 +01006913static void valleyview_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03006914{
Chris Wilsondc979972016-05-10 14:10:04 +01006915 valleyview_cleanup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03006916}
6917
Chris Wilsondc979972016-05-10 14:10:04 +01006918static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306919{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006920 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306921 enum intel_engine_id id;
Deepak S2b6b3a02014-05-27 15:59:30 +05306922 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05306923
6924 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
6925
Ville Syrjälä297b32e2016-04-13 21:09:30 +03006926 gtfifodbg = I915_READ(GTFIFODBG) & ~(GT_FIFO_SBDEDICATE_FREE_ENTRY_CHV |
6927 GT_FIFO_FREE_ENTRIES_CHV);
Deepak S38807742014-05-23 21:00:15 +05306928 if (gtfifodbg) {
6929 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
6930 gtfifodbg);
6931 I915_WRITE(GTFIFODBG, gtfifodbg);
6932 }
6933
6934 cherryview_check_pctx(dev_priv);
6935
6936 /* 1a & 1b: Get forcewake during program sequence. Although the driver
6937 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006938 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05306939
Ville Syrjälä160614a2015-01-19 13:50:47 +02006940 /* Disable RC states. */
6941 I915_WRITE(GEN6_RC_CONTROL, 0);
6942
Deepak S38807742014-05-23 21:00:15 +05306943 /* 2a: Program RC6 thresholds.*/
6944 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
6945 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6946 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
6947
Akash Goel3b3f1652016-10-13 22:44:48 +05306948 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006949 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Deepak S38807742014-05-23 21:00:15 +05306950 I915_WRITE(GEN6_RC_SLEEP, 0);
6951
Deepak Sf4f71c72015-03-28 15:23:35 +05306952 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
6953 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
Deepak S38807742014-05-23 21:00:15 +05306954
6955 /* allows RC6 residency counter to work */
6956 I915_WRITE(VLV_COUNTER_CONTROL,
6957 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
6958 VLV_MEDIA_RC6_COUNT_EN |
6959 VLV_RENDER_RC6_COUNT_EN));
6960
6961 /* For now we assume BIOS is allocating and populating the PCBR */
6962 pcbr = I915_READ(VLV_PCBR);
6963
Deepak S38807742014-05-23 21:00:15 +05306964 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006965 if ((intel_enable_rc6() & INTEL_RC6_ENABLE) &&
6966 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02006967 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05306968
6969 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
6970
Deepak S2b6b3a02014-05-27 15:59:30 +05306971 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02006972 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05306973 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
6974 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
6975 I915_WRITE(GEN6_RP_UP_EI, 66000);
6976 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
6977
6978 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
6979
6980 /* 5: Enable RPS */
6981 I915_WRITE(GEN6_RP_CONTROL,
6982 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02006983 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05306984 GEN6_RP_ENABLE |
6985 GEN6_RP_UP_BUSY_AVG |
6986 GEN6_RP_DOWN_IDLE_AVG);
6987
Deepak S3ef62342015-04-29 08:36:24 +05306988 /* Setting Fixed Bias */
6989 val = VLV_OVERRIDE_EN |
6990 VLV_SOC_TDP_EN |
6991 CHV_BIAS_CPU_50_SOC_50;
6992 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
6993
Deepak S2b6b3a02014-05-27 15:59:30 +05306994 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
6995
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02006996 /* RPS code assumes GPLL is used */
6997 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
6998
Jani Nikula742f4912015-09-03 11:16:09 +03006999 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05307000 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7001
Chris Wilson3a45b052016-07-13 09:10:32 +01007002 reset_rps(dev_priv, valleyview_set_rps);
Deepak S2b6b3a02014-05-27 15:59:30 +05307003
Mika Kuoppala59bad942015-01-16 11:34:40 +02007004 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05307005}
7006
Chris Wilsondc979972016-05-10 14:10:04 +01007007static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07007008{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007009 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05307010 enum intel_engine_id id;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07007011 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07007012
7013 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
7014
Imre Deakae484342014-03-31 15:10:44 +03007015 valleyview_check_pctx(dev_priv);
7016
Ville Syrjälä297b32e2016-04-13 21:09:30 +03007017 gtfifodbg = I915_READ(GTFIFODBG);
7018 if (gtfifodbg) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07007019 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
7020 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007021 I915_WRITE(GTFIFODBG, gtfifodbg);
7022 }
7023
Deepak Sc8d9a592013-11-23 14:55:42 +05307024 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02007025 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007026
Ville Syrjälä160614a2015-01-19 13:50:47 +02007027 /* Disable RC states. */
7028 I915_WRITE(GEN6_RC_CONTROL, 0);
7029
Ville Syrjäläcad725f2015-01-19 13:50:48 +02007030 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007031 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
7032 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
7033 I915_WRITE(GEN6_RP_UP_EI, 66000);
7034 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
7035
7036 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7037
7038 I915_WRITE(GEN6_RP_CONTROL,
7039 GEN6_RP_MEDIA_TURBO |
7040 GEN6_RP_MEDIA_HW_NORMAL_MODE |
7041 GEN6_RP_MEDIA_IS_GFX |
7042 GEN6_RP_ENABLE |
7043 GEN6_RP_UP_BUSY_AVG |
7044 GEN6_RP_DOWN_IDLE_CONT);
7045
7046 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
7047 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
7048 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
7049
Akash Goel3b3f1652016-10-13 22:44:48 +05307050 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007051 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007052
Jesse Barnes2f0aa3042013-11-15 09:32:11 -08007053 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007054
7055 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07007056 I915_WRITE(VLV_COUNTER_CONTROL,
Mika Kuoppala6b7f6aa2017-03-15 18:12:59 +02007057 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
7058 VLV_MEDIA_RC0_COUNT_EN |
Deepak S31685c22014-07-03 17:33:01 -04007059 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07007060 VLV_MEDIA_RC6_COUNT_EN |
7061 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04007062
Chris Wilsondc979972016-05-10 14:10:04 +01007063 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08007064 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07007065
Chris Wilsondc979972016-05-10 14:10:04 +01007066 intel_print_rc6_info(dev_priv, rc6_mode);
Ben Widawskydc39fff2013-10-18 12:32:07 -07007067
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07007068 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007069
Deepak S3ef62342015-04-29 08:36:24 +05307070 /* Setting Fixed Bias */
7071 val = VLV_OVERRIDE_EN |
7072 VLV_SOC_TDP_EN |
7073 VLV_BIAS_CPU_125_SOC_875;
7074 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
7075
Jani Nikula64936252013-05-22 15:36:20 +03007076 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007077
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02007078 /* RPS code assumes GPLL is used */
7079 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
7080
Jani Nikula742f4912015-09-03 11:16:09 +03007081 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07007082 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7083
Chris Wilson3a45b052016-07-13 09:10:32 +01007084 reset_rps(dev_priv, valleyview_set_rps);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007085
Mika Kuoppala59bad942015-01-16 11:34:40 +02007086 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007087}
7088
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007089static unsigned long intel_pxfreq(u32 vidfreq)
7090{
7091 unsigned long freq;
7092 int div = (vidfreq & 0x3f0000) >> 16;
7093 int post = (vidfreq & 0x3000) >> 12;
7094 int pre = (vidfreq & 0x7);
7095
7096 if (!pre)
7097 return 0;
7098
7099 freq = ((div * 133333) / ((1<<post) * pre));
7100
7101 return freq;
7102}
7103
Daniel Vettereb48eb02012-04-26 23:28:12 +02007104static const struct cparams {
7105 u16 i;
7106 u16 t;
7107 u16 m;
7108 u16 c;
7109} cparams[] = {
7110 { 1, 1333, 301, 28664 },
7111 { 1, 1066, 294, 24460 },
7112 { 1, 800, 294, 25192 },
7113 { 0, 1333, 276, 27605 },
7114 { 0, 1066, 276, 27605 },
7115 { 0, 800, 231, 23784 },
7116};
7117
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007118static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007119{
7120 u64 total_count, diff, ret;
7121 u32 count1, count2, count3, m = 0, c = 0;
7122 unsigned long now = jiffies_to_msecs(jiffies), diff1;
7123 int i;
7124
Chris Wilson67520412017-03-02 13:28:01 +00007125 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007126
Daniel Vetter20e4d402012-08-08 23:35:39 +02007127 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007128
7129 /* Prevent division-by-zero if we are asking too fast.
7130 * Also, we don't get interesting results if we are polling
7131 * faster than once in 10ms, so just return the saved value
7132 * in such cases.
7133 */
7134 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02007135 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007136
7137 count1 = I915_READ(DMIEC);
7138 count2 = I915_READ(DDREC);
7139 count3 = I915_READ(CSIEC);
7140
7141 total_count = count1 + count2 + count3;
7142
7143 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02007144 if (total_count < dev_priv->ips.last_count1) {
7145 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007146 diff += total_count;
7147 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007148 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007149 }
7150
7151 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007152 if (cparams[i].i == dev_priv->ips.c_m &&
7153 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02007154 m = cparams[i].m;
7155 c = cparams[i].c;
7156 break;
7157 }
7158 }
7159
7160 diff = div_u64(diff, diff1);
7161 ret = ((m * diff) + c);
7162 ret = div_u64(ret, 10);
7163
Daniel Vetter20e4d402012-08-08 23:35:39 +02007164 dev_priv->ips.last_count1 = total_count;
7165 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007166
Daniel Vetter20e4d402012-08-08 23:35:39 +02007167 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007168
7169 return ret;
7170}
7171
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007172unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
7173{
7174 unsigned long val;
7175
Chris Wilsondc979972016-05-10 14:10:04 +01007176 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007177 return 0;
7178
7179 spin_lock_irq(&mchdev_lock);
7180
7181 val = __i915_chipset_val(dev_priv);
7182
7183 spin_unlock_irq(&mchdev_lock);
7184
7185 return val;
7186}
7187
Daniel Vettereb48eb02012-04-26 23:28:12 +02007188unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
7189{
7190 unsigned long m, x, b;
7191 u32 tsfs;
7192
7193 tsfs = I915_READ(TSFS);
7194
7195 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
7196 x = I915_READ8(TR1);
7197
7198 b = tsfs & TSFS_INTR_MASK;
7199
7200 return ((m * x) / 127) - b;
7201}
7202
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007203static int _pxvid_to_vd(u8 pxvid)
7204{
7205 if (pxvid == 0)
7206 return 0;
7207
7208 if (pxvid >= 8 && pxvid < 31)
7209 pxvid = 31;
7210
7211 return (pxvid + 2) * 125;
7212}
7213
7214static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007215{
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007216 const int vd = _pxvid_to_vd(pxvid);
7217 const int vm = vd - 1125;
7218
Chris Wilsondc979972016-05-10 14:10:04 +01007219 if (INTEL_INFO(dev_priv)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007220 return vm > 0 ? vm : 0;
7221
7222 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007223}
7224
Daniel Vetter02d71952012-08-09 16:44:54 +02007225static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007226{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00007227 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007228 u32 count;
7229
Chris Wilson67520412017-03-02 13:28:01 +00007230 lockdep_assert_held(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007231
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00007232 now = ktime_get_raw_ns();
7233 diffms = now - dev_priv->ips.last_time2;
7234 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007235
7236 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02007237 if (!diffms)
7238 return;
7239
7240 count = I915_READ(GFXEC);
7241
Daniel Vetter20e4d402012-08-08 23:35:39 +02007242 if (count < dev_priv->ips.last_count2) {
7243 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007244 diff += count;
7245 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007246 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007247 }
7248
Daniel Vetter20e4d402012-08-08 23:35:39 +02007249 dev_priv->ips.last_count2 = count;
7250 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007251
7252 /* More magic constants... */
7253 diff = diff * 1181;
7254 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02007255 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007256}
7257
Daniel Vetter02d71952012-08-09 16:44:54 +02007258void i915_update_gfx_val(struct drm_i915_private *dev_priv)
7259{
Chris Wilsondc979972016-05-10 14:10:04 +01007260 if (INTEL_INFO(dev_priv)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02007261 return;
7262
Daniel Vetter92703882012-08-09 16:46:01 +02007263 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007264
7265 __i915_update_gfx_val(dev_priv);
7266
Daniel Vetter92703882012-08-09 16:46:01 +02007267 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007268}
7269
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007270static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007271{
7272 unsigned long t, corr, state1, corr2, state2;
7273 u32 pxvid, ext_v;
7274
Chris Wilson67520412017-03-02 13:28:01 +00007275 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007276
Ville Syrjälä616847e2015-09-18 20:03:19 +03007277 pxvid = I915_READ(PXVFREQ(dev_priv->rps.cur_freq));
Daniel Vettereb48eb02012-04-26 23:28:12 +02007278 pxvid = (pxvid >> 24) & 0x7f;
7279 ext_v = pvid_to_extvid(dev_priv, pxvid);
7280
7281 state1 = ext_v;
7282
7283 t = i915_mch_val(dev_priv);
7284
7285 /* Revel in the empirically derived constants */
7286
7287 /* Correction factor in 1/100000 units */
7288 if (t > 80)
7289 corr = ((t * 2349) + 135940);
7290 else if (t >= 50)
7291 corr = ((t * 964) + 29317);
7292 else /* < 50 */
7293 corr = ((t * 301) + 1004);
7294
7295 corr = corr * ((150142 * state1) / 10000 - 78642);
7296 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02007297 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007298
7299 state2 = (corr2 * state1) / 10000;
7300 state2 /= 100; /* convert to mW */
7301
Daniel Vetter02d71952012-08-09 16:44:54 +02007302 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007303
Daniel Vetter20e4d402012-08-08 23:35:39 +02007304 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007305}
7306
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007307unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
7308{
7309 unsigned long val;
7310
Chris Wilsondc979972016-05-10 14:10:04 +01007311 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007312 return 0;
7313
7314 spin_lock_irq(&mchdev_lock);
7315
7316 val = __i915_gfx_val(dev_priv);
7317
7318 spin_unlock_irq(&mchdev_lock);
7319
7320 return val;
7321}
7322
Daniel Vettereb48eb02012-04-26 23:28:12 +02007323/**
7324 * i915_read_mch_val - return value for IPS use
7325 *
7326 * Calculate and return a value for the IPS driver to use when deciding whether
7327 * we have thermal and power headroom to increase CPU or GPU power budget.
7328 */
7329unsigned long i915_read_mch_val(void)
7330{
7331 struct drm_i915_private *dev_priv;
7332 unsigned long chipset_val, graphics_val, ret = 0;
7333
Daniel Vetter92703882012-08-09 16:46:01 +02007334 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007335 if (!i915_mch_dev)
7336 goto out_unlock;
7337 dev_priv = i915_mch_dev;
7338
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007339 chipset_val = __i915_chipset_val(dev_priv);
7340 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007341
7342 ret = chipset_val + graphics_val;
7343
7344out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007345 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007346
7347 return ret;
7348}
7349EXPORT_SYMBOL_GPL(i915_read_mch_val);
7350
7351/**
7352 * i915_gpu_raise - raise GPU frequency limit
7353 *
7354 * Raise the limit; IPS indicates we have thermal headroom.
7355 */
7356bool i915_gpu_raise(void)
7357{
7358 struct drm_i915_private *dev_priv;
7359 bool ret = true;
7360
Daniel Vetter92703882012-08-09 16:46:01 +02007361 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007362 if (!i915_mch_dev) {
7363 ret = false;
7364 goto out_unlock;
7365 }
7366 dev_priv = i915_mch_dev;
7367
Daniel Vetter20e4d402012-08-08 23:35:39 +02007368 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
7369 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007370
7371out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007372 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007373
7374 return ret;
7375}
7376EXPORT_SYMBOL_GPL(i915_gpu_raise);
7377
7378/**
7379 * i915_gpu_lower - lower GPU frequency limit
7380 *
7381 * IPS indicates we're close to a thermal limit, so throttle back the GPU
7382 * frequency maximum.
7383 */
7384bool i915_gpu_lower(void)
7385{
7386 struct drm_i915_private *dev_priv;
7387 bool ret = true;
7388
Daniel Vetter92703882012-08-09 16:46:01 +02007389 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007390 if (!i915_mch_dev) {
7391 ret = false;
7392 goto out_unlock;
7393 }
7394 dev_priv = i915_mch_dev;
7395
Daniel Vetter20e4d402012-08-08 23:35:39 +02007396 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
7397 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007398
7399out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007400 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007401
7402 return ret;
7403}
7404EXPORT_SYMBOL_GPL(i915_gpu_lower);
7405
7406/**
7407 * i915_gpu_busy - indicate GPU business to IPS
7408 *
7409 * Tell the IPS driver whether or not the GPU is busy.
7410 */
7411bool i915_gpu_busy(void)
7412{
Daniel Vettereb48eb02012-04-26 23:28:12 +02007413 bool ret = false;
7414
Daniel Vetter92703882012-08-09 16:46:01 +02007415 spin_lock_irq(&mchdev_lock);
Chris Wilsondcff85c2016-08-05 10:14:11 +01007416 if (i915_mch_dev)
7417 ret = i915_mch_dev->gt.awake;
Daniel Vetter92703882012-08-09 16:46:01 +02007418 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007419
7420 return ret;
7421}
7422EXPORT_SYMBOL_GPL(i915_gpu_busy);
7423
7424/**
7425 * i915_gpu_turbo_disable - disable graphics turbo
7426 *
7427 * Disable graphics turbo by resetting the max frequency and setting the
7428 * current frequency to the default.
7429 */
7430bool i915_gpu_turbo_disable(void)
7431{
7432 struct drm_i915_private *dev_priv;
7433 bool ret = true;
7434
Daniel Vetter92703882012-08-09 16:46:01 +02007435 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007436 if (!i915_mch_dev) {
7437 ret = false;
7438 goto out_unlock;
7439 }
7440 dev_priv = i915_mch_dev;
7441
Daniel Vetter20e4d402012-08-08 23:35:39 +02007442 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007443
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01007444 if (!ironlake_set_drps(dev_priv, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02007445 ret = false;
7446
7447out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007448 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007449
7450 return ret;
7451}
7452EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
7453
7454/**
7455 * Tells the intel_ips driver that the i915 driver is now loaded, if
7456 * IPS got loaded first.
7457 *
7458 * This awkward dance is so that neither module has to depend on the
7459 * other in order for IPS to do the appropriate communication of
7460 * GPU turbo limits to i915.
7461 */
7462static void
7463ips_ping_for_i915_load(void)
7464{
7465 void (*link)(void);
7466
7467 link = symbol_get(ips_link_to_i915_driver);
7468 if (link) {
7469 link();
7470 symbol_put(ips_link_to_i915_driver);
7471 }
7472}
7473
7474void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
7475{
Daniel Vetter02d71952012-08-09 16:44:54 +02007476 /* We only register the i915 ips part with intel-ips once everything is
7477 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02007478 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007479 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02007480 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007481
7482 ips_ping_for_i915_load();
7483}
7484
7485void intel_gpu_ips_teardown(void)
7486{
Daniel Vetter92703882012-08-09 16:46:01 +02007487 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007488 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02007489 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007490}
Deepak S76c3552f2014-01-30 23:08:16 +05307491
Chris Wilsondc979972016-05-10 14:10:04 +01007492static void intel_init_emon(struct drm_i915_private *dev_priv)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007493{
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007494 u32 lcfuse;
7495 u8 pxw[16];
7496 int i;
7497
7498 /* Disable to program */
7499 I915_WRITE(ECR, 0);
7500 POSTING_READ(ECR);
7501
7502 /* Program energy weights for various events */
7503 I915_WRITE(SDEW, 0x15040d00);
7504 I915_WRITE(CSIEW0, 0x007f0000);
7505 I915_WRITE(CSIEW1, 0x1e220004);
7506 I915_WRITE(CSIEW2, 0x04000004);
7507
7508 for (i = 0; i < 5; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007509 I915_WRITE(PEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007510 for (i = 0; i < 3; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007511 I915_WRITE(DEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007512
7513 /* Program P-state weights to account for frequency power adjustment */
7514 for (i = 0; i < 16; i++) {
Ville Syrjälä616847e2015-09-18 20:03:19 +03007515 u32 pxvidfreq = I915_READ(PXVFREQ(i));
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007516 unsigned long freq = intel_pxfreq(pxvidfreq);
7517 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
7518 PXVFREQ_PX_SHIFT;
7519 unsigned long val;
7520
7521 val = vid * vid;
7522 val *= (freq / 1000);
7523 val *= 255;
7524 val /= (127*127*900);
7525 if (val > 0xff)
7526 DRM_ERROR("bad pxval: %ld\n", val);
7527 pxw[i] = val;
7528 }
7529 /* Render standby states get 0 weight */
7530 pxw[14] = 0;
7531 pxw[15] = 0;
7532
7533 for (i = 0; i < 4; i++) {
7534 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
7535 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
Ville Syrjälä616847e2015-09-18 20:03:19 +03007536 I915_WRITE(PXW(i), val);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007537 }
7538
7539 /* Adjust magic regs to magic values (more experimental results) */
7540 I915_WRITE(OGW0, 0);
7541 I915_WRITE(OGW1, 0);
7542 I915_WRITE(EG0, 0x00007f00);
7543 I915_WRITE(EG1, 0x0000000e);
7544 I915_WRITE(EG2, 0x000e0000);
7545 I915_WRITE(EG3, 0x68000300);
7546 I915_WRITE(EG4, 0x42000000);
7547 I915_WRITE(EG5, 0x00140031);
7548 I915_WRITE(EG6, 0);
7549 I915_WRITE(EG7, 0);
7550
7551 for (i = 0; i < 8; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007552 I915_WRITE(PXWL(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007553
7554 /* Enable PMON + select events */
7555 I915_WRITE(ECR, 0x80000019);
7556
7557 lcfuse = I915_READ(LCFUSE02);
7558
Daniel Vetter20e4d402012-08-08 23:35:39 +02007559 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007560}
7561
Chris Wilsondc979972016-05-10 14:10:04 +01007562void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007563{
Imre Deakb268c692015-12-15 20:10:31 +02007564 /*
7565 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
7566 * requirement.
7567 */
7568 if (!i915.enable_rc6) {
7569 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
7570 intel_runtime_pm_get(dev_priv);
7571 }
Imre Deake6069ca2014-04-18 16:01:02 +03007572
Chris Wilsonb5163db2016-08-10 13:58:24 +01007573 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson773ea9a2016-07-13 09:10:33 +01007574 mutex_lock(&dev_priv->rps.hw_lock);
7575
7576 /* Initialize RPS limits (for userspace) */
Chris Wilsondc979972016-05-10 14:10:04 +01007577 if (IS_CHERRYVIEW(dev_priv))
7578 cherryview_init_gt_powersave(dev_priv);
7579 else if (IS_VALLEYVIEW(dev_priv))
7580 valleyview_init_gt_powersave(dev_priv);
Chris Wilson2a13ae72016-08-02 11:15:27 +01007581 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson773ea9a2016-07-13 09:10:33 +01007582 gen6_init_rps_frequencies(dev_priv);
7583
7584 /* Derive initial user preferences/limits from the hardware limits */
7585 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
7586 dev_priv->rps.cur_freq = dev_priv->rps.idle_freq;
7587
7588 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
7589 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
7590
7591 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
7592 dev_priv->rps.min_freq_softlimit =
7593 max_t(int,
7594 dev_priv->rps.efficient_freq,
7595 intel_freq_opcode(dev_priv, 450));
7596
Chris Wilson99ac9612016-07-13 09:10:34 +01007597 /* After setting max-softlimit, find the overclock max freq */
7598 if (IS_GEN6(dev_priv) ||
7599 IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
7600 u32 params = 0;
7601
7602 sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &params);
7603 if (params & BIT(31)) { /* OC supported */
7604 DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
7605 (dev_priv->rps.max_freq & 0xff) * 50,
7606 (params & 0xff) * 50);
7607 dev_priv->rps.max_freq = params & 0xff;
7608 }
7609 }
7610
Chris Wilson29ecd78d2016-07-13 09:10:35 +01007611 /* Finally allow us to boost to max by default */
7612 dev_priv->rps.boost_freq = dev_priv->rps.max_freq;
7613
Chris Wilson773ea9a2016-07-13 09:10:33 +01007614 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb5163db2016-08-10 13:58:24 +01007615 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson54b4f682016-07-21 21:16:19 +01007616
7617 intel_autoenable_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03007618}
7619
Chris Wilsondc979972016-05-10 14:10:04 +01007620void intel_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007621{
Ville Syrjälä8dac1e12016-08-02 14:07:33 +03007622 if (IS_VALLEYVIEW(dev_priv))
Chris Wilsondc979972016-05-10 14:10:04 +01007623 valleyview_cleanup_gt_powersave(dev_priv);
Imre Deakb268c692015-12-15 20:10:31 +02007624
7625 if (!i915.enable_rc6)
7626 intel_runtime_pm_put(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03007627}
7628
Chris Wilson54b4f682016-07-21 21:16:19 +01007629/**
7630 * intel_suspend_gt_powersave - suspend PM work and helper threads
7631 * @dev_priv: i915 device
7632 *
7633 * We don't want to disable RC6 or other features here, we just want
7634 * to make sure any work we've queued has finished and won't bother
7635 * us while we're suspended.
7636 */
7637void intel_suspend_gt_powersave(struct drm_i915_private *dev_priv)
7638{
7639 if (INTEL_GEN(dev_priv) < 6)
7640 return;
7641
7642 if (cancel_delayed_work_sync(&dev_priv->rps.autoenable_work))
7643 intel_runtime_pm_put(dev_priv);
7644
7645 /* gen6_rps_idle() will be called later to disable interrupts */
7646}
7647
Chris Wilsonb7137e02016-07-13 09:10:37 +01007648void intel_sanitize_gt_powersave(struct drm_i915_private *dev_priv)
7649{
7650 dev_priv->rps.enabled = true; /* force disabling */
7651 intel_disable_gt_powersave(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007652
7653 gen6_reset_rps_interrupts(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07007654}
7655
Chris Wilsondc979972016-05-10 14:10:04 +01007656void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
Daniel Vetter8090c6b2012-06-24 16:42:32 +02007657{
Chris Wilsonb7137e02016-07-13 09:10:37 +01007658 if (!READ_ONCE(dev_priv->rps.enabled))
7659 return;
Jesse Barnes1a01ab32012-11-02 11:14:00 -07007660
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007661 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007662
Chris Wilsonb7137e02016-07-13 09:10:37 +01007663 if (INTEL_GEN(dev_priv) >= 9) {
7664 gen9_disable_rc6(dev_priv);
7665 gen9_disable_rps(dev_priv);
7666 } else if (IS_CHERRYVIEW(dev_priv)) {
7667 cherryview_disable_rps(dev_priv);
7668 } else if (IS_VALLEYVIEW(dev_priv)) {
7669 valleyview_disable_rps(dev_priv);
7670 } else if (INTEL_GEN(dev_priv) >= 6) {
7671 gen6_disable_rps(dev_priv);
7672 } else if (IS_IRONLAKE_M(dev_priv)) {
7673 ironlake_disable_drps(dev_priv);
7674 }
7675
7676 dev_priv->rps.enabled = false;
7677 mutex_unlock(&dev_priv->rps.hw_lock);
7678}
7679
7680void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
7681{
Chris Wilson54b4f682016-07-21 21:16:19 +01007682 /* We shouldn't be disabling as we submit, so this should be less
7683 * racy than it appears!
7684 */
Chris Wilsonb7137e02016-07-13 09:10:37 +01007685 if (READ_ONCE(dev_priv->rps.enabled))
7686 return;
7687
7688 /* Powersaving is controlled by the host when inside a VM */
7689 if (intel_vgpu_active(dev_priv))
7690 return;
7691
7692 mutex_lock(&dev_priv->rps.hw_lock);
Imre Deak3cc134e2014-11-19 15:30:03 +02007693
Chris Wilsondc979972016-05-10 14:10:04 +01007694 if (IS_CHERRYVIEW(dev_priv)) {
7695 cherryview_enable_rps(dev_priv);
7696 } else if (IS_VALLEYVIEW(dev_priv)) {
7697 valleyview_enable_rps(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007698 } else if (INTEL_GEN(dev_priv) >= 9) {
Chris Wilsondc979972016-05-10 14:10:04 +01007699 gen9_enable_rc6(dev_priv);
7700 gen9_enable_rps(dev_priv);
Rodrigo Vivib976dc52017-01-23 10:32:37 -08007701 if (IS_GEN9_BC(dev_priv))
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007702 gen6_update_ring_freq(dev_priv);
Chris Wilsondc979972016-05-10 14:10:04 +01007703 } else if (IS_BROADWELL(dev_priv)) {
7704 gen8_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007705 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007706 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilsondc979972016-05-10 14:10:04 +01007707 gen6_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007708 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007709 } else if (IS_IRONLAKE_M(dev_priv)) {
7710 ironlake_enable_drps(dev_priv);
7711 intel_init_emon(dev_priv);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007712 }
Chris Wilsonaed242f2015-03-18 09:48:21 +00007713
7714 WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
7715 WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
7716
7717 WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
7718 WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
7719
Chris Wilson54b4f682016-07-21 21:16:19 +01007720 dev_priv->rps.enabled = true;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007721 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007722}
Imre Deakc6df39b2014-04-14 20:24:29 +03007723
Chris Wilson54b4f682016-07-21 21:16:19 +01007724static void __intel_autoenable_gt_powersave(struct work_struct *work)
7725{
7726 struct drm_i915_private *dev_priv =
7727 container_of(work, typeof(*dev_priv), rps.autoenable_work.work);
7728 struct intel_engine_cs *rcs;
7729 struct drm_i915_gem_request *req;
7730
7731 if (READ_ONCE(dev_priv->rps.enabled))
7732 goto out;
7733
Akash Goel3b3f1652016-10-13 22:44:48 +05307734 rcs = dev_priv->engine[RCS];
Chris Wilsone8a9c582016-12-18 15:37:20 +00007735 if (rcs->last_retired_context)
Chris Wilson54b4f682016-07-21 21:16:19 +01007736 goto out;
7737
7738 if (!rcs->init_context)
7739 goto out;
7740
7741 mutex_lock(&dev_priv->drm.struct_mutex);
7742
7743 req = i915_gem_request_alloc(rcs, dev_priv->kernel_context);
7744 if (IS_ERR(req))
7745 goto unlock;
7746
7747 if (!i915.enable_execlists && i915_switch_context(req) == 0)
7748 rcs->init_context(req);
7749
7750 /* Mark the device busy, calling intel_enable_gt_powersave() */
Chris Wilsone642c852017-03-17 11:47:09 +00007751 i915_add_request(req);
Chris Wilson54b4f682016-07-21 21:16:19 +01007752
7753unlock:
7754 mutex_unlock(&dev_priv->drm.struct_mutex);
7755out:
7756 intel_runtime_pm_put(dev_priv);
7757}
7758
7759void intel_autoenable_gt_powersave(struct drm_i915_private *dev_priv)
7760{
7761 if (READ_ONCE(dev_priv->rps.enabled))
7762 return;
7763
7764 if (IS_IRONLAKE_M(dev_priv)) {
7765 ironlake_enable_drps(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007766 intel_init_emon(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007767 } else if (INTEL_INFO(dev_priv)->gen >= 6) {
7768 /*
7769 * PCU communication is slow and this doesn't need to be
7770 * done at any specific time, so do this out of our fast path
7771 * to make resume and init faster.
7772 *
7773 * We depend on the HW RC6 power context save/restore
7774 * mechanism when entering D3 through runtime PM suspend. So
7775 * disable RPM until RPS/RC6 is properly setup. We can only
7776 * get here via the driver load/system resume/runtime resume
7777 * paths, so the _noresume version is enough (and in case of
7778 * runtime resume it's necessary).
7779 */
7780 if (queue_delayed_work(dev_priv->wq,
7781 &dev_priv->rps.autoenable_work,
7782 round_jiffies_up_relative(HZ)))
7783 intel_runtime_pm_get_noresume(dev_priv);
7784 }
7785}
7786
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007787static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007788{
Daniel Vetter3107bd42012-10-31 22:52:31 +01007789 /*
7790 * On Ibex Peak and Cougar Point, we need to disable clock
7791 * gating for the panel power sequencer or it will fail to
7792 * start up when no ports are active.
7793 */
7794 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
7795}
7796
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007797static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007798{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007799 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007800
Damien Lespiau055e3932014-08-18 13:49:10 +01007801 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007802 I915_WRITE(DSPCNTR(pipe),
7803 I915_READ(DSPCNTR(pipe)) |
7804 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007805
7806 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
7807 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007808 }
7809}
7810
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007811static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä017636c2013-12-05 15:51:37 +02007812{
Ville Syrjälä017636c2013-12-05 15:51:37 +02007813 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
7814 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
7815 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
7816
7817 /*
7818 * Don't touch WM1S_LP_EN here.
7819 * Doing so could cause underruns.
7820 */
7821}
7822
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007823static void ironlake_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007824{
Damien Lespiau231e54f2012-10-19 17:55:41 +01007825 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007826
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01007827 /*
7828 * Required for FBC
7829 * WaFbcDisableDpfcClockGating:ilk
7830 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007831 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
7832 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
7833 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007834
7835 I915_WRITE(PCH_3DCGDIS0,
7836 MARIUNIT_CLOCK_GATE_DISABLE |
7837 SVSMUNIT_CLOCK_GATE_DISABLE);
7838 I915_WRITE(PCH_3DCGDIS1,
7839 VFMUNIT_CLOCK_GATE_DISABLE);
7840
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007841 /*
7842 * According to the spec the following bits should be set in
7843 * order to enable memory self-refresh
7844 * The bit 22/21 of 0x42004
7845 * The bit 5 of 0x42020
7846 * The bit 15 of 0x45000
7847 */
7848 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7849 (I915_READ(ILK_DISPLAY_CHICKEN2) |
7850 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007851 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007852 I915_WRITE(DISP_ARB_CTL,
7853 (I915_READ(DISP_ARB_CTL) |
7854 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02007855
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007856 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007857
7858 /*
7859 * Based on the document from hardware guys the following bits
7860 * should be set unconditionally in order to enable FBC.
7861 * The bit 22 of 0x42000
7862 * The bit 22 of 0x42004
7863 * The bit 7,8,9 of 0x42020.
7864 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007865 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01007866 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007867 I915_WRITE(ILK_DISPLAY_CHICKEN1,
7868 I915_READ(ILK_DISPLAY_CHICKEN1) |
7869 ILK_FBCQ_DIS);
7870 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7871 I915_READ(ILK_DISPLAY_CHICKEN2) |
7872 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007873 }
7874
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007875 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
7876
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007877 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7878 I915_READ(ILK_DISPLAY_CHICKEN2) |
7879 ILK_ELPIN_409_SELECT);
7880 I915_WRITE(_3D_CHICKEN2,
7881 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
7882 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02007883
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007884 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02007885 I915_WRITE(CACHE_MODE_0,
7886 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01007887
Akash Goel4e046322014-04-04 17:14:38 +05307888 /* WaDisable_RenderCache_OperationalFlush:ilk */
7889 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7890
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007891 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03007892
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007893 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007894}
7895
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007896static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007897{
Daniel Vetter3107bd42012-10-31 22:52:31 +01007898 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007899 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01007900
7901 /*
7902 * On Ibex Peak and Cougar Point, we need to disable clock
7903 * gating for the panel power sequencer or it will fail to
7904 * start up when no ports are active.
7905 */
Jesse Barnescd664072013-10-02 10:34:19 -07007906 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
7907 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
7908 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007909 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
7910 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01007911 /* The below fixes the weird display corruption, a few pixels shifted
7912 * downward, on (only) LVDS of some HP laptops with IVY.
7913 */
Damien Lespiau055e3932014-08-18 13:49:10 +01007914 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007915 val = I915_READ(TRANS_CHICKEN2(pipe));
7916 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
7917 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007918 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007919 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007920 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
7921 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
7922 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007923 I915_WRITE(TRANS_CHICKEN2(pipe), val);
7924 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01007925 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01007926 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01007927 I915_WRITE(TRANS_CHICKEN1(pipe),
7928 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
7929 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007930}
7931
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007932static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007933{
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007934 uint32_t tmp;
7935
7936 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02007937 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
7938 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
7939 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007940}
7941
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007942static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007943{
Damien Lespiau231e54f2012-10-19 17:55:41 +01007944 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007945
Damien Lespiau231e54f2012-10-19 17:55:41 +01007946 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007947
7948 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7949 I915_READ(ILK_DISPLAY_CHICKEN2) |
7950 ILK_ELPIN_409_SELECT);
7951
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007952 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01007953 I915_WRITE(_3D_CHICKEN,
7954 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
7955
Akash Goel4e046322014-04-04 17:14:38 +05307956 /* WaDisable_RenderCache_OperationalFlush:snb */
7957 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7958
Ville Syrjälä8d85d272014-02-04 21:59:15 +02007959 /*
7960 * BSpec recoomends 8x4 when MSAA is used,
7961 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007962 *
7963 * Note that PS/WM thread counts depend on the WIZ hashing
7964 * disable bit, which we don't touch here, but it's good
7965 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02007966 */
7967 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007968 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02007969
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007970 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007971
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007972 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02007973 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007974
7975 I915_WRITE(GEN6_UCGCTL1,
7976 I915_READ(GEN6_UCGCTL1) |
7977 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
7978 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
7979
7980 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
7981 * gating disable must be set. Failure to set it results in
7982 * flickering pixels due to Z write ordering failures after
7983 * some amount of runtime in the Mesa "fire" demo, and Unigine
7984 * Sanctuary and Tropics, and apparently anything else with
7985 * alpha test or pixel discard.
7986 *
7987 * According to the spec, bit 11 (RCCUNIT) must also be set,
7988 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007989 *
Ville Syrjäläef593182014-01-22 21:32:47 +02007990 * WaDisableRCCUnitClockGating:snb
7991 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007992 */
7993 I915_WRITE(GEN6_UCGCTL2,
7994 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
7995 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
7996
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02007997 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02007998 I915_WRITE(_3D_CHICKEN3,
7999 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008000
8001 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02008002 * Bspec says:
8003 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
8004 * 3DSTATE_SF number of SF output attributes is more than 16."
8005 */
8006 I915_WRITE(_3D_CHICKEN3,
8007 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
8008
8009 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008010 * According to the spec the following bits should be
8011 * set in order to enable memory self-refresh and fbc:
8012 * The bit21 and bit22 of 0x42000
8013 * The bit21 and bit22 of 0x42004
8014 * The bit5 and bit7 of 0x42020
8015 * The bit14 of 0x70180
8016 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01008017 *
8018 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008019 */
8020 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8021 I915_READ(ILK_DISPLAY_CHICKEN1) |
8022 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
8023 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8024 I915_READ(ILK_DISPLAY_CHICKEN2) |
8025 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01008026 I915_WRITE(ILK_DSPCLK_GATE_D,
8027 I915_READ(ILK_DSPCLK_GATE_D) |
8028 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
8029 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008030
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008031 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07008032
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008033 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008034
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008035 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008036}
8037
8038static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
8039{
8040 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
8041
Ville Syrjälä3aad9052014-01-22 21:32:59 +02008042 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02008043 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02008044 *
8045 * This actually overrides the dispatch
8046 * mode for all thread types.
8047 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008048 reg &= ~GEN7_FF_SCHED_MASK;
8049 reg |= GEN7_FF_TS_SCHED_HW;
8050 reg |= GEN7_FF_VS_SCHED_HW;
8051 reg |= GEN7_FF_DS_SCHED_HW;
8052
8053 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
8054}
8055
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008056static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008057{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008058 /*
8059 * TODO: this bit should only be enabled when really needed, then
8060 * disabled when not needed anymore in order to save power.
8061 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008062 if (HAS_PCH_LPT_LP(dev_priv))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008063 I915_WRITE(SOUTH_DSPCLK_GATE_D,
8064 I915_READ(SOUTH_DSPCLK_GATE_D) |
8065 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03008066
8067 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03008068 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
8069 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03008070 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008071}
8072
Ville Syrjälä712bf362016-10-31 22:37:23 +02008073static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03008074{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008075 if (HAS_PCH_LPT_LP(dev_priv)) {
Imre Deak7d708ee2013-04-17 14:04:50 +03008076 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
8077
8078 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8079 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8080 }
8081}
8082
Imre Deak450174f2016-05-03 15:54:21 +03008083static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
8084 int general_prio_credits,
8085 int high_prio_credits)
8086{
8087 u32 misccpctl;
8088
8089 /* WaTempDisableDOPClkGating:bdw */
8090 misccpctl = I915_READ(GEN7_MISCCPCTL);
8091 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
8092
8093 I915_WRITE(GEN8_L3SQCREG1,
8094 L3_GENERAL_PRIO_CREDITS(general_prio_credits) |
8095 L3_HIGH_PRIO_CREDITS(high_prio_credits));
8096
8097 /*
8098 * Wait at least 100 clocks before re-enabling clock gating.
8099 * See the definition of L3SQCREG1 in BSpec.
8100 */
8101 POSTING_READ(GEN8_L3SQCREG1);
8102 udelay(1);
8103 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
8104}
8105
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008106static void kabylake_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008107{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008108 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008109
8110 /* WaDisableSDEUnitClockGating:kbl */
8111 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
8112 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8113 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03008114
8115 /* WaDisableGamClockGating:kbl */
8116 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
8117 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
8118 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008119
8120 /* WaFbcNukeOnHostModify:kbl */
8121 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8122 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008123}
8124
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008125static void skylake_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008126{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008127 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03008128
8129 /* WAC6entrylatency:skl */
8130 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
8131 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008132
8133 /* WaFbcNukeOnHostModify:skl */
8134 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8135 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008136}
8137
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008138static void broadwell_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008139{
Damien Lespiau07d27e22014-03-03 17:31:46 +00008140 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008141
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008142 ilk_init_lp_watermarks(dev_priv);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07008143
Ben Widawskyab57fff2013-12-12 15:28:04 -08008144 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07008145 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008146
Ben Widawskyab57fff2013-12-12 15:28:04 -08008147 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008148 I915_WRITE(CHICKEN_PAR1_1,
8149 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
8150
Ben Widawskyab57fff2013-12-12 15:28:04 -08008151 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01008152 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00008153 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02008154 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02008155 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008156 }
Ben Widawsky63801f22013-12-12 17:26:03 -08008157
Ben Widawskyab57fff2013-12-12 15:28:04 -08008158 /* WaVSRefCountFullforceMissDisable:bdw */
8159 /* WaDSRefCountFullforceMissDisable:bdw */
8160 I915_WRITE(GEN7_FF_THREAD_MODE,
8161 I915_READ(GEN7_FF_THREAD_MODE) &
8162 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02008163
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02008164 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
8165 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02008166
8167 /* WaDisableSDEUnitClockGating:bdw */
8168 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8169 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00008170
Imre Deak450174f2016-05-03 15:54:21 +03008171 /* WaProgramL3SqcReg1Default:bdw */
8172 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03008173
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008174 /*
8175 * WaGttCachingOffByDefault:bdw
8176 * GTT cache may not work with big pages, so if those
8177 * are ever enabled GTT cache may need to be disabled.
8178 */
8179 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
8180
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03008181 /* WaKVMNotificationOnConfigChange:bdw */
8182 I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
8183 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
8184
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008185 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00008186
8187 /* WaDisableDopClockGating:bdw
8188 *
8189 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
8190 * clock gating.
8191 */
8192 I915_WRITE(GEN6_UCGCTL1,
8193 I915_READ(GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008194}
8195
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008196static void haswell_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008197{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008198 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008199
Francisco Jerezf3fc4882013-10-02 15:53:16 -07008200 /* L3 caching of data atomics doesn't work -- disable it. */
8201 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
8202 I915_WRITE(HSW_ROW_CHICKEN3,
8203 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
8204
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008205 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008206 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8207 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8208 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8209
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02008210 /* WaVSRefCountFullforceMissDisable:hsw */
8211 I915_WRITE(GEN7_FF_THREAD_MODE,
8212 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008213
Akash Goel4e046322014-04-04 17:14:38 +05308214 /* WaDisable_RenderCache_OperationalFlush:hsw */
8215 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8216
Chia-I Wufe27c602014-01-28 13:29:33 +08008217 /* enable HiZ Raw Stall Optimization */
8218 I915_WRITE(CACHE_MODE_0_GEN7,
8219 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
8220
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008221 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008222 I915_WRITE(CACHE_MODE_1,
8223 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03008224
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008225 /*
8226 * BSpec recommends 8x4 when MSAA is used,
8227 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008228 *
8229 * Note that PS/WM thread counts depend on the WIZ hashing
8230 * disable bit, which we don't touch here, but it's good
8231 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008232 */
8233 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008234 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008235
Kenneth Graunke94411592014-12-31 16:23:00 -08008236 /* WaSampleCChickenBitEnable:hsw */
8237 I915_WRITE(HALF_SLICE_CHICKEN3,
8238 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
8239
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008240 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07008241 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
8242
Paulo Zanoni90a88642013-05-03 17:23:45 -03008243 /* WaRsPkgCStateDisplayPMReq:hsw */
8244 I915_WRITE(CHICKEN_PAR1_1,
8245 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03008246
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008247 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008248}
8249
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008250static void ivybridge_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008251{
Ben Widawsky20848222012-05-04 18:58:59 -07008252 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008253
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008254 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008255
Damien Lespiau231e54f2012-10-19 17:55:41 +01008256 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008257
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008258 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05008259 I915_WRITE(_3D_CHICKEN3,
8260 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
8261
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008262 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008263 I915_WRITE(IVB_CHICKEN3,
8264 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8265 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8266
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008267 /* WaDisablePSDDualDispatchEnable:ivb */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008268 if (IS_IVB_GT1(dev_priv))
Jesse Barnes12f33822012-10-25 12:15:45 -07008269 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
8270 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07008271
Akash Goel4e046322014-04-04 17:14:38 +05308272 /* WaDisable_RenderCache_OperationalFlush:ivb */
8273 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8274
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008275 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008276 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
8277 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
8278
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008279 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008280 I915_WRITE(GEN7_L3CNTLREG1,
8281 GEN7_WA_FOR_GEN7_L3_CONTROL);
8282 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07008283 GEN7_WA_L3_CHICKEN_MODE);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008284 if (IS_IVB_GT1(dev_priv))
Jesse Barnes8ab43972012-10-25 12:15:42 -07008285 I915_WRITE(GEN7_ROW_CHICKEN2,
8286 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02008287 else {
8288 /* must write both registers */
8289 I915_WRITE(GEN7_ROW_CHICKEN2,
8290 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07008291 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
8292 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02008293 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008294
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008295 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05008296 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
8297 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
8298
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02008299 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07008300 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008301 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008302 */
8303 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02008304 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07008305
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008306 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008307 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8308 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8309 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8310
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008311 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008312
8313 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02008314
Chris Wilson22721342014-03-04 09:41:43 +00008315 if (0) { /* causes HiZ corruption on ivb:gt1 */
8316 /* enable HiZ Raw Stall Optimization */
8317 I915_WRITE(CACHE_MODE_0_GEN7,
8318 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
8319 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08008320
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008321 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02008322 I915_WRITE(CACHE_MODE_1,
8323 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07008324
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008325 /*
8326 * BSpec recommends 8x4 when MSAA is used,
8327 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008328 *
8329 * Note that PS/WM thread counts depend on the WIZ hashing
8330 * disable bit, which we don't touch here, but it's good
8331 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008332 */
8333 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008334 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008335
Ben Widawsky20848222012-05-04 18:58:59 -07008336 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
8337 snpcr &= ~GEN6_MBC_SNPCR_MASK;
8338 snpcr |= GEN6_MBC_SNPCR_MED;
8339 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008340
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008341 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008342 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008343
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008344 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008345}
8346
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008347static void valleyview_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008348{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008349 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05008350 I915_WRITE(_3D_CHICKEN3,
8351 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
8352
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008353 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008354 I915_WRITE(IVB_CHICKEN3,
8355 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8356 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8357
Ville Syrjäläfad7d362014-01-22 21:32:39 +02008358 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008359 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07008360 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08008361 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
8362 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07008363
Akash Goel4e046322014-04-04 17:14:38 +05308364 /* WaDisable_RenderCache_OperationalFlush:vlv */
8365 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8366
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008367 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05008368 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
8369 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
8370
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008371 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07008372 I915_WRITE(GEN7_ROW_CHICKEN2,
8373 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
8374
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008375 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008376 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8377 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8378 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8379
Ville Syrjälä46680e02014-01-22 21:33:01 +02008380 gen7_setup_fixed_func_scheduler(dev_priv);
8381
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02008382 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07008383 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008384 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008385 */
8386 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02008387 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07008388
Akash Goelc98f5062014-03-24 23:00:07 +05308389 /* WaDisableL3Bank2xClockGate:vlv
8390 * Disabling L3 clock gating- MMIO 940c[25] = 1
8391 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
8392 I915_WRITE(GEN7_UCGCTL4,
8393 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07008394
Ville Syrjäläafd58e72014-01-22 21:33:03 +02008395 /*
8396 * BSpec says this must be set, even though
8397 * WaDisable4x2SubspanOptimization isn't listed for VLV.
8398 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02008399 I915_WRITE(CACHE_MODE_1,
8400 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07008401
8402 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02008403 * BSpec recommends 8x4 when MSAA is used,
8404 * however in practice 16x4 seems fastest.
8405 *
8406 * Note that PS/WM thread counts depend on the WIZ hashing
8407 * disable bit, which we don't touch here, but it's good
8408 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
8409 */
8410 I915_WRITE(GEN7_GT_MODE,
8411 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
8412
8413 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02008414 * WaIncreaseL3CreditsForVLVB0:vlv
8415 * This is the hardware default actually.
8416 */
8417 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
8418
8419 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008420 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07008421 * Disable clock gating on th GCFG unit to prevent a delay
8422 * in the reporting of vblank events.
8423 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02008424 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008425}
8426
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008427static void cherryview_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03008428{
Ville Syrjälä232ce332014-04-09 13:28:35 +03008429 /* WaVSRefCountFullforceMissDisable:chv */
8430 /* WaDSRefCountFullforceMissDisable:chv */
8431 I915_WRITE(GEN7_FF_THREAD_MODE,
8432 I915_READ(GEN7_FF_THREAD_MODE) &
8433 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03008434
8435 /* WaDisableSemaphoreAndSyncFlipWait:chv */
8436 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
8437 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03008438
8439 /* WaDisableCSUnitClockGating:chv */
8440 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
8441 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03008442
8443 /* WaDisableSDEUnitClockGating:chv */
8444 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8445 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008446
8447 /*
Imre Deak450174f2016-05-03 15:54:21 +03008448 * WaProgramL3SqcReg1Default:chv
8449 * See gfxspecs/Related Documents/Performance Guide/
8450 * LSQC Setting Recommendations.
8451 */
8452 gen8_set_l3sqc_credits(dev_priv, 38, 2);
8453
8454 /*
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008455 * GTT cache may not work with big pages, so if those
8456 * are ever enabled GTT cache may need to be disabled.
8457 */
8458 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03008459}
8460
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008461static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008462{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008463 uint32_t dspclk_gate;
8464
8465 I915_WRITE(RENCLK_GATE_D1, 0);
8466 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
8467 GS_UNIT_CLOCK_GATE_DISABLE |
8468 CL_UNIT_CLOCK_GATE_DISABLE);
8469 I915_WRITE(RAMCLK_GATE_D, 0);
8470 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
8471 OVRUNIT_CLOCK_GATE_DISABLE |
8472 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008473 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008474 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
8475 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02008476
8477 /* WaDisableRenderCachePipelinedFlush */
8478 I915_WRITE(CACHE_MODE_0,
8479 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03008480
Akash Goel4e046322014-04-04 17:14:38 +05308481 /* WaDisable_RenderCache_OperationalFlush:g4x */
8482 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8483
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008484 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008485}
8486
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008487static void crestline_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008488{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008489 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
8490 I915_WRITE(RENCLK_GATE_D2, 0);
8491 I915_WRITE(DSPCLK_GATE_D, 0);
8492 I915_WRITE(RAMCLK_GATE_D, 0);
8493 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03008494 I915_WRITE(MI_ARB_STATE,
8495 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05308496
8497 /* WaDisable_RenderCache_OperationalFlush:gen4 */
8498 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008499}
8500
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008501static void broadwater_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008502{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008503 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
8504 I965_RCC_CLOCK_GATE_DISABLE |
8505 I965_RCPB_CLOCK_GATE_DISABLE |
8506 I965_ISC_CLOCK_GATE_DISABLE |
8507 I965_FBC_CLOCK_GATE_DISABLE);
8508 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03008509 I915_WRITE(MI_ARB_STATE,
8510 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05308511
8512 /* WaDisable_RenderCache_OperationalFlush:gen4 */
8513 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008514}
8515
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008516static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008517{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008518 u32 dstate = I915_READ(D_STATE);
8519
8520 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
8521 DSTATE_DOT_CLOCK_GATING;
8522 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01008523
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008524 if (IS_PINEVIEW(dev_priv))
Chris Wilson13a86b82012-04-24 14:51:43 +01008525 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02008526
8527 /* IIR "flip pending" means done if this bit is set */
8528 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02008529
8530 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02008531 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02008532
8533 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
8534 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03008535
8536 I915_WRITE(MI_ARB_STATE,
8537 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008538}
8539
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008540static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008541{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008542 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02008543
8544 /* interrupts should cause a wake up from C3 */
8545 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
8546 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03008547
8548 I915_WRITE(MEM_MODE,
8549 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008550}
8551
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008552static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008553{
Ville Syrjälä10383922014-08-15 01:21:54 +03008554 I915_WRITE(MEM_MODE,
8555 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
8556 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008557}
8558
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008559void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008560{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008561 dev_priv->display.init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008562}
8563
Ville Syrjälä712bf362016-10-31 22:37:23 +02008564void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03008565{
Ville Syrjälä712bf362016-10-31 22:37:23 +02008566 if (HAS_PCH_LPT(dev_priv))
8567 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03008568}
8569
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008570static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02008571{
8572 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
8573}
8574
8575/**
8576 * intel_init_clock_gating_hooks - setup the clock gating hooks
8577 * @dev_priv: device private
8578 *
8579 * Setup the hooks that configure which clocks of a given platform can be
8580 * gated and also apply various GT and display specific workarounds for these
8581 * platforms. Note that some GT specific workarounds are applied separately
8582 * when GPU contexts or batchbuffers start their execution.
8583 */
8584void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
8585{
8586 if (IS_SKYLAKE(dev_priv))
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008587 dev_priv->display.init_clock_gating = skylake_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008588 else if (IS_KABYLAKE(dev_priv))
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008589 dev_priv->display.init_clock_gating = kabylake_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02008590 else if (IS_BROXTON(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02008591 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02008592 else if (IS_GEMINILAKE(dev_priv))
8593 dev_priv->display.init_clock_gating = glk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008594 else if (IS_BROADWELL(dev_priv))
8595 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
8596 else if (IS_CHERRYVIEW(dev_priv))
8597 dev_priv->display.init_clock_gating = cherryview_init_clock_gating;
8598 else if (IS_HASWELL(dev_priv))
8599 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
8600 else if (IS_IVYBRIDGE(dev_priv))
8601 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
8602 else if (IS_VALLEYVIEW(dev_priv))
8603 dev_priv->display.init_clock_gating = valleyview_init_clock_gating;
8604 else if (IS_GEN6(dev_priv))
8605 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
8606 else if (IS_GEN5(dev_priv))
8607 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
8608 else if (IS_G4X(dev_priv))
8609 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02008610 else if (IS_I965GM(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02008611 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02008612 else if (IS_I965G(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02008613 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
8614 else if (IS_GEN3(dev_priv))
8615 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
8616 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
8617 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
8618 else if (IS_GEN2(dev_priv))
8619 dev_priv->display.init_clock_gating = i830_init_clock_gating;
8620 else {
8621 MISSING_CASE(INTEL_DEVID(dev_priv));
8622 dev_priv->display.init_clock_gating = nop_init_clock_gating;
8623 }
8624}
8625
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008626/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008627void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008628{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02008629 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008630
Daniel Vetterc921aba2012-04-26 23:28:17 +02008631 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008632 if (IS_PINEVIEW(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02008633 i915_pineview_get_mem_freq(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008634 else if (IS_GEN5(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02008635 i915_ironlake_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02008636
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008637 /* For FIFO watermark updates */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008638 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008639 skl_setup_wm_latency(dev_priv);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01008640 dev_priv->display.initial_watermarks = skl_initial_wm;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01008641 dev_priv->display.atomic_update_watermarks = skl_atomic_update_crtc_wm;
Matt Roper98d39492016-05-12 07:06:03 -07008642 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008643 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008644 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03008645
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008646 if ((IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008647 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008648 (!IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008649 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07008650 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08008651 dev_priv->display.compute_intermediate_wm =
8652 ilk_compute_intermediate_wm;
8653 dev_priv->display.initial_watermarks =
8654 ilk_initial_watermarks;
8655 dev_priv->display.optimize_watermarks =
8656 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02008657 } else {
8658 DRM_DEBUG_KMS("Failed to read display plane latency. "
8659 "Disable CxSR\n");
8660 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02008661 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008662 vlv_setup_wm_latency(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02008663 dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
Ville Syrjälä4841da52017-03-02 19:14:59 +02008664 dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02008665 dev_priv->display.initial_watermarks = vlv_initial_watermarks;
Ville Syrjälä4841da52017-03-02 19:14:59 +02008666 dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
Ville Syrjäläff32c542017-03-02 19:14:57 +02008667 dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03008668 } else if (IS_G4X(dev_priv)) {
8669 g4x_setup_wm_latency(dev_priv);
8670 dev_priv->display.compute_pipe_wm = g4x_compute_pipe_wm;
8671 dev_priv->display.compute_intermediate_wm = g4x_compute_intermediate_wm;
8672 dev_priv->display.initial_watermarks = g4x_initial_watermarks;
8673 dev_priv->display.optimize_watermarks = g4x_optimize_watermarks;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008674 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008675 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008676 dev_priv->is_ddr3,
8677 dev_priv->fsb_freq,
8678 dev_priv->mem_freq)) {
8679 DRM_INFO("failed to find known CxSR latency "
8680 "(found ddr%s fsb freq %d, mem freq %d), "
8681 "disabling CxSR\n",
8682 (dev_priv->is_ddr3 == 1) ? "3" : "2",
8683 dev_priv->fsb_freq, dev_priv->mem_freq);
8684 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03008685 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008686 dev_priv->display.update_wm = NULL;
8687 } else
8688 dev_priv->display.update_wm = pineview_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008689 } else if (IS_GEN4(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008690 dev_priv->display.update_wm = i965_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008691 } else if (IS_GEN3(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008692 dev_priv->display.update_wm = i9xx_update_wm;
8693 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008694 } else if (IS_GEN2(dev_priv)) {
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008695 if (INTEL_INFO(dev_priv)->num_pipes == 1) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008696 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008697 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008698 } else {
8699 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008700 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008701 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008702 } else {
8703 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008704 }
8705}
8706
Lyude87660502016-08-17 15:55:53 -04008707static inline int gen6_check_mailbox_status(struct drm_i915_private *dev_priv)
8708{
8709 uint32_t flags =
8710 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
8711
8712 switch (flags) {
8713 case GEN6_PCODE_SUCCESS:
8714 return 0;
8715 case GEN6_PCODE_UNIMPLEMENTED_CMD:
8716 case GEN6_PCODE_ILLEGAL_CMD:
8717 return -ENXIO;
8718 case GEN6_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Chris Wilson7850d1c2016-08-26 11:59:26 +01008719 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Lyude87660502016-08-17 15:55:53 -04008720 return -EOVERFLOW;
8721 case GEN6_PCODE_TIMEOUT:
8722 return -ETIMEDOUT;
8723 default:
Michal Wajdeczkof0d66152017-03-28 08:45:12 +00008724 MISSING_CASE(flags);
Lyude87660502016-08-17 15:55:53 -04008725 return 0;
8726 }
8727}
8728
8729static inline int gen7_check_mailbox_status(struct drm_i915_private *dev_priv)
8730{
8731 uint32_t flags =
8732 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
8733
8734 switch (flags) {
8735 case GEN6_PCODE_SUCCESS:
8736 return 0;
8737 case GEN6_PCODE_ILLEGAL_CMD:
8738 return -ENXIO;
8739 case GEN7_PCODE_TIMEOUT:
8740 return -ETIMEDOUT;
8741 case GEN7_PCODE_ILLEGAL_DATA:
8742 return -EINVAL;
8743 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
8744 return -EOVERFLOW;
8745 default:
8746 MISSING_CASE(flags);
8747 return 0;
8748 }
8749}
8750
Tom O'Rourke151a49d2014-11-13 18:50:10 -08008751int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07008752{
Lyude87660502016-08-17 15:55:53 -04008753 int status;
8754
Jesse Barnes4fc688c2012-11-02 11:14:01 -07008755 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07008756
Chris Wilson3f5582d2016-06-30 15:32:45 +01008757 /* GEN6_PCODE_* are outside of the forcewake domain, we can
8758 * use te fw I915_READ variants to reduce the amount of work
8759 * required when reading/writing.
8760 */
8761
8762 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008763 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
8764 return -EAGAIN;
8765 }
8766
Chris Wilson3f5582d2016-06-30 15:32:45 +01008767 I915_WRITE_FW(GEN6_PCODE_DATA, *val);
8768 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
8769 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07008770
Chris Wilsone09a3032017-04-11 11:13:39 +01008771 if (__intel_wait_for_register_fw(dev_priv,
8772 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
8773 500, 0, NULL)) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008774 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
8775 return -ETIMEDOUT;
8776 }
8777
Chris Wilson3f5582d2016-06-30 15:32:45 +01008778 *val = I915_READ_FW(GEN6_PCODE_DATA);
8779 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07008780
Lyude87660502016-08-17 15:55:53 -04008781 if (INTEL_GEN(dev_priv) > 6)
8782 status = gen7_check_mailbox_status(dev_priv);
8783 else
8784 status = gen6_check_mailbox_status(dev_priv);
8785
8786 if (status) {
8787 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed: %d\n",
8788 status);
8789 return status;
8790 }
8791
Ben Widawsky42c05262012-09-26 10:34:00 -07008792 return 0;
8793}
8794
Chris Wilson3f5582d2016-06-30 15:32:45 +01008795int sandybridge_pcode_write(struct drm_i915_private *dev_priv,
Lyude87660502016-08-17 15:55:53 -04008796 u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07008797{
Lyude87660502016-08-17 15:55:53 -04008798 int status;
8799
Jesse Barnes4fc688c2012-11-02 11:14:01 -07008800 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07008801
Chris Wilson3f5582d2016-06-30 15:32:45 +01008802 /* GEN6_PCODE_* are outside of the forcewake domain, we can
8803 * use te fw I915_READ variants to reduce the amount of work
8804 * required when reading/writing.
8805 */
8806
8807 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008808 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
8809 return -EAGAIN;
8810 }
8811
Chris Wilson3f5582d2016-06-30 15:32:45 +01008812 I915_WRITE_FW(GEN6_PCODE_DATA, val);
Imre Deak8bf41b72016-11-28 17:29:27 +02008813 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
Chris Wilson3f5582d2016-06-30 15:32:45 +01008814 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07008815
Chris Wilsone09a3032017-04-11 11:13:39 +01008816 if (__intel_wait_for_register_fw(dev_priv,
8817 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
8818 500, 0, NULL)) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008819 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
8820 return -ETIMEDOUT;
8821 }
8822
Chris Wilson3f5582d2016-06-30 15:32:45 +01008823 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07008824
Lyude87660502016-08-17 15:55:53 -04008825 if (INTEL_GEN(dev_priv) > 6)
8826 status = gen7_check_mailbox_status(dev_priv);
8827 else
8828 status = gen6_check_mailbox_status(dev_priv);
8829
8830 if (status) {
8831 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed: %d\n",
8832 status);
8833 return status;
8834 }
8835
Ben Widawsky42c05262012-09-26 10:34:00 -07008836 return 0;
8837}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07008838
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008839static bool skl_pcode_try_request(struct drm_i915_private *dev_priv, u32 mbox,
8840 u32 request, u32 reply_mask, u32 reply,
8841 u32 *status)
8842{
8843 u32 val = request;
8844
8845 *status = sandybridge_pcode_read(dev_priv, mbox, &val);
8846
8847 return *status || ((val & reply_mask) == reply);
8848}
8849
8850/**
8851 * skl_pcode_request - send PCODE request until acknowledgment
8852 * @dev_priv: device private
8853 * @mbox: PCODE mailbox ID the request is targeted for
8854 * @request: request ID
8855 * @reply_mask: mask used to check for request acknowledgment
8856 * @reply: value used to check for request acknowledgment
8857 * @timeout_base_ms: timeout for polling with preemption enabled
8858 *
8859 * Keep resending the @request to @mbox until PCODE acknowledges it, PCODE
Imre Deak01299362017-02-24 16:32:10 +02008860 * reports an error or an overall timeout of @timeout_base_ms+50 ms expires.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008861 * The request is acknowledged once the PCODE reply dword equals @reply after
8862 * applying @reply_mask. Polling is first attempted with preemption enabled
Imre Deak01299362017-02-24 16:32:10 +02008863 * for @timeout_base_ms and if this times out for another 50 ms with
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008864 * preemption disabled.
8865 *
8866 * Returns 0 on success, %-ETIMEDOUT in case of a timeout, <0 in case of some
8867 * other error as reported by PCODE.
8868 */
8869int skl_pcode_request(struct drm_i915_private *dev_priv, u32 mbox, u32 request,
8870 u32 reply_mask, u32 reply, int timeout_base_ms)
8871{
8872 u32 status;
8873 int ret;
8874
8875 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
8876
8877#define COND skl_pcode_try_request(dev_priv, mbox, request, reply_mask, reply, \
8878 &status)
8879
8880 /*
8881 * Prime the PCODE by doing a request first. Normally it guarantees
8882 * that a subsequent request, at most @timeout_base_ms later, succeeds.
8883 * _wait_for() doesn't guarantee when its passed condition is evaluated
8884 * first, so send the first request explicitly.
8885 */
8886 if (COND) {
8887 ret = 0;
8888 goto out;
8889 }
8890 ret = _wait_for(COND, timeout_base_ms * 1000, 10);
8891 if (!ret)
8892 goto out;
8893
8894 /*
8895 * The above can time out if the number of requests was low (2 in the
8896 * worst case) _and_ PCODE was busy for some reason even after a
8897 * (queued) request and @timeout_base_ms delay. As a workaround retry
8898 * the poll with preemption disabled to maximize the number of
Imre Deak01299362017-02-24 16:32:10 +02008899 * requests. Increase the timeout from @timeout_base_ms to 50ms to
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008900 * account for interrupts that could reduce the number of these
Imre Deak01299362017-02-24 16:32:10 +02008901 * requests, and for any quirks of the PCODE firmware that delays
8902 * the request completion.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008903 */
8904 DRM_DEBUG_KMS("PCODE timeout, retrying with preemption disabled\n");
8905 WARN_ON_ONCE(timeout_base_ms > 3);
8906 preempt_disable();
Imre Deak01299362017-02-24 16:32:10 +02008907 ret = wait_for_atomic(COND, 50);
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008908 preempt_enable();
8909
8910out:
8911 return ret ? ret : status;
8912#undef COND
8913}
8914
Ville Syrjälädd06f882014-11-10 22:55:12 +02008915static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
8916{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008917 /*
8918 * N = val - 0xb7
8919 * Slow = Fast = GPLL ref * N
8920 */
8921 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * (val - 0xb7), 1000);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07008922}
8923
Fengguang Wub55dd642014-07-12 11:21:39 +02008924static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07008925{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008926 return DIV_ROUND_CLOSEST(1000 * val, dev_priv->rps.gpll_ref_freq) + 0xb7;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07008927}
8928
Fengguang Wub55dd642014-07-12 11:21:39 +02008929static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05308930{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008931 /*
8932 * N = val / 2
8933 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
8934 */
8935 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * val, 2 * 2 * 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05308936}
8937
Fengguang Wub55dd642014-07-12 11:21:39 +02008938static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05308939{
Ville Syrjälä1c147622014-08-18 14:42:43 +03008940 /* CHV needs even values */
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008941 return DIV_ROUND_CLOSEST(2 * 1000 * val, dev_priv->rps.gpll_ref_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05308942}
8943
Ville Syrjälä616bc822015-01-23 21:04:25 +02008944int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
8945{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008946 if (IS_GEN9(dev_priv))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02008947 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
8948 GEN9_FREQ_SCALER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008949 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008950 return chv_gpu_freq(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008951 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008952 return byt_gpu_freq(dev_priv, val);
8953 else
8954 return val * GT_FREQUENCY_MULTIPLIER;
8955}
8956
Ville Syrjälä616bc822015-01-23 21:04:25 +02008957int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
8958{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008959 if (IS_GEN9(dev_priv))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02008960 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
8961 GT_FREQUENCY_MULTIPLIER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008962 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008963 return chv_freq_opcode(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008964 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008965 return byt_freq_opcode(dev_priv, val);
8966 else
Mika Kuoppala500a3d22015-11-13 19:29:41 +02008967 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
Deepak S22b1b2f2014-07-12 14:54:33 +05308968}
8969
Chris Wilson6ad790c2015-04-07 16:20:31 +01008970struct request_boost {
8971 struct work_struct work;
Daniel Vettereed29a52015-05-21 14:21:25 +02008972 struct drm_i915_gem_request *req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01008973};
8974
8975static void __intel_rps_boost_work(struct work_struct *work)
8976{
8977 struct request_boost *boost = container_of(work, struct request_boost, work);
Chris Wilsone61b9952015-04-27 13:41:24 +01008978 struct drm_i915_gem_request *req = boost->req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01008979
Chris Wilsonf69a02c2016-07-01 17:23:16 +01008980 if (!i915_gem_request_completed(req))
Chris Wilsonc0336662016-05-06 15:40:21 +01008981 gen6_rps_boost(req->i915, NULL, req->emitted_jiffies);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008982
Chris Wilsone8a261e2016-07-20 13:31:49 +01008983 i915_gem_request_put(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008984 kfree(boost);
8985}
8986
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01008987void intel_queue_rps_boost_for_request(struct drm_i915_gem_request *req)
Chris Wilson6ad790c2015-04-07 16:20:31 +01008988{
8989 struct request_boost *boost;
8990
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01008991 if (req == NULL || INTEL_GEN(req->i915) < 6)
Chris Wilson6ad790c2015-04-07 16:20:31 +01008992 return;
8993
Chris Wilsonf69a02c2016-07-01 17:23:16 +01008994 if (i915_gem_request_completed(req))
Chris Wilsone61b9952015-04-27 13:41:24 +01008995 return;
8996
Chris Wilson6ad790c2015-04-07 16:20:31 +01008997 boost = kmalloc(sizeof(*boost), GFP_ATOMIC);
8998 if (boost == NULL)
8999 return;
9000
Chris Wilsone8a261e2016-07-20 13:31:49 +01009001 boost->req = i915_gem_request_get(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01009002
9003 INIT_WORK(&boost->work, __intel_rps_boost_work);
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01009004 queue_work(req->i915->wq, &boost->work);
Chris Wilson6ad790c2015-04-07 16:20:31 +01009005}
9006
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00009007void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01009008{
Daniel Vetterf742a552013-12-06 10:17:53 +01009009 mutex_init(&dev_priv->rps.hw_lock);
Chris Wilson8d3afd72015-05-21 21:01:47 +01009010 spin_lock_init(&dev_priv->rps.client_lock);
Daniel Vetterf742a552013-12-06 10:17:53 +01009011
Chris Wilson54b4f682016-07-21 21:16:19 +01009012 INIT_DELAYED_WORK(&dev_priv->rps.autoenable_work,
9013 __intel_autoenable_gt_powersave);
Chris Wilson1854d5c2015-04-07 16:20:32 +01009014 INIT_LIST_HEAD(&dev_priv->rps.clients);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03009015
Paulo Zanoni33688d92014-03-07 20:08:19 -03009016 dev_priv->pm.suspended = false;
Imre Deak1f814da2015-12-16 02:52:19 +02009017 atomic_set(&dev_priv->pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01009018}
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009019
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009020static u64 vlv_residency_raw(struct drm_i915_private *dev_priv,
9021 const i915_reg_t reg)
9022{
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009023 u32 lower, upper, tmp;
Chris Wilson71cc2b12017-03-24 16:54:18 +00009024 int loop = 2;
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009025
9026 /* The register accessed do not need forcewake. We borrow
9027 * uncore lock to prevent concurrent access to range reg.
9028 */
9029 spin_lock_irq(&dev_priv->uncore.lock);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009030
9031 /* vlv and chv residency counters are 40 bits in width.
9032 * With a control bit, we can choose between upper or lower
9033 * 32bit window into this counter.
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009034 *
9035 * Although we always use the counter in high-range mode elsewhere,
9036 * userspace may attempt to read the value before rc6 is initialised,
9037 * before we have set the default VLV_COUNTER_CONTROL value. So always
9038 * set the high bit to be safe.
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009039 */
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009040 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9041 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009042 upper = I915_READ_FW(reg);
9043 do {
9044 tmp = upper;
9045
9046 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9047 _MASKED_BIT_DISABLE(VLV_COUNT_RANGE_HIGH));
9048 lower = I915_READ_FW(reg);
9049
9050 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9051 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
9052 upper = I915_READ_FW(reg);
Chris Wilson71cc2b12017-03-24 16:54:18 +00009053 } while (upper != tmp && --loop);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009054
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009055 /* Everywhere else we always use VLV_COUNTER_CONTROL with the
9056 * VLV_COUNT_RANGE_HIGH bit set - so it is safe to leave it set
9057 * now.
9058 */
9059
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009060 spin_unlock_irq(&dev_priv->uncore.lock);
9061
9062 return lower | (u64)upper << 8;
9063}
9064
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009065u64 intel_rc6_residency_us(struct drm_i915_private *dev_priv,
9066 const i915_reg_t reg)
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009067{
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009068 u64 time_hw, units, div;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009069
9070 if (!intel_enable_rc6())
9071 return 0;
9072
9073 intel_runtime_pm_get(dev_priv);
9074
9075 /* On VLV and CHV, residency time is in CZ units rather than 1.28us */
9076 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009077 units = 1000;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009078 div = dev_priv->czclk_freq;
9079
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009080 time_hw = vlv_residency_raw(dev_priv, reg);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009081 } else if (IS_GEN9_LP(dev_priv)) {
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009082 units = 1000;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009083 div = 1200; /* 833.33ns */
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009084
9085 time_hw = I915_READ(reg);
9086 } else {
9087 units = 128000; /* 1.28us */
9088 div = 100000;
9089
9090 time_hw = I915_READ(reg);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009091 }
9092
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009093 intel_runtime_pm_put(dev_priv);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009094 return DIV_ROUND_UP_ULL(time_hw * units, div);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009095}