blob: 9b0a6a4572cefc60e2aa00ecaf8d4c0763debada [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;
1023
1024 dev_priv->wm.max_level = G4X_WM_LEVEL_SR;
1025}
1026
1027static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
1028{
1029 /*
1030 * DSPCNTR[13] supposedly controls whether the
1031 * primary plane can use the FIFO space otherwise
1032 * reserved for the sprite plane. It's not 100% clear
1033 * what the actual FIFO size is, but it looks like we
1034 * can happily set both primary and sprite watermarks
1035 * up to 127 cachelines. So that would seem to mean
1036 * that either DSPCNTR[13] doesn't do anything, or that
1037 * the total FIFO is >= 256 cachelines in size. Either
1038 * way, we don't seem to have to worry about this
1039 * repartitioning as the maximum watermark value the
1040 * register can hold for each plane is lower than the
1041 * minimum FIFO size.
1042 */
1043 switch (plane_id) {
1044 case PLANE_CURSOR:
1045 return 63;
1046 case PLANE_PRIMARY:
1047 return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
1048 case PLANE_SPRITE0:
1049 return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
1050 default:
1051 MISSING_CASE(plane_id);
1052 return 0;
1053 }
1054}
1055
1056static int g4x_fbc_fifo_size(int level)
1057{
1058 switch (level) {
1059 case G4X_WM_LEVEL_SR:
1060 return 7;
1061 case G4X_WM_LEVEL_HPLL:
1062 return 15;
1063 default:
1064 MISSING_CASE(level);
1065 return 0;
1066 }
1067}
1068
1069static uint16_t g4x_compute_wm(const struct intel_crtc_state *crtc_state,
1070 const struct intel_plane_state *plane_state,
1071 int level)
1072{
1073 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1074 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1075 const struct drm_display_mode *adjusted_mode =
1076 &crtc_state->base.adjusted_mode;
1077 int clock, htotal, cpp, width, wm;
1078 int latency = dev_priv->wm.pri_latency[level] * 10;
1079
1080 if (latency == 0)
1081 return USHRT_MAX;
1082
1083 if (!intel_wm_plane_visible(crtc_state, plane_state))
1084 return 0;
1085
1086 /*
1087 * Not 100% sure which way ELK should go here as the
1088 * spec only says CL/CTG should assume 32bpp and BW
1089 * doesn't need to. But as these things followed the
1090 * mobile vs. desktop lines on gen3 as well, let's
1091 * assume ELK doesn't need this.
1092 *
1093 * The spec also fails to list such a restriction for
1094 * the HPLL watermark, which seems a little strange.
1095 * Let's use 32bpp for the HPLL watermark as well.
1096 */
1097 if (IS_GM45(dev_priv) && plane->id == PLANE_PRIMARY &&
1098 level != G4X_WM_LEVEL_NORMAL)
1099 cpp = 4;
1100 else
1101 cpp = plane_state->base.fb->format->cpp[0];
1102
1103 clock = adjusted_mode->crtc_clock;
1104 htotal = adjusted_mode->crtc_htotal;
1105
1106 if (plane->id == PLANE_CURSOR)
1107 width = plane_state->base.crtc_w;
1108 else
1109 width = drm_rect_width(&plane_state->base.dst);
1110
1111 if (plane->id == PLANE_CURSOR) {
1112 wm = intel_wm_method2(clock, htotal, width, cpp, latency);
1113 } else if (plane->id == PLANE_PRIMARY &&
1114 level == G4X_WM_LEVEL_NORMAL) {
1115 wm = intel_wm_method1(clock, cpp, latency);
1116 } else {
1117 int small, large;
1118
1119 small = intel_wm_method1(clock, cpp, latency);
1120 large = intel_wm_method2(clock, htotal, width, cpp, latency);
1121
1122 wm = min(small, large);
1123 }
1124
1125 wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
1126 width, cpp);
1127
1128 wm = DIV_ROUND_UP(wm, 64) + 2;
1129
1130 return min_t(int, wm, USHRT_MAX);
1131}
1132
1133static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1134 int level, enum plane_id plane_id, u16 value)
1135{
1136 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1137 bool dirty = false;
1138
1139 for (; level < intel_wm_num_levels(dev_priv); level++) {
1140 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1141
1142 dirty |= raw->plane[plane_id] != value;
1143 raw->plane[plane_id] = value;
1144 }
1145
1146 return dirty;
1147}
1148
1149static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
1150 int level, u16 value)
1151{
1152 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1153 bool dirty = false;
1154
1155 /* NORMAL level doesn't have an FBC watermark */
1156 level = max(level, G4X_WM_LEVEL_SR);
1157
1158 for (; level < intel_wm_num_levels(dev_priv); level++) {
1159 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1160
1161 dirty |= raw->fbc != value;
1162 raw->fbc = value;
1163 }
1164
1165 return dirty;
1166}
1167
1168static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
1169 const struct intel_plane_state *pstate,
1170 uint32_t pri_val);
1171
1172static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1173 const struct intel_plane_state *plane_state)
1174{
1175 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1176 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
1177 enum plane_id plane_id = plane->id;
1178 bool dirty = false;
1179 int level;
1180
1181 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1182 dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1183 if (plane_id == PLANE_PRIMARY)
1184 dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
1185 goto out;
1186 }
1187
1188 for (level = 0; level < num_levels; level++) {
1189 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1190 int wm, max_wm;
1191
1192 wm = g4x_compute_wm(crtc_state, plane_state, level);
1193 max_wm = g4x_plane_fifo_size(plane_id, level);
1194
1195 if (wm > max_wm)
1196 break;
1197
1198 dirty |= raw->plane[plane_id] != wm;
1199 raw->plane[plane_id] = wm;
1200
1201 if (plane_id != PLANE_PRIMARY ||
1202 level == G4X_WM_LEVEL_NORMAL)
1203 continue;
1204
1205 wm = ilk_compute_fbc_wm(crtc_state, plane_state,
1206 raw->plane[plane_id]);
1207 max_wm = g4x_fbc_fifo_size(level);
1208
1209 /*
1210 * FBC wm is not mandatory as we
1211 * can always just disable its use.
1212 */
1213 if (wm > max_wm)
1214 wm = USHRT_MAX;
1215
1216 dirty |= raw->fbc != wm;
1217 raw->fbc = wm;
1218 }
1219
1220 /* mark watermarks as invalid */
1221 dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1222
1223 if (plane_id == PLANE_PRIMARY)
1224 dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
1225
1226 out:
1227 if (dirty) {
1228 DRM_DEBUG_KMS("%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
1229 plane->base.name,
1230 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
1231 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
1232 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
1233
1234 if (plane_id == PLANE_PRIMARY)
1235 DRM_DEBUG_KMS("FBC watermarks: SR=%d, HPLL=%d\n",
1236 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
1237 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
1238 }
1239
1240 return dirty;
1241}
1242
1243static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1244 enum plane_id plane_id, int level)
1245{
1246 const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1247
1248 return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
1249}
1250
1251static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
1252 int level)
1253{
1254 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1255
1256 if (level > dev_priv->wm.max_level)
1257 return false;
1258
1259 return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1260 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1261 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1262}
1263
1264/* mark all levels starting from 'level' as invalid */
1265static void g4x_invalidate_wms(struct intel_crtc *crtc,
1266 struct g4x_wm_state *wm_state, int level)
1267{
1268 if (level <= G4X_WM_LEVEL_NORMAL) {
1269 enum plane_id plane_id;
1270
1271 for_each_plane_id_on_crtc(crtc, plane_id)
1272 wm_state->wm.plane[plane_id] = USHRT_MAX;
1273 }
1274
1275 if (level <= G4X_WM_LEVEL_SR) {
1276 wm_state->cxsr = false;
1277 wm_state->sr.cursor = USHRT_MAX;
1278 wm_state->sr.plane = USHRT_MAX;
1279 wm_state->sr.fbc = USHRT_MAX;
1280 }
1281
1282 if (level <= G4X_WM_LEVEL_HPLL) {
1283 wm_state->hpll_en = false;
1284 wm_state->hpll.cursor = USHRT_MAX;
1285 wm_state->hpll.plane = USHRT_MAX;
1286 wm_state->hpll.fbc = USHRT_MAX;
1287 }
1288}
1289
1290static int g4x_compute_pipe_wm(struct intel_crtc_state *crtc_state)
1291{
1292 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1293 struct intel_atomic_state *state =
1294 to_intel_atomic_state(crtc_state->base.state);
1295 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
1296 int num_active_planes = hweight32(crtc_state->active_planes &
1297 ~BIT(PLANE_CURSOR));
1298 const struct g4x_pipe_wm *raw;
1299 struct intel_plane_state *plane_state;
1300 struct intel_plane *plane;
1301 enum plane_id plane_id;
1302 int i, level;
1303 unsigned int dirty = 0;
1304
1305 for_each_intel_plane_in_state(state, plane, plane_state, i) {
1306 const struct intel_plane_state *old_plane_state =
1307 to_intel_plane_state(plane->base.state);
1308
1309 if (plane_state->base.crtc != &crtc->base &&
1310 old_plane_state->base.crtc != &crtc->base)
1311 continue;
1312
1313 if (g4x_raw_plane_wm_compute(crtc_state, plane_state))
1314 dirty |= BIT(plane->id);
1315 }
1316
1317 if (!dirty)
1318 return 0;
1319
1320 level = G4X_WM_LEVEL_NORMAL;
1321 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1322 goto out;
1323
1324 raw = &crtc_state->wm.g4x.raw[level];
1325 for_each_plane_id_on_crtc(crtc, plane_id)
1326 wm_state->wm.plane[plane_id] = raw->plane[plane_id];
1327
1328 level = G4X_WM_LEVEL_SR;
1329
1330 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1331 goto out;
1332
1333 raw = &crtc_state->wm.g4x.raw[level];
1334 wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
1335 wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
1336 wm_state->sr.fbc = raw->fbc;
1337
1338 wm_state->cxsr = num_active_planes == BIT(PLANE_PRIMARY);
1339
1340 level = G4X_WM_LEVEL_HPLL;
1341
1342 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1343 goto out;
1344
1345 raw = &crtc_state->wm.g4x.raw[level];
1346 wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
1347 wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
1348 wm_state->hpll.fbc = raw->fbc;
1349
1350 wm_state->hpll_en = wm_state->cxsr;
1351
1352 level++;
1353
1354 out:
1355 if (level == G4X_WM_LEVEL_NORMAL)
1356 return -EINVAL;
1357
1358 /* invalidate the higher levels */
1359 g4x_invalidate_wms(crtc, wm_state, level);
1360
1361 /*
1362 * Determine if the FBC watermark(s) can be used. IF
1363 * this isn't the case we prefer to disable the FBC
1364 ( watermark(s) rather than disable the SR/HPLL
1365 * level(s) entirely.
1366 */
1367 wm_state->fbc_en = level > G4X_WM_LEVEL_NORMAL;
1368
1369 if (level >= G4X_WM_LEVEL_SR &&
1370 wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
1371 wm_state->fbc_en = false;
1372 else if (level >= G4X_WM_LEVEL_HPLL &&
1373 wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
1374 wm_state->fbc_en = false;
1375
1376 return 0;
1377}
1378
1379static int g4x_compute_intermediate_wm(struct drm_device *dev,
1380 struct intel_crtc *crtc,
1381 struct intel_crtc_state *crtc_state)
1382{
1383 struct g4x_wm_state *intermediate = &crtc_state->wm.g4x.intermediate;
1384 const struct g4x_wm_state *optimal = &crtc_state->wm.g4x.optimal;
1385 const struct g4x_wm_state *active = &crtc->wm.active.g4x;
1386 enum plane_id plane_id;
1387
1388 intermediate->cxsr = optimal->cxsr && active->cxsr &&
1389 !crtc_state->disable_cxsr;
1390 intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
1391 !crtc_state->disable_cxsr;
1392 intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
1393
1394 for_each_plane_id_on_crtc(crtc, plane_id) {
1395 intermediate->wm.plane[plane_id] =
1396 max(optimal->wm.plane[plane_id],
1397 active->wm.plane[plane_id]);
1398
1399 WARN_ON(intermediate->wm.plane[plane_id] >
1400 g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
1401 }
1402
1403 intermediate->sr.plane = max(optimal->sr.plane,
1404 active->sr.plane);
1405 intermediate->sr.cursor = max(optimal->sr.cursor,
1406 active->sr.cursor);
1407 intermediate->sr.fbc = max(optimal->sr.fbc,
1408 active->sr.fbc);
1409
1410 intermediate->hpll.plane = max(optimal->hpll.plane,
1411 active->hpll.plane);
1412 intermediate->hpll.cursor = max(optimal->hpll.cursor,
1413 active->hpll.cursor);
1414 intermediate->hpll.fbc = max(optimal->hpll.fbc,
1415 active->hpll.fbc);
1416
1417 WARN_ON((intermediate->sr.plane >
1418 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
1419 intermediate->sr.cursor >
1420 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
1421 intermediate->cxsr);
1422 WARN_ON((intermediate->sr.plane >
1423 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
1424 intermediate->sr.cursor >
1425 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
1426 intermediate->hpll_en);
1427
1428 WARN_ON(intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
1429 intermediate->fbc_en && intermediate->cxsr);
1430 WARN_ON(intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
1431 intermediate->fbc_en && intermediate->hpll_en);
1432
1433 /*
1434 * If our intermediate WM are identical to the final WM, then we can
1435 * omit the post-vblank programming; only update if it's different.
1436 */
1437 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
1438 crtc_state->wm.need_postvbl_update = true;
1439
1440 return 0;
1441}
1442
1443static void g4x_merge_wm(struct drm_i915_private *dev_priv,
1444 struct g4x_wm_values *wm)
1445{
1446 struct intel_crtc *crtc;
1447 int num_active_crtcs = 0;
1448
1449 wm->cxsr = true;
1450 wm->hpll_en = true;
1451 wm->fbc_en = true;
1452
1453 for_each_intel_crtc(&dev_priv->drm, crtc) {
1454 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1455
1456 if (!crtc->active)
1457 continue;
1458
1459 if (!wm_state->cxsr)
1460 wm->cxsr = false;
1461 if (!wm_state->hpll_en)
1462 wm->hpll_en = false;
1463 if (!wm_state->fbc_en)
1464 wm->fbc_en = false;
1465
1466 num_active_crtcs++;
1467 }
1468
1469 if (num_active_crtcs != 1) {
1470 wm->cxsr = false;
1471 wm->hpll_en = false;
1472 wm->fbc_en = false;
1473 }
1474
1475 for_each_intel_crtc(&dev_priv->drm, crtc) {
1476 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1477 enum pipe pipe = crtc->pipe;
1478
1479 wm->pipe[pipe] = wm_state->wm;
1480 if (crtc->active && wm->cxsr)
1481 wm->sr = wm_state->sr;
1482 if (crtc->active && wm->hpll_en)
1483 wm->hpll = wm_state->hpll;
1484 }
1485}
1486
1487static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
1488{
1489 struct g4x_wm_values *old_wm = &dev_priv->wm.g4x;
1490 struct g4x_wm_values new_wm = {};
1491
1492 g4x_merge_wm(dev_priv, &new_wm);
1493
1494 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
1495 return;
1496
1497 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
1498 _intel_set_memory_cxsr(dev_priv, false);
1499
1500 g4x_write_wm_values(dev_priv, &new_wm);
1501
1502 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
1503 _intel_set_memory_cxsr(dev_priv, true);
1504
1505 *old_wm = new_wm;
1506}
1507
1508static void g4x_initial_watermarks(struct intel_atomic_state *state,
1509 struct intel_crtc_state *crtc_state)
1510{
1511 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1512 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1513
1514 mutex_lock(&dev_priv->wm.wm_mutex);
1515 crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
1516 g4x_program_watermarks(dev_priv);
1517 mutex_unlock(&dev_priv->wm.wm_mutex);
1518}
1519
1520static void g4x_optimize_watermarks(struct intel_atomic_state *state,
1521 struct intel_crtc_state *crtc_state)
1522{
1523 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1524 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
1525
1526 if (!crtc_state->wm.need_postvbl_update)
1527 return;
1528
1529 mutex_lock(&dev_priv->wm.wm_mutex);
1530 intel_crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
1531 g4x_program_watermarks(dev_priv);
1532 mutex_unlock(&dev_priv->wm.wm_mutex);
1533}
1534
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001535/* latency must be in 0.1us units. */
1536static unsigned int vlv_wm_method2(unsigned int pixel_rate,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001537 unsigned int htotal,
1538 unsigned int width,
Ville Syrjäläac484962016-01-20 21:05:26 +02001539 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001540 unsigned int latency)
1541{
1542 unsigned int ret;
1543
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001544 ret = intel_wm_method2(pixel_rate, htotal,
1545 width, cpp, latency);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001546 ret = DIV_ROUND_UP(ret, 64);
1547
1548 return ret;
1549}
1550
Ville Syrjäläbb726512016-10-31 22:37:24 +02001551static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001552{
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001553 /* all latencies in usec */
1554 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1555
Ville Syrjälä58590c12015-09-08 21:05:12 +03001556 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
1557
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001558 if (IS_CHERRYVIEW(dev_priv)) {
1559 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1560 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001561
1562 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001563 }
1564}
1565
Ville Syrjäläe339d672016-11-28 19:37:17 +02001566static uint16_t vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1567 const struct intel_plane_state *plane_state,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001568 int level)
1569{
Ville Syrjäläe339d672016-11-28 19:37:17 +02001570 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001571 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläe339d672016-11-28 19:37:17 +02001572 const struct drm_display_mode *adjusted_mode =
1573 &crtc_state->base.adjusted_mode;
Ville Syrjäläac484962016-01-20 21:05:26 +02001574 int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001575
1576 if (dev_priv->wm.pri_latency[level] == 0)
1577 return USHRT_MAX;
1578
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001579 if (!intel_wm_plane_visible(crtc_state, plane_state))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001580 return 0;
1581
Daniel Vetteref426c12017-01-04 11:41:10 +01001582 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjäläe339d672016-11-28 19:37:17 +02001583 clock = adjusted_mode->crtc_clock;
1584 htotal = adjusted_mode->crtc_htotal;
1585 width = crtc_state->pipe_src_w;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001586
Ville Syrjälä709f3fc2017-03-03 17:19:26 +02001587 if (plane->id == PLANE_CURSOR) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001588 /*
1589 * FIXME the formula gives values that are
1590 * too big for the cursor FIFO, and hence we
1591 * would never be able to use cursors. For
1592 * now just hardcode the watermark.
1593 */
1594 wm = 63;
1595 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +02001596 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001597 dev_priv->wm.pri_latency[level] * 10);
1598 }
1599
1600 return min_t(int, wm, USHRT_MAX);
1601}
1602
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001603static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1604{
1605 return (active_planes & (BIT(PLANE_SPRITE0) |
1606 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1607}
1608
Ville Syrjälä5012e602017-03-02 19:14:56 +02001609static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001610{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001611 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001612 const struct g4x_pipe_wm *raw =
Ville Syrjälä5012e602017-03-02 19:14:56 +02001613 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001614 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001615 unsigned int active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1616 int num_active_planes = hweight32(active_planes);
1617 const int fifo_size = 511;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001618 int fifo_extra, fifo_left = fifo_size;
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001619 int sprite0_fifo_extra = 0;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001620 unsigned int total_rate;
1621 enum plane_id plane_id;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001622
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001623 /*
1624 * When enabling sprite0 after sprite1 has already been enabled
1625 * we tend to get an underrun unless sprite0 already has some
1626 * FIFO space allcoated. Hence we always allocate at least one
1627 * cacheline for sprite0 whenever sprite1 is enabled.
1628 *
1629 * All other plane enable sequences appear immune to this problem.
1630 */
1631 if (vlv_need_sprite0_fifo_workaround(active_planes))
1632 sprite0_fifo_extra = 1;
1633
Ville Syrjälä5012e602017-03-02 19:14:56 +02001634 total_rate = raw->plane[PLANE_PRIMARY] +
1635 raw->plane[PLANE_SPRITE0] +
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001636 raw->plane[PLANE_SPRITE1] +
1637 sprite0_fifo_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001638
Ville Syrjälä5012e602017-03-02 19:14:56 +02001639 if (total_rate > fifo_size)
1640 return -EINVAL;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001641
Ville Syrjälä5012e602017-03-02 19:14:56 +02001642 if (total_rate == 0)
1643 total_rate = 1;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001644
Ville Syrjälä5012e602017-03-02 19:14:56 +02001645 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001646 unsigned int rate;
1647
Ville Syrjälä5012e602017-03-02 19:14:56 +02001648 if ((active_planes & BIT(plane_id)) == 0) {
1649 fifo_state->plane[plane_id] = 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001650 continue;
1651 }
1652
Ville Syrjälä5012e602017-03-02 19:14:56 +02001653 rate = raw->plane[plane_id];
1654 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1655 fifo_left -= fifo_state->plane[plane_id];
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001656 }
1657
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001658 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1659 fifo_left -= sprite0_fifo_extra;
1660
Ville Syrjälä5012e602017-03-02 19:14:56 +02001661 fifo_state->plane[PLANE_CURSOR] = 63;
1662
1663 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001664
1665 /* spread the remainder evenly */
Ville Syrjälä5012e602017-03-02 19:14:56 +02001666 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001667 int plane_extra;
1668
1669 if (fifo_left == 0)
1670 break;
1671
Ville Syrjälä5012e602017-03-02 19:14:56 +02001672 if ((active_planes & BIT(plane_id)) == 0)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001673 continue;
1674
1675 plane_extra = min(fifo_extra, fifo_left);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001676 fifo_state->plane[plane_id] += plane_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001677 fifo_left -= plane_extra;
1678 }
1679
Ville Syrjälä5012e602017-03-02 19:14:56 +02001680 WARN_ON(active_planes != 0 && fifo_left != 0);
1681
1682 /* give it all to the first plane if none are active */
1683 if (active_planes == 0) {
1684 WARN_ON(fifo_left != fifo_size);
1685 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1686 }
1687
1688 return 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001689}
1690
Ville Syrjäläff32c542017-03-02 19:14:57 +02001691/* mark all levels starting from 'level' as invalid */
1692static void vlv_invalidate_wms(struct intel_crtc *crtc,
1693 struct vlv_wm_state *wm_state, int level)
1694{
1695 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1696
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001697 for (; level < intel_wm_num_levels(dev_priv); level++) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02001698 enum plane_id plane_id;
1699
1700 for_each_plane_id_on_crtc(crtc, plane_id)
1701 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1702
1703 wm_state->sr[level].cursor = USHRT_MAX;
1704 wm_state->sr[level].plane = USHRT_MAX;
1705 }
1706}
1707
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001708static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1709{
1710 if (wm > fifo_size)
1711 return USHRT_MAX;
1712 else
1713 return fifo_size - wm;
1714}
1715
Ville Syrjäläff32c542017-03-02 19:14:57 +02001716/*
1717 * Starting from 'level' set all higher
1718 * levels to 'value' in the "raw" watermarks.
1719 */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001720static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001721 int level, enum plane_id plane_id, u16 value)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001722{
Ville Syrjäläff32c542017-03-02 19:14:57 +02001723 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001724 int num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001725 bool dirty = false;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001726
Ville Syrjäläff32c542017-03-02 19:14:57 +02001727 for (; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001728 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001729
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001730 dirty |= raw->plane[plane_id] != value;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001731 raw->plane[plane_id] = value;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001732 }
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001733
1734 return dirty;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001735}
1736
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001737static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1738 const struct intel_plane_state *plane_state)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001739{
1740 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1741 enum plane_id plane_id = plane->id;
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001742 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
Ville Syrjäläff32c542017-03-02 19:14:57 +02001743 int level;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001744 bool dirty = false;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001745
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001746 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001747 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1748 goto out;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001749 }
1750
1751 for (level = 0; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001752 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001753 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1754 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1755
Ville Syrjäläff32c542017-03-02 19:14:57 +02001756 if (wm > max_wm)
1757 break;
1758
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001759 dirty |= raw->plane[plane_id] != wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001760 raw->plane[plane_id] = wm;
1761 }
1762
1763 /* mark all higher levels as invalid */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001764 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001765
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001766out:
1767 if (dirty)
Ville Syrjälä57a65282017-04-21 21:14:22 +03001768 DRM_DEBUG_KMS("%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001769 plane->base.name,
1770 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1771 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1772 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
1773
1774 return dirty;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001775}
1776
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001777static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1778 enum plane_id plane_id, int level)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001779{
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001780 const struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02001781 &crtc_state->wm.vlv.raw[level];
1782 const struct vlv_fifo_state *fifo_state =
1783 &crtc_state->wm.vlv.fifo_state;
1784
1785 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1786}
1787
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001788static 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 +02001789{
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001790 return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1791 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1792 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1793 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001794}
1795
1796static int vlv_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001797{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001798 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001799 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001800 struct intel_atomic_state *state =
1801 to_intel_atomic_state(crtc_state->base.state);
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001802 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001803 const struct vlv_fifo_state *fifo_state =
1804 &crtc_state->wm.vlv.fifo_state;
1805 int num_active_planes = hweight32(crtc_state->active_planes &
1806 ~BIT(PLANE_CURSOR));
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001807 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->base);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001808 struct intel_plane_state *plane_state;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001809 struct intel_plane *plane;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001810 enum plane_id plane_id;
1811 int level, ret, i;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001812 unsigned int dirty = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001813
Ville Syrjäläff32c542017-03-02 19:14:57 +02001814 for_each_intel_plane_in_state(state, plane, plane_state, i) {
1815 const struct intel_plane_state *old_plane_state =
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001816 to_intel_plane_state(plane->base.state);
1817
Ville Syrjäläff32c542017-03-02 19:14:57 +02001818 if (plane_state->base.crtc != &crtc->base &&
1819 old_plane_state->base.crtc != &crtc->base)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001820 continue;
1821
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001822 if (vlv_raw_plane_wm_compute(crtc_state, plane_state))
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001823 dirty |= BIT(plane->id);
1824 }
1825
1826 /*
1827 * DSPARB registers may have been reset due to the
1828 * power well being turned off. Make sure we restore
1829 * them to a consistent state even if no primary/sprite
1830 * planes are initially active.
1831 */
1832 if (needs_modeset)
1833 crtc_state->fifo_changed = true;
1834
1835 if (!dirty)
1836 return 0;
1837
1838 /* cursor changes don't warrant a FIFO recompute */
1839 if (dirty & ~BIT(PLANE_CURSOR)) {
1840 const struct intel_crtc_state *old_crtc_state =
1841 to_intel_crtc_state(crtc->base.state);
1842 const struct vlv_fifo_state *old_fifo_state =
1843 &old_crtc_state->wm.vlv.fifo_state;
1844
1845 ret = vlv_compute_fifo(crtc_state);
1846 if (ret)
1847 return ret;
1848
1849 if (needs_modeset ||
1850 memcmp(old_fifo_state, fifo_state,
1851 sizeof(*fifo_state)) != 0)
1852 crtc_state->fifo_changed = true;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001853 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001854
Ville Syrjäläff32c542017-03-02 19:14:57 +02001855 /* initially allow all levels */
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001856 wm_state->num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001857 /*
1858 * Note that enabling cxsr with no primary/sprite planes
1859 * enabled can wedge the pipe. Hence we only allow cxsr
1860 * with exactly one enabled primary/sprite plane.
1861 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001862 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001863
Ville Syrjälä5012e602017-03-02 19:14:56 +02001864 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001865 const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001866 const int sr_fifo_size = INTEL_INFO(dev_priv)->num_pipes * 512 - 1;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001867
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001868 if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
Ville Syrjäläff32c542017-03-02 19:14:57 +02001869 break;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001870
Ville Syrjäläff32c542017-03-02 19:14:57 +02001871 for_each_plane_id_on_crtc(crtc, plane_id) {
1872 wm_state->wm[level].plane[plane_id] =
1873 vlv_invert_wm_value(raw->plane[plane_id],
1874 fifo_state->plane[plane_id]);
1875 }
1876
1877 wm_state->sr[level].plane =
1878 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
Ville Syrjälä5012e602017-03-02 19:14:56 +02001879 raw->plane[PLANE_SPRITE0],
Ville Syrjäläff32c542017-03-02 19:14:57 +02001880 raw->plane[PLANE_SPRITE1]),
1881 sr_fifo_size);
1882
1883 wm_state->sr[level].cursor =
1884 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1885 63);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001886 }
1887
Ville Syrjäläff32c542017-03-02 19:14:57 +02001888 if (level == 0)
1889 return -EINVAL;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001890
Ville Syrjäläff32c542017-03-02 19:14:57 +02001891 /* limit to only levels we can actually handle */
1892 wm_state->num_levels = level;
1893
1894 /* invalidate the higher levels */
1895 vlv_invalidate_wms(crtc, wm_state, level);
1896
1897 return 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001898}
1899
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001900#define VLV_FIFO(plane, value) \
1901 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1902
Ville Syrjäläff32c542017-03-02 19:14:57 +02001903static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
1904 struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001905{
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001906 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001907 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001908 const struct vlv_fifo_state *fifo_state =
1909 &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001910 int sprite0_start, sprite1_start, fifo_size;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001911
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001912 if (!crtc_state->fifo_changed)
1913 return;
1914
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001915 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
1916 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
1917 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001918
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001919 WARN_ON(fifo_state->plane[PLANE_CURSOR] != 63);
1920 WARN_ON(fifo_size != 511);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001921
Ville Syrjäläc137d662017-03-02 19:15:06 +02001922 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
1923
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001924 /*
1925 * uncore.lock serves a double purpose here. It allows us to
1926 * use the less expensive I915_{READ,WRITE}_FW() functions, and
1927 * it protects the DSPARB registers from getting clobbered by
1928 * parallel updates from multiple pipes.
1929 *
1930 * intel_pipe_update_start() has already disabled interrupts
1931 * for us, so a plain spin_lock() is sufficient here.
1932 */
1933 spin_lock(&dev_priv->uncore.lock);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001934
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001935 switch (crtc->pipe) {
1936 uint32_t dsparb, dsparb2, dsparb3;
1937 case PIPE_A:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001938 dsparb = I915_READ_FW(DSPARB);
1939 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001940
1941 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1942 VLV_FIFO(SPRITEB, 0xff));
1943 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1944 VLV_FIFO(SPRITEB, sprite1_start));
1945
1946 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1947 VLV_FIFO(SPRITEB_HI, 0x1));
1948 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1949 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1950
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001951 I915_WRITE_FW(DSPARB, dsparb);
1952 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001953 break;
1954 case PIPE_B:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001955 dsparb = I915_READ_FW(DSPARB);
1956 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001957
1958 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1959 VLV_FIFO(SPRITED, 0xff));
1960 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1961 VLV_FIFO(SPRITED, sprite1_start));
1962
1963 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
1964 VLV_FIFO(SPRITED_HI, 0xff));
1965 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
1966 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
1967
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001968 I915_WRITE_FW(DSPARB, dsparb);
1969 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001970 break;
1971 case PIPE_C:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001972 dsparb3 = I915_READ_FW(DSPARB3);
1973 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001974
1975 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
1976 VLV_FIFO(SPRITEF, 0xff));
1977 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
1978 VLV_FIFO(SPRITEF, sprite1_start));
1979
1980 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
1981 VLV_FIFO(SPRITEF_HI, 0xff));
1982 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
1983 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
1984
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001985 I915_WRITE_FW(DSPARB3, dsparb3);
1986 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001987 break;
1988 default:
1989 break;
1990 }
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001991
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001992 POSTING_READ_FW(DSPARB);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001993
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001994 spin_unlock(&dev_priv->uncore.lock);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001995}
1996
1997#undef VLV_FIFO
1998
Ville Syrjälä4841da52017-03-02 19:14:59 +02001999static int vlv_compute_intermediate_wm(struct drm_device *dev,
2000 struct intel_crtc *crtc,
2001 struct intel_crtc_state *crtc_state)
2002{
2003 struct vlv_wm_state *intermediate = &crtc_state->wm.vlv.intermediate;
2004 const struct vlv_wm_state *optimal = &crtc_state->wm.vlv.optimal;
2005 const struct vlv_wm_state *active = &crtc->wm.active.vlv;
2006 int level;
2007
2008 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002009 intermediate->cxsr = optimal->cxsr && active->cxsr &&
2010 !crtc_state->disable_cxsr;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002011
2012 for (level = 0; level < intermediate->num_levels; level++) {
2013 enum plane_id plane_id;
2014
2015 for_each_plane_id_on_crtc(crtc, plane_id) {
2016 intermediate->wm[level].plane[plane_id] =
2017 min(optimal->wm[level].plane[plane_id],
2018 active->wm[level].plane[plane_id]);
2019 }
2020
2021 intermediate->sr[level].plane = min(optimal->sr[level].plane,
2022 active->sr[level].plane);
2023 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
2024 active->sr[level].cursor);
2025 }
2026
2027 vlv_invalidate_wms(crtc, intermediate, level);
2028
2029 /*
2030 * If our intermediate WM are identical to the final WM, then we can
2031 * omit the post-vblank programming; only update if it's different.
2032 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002033 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
2034 crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002035
2036 return 0;
2037}
2038
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002039static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002040 struct vlv_wm_values *wm)
2041{
2042 struct intel_crtc *crtc;
2043 int num_active_crtcs = 0;
2044
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002045 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002046 wm->cxsr = true;
2047
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002048 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002049 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002050
2051 if (!crtc->active)
2052 continue;
2053
2054 if (!wm_state->cxsr)
2055 wm->cxsr = false;
2056
2057 num_active_crtcs++;
2058 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
2059 }
2060
2061 if (num_active_crtcs != 1)
2062 wm->cxsr = false;
2063
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03002064 if (num_active_crtcs > 1)
2065 wm->level = VLV_WM_LEVEL_PM2;
2066
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002067 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002068 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002069 enum pipe pipe = crtc->pipe;
2070
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002071 wm->pipe[pipe] = wm_state->wm[wm->level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02002072 if (crtc->active && wm->cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002073 wm->sr = wm_state->sr[wm->level];
2074
Ville Syrjälä1b313892016-11-28 19:37:08 +02002075 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
2076 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
2077 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
2078 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002079 }
2080}
2081
Ville Syrjäläff32c542017-03-02 19:14:57 +02002082static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002083{
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002084 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
2085 struct vlv_wm_values new_wm = {};
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002086
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002087 vlv_merge_wm(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002088
Ville Syrjäläff32c542017-03-02 19:14:57 +02002089 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002090 return;
2091
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002092 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002093 chv_set_memory_dvfs(dev_priv, false);
2094
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002095 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002096 chv_set_memory_pm5(dev_priv, false);
2097
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002098 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002099 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002100
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002101 vlv_write_wm_values(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002102
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002103 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002104 _intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002105
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002106 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002107 chv_set_memory_pm5(dev_priv, true);
2108
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002109 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002110 chv_set_memory_dvfs(dev_priv, true);
2111
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002112 *old_wm = new_wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03002113}
2114
Ville Syrjäläff32c542017-03-02 19:14:57 +02002115static void vlv_initial_watermarks(struct intel_atomic_state *state,
2116 struct intel_crtc_state *crtc_state)
2117{
2118 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2119 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
2120
2121 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002122 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
2123 vlv_program_watermarks(dev_priv);
2124 mutex_unlock(&dev_priv->wm.wm_mutex);
2125}
2126
2127static void vlv_optimize_watermarks(struct intel_atomic_state *state,
2128 struct intel_crtc_state *crtc_state)
2129{
2130 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2131 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2132
2133 if (!crtc_state->wm.need_postvbl_update)
2134 return;
2135
2136 mutex_lock(&dev_priv->wm.wm_mutex);
2137 intel_crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02002138 vlv_program_watermarks(dev_priv);
2139 mutex_unlock(&dev_priv->wm.wm_mutex);
2140}
2141
Ville Syrjälä432081b2016-10-31 22:37:03 +02002142static void i965_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002143{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002144 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002145 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002146 int srwm = 1;
2147 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03002148 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002149
2150 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002151 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002152 if (crtc) {
2153 /* self-refresh has much higher latency */
2154 static const int sr_latency_ns = 12000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002155 const struct drm_display_mode *adjusted_mode =
2156 &crtc->config->base.adjusted_mode;
2157 const struct drm_framebuffer *fb =
2158 crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002159 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002160 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002161 int hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002162 int cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002163 int entries;
2164
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002165 entries = intel_wm_method2(clock, htotal,
2166 hdisplay, cpp, sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002167 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
2168 srwm = I965_FIFO_SIZE - entries;
2169 if (srwm < 0)
2170 srwm = 1;
2171 srwm &= 0x1ff;
2172 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
2173 entries, srwm);
2174
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002175 entries = intel_wm_method2(clock, htotal,
2176 crtc->base.cursor->state->crtc_w, 4,
2177 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002178 entries = DIV_ROUND_UP(entries,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002179 i965_cursor_wm_info.cacheline_size) +
2180 i965_cursor_wm_info.guard_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002181
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002182 cursor_sr = i965_cursor_wm_info.fifo_size - entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002183 if (cursor_sr > i965_cursor_wm_info.max_wm)
2184 cursor_sr = i965_cursor_wm_info.max_wm;
2185
2186 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
2187 "cursor %d\n", srwm, cursor_sr);
2188
Imre Deak98584252014-06-13 14:54:20 +03002189 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002190 } else {
Imre Deak98584252014-06-13 14:54:20 +03002191 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002192 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03002193 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002194 }
2195
2196 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2197 srwm);
2198
2199 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002200 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
2201 FW_WM(8, CURSORB) |
2202 FW_WM(8, PLANEB) |
2203 FW_WM(8, PLANEA));
2204 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
2205 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002206 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002207 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03002208
2209 if (cxsr_enabled)
2210 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002211}
2212
Ville Syrjäläf4998962015-03-10 17:02:21 +02002213#undef FW_WM
2214
Ville Syrjälä432081b2016-10-31 22:37:03 +02002215static void i9xx_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002216{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002217 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002218 const struct intel_watermark_params *wm_info;
2219 uint32_t fwater_lo;
2220 uint32_t fwater_hi;
2221 int cwm, srwm = 1;
2222 int fifo_size;
2223 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002224 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002225
Ville Syrjäläa9097be2016-10-31 22:37:20 +02002226 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002227 wm_info = &i945_wm_info;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002228 else if (!IS_GEN2(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002229 wm_info = &i915_wm_info;
2230 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03002231 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002232
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02002233 fifo_size = dev_priv->display.get_fifo_size(dev_priv, 0);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02002234 crtc = intel_get_crtc_for_plane(dev_priv, 0);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002235 if (intel_crtc_active(crtc)) {
2236 const struct drm_display_mode *adjusted_mode =
2237 &crtc->config->base.adjusted_mode;
2238 const struct drm_framebuffer *fb =
2239 crtc->base.primary->state->fb;
2240 int cpp;
2241
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002242 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002243 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002244 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002245 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002246
Damien Lespiau241bfc32013-09-25 16:45:37 +01002247 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002248 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002249 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002250 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002251 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002252 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002253 if (planea_wm > (long)wm_info->max_wm)
2254 planea_wm = wm_info->max_wm;
2255 }
2256
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002257 if (IS_GEN2(dev_priv))
Ville Syrjälä9d539102014-08-15 01:21:53 +03002258 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002259
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02002260 fifo_size = dev_priv->display.get_fifo_size(dev_priv, 1);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02002261 crtc = intel_get_crtc_for_plane(dev_priv, 1);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002262 if (intel_crtc_active(crtc)) {
2263 const struct drm_display_mode *adjusted_mode =
2264 &crtc->config->base.adjusted_mode;
2265 const struct drm_framebuffer *fb =
2266 crtc->base.primary->state->fb;
2267 int cpp;
2268
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002269 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002270 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002271 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002272 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002273
Damien Lespiau241bfc32013-09-25 16:45:37 +01002274 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002275 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002276 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002277 if (enabled == NULL)
2278 enabled = crtc;
2279 else
2280 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002281 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002282 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002283 if (planeb_wm > (long)wm_info->max_wm)
2284 planeb_wm = wm_info->max_wm;
2285 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002286
2287 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
2288
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002289 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07002290 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002291
Ville Syrjäläefc26112016-10-31 22:37:04 +02002292 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002293
2294 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01002295 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002296 enabled = NULL;
2297 }
2298
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002299 /*
2300 * Overlay gets an aggressive default since video jitter is bad.
2301 */
2302 cwm = 2;
2303
2304 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03002305 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002306
2307 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02002308 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002309 /* self-refresh has much higher latency */
2310 static const int sr_latency_ns = 6000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002311 const struct drm_display_mode *adjusted_mode =
2312 &enabled->config->base.adjusted_mode;
2313 const struct drm_framebuffer *fb =
2314 enabled->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002315 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002316 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002317 int hdisplay = enabled->config->pipe_src_w;
2318 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002319 int entries;
2320
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002321 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002322 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002323 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002324 cpp = fb->format->cpp[0];
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002325
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002326 entries = intel_wm_method2(clock, htotal, hdisplay, cpp,
2327 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002328 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
2329 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
2330 srwm = wm_info->fifo_size - entries;
2331 if (srwm < 0)
2332 srwm = 1;
2333
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002334 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002335 I915_WRITE(FW_BLC_SELF,
2336 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03002337 else
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002338 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
2339 }
2340
2341 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
2342 planea_wm, planeb_wm, cwm, srwm);
2343
2344 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2345 fwater_hi = (cwm & 0x1f);
2346
2347 /* Set request length to 8 cachelines per fetch */
2348 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2349 fwater_hi = fwater_hi | (1 << 8);
2350
2351 I915_WRITE(FW_BLC, fwater_lo);
2352 I915_WRITE(FW_BLC2, fwater_hi);
2353
Imre Deak5209b1f2014-07-01 12:36:17 +03002354 if (enabled)
2355 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002356}
2357
Ville Syrjälä432081b2016-10-31 22:37:03 +02002358static void i845_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002359{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002360 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002361 struct intel_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002362 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002363 uint32_t fwater_lo;
2364 int planea_wm;
2365
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002366 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002367 if (crtc == NULL)
2368 return;
2369
Ville Syrjäläefc26112016-10-31 22:37:04 +02002370 adjusted_mode = &crtc->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002371 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02002372 &i845_wm_info,
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02002373 dev_priv->display.get_fifo_size(dev_priv, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01002374 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002375 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
2376 fwater_lo |= (3<<8) | planea_wm;
2377
2378 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
2379
2380 I915_WRITE(FW_BLC, fwater_lo);
2381}
2382
Ville Syrjälä37126462013-08-01 16:18:55 +03002383/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002384static unsigned int ilk_wm_method1(unsigned int pixel_rate,
2385 unsigned int cpp,
2386 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002387{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002388 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002389
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002390 ret = intel_wm_method1(pixel_rate, cpp, latency);
2391 ret = DIV_ROUND_UP(ret, 64) + 2;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002392
2393 return ret;
2394}
2395
Ville Syrjälä37126462013-08-01 16:18:55 +03002396/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002397static unsigned int ilk_wm_method2(unsigned int pixel_rate,
2398 unsigned int htotal,
2399 unsigned int width,
2400 unsigned int cpp,
2401 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002402{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002403 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002404
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002405 ret = intel_wm_method2(pixel_rate, htotal,
2406 width, cpp, latency);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002407 ret = DIV_ROUND_UP(ret, 64) + 2;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002408
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002409 return ret;
2410}
2411
Ville Syrjälä23297042013-07-05 11:57:17 +03002412static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Ville Syrjäläac484962016-01-20 21:05:26 +02002413 uint8_t cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002414{
Matt Roper15126882015-12-03 11:37:40 -08002415 /*
2416 * Neither of these should be possible since this function shouldn't be
2417 * called if the CRTC is off or the plane is invisible. But let's be
2418 * extra paranoid to avoid a potential divide-by-zero if we screw up
2419 * elsewhere in the driver.
2420 */
Ville Syrjäläac484962016-01-20 21:05:26 +02002421 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08002422 return 0;
2423 if (WARN_ON(!horiz_pixels))
2424 return 0;
2425
Ville Syrjäläac484962016-01-20 21:05:26 +02002426 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002427}
2428
Imre Deak820c1982013-12-17 14:46:36 +02002429struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002430 uint16_t pri;
2431 uint16_t spr;
2432 uint16_t cur;
2433 uint16_t fbc;
2434};
2435
Ville Syrjälä37126462013-08-01 16:18:55 +03002436/*
2437 * For both WM_PIPE and WM_LP.
2438 * mem_value must be in 0.1us units.
2439 */
Matt Roper7221fc32015-09-24 15:53:08 -07002440static uint32_t ilk_compute_pri_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002441 const struct intel_plane_state *pstate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03002442 uint32_t mem_value,
2443 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002444{
Paulo Zanonicca32e92013-05-31 11:45:06 -03002445 uint32_t method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002446 int cpp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002447
Ville Syrjälä24304d812017-03-14 17:10:49 +02002448 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002449 return 0;
2450
Ville Syrjälä353c8592016-12-14 23:30:57 +02002451 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002452
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002453 method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002454
2455 if (!is_lp)
2456 return method1;
2457
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002458 method2 = ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002459 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002460 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002461 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002462
2463 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002464}
2465
Ville Syrjälä37126462013-08-01 16:18:55 +03002466/*
2467 * For both WM_PIPE and WM_LP.
2468 * mem_value must be in 0.1us units.
2469 */
Matt Roper7221fc32015-09-24 15:53:08 -07002470static uint32_t ilk_compute_spr_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002471 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002472 uint32_t mem_value)
2473{
2474 uint32_t method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002475 int cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002476
Ville Syrjälä24304d812017-03-14 17:10:49 +02002477 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002478 return 0;
2479
Ville Syrjälä353c8592016-12-14 23:30:57 +02002480 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002481
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002482 method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
2483 method2 = ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002484 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002485 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002486 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002487 return min(method1, method2);
2488}
2489
Ville Syrjälä37126462013-08-01 16:18:55 +03002490/*
2491 * For both WM_PIPE and WM_LP.
2492 * mem_value must be in 0.1us units.
2493 */
Matt Roper7221fc32015-09-24 15:53:08 -07002494static uint32_t ilk_compute_cur_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002495 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002496 uint32_t mem_value)
2497{
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002498 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002499
Ville Syrjälä24304d812017-03-14 17:10:49 +02002500 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002501 return 0;
2502
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002503 cpp = pstate->base.fb->format->cpp[0];
2504
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002505 return ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002506 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002507 pstate->base.crtc_w, cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002508}
2509
Paulo Zanonicca32e92013-05-31 11:45:06 -03002510/* Only for WM_LP. */
Matt Roper7221fc32015-09-24 15:53:08 -07002511static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002512 const struct intel_plane_state *pstate,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03002513 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002514{
Ville Syrjälä83054942016-11-18 21:53:00 +02002515 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002516
Ville Syrjälä24304d812017-03-14 17:10:49 +02002517 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanonicca32e92013-05-31 11:45:06 -03002518 return 0;
2519
Ville Syrjälä353c8592016-12-14 23:30:57 +02002520 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002521
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002522 return ilk_wm_fbc(pri_val, drm_rect_width(&pstate->base.dst), cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002523}
2524
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002525static unsigned int
2526ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002527{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002528 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002529 return 3072;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002530 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002531 return 768;
2532 else
2533 return 512;
2534}
2535
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002536static unsigned int
2537ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2538 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002539{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002540 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002541 /* BDW primary/sprite plane watermarks */
2542 return level == 0 ? 255 : 2047;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002543 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002544 /* IVB/HSW primary/sprite plane watermarks */
2545 return level == 0 ? 127 : 1023;
2546 else if (!is_sprite)
2547 /* ILK/SNB primary plane watermarks */
2548 return level == 0 ? 127 : 511;
2549 else
2550 /* ILK/SNB sprite plane watermarks */
2551 return level == 0 ? 63 : 255;
2552}
2553
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002554static unsigned int
2555ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002556{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002557 if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002558 return level == 0 ? 63 : 255;
2559 else
2560 return level == 0 ? 31 : 63;
2561}
2562
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002563static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002564{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002565 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002566 return 31;
2567 else
2568 return 15;
2569}
2570
Ville Syrjälä158ae642013-08-07 13:28:19 +03002571/* Calculate the maximum primary/sprite plane watermark */
2572static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
2573 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002574 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002575 enum intel_ddb_partitioning ddb_partitioning,
2576 bool is_sprite)
2577{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002578 struct drm_i915_private *dev_priv = to_i915(dev);
2579 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002580
2581 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002582 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002583 return 0;
2584
2585 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002586 if (level == 0 || config->num_pipes_active > 1) {
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002587 fifo_size /= INTEL_INFO(dev_priv)->num_pipes;
Ville Syrjälä158ae642013-08-07 13:28:19 +03002588
2589 /*
2590 * For some reason the non self refresh
2591 * FIFO size is only half of the self
2592 * refresh FIFO size on ILK/SNB.
2593 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002594 if (INTEL_GEN(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002595 fifo_size /= 2;
2596 }
2597
Ville Syrjälä240264f2013-08-07 13:29:12 +03002598 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002599 /* level 0 is always calculated with 1:1 split */
2600 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2601 if (is_sprite)
2602 fifo_size *= 5;
2603 fifo_size /= 6;
2604 } else {
2605 fifo_size /= 2;
2606 }
2607 }
2608
2609 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002610 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002611}
2612
2613/* Calculate the maximum cursor plane watermark */
2614static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002615 int level,
2616 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002617{
2618 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002619 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002620 return 64;
2621
2622 /* otherwise just report max that registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002623 return ilk_cursor_wm_reg_max(to_i915(dev), level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002624}
2625
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002626static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002627 int level,
2628 const struct intel_wm_config *config,
2629 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002630 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002631{
Ville Syrjälä240264f2013-08-07 13:29:12 +03002632 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
2633 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
2634 max->cur = ilk_cursor_wm_max(dev, level, config);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002635 max->fbc = ilk_fbc_wm_reg_max(to_i915(dev));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002636}
2637
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002638static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002639 int level,
2640 struct ilk_wm_maximums *max)
2641{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002642 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2643 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2644 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2645 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002646}
2647
Ville Syrjäläd9395652013-10-09 19:18:10 +03002648static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002649 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002650 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002651{
2652 bool ret;
2653
2654 /* already determined to be invalid? */
2655 if (!result->enable)
2656 return false;
2657
2658 result->enable = result->pri_val <= max->pri &&
2659 result->spr_val <= max->spr &&
2660 result->cur_val <= max->cur;
2661
2662 ret = result->enable;
2663
2664 /*
2665 * HACK until we can pre-compute everything,
2666 * and thus fail gracefully if LP0 watermarks
2667 * are exceeded...
2668 */
2669 if (level == 0 && !result->enable) {
2670 if (result->pri_val > max->pri)
2671 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2672 level, result->pri_val, max->pri);
2673 if (result->spr_val > max->spr)
2674 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2675 level, result->spr_val, max->spr);
2676 if (result->cur_val > max->cur)
2677 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2678 level, result->cur_val, max->cur);
2679
2680 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
2681 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
2682 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
2683 result->enable = true;
2684 }
2685
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002686 return ret;
2687}
2688
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002689static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Matt Roper43d59ed2015-09-24 15:53:07 -07002690 const struct intel_crtc *intel_crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002691 int level,
Matt Roper7221fc32015-09-24 15:53:08 -07002692 struct intel_crtc_state *cstate,
Matt Roper86c8bbb2015-09-24 15:53:16 -07002693 struct intel_plane_state *pristate,
2694 struct intel_plane_state *sprstate,
2695 struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002696 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002697{
2698 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2699 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2700 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2701
2702 /* WM1+ latency values stored in 0.5us units */
2703 if (level > 0) {
2704 pri_latency *= 5;
2705 spr_latency *= 5;
2706 cur_latency *= 5;
2707 }
2708
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002709 if (pristate) {
2710 result->pri_val = ilk_compute_pri_wm(cstate, pristate,
2711 pri_latency, level);
2712 result->fbc_val = ilk_compute_fbc_wm(cstate, pristate, result->pri_val);
2713 }
2714
2715 if (sprstate)
2716 result->spr_val = ilk_compute_spr_wm(cstate, sprstate, spr_latency);
2717
2718 if (curstate)
2719 result->cur_val = ilk_compute_cur_wm(cstate, curstate, cur_latency);
2720
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002721 result->enable = true;
2722}
2723
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002724static uint32_t
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002725hsw_compute_linetime_wm(const struct intel_crtc_state *cstate)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002726{
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002727 const struct intel_atomic_state *intel_state =
2728 to_intel_atomic_state(cstate->base.state);
Matt Roperee91a152015-12-03 11:37:39 -08002729 const struct drm_display_mode *adjusted_mode =
2730 &cstate->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002731 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002732
Matt Roperee91a152015-12-03 11:37:39 -08002733 if (!cstate->base.active)
2734 return 0;
2735 if (WARN_ON(adjusted_mode->crtc_clock == 0))
2736 return 0;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002737 if (WARN_ON(intel_state->cdclk.logical.cdclk == 0))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002738 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002739
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002740 /* The WM are computed with base on how long it takes to fill a single
2741 * row at the given clock rate, multiplied by 8.
2742 * */
Ville Syrjälä124abe02015-09-08 13:40:45 +03002743 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2744 adjusted_mode->crtc_clock);
2745 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002746 intel_state->cdclk.logical.cdclk);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002747
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002748 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2749 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002750}
2751
Ville Syrjäläbb726512016-10-31 22:37:24 +02002752static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
2753 uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002754{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002755 if (IS_GEN9(dev_priv)) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002756 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002757 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002758 int level, max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002759
2760 /* read the first set of memory latencies[0:3] */
2761 val = 0; /* data0 to be programmed to 0 for first set */
2762 mutex_lock(&dev_priv->rps.hw_lock);
2763 ret = sandybridge_pcode_read(dev_priv,
2764 GEN9_PCODE_READ_MEM_LATENCY,
2765 &val);
2766 mutex_unlock(&dev_priv->rps.hw_lock);
2767
2768 if (ret) {
2769 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2770 return;
2771 }
2772
2773 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2774 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2775 GEN9_MEM_LATENCY_LEVEL_MASK;
2776 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2777 GEN9_MEM_LATENCY_LEVEL_MASK;
2778 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2779 GEN9_MEM_LATENCY_LEVEL_MASK;
2780
2781 /* read the second set of memory latencies[4:7] */
2782 val = 1; /* data0 to be programmed to 1 for second set */
2783 mutex_lock(&dev_priv->rps.hw_lock);
2784 ret = sandybridge_pcode_read(dev_priv,
2785 GEN9_PCODE_READ_MEM_LATENCY,
2786 &val);
2787 mutex_unlock(&dev_priv->rps.hw_lock);
2788 if (ret) {
2789 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2790 return;
2791 }
2792
2793 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2794 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2795 GEN9_MEM_LATENCY_LEVEL_MASK;
2796 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2797 GEN9_MEM_LATENCY_LEVEL_MASK;
2798 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2799 GEN9_MEM_LATENCY_LEVEL_MASK;
2800
Vandana Kannan367294b2014-11-04 17:06:46 +00002801 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002802 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2803 * need to be disabled. We make sure to sanitize the values out
2804 * of the punit to satisfy this requirement.
2805 */
2806 for (level = 1; level <= max_level; level++) {
2807 if (wm[level] == 0) {
2808 for (i = level + 1; i <= max_level; i++)
2809 wm[i] = 0;
2810 break;
2811 }
2812 }
2813
2814 /*
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02002815 * WaWmMemoryReadLatency:skl,glk
Damien Lespiau6f972352015-02-09 19:33:07 +00002816 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002817 * punit doesn't take into account the read latency so we need
Paulo Zanoni0727e402016-09-22 18:00:30 -03002818 * to add 2us to the various latency levels we retrieve from the
2819 * punit when level 0 response data us 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002820 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002821 if (wm[0] == 0) {
2822 wm[0] += 2;
2823 for (level = 1; level <= max_level; level++) {
2824 if (wm[level] == 0)
2825 break;
Vandana Kannan367294b2014-11-04 17:06:46 +00002826 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002827 }
Paulo Zanoni0727e402016-09-22 18:00:30 -03002828 }
2829
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002830 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002831 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2832
2833 wm[0] = (sskpd >> 56) & 0xFF;
2834 if (wm[0] == 0)
2835 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002836 wm[1] = (sskpd >> 4) & 0xFF;
2837 wm[2] = (sskpd >> 12) & 0xFF;
2838 wm[3] = (sskpd >> 20) & 0x1FF;
2839 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002840 } else if (INTEL_GEN(dev_priv) >= 6) {
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002841 uint32_t sskpd = I915_READ(MCH_SSKPD);
2842
2843 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2844 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2845 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2846 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002847 } else if (INTEL_GEN(dev_priv) >= 5) {
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002848 uint32_t mltr = I915_READ(MLTR_ILK);
2849
2850 /* ILK primary LP0 latency is 700 ns */
2851 wm[0] = 7;
2852 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2853 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002854 }
2855}
2856
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002857static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
2858 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002859{
2860 /* ILK sprite LP0 latency is 1300 ns */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002861 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002862 wm[0] = 13;
2863}
2864
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002865static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
2866 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002867{
2868 /* ILK cursor LP0 latency is 1300 ns */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002869 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002870 wm[0] = 13;
2871
2872 /* WaDoubleCursorLP3Latency:ivb */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002873 if (IS_IVYBRIDGE(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002874 wm[3] *= 2;
2875}
2876
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002877int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002878{
2879 /* how many WM levels are we expecting */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002880 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002881 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002882 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002883 return 4;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002884 else if (INTEL_GEN(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002885 return 3;
2886 else
2887 return 2;
2888}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002889
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002890static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002891 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002892 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002893{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002894 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002895
2896 for (level = 0; level <= max_level; level++) {
2897 unsigned int latency = wm[level];
2898
2899 if (latency == 0) {
2900 DRM_ERROR("%s WM%d latency not provided\n",
2901 name, level);
2902 continue;
2903 }
2904
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002905 /*
2906 * - latencies are in us on gen9.
2907 * - before then, WM1+ latency values are in 0.5us units
2908 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002909 if (IS_GEN9(dev_priv))
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002910 latency *= 10;
2911 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002912 latency *= 5;
2913
2914 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2915 name, level, wm[level],
2916 latency / 10, latency % 10);
2917 }
2918}
2919
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002920static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2921 uint16_t wm[5], uint16_t min)
2922{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002923 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002924
2925 if (wm[0] >= min)
2926 return false;
2927
2928 wm[0] = max(wm[0], min);
2929 for (level = 1; level <= max_level; level++)
2930 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2931
2932 return true;
2933}
2934
Ville Syrjäläbb726512016-10-31 22:37:24 +02002935static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002936{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002937 bool changed;
2938
2939 /*
2940 * The BIOS provided WM memory latency values are often
2941 * inadequate for high resolution displays. Adjust them.
2942 */
2943 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2944 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2945 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2946
2947 if (!changed)
2948 return;
2949
2950 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002951 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
2952 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
2953 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002954}
2955
Ville Syrjäläbb726512016-10-31 22:37:24 +02002956static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002957{
Ville Syrjäläbb726512016-10-31 22:37:24 +02002958 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002959
2960 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2961 sizeof(dev_priv->wm.pri_latency));
2962 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2963 sizeof(dev_priv->wm.pri_latency));
2964
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002965 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002966 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002967
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002968 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
2969 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
2970 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002971
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002972 if (IS_GEN6(dev_priv))
Ville Syrjäläbb726512016-10-31 22:37:24 +02002973 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002974}
2975
Ville Syrjäläbb726512016-10-31 22:37:24 +02002976static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002977{
Ville Syrjäläbb726512016-10-31 22:37:24 +02002978 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002979 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002980}
2981
Matt Ropered4a6a72016-02-23 17:20:13 -08002982static bool ilk_validate_pipe_wm(struct drm_device *dev,
2983 struct intel_pipe_wm *pipe_wm)
2984{
2985 /* LP0 watermark maximums depend on this pipe alone */
2986 const struct intel_wm_config config = {
2987 .num_pipes_active = 1,
2988 .sprites_enabled = pipe_wm->sprites_enabled,
2989 .sprites_scaled = pipe_wm->sprites_scaled,
2990 };
2991 struct ilk_wm_maximums max;
2992
2993 /* LP0 watermarks always use 1/2 DDB partitioning */
2994 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2995
2996 /* At least LP0 must be valid */
2997 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
2998 DRM_DEBUG_KMS("LP0 watermark invalid\n");
2999 return false;
3000 }
3001
3002 return true;
3003}
3004
Matt Roper261a27d2015-10-08 15:28:25 -07003005/* Compute new watermarks for the pipe */
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003006static int ilk_compute_pipe_wm(struct intel_crtc_state *cstate)
Matt Roper261a27d2015-10-08 15:28:25 -07003007{
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003008 struct drm_atomic_state *state = cstate->base.state;
3009 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07003010 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003011 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003012 const struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper43d59ed2015-09-24 15:53:07 -07003013 struct intel_plane *intel_plane;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003014 struct intel_plane_state *pristate = NULL;
Matt Roper43d59ed2015-09-24 15:53:07 -07003015 struct intel_plane_state *sprstate = NULL;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003016 struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003017 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02003018 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003019
Matt Ropere8f1f022016-05-12 07:05:55 -07003020 pipe_wm = &cstate->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003021
Matt Roper43d59ed2015-09-24 15:53:07 -07003022 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003023 struct intel_plane_state *ps;
3024
3025 ps = intel_atomic_get_existing_plane_state(state,
3026 intel_plane);
3027 if (!ps)
3028 continue;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003029
3030 if (intel_plane->base.type == DRM_PLANE_TYPE_PRIMARY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003031 pristate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003032 else if (intel_plane->base.type == DRM_PLANE_TYPE_OVERLAY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003033 sprstate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003034 else if (intel_plane->base.type == DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003035 curstate = ps;
Matt Roper43d59ed2015-09-24 15:53:07 -07003036 }
3037
Matt Ropered4a6a72016-02-23 17:20:13 -08003038 pipe_wm->pipe_enabled = cstate->base.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003039 if (sprstate) {
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003040 pipe_wm->sprites_enabled = sprstate->base.visible;
3041 pipe_wm->sprites_scaled = sprstate->base.visible &&
3042 (drm_rect_width(&sprstate->base.dst) != drm_rect_width(&sprstate->base.src) >> 16 ||
3043 drm_rect_height(&sprstate->base.dst) != drm_rect_height(&sprstate->base.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003044 }
3045
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003046 usable_level = max_level;
3047
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003048 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003049 if (INTEL_GEN(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003050 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003051
3052 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08003053 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003054 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003055
Matt Roper86c8bbb2015-09-24 15:53:16 -07003056 ilk_compute_wm_level(dev_priv, intel_crtc, 0, cstate,
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003057 pristate, sprstate, curstate, &pipe_wm->raw_wm[0]);
3058
3059 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
3060 pipe_wm->wm[0] = pipe_wm->raw_wm[0];
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003061
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003062 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjälä532f7a72016-04-29 17:31:17 +03003063 pipe_wm->linetime = hsw_compute_linetime_wm(cstate);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003064
Matt Ropered4a6a72016-02-23 17:20:13 -08003065 if (!ilk_validate_pipe_wm(dev, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01003066 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003067
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003068 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003069
3070 for (level = 1; level <= max_level; level++) {
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003071 struct intel_wm_level *wm = &pipe_wm->raw_wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003072
Matt Roper86c8bbb2015-09-24 15:53:16 -07003073 ilk_compute_wm_level(dev_priv, intel_crtc, level, cstate,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003074 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003075
3076 /*
3077 * Disable any watermark level that exceeds the
3078 * register maximums since such watermarks are
3079 * always invalid.
3080 */
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003081 if (level > usable_level)
3082 continue;
3083
3084 if (ilk_validate_wm_level(level, &max, wm))
3085 pipe_wm->wm[level] = *wm;
3086 else
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003087 usable_level = level;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003088 }
3089
Matt Roper86c8bbb2015-09-24 15:53:16 -07003090 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003091}
3092
3093/*
Matt Ropered4a6a72016-02-23 17:20:13 -08003094 * Build a set of 'intermediate' watermark values that satisfy both the old
3095 * state and the new state. These can be programmed to the hardware
3096 * immediately.
3097 */
3098static int ilk_compute_intermediate_wm(struct drm_device *dev,
3099 struct intel_crtc *intel_crtc,
3100 struct intel_crtc_state *newstate)
3101{
Matt Ropere8f1f022016-05-12 07:05:55 -07003102 struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08003103 struct intel_pipe_wm *b = &intel_crtc->wm.active.ilk;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003104 int level, max_level = ilk_wm_max_level(to_i915(dev));
Matt Ropered4a6a72016-02-23 17:20:13 -08003105
3106 /*
3107 * Start with the final, target watermarks, then combine with the
3108 * currently active watermarks to get values that are safe both before
3109 * and after the vblank.
3110 */
Matt Ropere8f1f022016-05-12 07:05:55 -07003111 *a = newstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08003112 a->pipe_enabled |= b->pipe_enabled;
3113 a->sprites_enabled |= b->sprites_enabled;
3114 a->sprites_scaled |= b->sprites_scaled;
3115
3116 for (level = 0; level <= max_level; level++) {
3117 struct intel_wm_level *a_wm = &a->wm[level];
3118 const struct intel_wm_level *b_wm = &b->wm[level];
3119
3120 a_wm->enable &= b_wm->enable;
3121 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
3122 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
3123 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
3124 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
3125 }
3126
3127 /*
3128 * We need to make sure that these merged watermark values are
3129 * actually a valid configuration themselves. If they're not,
3130 * there's no safe way to transition from the old state to
3131 * the new state, so we need to fail the atomic transaction.
3132 */
3133 if (!ilk_validate_pipe_wm(dev, a))
3134 return -EINVAL;
3135
3136 /*
3137 * If our intermediate WM are identical to the final WM, then we can
3138 * omit the post-vblank programming; only update if it's different.
3139 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02003140 if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) != 0)
3141 newstate->wm.need_postvbl_update = true;
Matt Ropered4a6a72016-02-23 17:20:13 -08003142
3143 return 0;
3144}
3145
3146/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003147 * Merge the watermarks from all active pipes for a specific level.
3148 */
3149static void ilk_merge_wm_level(struct drm_device *dev,
3150 int level,
3151 struct intel_wm_level *ret_wm)
3152{
3153 const struct intel_crtc *intel_crtc;
3154
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003155 ret_wm->enable = true;
3156
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003157 for_each_intel_crtc(dev, intel_crtc) {
Matt Ropered4a6a72016-02-23 17:20:13 -08003158 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02003159 const struct intel_wm_level *wm = &active->wm[level];
3160
3161 if (!active->pipe_enabled)
3162 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003163
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003164 /*
3165 * The watermark values may have been used in the past,
3166 * so we must maintain them in the registers for some
3167 * time even if the level is now disabled.
3168 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003169 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003170 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003171
3172 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
3173 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
3174 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
3175 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
3176 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003177}
3178
3179/*
3180 * Merge all low power watermarks for all active pipes.
3181 */
3182static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003183 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02003184 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003185 struct intel_pipe_wm *merged)
3186{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003187 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003188 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003189 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003190
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003191 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003192 if ((INTEL_GEN(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003193 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03003194 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003195
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003196 /* ILK: FBC WM must be disabled always */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003197 merged->fbc_wm_enabled = INTEL_GEN(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003198
3199 /* merge each WM1+ level */
3200 for (level = 1; level <= max_level; level++) {
3201 struct intel_wm_level *wm = &merged->wm[level];
3202
3203 ilk_merge_wm_level(dev, level, wm);
3204
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003205 if (level > last_enabled_level)
3206 wm->enable = false;
3207 else if (!ilk_validate_wm_level(level, max, wm))
3208 /* make sure all following levels get disabled */
3209 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003210
3211 /*
3212 * The spec says it is preferred to disable
3213 * FBC WMs instead of disabling a WM level.
3214 */
3215 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003216 if (wm->enable)
3217 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003218 wm->fbc_val = 0;
3219 }
3220 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003221
3222 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
3223 /*
3224 * FIXME this is racy. FBC might get enabled later.
3225 * What we should check here is whether FBC can be
3226 * enabled sometime later.
3227 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003228 if (IS_GEN5(dev_priv) && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03003229 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003230 for (level = 2; level <= max_level; level++) {
3231 struct intel_wm_level *wm = &merged->wm[level];
3232
3233 wm->enable = false;
3234 }
3235 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003236}
3237
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003238static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
3239{
3240 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
3241 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
3242}
3243
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003244/* The value we need to program into the WM_LPx latency field */
3245static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
3246{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003247 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003248
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003249 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003250 return 2 * level;
3251 else
3252 return dev_priv->wm.pri_latency[level];
3253}
3254
Imre Deak820c1982013-12-17 14:46:36 +02003255static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03003256 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03003257 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02003258 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003259{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003260 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003261 struct intel_crtc *intel_crtc;
3262 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003263
Ville Syrjälä0362c782013-10-09 19:17:57 +03003264 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03003265 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003266
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003267 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03003268 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03003269 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003270
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003271 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003272
Ville Syrjälä0362c782013-10-09 19:17:57 +03003273 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03003274
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003275 /*
3276 * Maintain the watermark values even if the level is
3277 * disabled. Doing otherwise could cause underruns.
3278 */
3279 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003280 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07003281 (r->pri_val << WM1_LP_SR_SHIFT) |
3282 r->cur_val;
3283
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003284 if (r->enable)
3285 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
3286
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003287 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07003288 results->wm_lp[wm_lp - 1] |=
3289 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
3290 else
3291 results->wm_lp[wm_lp - 1] |=
3292 r->fbc_val << WM1_LP_FBC_SHIFT;
3293
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003294 /*
3295 * Always set WM1S_LP_EN when spr_val != 0, even if the
3296 * level is disabled. Doing otherwise could cause underruns.
3297 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003298 if (INTEL_GEN(dev_priv) <= 6 && r->spr_val) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003299 WARN_ON(wm_lp != 1);
3300 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
3301 } else
3302 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003303 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003304
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003305 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003306 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003307 enum pipe pipe = intel_crtc->pipe;
Matt Ropered4a6a72016-02-23 17:20:13 -08003308 const struct intel_wm_level *r =
3309 &intel_crtc->wm.active.ilk.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003310
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003311 if (WARN_ON(!r->enable))
3312 continue;
3313
Matt Ropered4a6a72016-02-23 17:20:13 -08003314 results->wm_linetime[pipe] = intel_crtc->wm.active.ilk.linetime;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003315
3316 results->wm_pipe[pipe] =
3317 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
3318 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
3319 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003320 }
3321}
3322
Paulo Zanoni861f3382013-05-31 10:19:21 -03003323/* Find the result with the highest level enabled. Check for enable_fbc_wm in
3324 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02003325static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003326 struct intel_pipe_wm *r1,
3327 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003328{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003329 int level, max_level = ilk_wm_max_level(to_i915(dev));
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003330 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003331
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003332 for (level = 1; level <= max_level; level++) {
3333 if (r1->wm[level].enable)
3334 level1 = level;
3335 if (r2->wm[level].enable)
3336 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003337 }
3338
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003339 if (level1 == level2) {
3340 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003341 return r2;
3342 else
3343 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003344 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03003345 return r1;
3346 } else {
3347 return r2;
3348 }
3349}
3350
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003351/* dirty bits used to track which watermarks need changes */
3352#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
3353#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
3354#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
3355#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
3356#define WM_DIRTY_FBC (1 << 24)
3357#define WM_DIRTY_DDB (1 << 25)
3358
Damien Lespiau055e3932014-08-18 13:49:10 +01003359static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02003360 const struct ilk_wm_values *old,
3361 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003362{
3363 unsigned int dirty = 0;
3364 enum pipe pipe;
3365 int wm_lp;
3366
Damien Lespiau055e3932014-08-18 13:49:10 +01003367 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003368 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
3369 dirty |= WM_DIRTY_LINETIME(pipe);
3370 /* Must disable LP1+ watermarks too */
3371 dirty |= WM_DIRTY_LP_ALL;
3372 }
3373
3374 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
3375 dirty |= WM_DIRTY_PIPE(pipe);
3376 /* Must disable LP1+ watermarks too */
3377 dirty |= WM_DIRTY_LP_ALL;
3378 }
3379 }
3380
3381 if (old->enable_fbc_wm != new->enable_fbc_wm) {
3382 dirty |= WM_DIRTY_FBC;
3383 /* Must disable LP1+ watermarks too */
3384 dirty |= WM_DIRTY_LP_ALL;
3385 }
3386
3387 if (old->partitioning != new->partitioning) {
3388 dirty |= WM_DIRTY_DDB;
3389 /* Must disable LP1+ watermarks too */
3390 dirty |= WM_DIRTY_LP_ALL;
3391 }
3392
3393 /* LP1+ watermarks already deemed dirty, no need to continue */
3394 if (dirty & WM_DIRTY_LP_ALL)
3395 return dirty;
3396
3397 /* Find the lowest numbered LP1+ watermark in need of an update... */
3398 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3399 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
3400 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
3401 break;
3402 }
3403
3404 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
3405 for (; wm_lp <= 3; wm_lp++)
3406 dirty |= WM_DIRTY_LP(wm_lp);
3407
3408 return dirty;
3409}
3410
Ville Syrjälä8553c182013-12-05 15:51:39 +02003411static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
3412 unsigned int dirty)
3413{
Imre Deak820c1982013-12-17 14:46:36 +02003414 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02003415 bool changed = false;
3416
3417 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
3418 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
3419 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
3420 changed = true;
3421 }
3422 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
3423 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
3424 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
3425 changed = true;
3426 }
3427 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
3428 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
3429 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
3430 changed = true;
3431 }
3432
3433 /*
3434 * Don't touch WM1S_LP_EN here.
3435 * Doing so could cause underruns.
3436 */
3437
3438 return changed;
3439}
3440
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003441/*
3442 * The spec says we shouldn't write when we don't need, because every write
3443 * causes WMs to be re-evaluated, expending some power.
3444 */
Imre Deak820c1982013-12-17 14:46:36 +02003445static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
3446 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003447{
Imre Deak820c1982013-12-17 14:46:36 +02003448 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003449 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003450 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003451
Damien Lespiau055e3932014-08-18 13:49:10 +01003452 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003453 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003454 return;
3455
Ville Syrjälä8553c182013-12-05 15:51:39 +02003456 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003457
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003458 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003459 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003460 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003461 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003462 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003463 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
3464
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003465 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003466 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003467 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003468 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003469 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003470 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
3471
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003472 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003473 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003474 val = I915_READ(WM_MISC);
3475 if (results->partitioning == INTEL_DDB_PART_1_2)
3476 val &= ~WM_MISC_DATA_PARTITION_5_6;
3477 else
3478 val |= WM_MISC_DATA_PARTITION_5_6;
3479 I915_WRITE(WM_MISC, val);
3480 } else {
3481 val = I915_READ(DISP_ARB_CTL2);
3482 if (results->partitioning == INTEL_DDB_PART_1_2)
3483 val &= ~DISP_DATA_PARTITION_5_6;
3484 else
3485 val |= DISP_DATA_PARTITION_5_6;
3486 I915_WRITE(DISP_ARB_CTL2, val);
3487 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003488 }
3489
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003490 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03003491 val = I915_READ(DISP_ARB_CTL);
3492 if (results->enable_fbc_wm)
3493 val &= ~DISP_FBC_WM_DIS;
3494 else
3495 val |= DISP_FBC_WM_DIS;
3496 I915_WRITE(DISP_ARB_CTL, val);
3497 }
3498
Imre Deak954911e2013-12-17 14:46:34 +02003499 if (dirty & WM_DIRTY_LP(1) &&
3500 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
3501 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
3502
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003503 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003504 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
3505 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
3506 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
3507 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
3508 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003509
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003510 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003511 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003512 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003513 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003514 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003515 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003516
3517 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003518}
3519
Matt Ropered4a6a72016-02-23 17:20:13 -08003520bool ilk_disable_lp_wm(struct drm_device *dev)
Ville Syrjälä8553c182013-12-05 15:51:39 +02003521{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003522 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003523
3524 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3525}
3526
Lyude656d1b82016-08-17 15:55:54 -04003527#define SKL_SAGV_BLOCK_TIME 30 /* µs */
Damien Lespiaub9cec072014-11-04 17:06:43 +00003528
Matt Roper024c9042015-09-24 15:53:11 -07003529/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003530 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3531 * so assume we'll always need it in order to avoid underruns.
3532 */
3533static bool skl_needs_memory_bw_wa(struct intel_atomic_state *state)
3534{
3535 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3536
Rodrigo Vivib976dc52017-01-23 10:32:37 -08003537 if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv))
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003538 return true;
3539
3540 return false;
3541}
3542
Paulo Zanoni56feca92016-09-22 18:00:28 -03003543static bool
3544intel_has_sagv(struct drm_i915_private *dev_priv)
3545{
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003546 if (IS_KABYLAKE(dev_priv))
3547 return true;
3548
3549 if (IS_SKYLAKE(dev_priv) &&
3550 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED)
3551 return true;
3552
3553 return false;
Paulo Zanoni56feca92016-09-22 18:00:28 -03003554}
3555
Lyude656d1b82016-08-17 15:55:54 -04003556/*
3557 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3558 * depending on power and performance requirements. The display engine access
3559 * to system memory is blocked during the adjustment time. Because of the
3560 * blocking time, having this enabled can cause full system hangs and/or pipe
3561 * underruns if we don't meet all of the following requirements:
3562 *
3563 * - <= 1 pipe enabled
3564 * - All planes can enable watermarks for latencies >= SAGV engine block time
3565 * - We're not using an interlaced display configuration
3566 */
3567int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003568intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003569{
3570 int ret;
3571
Paulo Zanoni56feca92016-09-22 18:00:28 -03003572 if (!intel_has_sagv(dev_priv))
3573 return 0;
3574
3575 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003576 return 0;
3577
3578 DRM_DEBUG_KMS("Enabling the SAGV\n");
3579 mutex_lock(&dev_priv->rps.hw_lock);
3580
3581 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3582 GEN9_SAGV_ENABLE);
3583
3584 /* We don't need to wait for the SAGV when enabling */
3585 mutex_unlock(&dev_priv->rps.hw_lock);
3586
3587 /*
3588 * Some skl systems, pre-release machines in particular,
3589 * don't actually have an SAGV.
3590 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003591 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003592 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003593 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003594 return 0;
3595 } else if (ret < 0) {
3596 DRM_ERROR("Failed to enable the SAGV\n");
3597 return ret;
3598 }
3599
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003600 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003601 return 0;
3602}
3603
Lyude656d1b82016-08-17 15:55:54 -04003604int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003605intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003606{
Imre Deakb3b8e992016-12-05 18:27:38 +02003607 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003608
Paulo Zanoni56feca92016-09-22 18:00:28 -03003609 if (!intel_has_sagv(dev_priv))
3610 return 0;
3611
3612 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003613 return 0;
3614
3615 DRM_DEBUG_KMS("Disabling the SAGV\n");
3616 mutex_lock(&dev_priv->rps.hw_lock);
3617
3618 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003619 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3620 GEN9_SAGV_DISABLE,
3621 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3622 1);
Lyude656d1b82016-08-17 15:55:54 -04003623 mutex_unlock(&dev_priv->rps.hw_lock);
3624
Lyude656d1b82016-08-17 15:55:54 -04003625 /*
3626 * Some skl systems, pre-release machines in particular,
3627 * don't actually have an SAGV.
3628 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003629 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003630 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003631 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003632 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003633 } else if (ret < 0) {
3634 DRM_ERROR("Failed to disable the SAGV (%d)\n", ret);
3635 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003636 }
3637
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003638 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003639 return 0;
3640}
3641
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003642bool intel_can_enable_sagv(struct drm_atomic_state *state)
Lyude656d1b82016-08-17 15:55:54 -04003643{
3644 struct drm_device *dev = state->dev;
3645 struct drm_i915_private *dev_priv = to_i915(dev);
3646 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003647 struct intel_crtc *crtc;
3648 struct intel_plane *plane;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003649 struct intel_crtc_state *cstate;
Lyude656d1b82016-08-17 15:55:54 -04003650 enum pipe pipe;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003651 int level, latency;
Lyude656d1b82016-08-17 15:55:54 -04003652
Paulo Zanoni56feca92016-09-22 18:00:28 -03003653 if (!intel_has_sagv(dev_priv))
3654 return false;
3655
Lyude656d1b82016-08-17 15:55:54 -04003656 /*
3657 * SKL workaround: bspec recommends we disable the SAGV when we have
3658 * more then one pipe enabled
3659 *
3660 * If there are no active CRTCs, no additional checks need be performed
3661 */
3662 if (hweight32(intel_state->active_crtcs) == 0)
3663 return true;
3664 else if (hweight32(intel_state->active_crtcs) > 1)
3665 return false;
3666
3667 /* Since we're now guaranteed to only have one active CRTC... */
3668 pipe = ffs(intel_state->active_crtcs) - 1;
Ville Syrjälä98187832016-10-31 22:37:10 +02003669 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003670 cstate = to_intel_crtc_state(crtc->base.state);
Lyude656d1b82016-08-17 15:55:54 -04003671
Paulo Zanonic89cadd2016-10-10 17:30:59 -03003672 if (crtc->base.state->adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003673 return false;
3674
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003675 for_each_intel_plane_on_crtc(dev, crtc, plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003676 struct skl_plane_wm *wm =
3677 &cstate->wm.skl.optimal.planes[plane->id];
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003678
Lyude656d1b82016-08-17 15:55:54 -04003679 /* Skip this plane if it's not enabled */
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003680 if (!wm->wm[0].plane_en)
Lyude656d1b82016-08-17 15:55:54 -04003681 continue;
3682
3683 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003684 for (level = ilk_wm_max_level(dev_priv);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003685 !wm->wm[level].plane_en; --level)
Lyude656d1b82016-08-17 15:55:54 -04003686 { }
3687
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003688 latency = dev_priv->wm.skl_latency[level];
3689
3690 if (skl_needs_memory_bw_wa(intel_state) &&
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003691 plane->base.state->fb->modifier ==
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003692 I915_FORMAT_MOD_X_TILED)
3693 latency += 15;
3694
Lyude656d1b82016-08-17 15:55:54 -04003695 /*
3696 * If any of the planes on this pipe don't enable wm levels
3697 * that incur memory latencies higher then 30µs we can't enable
3698 * the SAGV
3699 */
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003700 if (latency < SKL_SAGV_BLOCK_TIME)
Lyude656d1b82016-08-17 15:55:54 -04003701 return false;
3702 }
3703
3704 return true;
3705}
3706
Damien Lespiaub9cec072014-11-04 17:06:43 +00003707static void
3708skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
Matt Roper024c9042015-09-24 15:53:11 -07003709 const struct intel_crtc_state *cstate,
Matt Roperc107acf2016-05-12 07:06:01 -07003710 struct skl_ddb_entry *alloc, /* out */
3711 int *num_active /* out */)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003712{
Matt Roperc107acf2016-05-12 07:06:01 -07003713 struct drm_atomic_state *state = cstate->base.state;
3714 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
3715 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper024c9042015-09-24 15:53:11 -07003716 struct drm_crtc *for_crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003717 unsigned int pipe_size, ddb_size;
3718 int nth_active_pipe;
Matt Roperc107acf2016-05-12 07:06:01 -07003719
Matt Ropera6d3460e2016-05-12 07:06:04 -07003720 if (WARN_ON(!state) || !cstate->base.active) {
Damien Lespiaub9cec072014-11-04 17:06:43 +00003721 alloc->start = 0;
3722 alloc->end = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003723 *num_active = hweight32(dev_priv->active_crtcs);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003724 return;
3725 }
3726
Matt Ropera6d3460e2016-05-12 07:06:04 -07003727 if (intel_state->active_pipe_changes)
3728 *num_active = hweight32(intel_state->active_crtcs);
3729 else
3730 *num_active = hweight32(dev_priv->active_crtcs);
3731
Deepak M6f3fff62016-09-15 15:01:10 +05303732 ddb_size = INTEL_INFO(dev_priv)->ddb_size;
3733 WARN_ON(ddb_size == 0);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003734
3735 ddb_size -= 4; /* 4 blocks for bypass path allocation */
3736
Matt Roperc107acf2016-05-12 07:06:01 -07003737 /*
Matt Ropera6d3460e2016-05-12 07:06:04 -07003738 * If the state doesn't change the active CRTC's, then there's
3739 * no need to recalculate; the existing pipe allocation limits
3740 * should remain unchanged. Note that we're safe from racing
3741 * commits since any racing commit that changes the active CRTC
3742 * list would need to grab _all_ crtc locks, including the one
3743 * we currently hold.
Matt Roperc107acf2016-05-12 07:06:01 -07003744 */
Matt Ropera6d3460e2016-05-12 07:06:04 -07003745 if (!intel_state->active_pipe_changes) {
Maarten Lankhorst512b5522016-11-08 13:55:34 +01003746 /*
3747 * alloc may be cleared by clear_intel_crtc_state,
3748 * copy from old state to be sure
3749 */
3750 *alloc = to_intel_crtc_state(for_crtc->state)->wm.skl.ddb;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003751 return;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003752 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07003753
3754 nth_active_pipe = hweight32(intel_state->active_crtcs &
3755 (drm_crtc_mask(for_crtc) - 1));
3756 pipe_size = ddb_size / hweight32(intel_state->active_crtcs);
3757 alloc->start = nth_active_pipe * ddb_size / *num_active;
3758 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003759}
3760
Matt Roperc107acf2016-05-12 07:06:01 -07003761static unsigned int skl_cursor_allocation(int num_active)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003762{
Matt Roperc107acf2016-05-12 07:06:01 -07003763 if (num_active == 1)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003764 return 32;
3765
3766 return 8;
3767}
3768
Damien Lespiaua269c582014-11-04 17:06:49 +00003769static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
3770{
3771 entry->start = reg & 0x3ff;
3772 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00003773 if (entry->end)
3774 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00003775}
3776
Damien Lespiau08db6652014-11-04 17:06:52 +00003777void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
3778 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00003779{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003780 struct intel_crtc *crtc;
Damien Lespiaua269c582014-11-04 17:06:49 +00003781
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003782 memset(ddb, 0, sizeof(*ddb));
3783
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003784 for_each_intel_crtc(&dev_priv->drm, crtc) {
Imre Deak4d800032016-02-17 16:31:29 +02003785 enum intel_display_power_domain power_domain;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003786 enum plane_id plane_id;
3787 enum pipe pipe = crtc->pipe;
Imre Deak4d800032016-02-17 16:31:29 +02003788
3789 power_domain = POWER_DOMAIN_PIPE(pipe);
3790 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003791 continue;
3792
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003793 for_each_plane_id_on_crtc(crtc, plane_id) {
3794 u32 val;
Damien Lespiaua269c582014-11-04 17:06:49 +00003795
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003796 if (plane_id != PLANE_CURSOR)
3797 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
3798 else
3799 val = I915_READ(CUR_BUF_CFG(pipe));
3800
3801 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane_id], val);
3802 }
Imre Deak4d800032016-02-17 16:31:29 +02003803
3804 intel_display_power_put(dev_priv, power_domain);
Damien Lespiaua269c582014-11-04 17:06:49 +00003805 }
3806}
3807
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003808/*
3809 * Determines the downscale amount of a plane for the purposes of watermark calculations.
3810 * The bspec defines downscale amount as:
3811 *
3812 * """
3813 * Horizontal down scale amount = maximum[1, Horizontal source size /
3814 * Horizontal destination size]
3815 * Vertical down scale amount = maximum[1, Vertical source size /
3816 * Vertical destination size]
3817 * Total down scale amount = Horizontal down scale amount *
3818 * Vertical down scale amount
3819 * """
3820 *
3821 * Return value is provided in 16.16 fixed point form to retain fractional part.
3822 * Caller should take care of dividing & rounding off the value.
3823 */
3824static uint32_t
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003825skl_plane_downscale_amount(const struct intel_crtc_state *cstate,
3826 const struct intel_plane_state *pstate)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003827{
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003828 struct intel_plane *plane = to_intel_plane(pstate->base.plane);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003829 uint32_t downscale_h, downscale_w;
3830 uint32_t src_w, src_h, dst_w, dst_h;
3831
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003832 if (WARN_ON(!intel_wm_plane_visible(cstate, pstate)))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003833 return DRM_PLANE_HELPER_NO_SCALING;
3834
3835 /* n.b., src is 16.16 fixed point, dst is whole integer */
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003836 if (plane->id == PLANE_CURSOR) {
3837 src_w = pstate->base.src_w;
3838 src_h = pstate->base.src_h;
3839 dst_w = pstate->base.crtc_w;
3840 dst_h = pstate->base.crtc_h;
3841 } else {
3842 src_w = drm_rect_width(&pstate->base.src);
3843 src_h = drm_rect_height(&pstate->base.src);
3844 dst_w = drm_rect_width(&pstate->base.dst);
3845 dst_h = drm_rect_height(&pstate->base.dst);
3846 }
3847
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003848 if (drm_rotation_90_or_270(pstate->base.rotation))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003849 swap(dst_w, dst_h);
3850
3851 downscale_h = max(src_h / dst_h, (uint32_t)DRM_PLANE_HELPER_NO_SCALING);
3852 downscale_w = max(src_w / dst_w, (uint32_t)DRM_PLANE_HELPER_NO_SCALING);
3853
3854 /* Provide result in 16.16 fixed point */
3855 return (uint64_t)downscale_w * downscale_h >> 16;
3856}
3857
Damien Lespiaub9cec072014-11-04 17:06:43 +00003858static unsigned int
Matt Roper024c9042015-09-24 15:53:11 -07003859skl_plane_relative_data_rate(const struct intel_crtc_state *cstate,
3860 const struct drm_plane_state *pstate,
3861 int y)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003862{
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003863 struct intel_plane *plane = to_intel_plane(pstate->plane);
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003864 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003865 uint32_t down_scale_amount, data_rate;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003866 uint32_t width = 0, height = 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02003867 struct drm_framebuffer *fb;
3868 u32 format;
Matt Ropera1de91e2016-05-12 07:05:57 -07003869
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003870 if (!intel_pstate->base.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07003871 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02003872
3873 fb = pstate->fb;
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003874 format = fb->format->format;
Ville Syrjälä83054942016-11-18 21:53:00 +02003875
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003876 if (plane->id == PLANE_CURSOR)
Matt Ropera1de91e2016-05-12 07:05:57 -07003877 return 0;
3878 if (y && format != DRM_FORMAT_NV12)
3879 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003880
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003881 width = drm_rect_width(&intel_pstate->base.src) >> 16;
3882 height = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003883
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003884 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003885 swap(width, height);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003886
3887 /* for planar format */
Matt Ropera1de91e2016-05-12 07:05:57 -07003888 if (format == DRM_FORMAT_NV12) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003889 if (y) /* y-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003890 data_rate = width * height *
Ville Syrjälä353c8592016-12-14 23:30:57 +02003891 fb->format->cpp[0];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003892 else /* uv-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003893 data_rate = (width / 2) * (height / 2) *
Ville Syrjälä353c8592016-12-14 23:30:57 +02003894 fb->format->cpp[1];
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003895 } else {
3896 /* for packed formats */
Ville Syrjälä353c8592016-12-14 23:30:57 +02003897 data_rate = width * height * fb->format->cpp[0];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003898 }
3899
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003900 down_scale_amount = skl_plane_downscale_amount(cstate, intel_pstate);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003901
3902 return (uint64_t)data_rate * down_scale_amount >> 16;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003903}
3904
3905/*
3906 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
3907 * a 8192x4096@32bpp framebuffer:
3908 * 3 * 4096 * 8192 * 4 < 2^32
3909 */
3910static unsigned int
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003911skl_get_total_relative_data_rate(struct intel_crtc_state *intel_cstate,
3912 unsigned *plane_data_rate,
3913 unsigned *plane_y_data_rate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003914{
Matt Roper9c74d822016-05-12 07:05:58 -07003915 struct drm_crtc_state *cstate = &intel_cstate->base;
3916 struct drm_atomic_state *state = cstate->state;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003917 struct drm_plane *plane;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003918 const struct drm_plane_state *pstate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003919 unsigned int total_data_rate = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003920
3921 if (WARN_ON(!state))
3922 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003923
Matt Ropera1de91e2016-05-12 07:05:57 -07003924 /* Calculate and cache data rate for each plane */
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003925 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, cstate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003926 enum plane_id plane_id = to_intel_plane(plane)->id;
3927 unsigned int rate;
Matt Roper024c9042015-09-24 15:53:11 -07003928
Matt Ropera6d3460e2016-05-12 07:06:04 -07003929 /* packed/uv */
3930 rate = skl_plane_relative_data_rate(intel_cstate,
3931 pstate, 0);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003932 plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003933
3934 total_data_rate += rate;
Matt Roper9c74d822016-05-12 07:05:58 -07003935
Matt Ropera6d3460e2016-05-12 07:06:04 -07003936 /* y-plane */
3937 rate = skl_plane_relative_data_rate(intel_cstate,
3938 pstate, 1);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003939 plane_y_data_rate[plane_id] = rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07003940
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003941 total_data_rate += rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003942 }
3943
3944 return total_data_rate;
3945}
3946
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003947static uint16_t
3948skl_ddb_min_alloc(const struct drm_plane_state *pstate,
3949 const int y)
3950{
3951 struct drm_framebuffer *fb = pstate->fb;
3952 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
3953 uint32_t src_w, src_h;
3954 uint32_t min_scanlines = 8;
3955 uint8_t plane_bpp;
3956
3957 if (WARN_ON(!fb))
3958 return 0;
3959
3960 /* For packed formats, no y-plane, return 0 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003961 if (y && fb->format->format != DRM_FORMAT_NV12)
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003962 return 0;
3963
3964 /* For Non Y-tile return 8-blocks */
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003965 if (fb->modifier != I915_FORMAT_MOD_Y_TILED &&
3966 fb->modifier != I915_FORMAT_MOD_Yf_TILED)
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003967 return 8;
3968
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003969 src_w = drm_rect_width(&intel_pstate->base.src) >> 16;
3970 src_h = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003971
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003972 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003973 swap(src_w, src_h);
3974
3975 /* Halve UV plane width and height for NV12 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003976 if (fb->format->format == DRM_FORMAT_NV12 && !y) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003977 src_w /= 2;
3978 src_h /= 2;
3979 }
3980
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003981 if (fb->format->format == DRM_FORMAT_NV12 && !y)
Ville Syrjälä353c8592016-12-14 23:30:57 +02003982 plane_bpp = fb->format->cpp[1];
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003983 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02003984 plane_bpp = fb->format->cpp[0];
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003985
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003986 if (drm_rotation_90_or_270(pstate->rotation)) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003987 switch (plane_bpp) {
3988 case 1:
3989 min_scanlines = 32;
3990 break;
3991 case 2:
3992 min_scanlines = 16;
3993 break;
3994 case 4:
3995 min_scanlines = 8;
3996 break;
3997 case 8:
3998 min_scanlines = 4;
3999 break;
4000 default:
4001 WARN(1, "Unsupported pixel depth %u for rotation",
4002 plane_bpp);
4003 min_scanlines = 32;
4004 }
4005 }
4006
4007 return DIV_ROUND_UP((4 * src_w * plane_bpp), 512) * min_scanlines/4 + 3;
4008}
4009
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004010static void
4011skl_ddb_calc_min(const struct intel_crtc_state *cstate, int num_active,
4012 uint16_t *minimum, uint16_t *y_minimum)
4013{
4014 const struct drm_plane_state *pstate;
4015 struct drm_plane *plane;
4016
4017 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, &cstate->base) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004018 enum plane_id plane_id = to_intel_plane(plane)->id;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004019
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004020 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004021 continue;
4022
4023 if (!pstate->visible)
4024 continue;
4025
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004026 minimum[plane_id] = skl_ddb_min_alloc(pstate, 0);
4027 y_minimum[plane_id] = skl_ddb_min_alloc(pstate, 1);
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004028 }
4029
4030 minimum[PLANE_CURSOR] = skl_cursor_allocation(num_active);
4031}
4032
Matt Roperc107acf2016-05-12 07:06:01 -07004033static int
Matt Roper024c9042015-09-24 15:53:11 -07004034skl_allocate_pipe_ddb(struct intel_crtc_state *cstate,
Damien Lespiaub9cec072014-11-04 17:06:43 +00004035 struct skl_ddb_allocation *ddb /* out */)
4036{
Matt Roperc107acf2016-05-12 07:06:01 -07004037 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07004038 struct drm_crtc *crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004039 struct drm_device *dev = crtc->dev;
4040 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4041 enum pipe pipe = intel_crtc->pipe;
Lyudece0ba282016-09-15 10:46:35 -04004042 struct skl_ddb_entry *alloc = &cstate->wm.skl.ddb;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004043 uint16_t alloc_size, start;
Maarten Lankhorstfefdd812016-10-26 15:41:33 +02004044 uint16_t minimum[I915_MAX_PLANES] = {};
4045 uint16_t y_minimum[I915_MAX_PLANES] = {};
Damien Lespiaub9cec072014-11-04 17:06:43 +00004046 unsigned int total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004047 enum plane_id plane_id;
Matt Roperc107acf2016-05-12 07:06:01 -07004048 int num_active;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004049 unsigned plane_data_rate[I915_MAX_PLANES] = {};
4050 unsigned plane_y_data_rate[I915_MAX_PLANES] = {};
Damien Lespiaub9cec072014-11-04 17:06:43 +00004051
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004052 /* Clear the partitioning for disabled planes. */
4053 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
4054 memset(ddb->y_plane[pipe], 0, sizeof(ddb->y_plane[pipe]));
4055
Matt Ropera6d3460e2016-05-12 07:06:04 -07004056 if (WARN_ON(!state))
4057 return 0;
4058
Matt Roperc107acf2016-05-12 07:06:01 -07004059 if (!cstate->base.active) {
Lyudece0ba282016-09-15 10:46:35 -04004060 alloc->start = alloc->end = 0;
Matt Roperc107acf2016-05-12 07:06:01 -07004061 return 0;
4062 }
4063
Matt Ropera6d3460e2016-05-12 07:06:04 -07004064 skl_ddb_get_pipe_allocation_limits(dev, cstate, alloc, &num_active);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00004065 alloc_size = skl_ddb_entry_size(alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004066 if (alloc_size == 0) {
4067 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
Matt Roperc107acf2016-05-12 07:06:01 -07004068 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004069 }
4070
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004071 skl_ddb_calc_min(cstate, num_active, minimum, y_minimum);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004072
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004073 /*
4074 * 1. Allocate the mininum required blocks for each active plane
4075 * and allocate the cursor, it doesn't require extra allocation
4076 * proportional to the data rate.
4077 */
Damien Lespiaub9cec072014-11-04 17:06:43 +00004078
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004079 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
4080 alloc_size -= minimum[plane_id];
4081 alloc_size -= y_minimum[plane_id];
Damien Lespiau80958152015-02-09 13:35:10 +00004082 }
4083
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004084 ddb->plane[pipe][PLANE_CURSOR].start = alloc->end - minimum[PLANE_CURSOR];
4085 ddb->plane[pipe][PLANE_CURSOR].end = alloc->end;
4086
Damien Lespiaub9cec072014-11-04 17:06:43 +00004087 /*
Damien Lespiau80958152015-02-09 13:35:10 +00004088 * 2. Distribute the remaining space in proportion to the amount of
4089 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00004090 *
4091 * FIXME: we may not allocate every single block here.
4092 */
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004093 total_data_rate = skl_get_total_relative_data_rate(cstate,
4094 plane_data_rate,
4095 plane_y_data_rate);
Matt Ropera1de91e2016-05-12 07:05:57 -07004096 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004097 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004098
Damien Lespiau34bb56a2014-11-04 17:07:01 +00004099 start = alloc->start;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004100 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004101 unsigned int data_rate, y_data_rate;
4102 uint16_t plane_blocks, y_plane_blocks = 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004103
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004104 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004105 continue;
4106
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004107 data_rate = plane_data_rate[plane_id];
Damien Lespiaub9cec072014-11-04 17:06:43 +00004108
4109 /*
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004110 * allocation for (packed formats) or (uv-plane part of planar format):
Damien Lespiaub9cec072014-11-04 17:06:43 +00004111 * promote the expression to 64 bits to avoid overflowing, the
4112 * result is < available as data_rate / total_data_rate < 1
4113 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004114 plane_blocks = minimum[plane_id];
Damien Lespiau80958152015-02-09 13:35:10 +00004115 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
4116 total_data_rate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004117
Matt Roperc107acf2016-05-12 07:06:01 -07004118 /* Leave disabled planes at (0,0) */
4119 if (data_rate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004120 ddb->plane[pipe][plane_id].start = start;
4121 ddb->plane[pipe][plane_id].end = start + plane_blocks;
Matt Roperc107acf2016-05-12 07:06:01 -07004122 }
Damien Lespiaub9cec072014-11-04 17:06:43 +00004123
4124 start += plane_blocks;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004125
4126 /*
4127 * allocation for y_plane part of planar format:
4128 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004129 y_data_rate = plane_y_data_rate[plane_id];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004130
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004131 y_plane_blocks = y_minimum[plane_id];
Matt Ropera1de91e2016-05-12 07:05:57 -07004132 y_plane_blocks += div_u64((uint64_t)alloc_size * y_data_rate,
4133 total_data_rate);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004134
Matt Roperc107acf2016-05-12 07:06:01 -07004135 if (y_data_rate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004136 ddb->y_plane[pipe][plane_id].start = start;
4137 ddb->y_plane[pipe][plane_id].end = start + y_plane_blocks;
Matt Roperc107acf2016-05-12 07:06:01 -07004138 }
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004139
Matt Ropera1de91e2016-05-12 07:05:57 -07004140 start += y_plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004141 }
4142
Matt Roperc107acf2016-05-12 07:06:01 -07004143 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004144}
4145
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004146/*
4147 * 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 +02004148 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004149 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
4150 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
4151*/
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304152static uint_fixed_16_16_t skl_wm_method1(uint32_t pixel_rate, uint8_t cpp,
4153 uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004154{
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304155 uint32_t wm_intermediate_val;
4156 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004157
4158 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304159 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004160
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304161 wm_intermediate_val = latency * pixel_rate * cpp;
4162 ret = fixed_16_16_div_round_up_u64(wm_intermediate_val, 1000 * 512);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004163 return ret;
4164}
4165
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304166static uint_fixed_16_16_t skl_wm_method2(uint32_t pixel_rate,
4167 uint32_t pipe_htotal,
4168 uint32_t latency,
4169 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004170{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004171 uint32_t wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304172 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004173
4174 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304175 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004176
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004177 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304178 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
4179 pipe_htotal * 1000);
4180 ret = mul_u32_fixed_16_16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004181 return ret;
4182}
4183
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004184static uint32_t skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *cstate,
4185 struct intel_plane_state *pstate)
4186{
4187 uint64_t adjusted_pixel_rate;
4188 uint64_t downscale_amount;
4189 uint64_t pixel_rate;
4190
4191 /* Shouldn't reach here on disabled planes... */
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004192 if (WARN_ON(!intel_wm_plane_visible(cstate, pstate)))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004193 return 0;
4194
4195 /*
4196 * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
4197 * with additional adjustments for plane-specific scaling.
4198 */
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02004199 adjusted_pixel_rate = cstate->pixel_rate;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004200 downscale_amount = skl_plane_downscale_amount(cstate, pstate);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004201
4202 pixel_rate = adjusted_pixel_rate * downscale_amount >> 16;
4203 WARN_ON(pixel_rate != clamp_t(uint32_t, pixel_rate, 0, ~0));
4204
4205 return pixel_rate;
4206}
4207
Matt Roper55994c22016-05-12 07:06:08 -07004208static int skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
4209 struct intel_crtc_state *cstate,
4210 struct intel_plane_state *intel_pstate,
4211 uint16_t ddb_allocation,
4212 int level,
4213 uint16_t *out_blocks, /* out */
4214 uint8_t *out_lines, /* out */
4215 bool *enabled /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004216{
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004217 struct intel_plane *plane = to_intel_plane(intel_pstate->base.plane);
Matt Roper33815fa2016-05-12 07:06:05 -07004218 struct drm_plane_state *pstate = &intel_pstate->base;
4219 struct drm_framebuffer *fb = pstate->fb;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004220 uint32_t latency = dev_priv->wm.skl_latency[level];
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304221 uint_fixed_16_16_t method1, method2;
4222 uint_fixed_16_16_t plane_blocks_per_line;
4223 uint_fixed_16_16_t selected_result;
4224 uint32_t interm_pbpl;
4225 uint32_t plane_bytes_per_line;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004226 uint32_t res_blocks, res_lines;
Ville Syrjäläac484962016-01-20 21:05:26 +02004227 uint8_t cpp;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004228 uint32_t width = 0, height = 0;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004229 uint32_t plane_pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304230 uint_fixed_16_16_t y_tile_minimum;
4231 uint32_t y_min_scanlines;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03004232 struct intel_atomic_state *state =
4233 to_intel_atomic_state(cstate->base.state);
4234 bool apply_memory_bw_wa = skl_needs_memory_bw_wa(state);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304235 bool y_tiled, x_tiled;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004236
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004237 if (latency == 0 ||
4238 !intel_wm_plane_visible(cstate, intel_pstate)) {
Matt Roper55994c22016-05-12 07:06:08 -07004239 *enabled = false;
4240 return 0;
4241 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004242
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304243 y_tiled = fb->modifier == I915_FORMAT_MOD_Y_TILED ||
4244 fb->modifier == I915_FORMAT_MOD_Yf_TILED;
4245 x_tiled = fb->modifier == I915_FORMAT_MOD_X_TILED;
4246
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05304247 /* Display WA #1141: kbl. */
4248 if (IS_KABYLAKE(dev_priv) && dev_priv->ipc_enabled)
4249 latency += 4;
4250
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304251 if (apply_memory_bw_wa && x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03004252 latency += 15;
4253
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004254 if (plane->id == PLANE_CURSOR) {
4255 width = intel_pstate->base.crtc_w;
4256 height = intel_pstate->base.crtc_h;
4257 } else {
4258 width = drm_rect_width(&intel_pstate->base.src) >> 16;
4259 height = drm_rect_height(&intel_pstate->base.src) >> 16;
4260 }
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004261
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03004262 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004263 swap(width, height);
4264
Ville Syrjälä353c8592016-12-14 23:30:57 +02004265 cpp = fb->format->cpp[0];
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004266 plane_pixel_rate = skl_adjusted_plane_pixel_rate(cstate, intel_pstate);
4267
Dave Airlie61d0a042016-10-25 16:35:20 +10004268 if (drm_rotation_90_or_270(pstate->rotation)) {
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004269 int cpp = (fb->format->format == DRM_FORMAT_NV12) ?
Ville Syrjälä353c8592016-12-14 23:30:57 +02004270 fb->format->cpp[1] :
4271 fb->format->cpp[0];
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004272
4273 switch (cpp) {
4274 case 1:
4275 y_min_scanlines = 16;
4276 break;
4277 case 2:
4278 y_min_scanlines = 8;
4279 break;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004280 case 4:
4281 y_min_scanlines = 4;
4282 break;
Paulo Zanoni86a462b2016-09-22 18:00:35 -03004283 default:
4284 MISSING_CASE(cpp);
4285 return -EINVAL;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004286 }
4287 } else {
4288 y_min_scanlines = 4;
4289 }
4290
Paulo Zanoni2ef32de2016-11-08 18:22:11 -02004291 if (apply_memory_bw_wa)
4292 y_min_scanlines *= 2;
4293
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03004294 plane_bytes_per_line = width * cpp;
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304295 if (y_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304296 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line *
4297 y_min_scanlines, 512);
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03004298 plane_blocks_per_line =
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304299 fixed_16_16_div_round_up(interm_pbpl, y_min_scanlines);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304300 } else if (x_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304301 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line, 512);
4302 plane_blocks_per_line = u32_to_fixed_16_16(interm_pbpl);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304303 } else {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304304 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line, 512) + 1;
4305 plane_blocks_per_line = u32_to_fixed_16_16(interm_pbpl);
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03004306 }
4307
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004308 method1 = skl_wm_method1(plane_pixel_rate, cpp, latency);
4309 method2 = skl_wm_method2(plane_pixel_rate,
Matt Roper024c9042015-09-24 15:53:11 -07004310 cstate->base.adjusted_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004311 latency,
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03004312 plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004313
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304314 y_tile_minimum = mul_u32_fixed_16_16(y_min_scanlines,
4315 plane_blocks_per_line);
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004316
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304317 if (y_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304318 selected_result = max_fixed_16_16(method2, y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004319 } else {
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03004320 if ((cpp * cstate->base.adjusted_mode.crtc_htotal / 512 < 1) &&
4321 (plane_bytes_per_line / 512 < 1))
4322 selected_result = method2;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304323 else if ((ddb_allocation /
4324 fixed_16_16_to_u32_round_up(plane_blocks_per_line)) >= 1)
4325 selected_result = min_fixed_16_16(method1, method2);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004326 else
4327 selected_result = method1;
4328 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004329
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304330 res_blocks = fixed_16_16_to_u32_round_up(selected_result) + 1;
4331 res_lines = DIV_ROUND_UP(selected_result.val,
4332 plane_blocks_per_line.val);
Damien Lespiaue6d66172014-11-04 17:06:55 +00004333
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004334 if (level >= 1 && level <= 7) {
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304335 if (y_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304336 res_blocks += fixed_16_16_to_u32_round_up(y_tile_minimum);
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004337 res_lines += y_min_scanlines;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004338 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004339 res_blocks++;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004340 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004341 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004342
Matt Roper55994c22016-05-12 07:06:08 -07004343 if (res_blocks >= ddb_allocation || res_lines > 31) {
4344 *enabled = false;
Matt Roper6b6bada2016-05-12 07:06:10 -07004345
4346 /*
4347 * If there are no valid level 0 watermarks, then we can't
4348 * support this display configuration.
4349 */
4350 if (level) {
4351 return 0;
4352 } else {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004353 struct drm_plane *plane = pstate->plane;
Matt Roper6b6bada2016-05-12 07:06:10 -07004354
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004355 DRM_DEBUG_KMS("Requested display configuration exceeds system watermark limitations\n");
4356 DRM_DEBUG_KMS("[PLANE:%d:%s] blocks required = %u/%u, lines required = %u/31\n",
4357 plane->base.id, plane->name,
4358 res_blocks, ddb_allocation, res_lines);
Matt Roper6b6bada2016-05-12 07:06:10 -07004359 return -EINVAL;
4360 }
Matt Roper55994c22016-05-12 07:06:08 -07004361 }
Damien Lespiaue6d66172014-11-04 17:06:55 +00004362
4363 *out_blocks = res_blocks;
4364 *out_lines = res_lines;
Matt Roper55994c22016-05-12 07:06:08 -07004365 *enabled = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004366
Matt Roper55994c22016-05-12 07:06:08 -07004367 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004368}
4369
Matt Roperf4a96752016-05-12 07:06:06 -07004370static int
4371skl_compute_wm_level(const struct drm_i915_private *dev_priv,
4372 struct skl_ddb_allocation *ddb,
4373 struct intel_crtc_state *cstate,
Lyudea62163e2016-10-04 14:28:20 -04004374 struct intel_plane *intel_plane,
Matt Roperf4a96752016-05-12 07:06:06 -07004375 int level,
4376 struct skl_wm_level *result)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004377{
Matt Roperf4a96752016-05-12 07:06:06 -07004378 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07004379 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Lyudea62163e2016-10-04 14:28:20 -04004380 struct drm_plane *plane = &intel_plane->base;
4381 struct intel_plane_state *intel_pstate = NULL;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004382 uint16_t ddb_blocks;
Matt Roper024c9042015-09-24 15:53:11 -07004383 enum pipe pipe = intel_crtc->pipe;
Matt Roper55994c22016-05-12 07:06:08 -07004384 int ret;
Lyudea62163e2016-10-04 14:28:20 -04004385
4386 if (state)
4387 intel_pstate =
4388 intel_atomic_get_existing_plane_state(state,
4389 intel_plane);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004390
Matt Roperf4a96752016-05-12 07:06:06 -07004391 /*
Lyudea62163e2016-10-04 14:28:20 -04004392 * Note: If we start supporting multiple pending atomic commits against
4393 * the same planes/CRTC's in the future, plane->state will no longer be
4394 * the correct pre-state to use for the calculations here and we'll
4395 * need to change where we get the 'unchanged' plane data from.
4396 *
4397 * For now this is fine because we only allow one queued commit against
4398 * a CRTC. Even if the plane isn't modified by this transaction and we
4399 * don't have a plane lock, we still have the CRTC's lock, so we know
4400 * that no other transactions are racing with us to update it.
Matt Roperf4a96752016-05-12 07:06:06 -07004401 */
Lyudea62163e2016-10-04 14:28:20 -04004402 if (!intel_pstate)
4403 intel_pstate = to_intel_plane_state(plane->state);
Matt Roperf4a96752016-05-12 07:06:06 -07004404
Lyudea62163e2016-10-04 14:28:20 -04004405 WARN_ON(!intel_pstate->base.fb);
Matt Roper024c9042015-09-24 15:53:11 -07004406
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004407 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][intel_plane->id]);
Matt Roperf4a96752016-05-12 07:06:06 -07004408
Lyudea62163e2016-10-04 14:28:20 -04004409 ret = skl_compute_plane_wm(dev_priv,
4410 cstate,
4411 intel_pstate,
4412 ddb_blocks,
4413 level,
4414 &result->plane_res_b,
4415 &result->plane_res_l,
4416 &result->plane_en);
4417 if (ret)
4418 return ret;
Matt Roperf4a96752016-05-12 07:06:06 -07004419
4420 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004421}
4422
Damien Lespiau407b50f2014-11-04 17:06:57 +00004423static uint32_t
Matt Roper024c9042015-09-24 15:53:11 -07004424skl_compute_linetime_wm(struct intel_crtc_state *cstate)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004425{
Mahesh Kumara3a89862016-12-01 21:19:34 +05304426 struct drm_atomic_state *state = cstate->base.state;
4427 struct drm_i915_private *dev_priv = to_i915(state->dev);
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03004428 uint32_t pixel_rate;
Mahesh Kumara3a89862016-12-01 21:19:34 +05304429 uint32_t linetime_wm;
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03004430
Matt Roper024c9042015-09-24 15:53:11 -07004431 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004432 return 0;
4433
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02004434 pixel_rate = cstate->pixel_rate;
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03004435
4436 if (WARN_ON(pixel_rate == 0))
Mika Kuoppala661abfc2015-07-16 19:36:51 +03004437 return 0;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004438
Mahesh Kumara3a89862016-12-01 21:19:34 +05304439 linetime_wm = DIV_ROUND_UP(8 * cstate->base.adjusted_mode.crtc_htotal *
4440 1000, pixel_rate);
4441
4442 /* Display WA #1135: bxt. */
4443 if (IS_BROXTON(dev_priv) && dev_priv->ipc_enabled)
4444 linetime_wm = DIV_ROUND_UP(linetime_wm, 2);
4445
4446 return linetime_wm;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004447}
4448
Matt Roper024c9042015-09-24 15:53:11 -07004449static void skl_compute_transition_wm(struct intel_crtc_state *cstate,
Damien Lespiau9414f562014-11-04 17:06:58 +00004450 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004451{
Matt Roper024c9042015-09-24 15:53:11 -07004452 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004453 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00004454
4455 /* Until we know more, just disable transition WMs */
Lyudea62163e2016-10-04 14:28:20 -04004456 trans_wm->plane_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004457}
4458
Matt Roper55994c22016-05-12 07:06:08 -07004459static int skl_build_pipe_wm(struct intel_crtc_state *cstate,
4460 struct skl_ddb_allocation *ddb,
4461 struct skl_pipe_wm *pipe_wm)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004462{
Matt Roper024c9042015-09-24 15:53:11 -07004463 struct drm_device *dev = cstate->base.crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004464 const struct drm_i915_private *dev_priv = to_i915(dev);
Lyudea62163e2016-10-04 14:28:20 -04004465 struct intel_plane *intel_plane;
4466 struct skl_plane_wm *wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004467 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Roper55994c22016-05-12 07:06:08 -07004468 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004469
Lyudea62163e2016-10-04 14:28:20 -04004470 /*
4471 * We'll only calculate watermarks for planes that are actually
4472 * enabled, so make sure all other planes are set as disabled.
4473 */
4474 memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
4475
4476 for_each_intel_plane_mask(&dev_priv->drm,
4477 intel_plane,
4478 cstate->base.plane_mask) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004479 wm = &pipe_wm->planes[intel_plane->id];
Lyudea62163e2016-10-04 14:28:20 -04004480
4481 for (level = 0; level <= max_level; level++) {
4482 ret = skl_compute_wm_level(dev_priv, ddb, cstate,
4483 intel_plane, level,
4484 &wm->wm[level]);
4485 if (ret)
4486 return ret;
4487 }
4488 skl_compute_transition_wm(cstate, &wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004489 }
Matt Roper024c9042015-09-24 15:53:11 -07004490 pipe_wm->linetime = skl_compute_linetime_wm(cstate);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004491
Matt Roper55994c22016-05-12 07:06:08 -07004492 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004493}
4494
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004495static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
4496 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00004497 const struct skl_ddb_entry *entry)
4498{
4499 if (entry->end)
4500 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
4501 else
4502 I915_WRITE(reg, 0);
4503}
4504
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004505static void skl_write_wm_level(struct drm_i915_private *dev_priv,
4506 i915_reg_t reg,
4507 const struct skl_wm_level *level)
4508{
4509 uint32_t val = 0;
4510
4511 if (level->plane_en) {
4512 val |= PLANE_WM_EN;
4513 val |= level->plane_res_b;
4514 val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
4515 }
4516
4517 I915_WRITE(reg, val);
4518}
4519
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004520static void skl_write_plane_wm(struct intel_crtc *intel_crtc,
4521 const struct skl_plane_wm *wm,
4522 const struct skl_ddb_allocation *ddb,
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004523 enum plane_id plane_id)
Lyude62e0fb82016-08-22 12:50:08 -04004524{
4525 struct drm_crtc *crtc = &intel_crtc->base;
4526 struct drm_device *dev = crtc->dev;
4527 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004528 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004529 enum pipe pipe = intel_crtc->pipe;
4530
4531 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004532 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004533 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004534 }
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004535 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004536 &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004537
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004538 skl_ddb_entry_write(dev_priv, PLANE_BUF_CFG(pipe, plane_id),
4539 &ddb->plane[pipe][plane_id]);
4540 skl_ddb_entry_write(dev_priv, PLANE_NV12_BUF_CFG(pipe, plane_id),
4541 &ddb->y_plane[pipe][plane_id]);
Lyude62e0fb82016-08-22 12:50:08 -04004542}
4543
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004544static void skl_write_cursor_wm(struct intel_crtc *intel_crtc,
4545 const struct skl_plane_wm *wm,
4546 const struct skl_ddb_allocation *ddb)
Lyude62e0fb82016-08-22 12:50:08 -04004547{
4548 struct drm_crtc *crtc = &intel_crtc->base;
4549 struct drm_device *dev = crtc->dev;
4550 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004551 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004552 enum pipe pipe = intel_crtc->pipe;
4553
4554 for (level = 0; level <= max_level; level++) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004555 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
4556 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004557 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004558 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004559
4560 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004561 &ddb->plane[pipe][PLANE_CURSOR]);
Lyude62e0fb82016-08-22 12:50:08 -04004562}
4563
cpaul@redhat.com45ece232016-10-14 17:31:56 -04004564bool skl_wm_level_equals(const struct skl_wm_level *l1,
4565 const struct skl_wm_level *l2)
4566{
4567 if (l1->plane_en != l2->plane_en)
4568 return false;
4569
4570 /* If both planes aren't enabled, the rest shouldn't matter */
4571 if (!l1->plane_en)
4572 return true;
4573
4574 return (l1->plane_res_l == l2->plane_res_l &&
4575 l1->plane_res_b == l2->plane_res_b);
4576}
4577
Lyude27082492016-08-24 07:48:10 +02004578static inline bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
4579 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004580{
Lyude27082492016-08-24 07:48:10 +02004581 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004582}
4583
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01004584bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry **entries,
4585 const struct skl_ddb_entry *ddb,
4586 int ignore)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004587{
Lyudece0ba282016-09-15 10:46:35 -04004588 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004589
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01004590 for (i = 0; i < I915_MAX_PIPES; i++)
4591 if (i != ignore && entries[i] &&
4592 skl_ddb_entries_overlap(ddb, entries[i]))
Lyude27082492016-08-24 07:48:10 +02004593 return true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004594
Lyude27082492016-08-24 07:48:10 +02004595 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004596}
4597
Matt Roper55994c22016-05-12 07:06:08 -07004598static int skl_update_pipe_wm(struct drm_crtc_state *cstate,
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004599 const struct skl_pipe_wm *old_pipe_wm,
Matt Roper55994c22016-05-12 07:06:08 -07004600 struct skl_pipe_wm *pipe_wm, /* out */
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004601 struct skl_ddb_allocation *ddb, /* out */
Matt Roper55994c22016-05-12 07:06:08 -07004602 bool *changed /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004603{
Matt Roperf4a96752016-05-12 07:06:06 -07004604 struct intel_crtc_state *intel_cstate = to_intel_crtc_state(cstate);
Matt Roper55994c22016-05-12 07:06:08 -07004605 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004606
Matt Roper55994c22016-05-12 07:06:08 -07004607 ret = skl_build_pipe_wm(intel_cstate, ddb, pipe_wm);
4608 if (ret)
4609 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004610
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004611 if (!memcmp(old_pipe_wm, pipe_wm, sizeof(*pipe_wm)))
Matt Roper55994c22016-05-12 07:06:08 -07004612 *changed = false;
4613 else
4614 *changed = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004615
Matt Roper55994c22016-05-12 07:06:08 -07004616 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004617}
4618
Matt Roper9b613022016-06-27 16:42:44 -07004619static uint32_t
4620pipes_modified(struct drm_atomic_state *state)
4621{
4622 struct drm_crtc *crtc;
4623 struct drm_crtc_state *cstate;
4624 uint32_t i, ret = 0;
4625
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004626 for_each_new_crtc_in_state(state, crtc, cstate, i)
Matt Roper9b613022016-06-27 16:42:44 -07004627 ret |= drm_crtc_mask(crtc);
4628
4629 return ret;
4630}
4631
Jani Nikulabb7791b2016-10-04 12:29:17 +03004632static int
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004633skl_ddb_add_affected_planes(struct intel_crtc_state *cstate)
4634{
4635 struct drm_atomic_state *state = cstate->base.state;
4636 struct drm_device *dev = state->dev;
4637 struct drm_crtc *crtc = cstate->base.crtc;
4638 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4639 struct drm_i915_private *dev_priv = to_i915(dev);
4640 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4641 struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
4642 struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
4643 struct drm_plane_state *plane_state;
4644 struct drm_plane *plane;
4645 enum pipe pipe = intel_crtc->pipe;
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004646
4647 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
4648
Maarten Lankhorst220b0962016-10-26 15:41:30 +02004649 drm_for_each_plane_mask(plane, dev, cstate->base.plane_mask) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004650 enum plane_id plane_id = to_intel_plane(plane)->id;
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004651
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004652 if (skl_ddb_entry_equal(&cur_ddb->plane[pipe][plane_id],
4653 &new_ddb->plane[pipe][plane_id]) &&
4654 skl_ddb_entry_equal(&cur_ddb->y_plane[pipe][plane_id],
4655 &new_ddb->y_plane[pipe][plane_id]))
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004656 continue;
4657
4658 plane_state = drm_atomic_get_plane_state(state, plane);
4659 if (IS_ERR(plane_state))
4660 return PTR_ERR(plane_state);
4661 }
4662
4663 return 0;
4664}
4665
Matt Roper98d39492016-05-12 07:06:03 -07004666static int
4667skl_compute_ddb(struct drm_atomic_state *state)
4668{
4669 struct drm_device *dev = state->dev;
4670 struct drm_i915_private *dev_priv = to_i915(dev);
4671 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4672 struct intel_crtc *intel_crtc;
Matt Roper734fa012016-05-12 15:11:40 -07004673 struct skl_ddb_allocation *ddb = &intel_state->wm_results.ddb;
Matt Roper9b613022016-06-27 16:42:44 -07004674 uint32_t realloc_pipes = pipes_modified(state);
Matt Roper98d39492016-05-12 07:06:03 -07004675 int ret;
4676
4677 /*
4678 * If this is our first atomic update following hardware readout,
4679 * we can't trust the DDB that the BIOS programmed for us. Let's
4680 * pretend that all pipes switched active status so that we'll
4681 * ensure a full DDB recompute.
4682 */
Matt Roper1b54a882016-06-17 13:42:18 -07004683 if (dev_priv->wm.distrust_bios_wm) {
4684 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
4685 state->acquire_ctx);
4686 if (ret)
4687 return ret;
4688
Matt Roper98d39492016-05-12 07:06:03 -07004689 intel_state->active_pipe_changes = ~0;
4690
Matt Roper1b54a882016-06-17 13:42:18 -07004691 /*
4692 * We usually only initialize intel_state->active_crtcs if we
4693 * we're doing a modeset; make sure this field is always
4694 * initialized during the sanitization process that happens
4695 * on the first commit too.
4696 */
4697 if (!intel_state->modeset)
4698 intel_state->active_crtcs = dev_priv->active_crtcs;
4699 }
4700
Matt Roper98d39492016-05-12 07:06:03 -07004701 /*
4702 * If the modeset changes which CRTC's are active, we need to
4703 * recompute the DDB allocation for *all* active pipes, even
4704 * those that weren't otherwise being modified in any way by this
4705 * atomic commit. Due to the shrinking of the per-pipe allocations
4706 * when new active CRTC's are added, it's possible for a pipe that
4707 * we were already using and aren't changing at all here to suddenly
4708 * become invalid if its DDB needs exceeds its new allocation.
4709 *
4710 * Note that if we wind up doing a full DDB recompute, we can't let
4711 * any other display updates race with this transaction, so we need
4712 * to grab the lock on *all* CRTC's.
4713 */
Matt Roper734fa012016-05-12 15:11:40 -07004714 if (intel_state->active_pipe_changes) {
Matt Roper98d39492016-05-12 07:06:03 -07004715 realloc_pipes = ~0;
Matt Roper734fa012016-05-12 15:11:40 -07004716 intel_state->wm_results.dirty_pipes = ~0;
4717 }
Matt Roper98d39492016-05-12 07:06:03 -07004718
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004719 /*
4720 * We're not recomputing for the pipes not included in the commit, so
4721 * make sure we start with the current state.
4722 */
4723 memcpy(ddb, &dev_priv->wm.skl_hw.ddb, sizeof(*ddb));
4724
Matt Roper98d39492016-05-12 07:06:03 -07004725 for_each_intel_crtc_mask(dev, intel_crtc, realloc_pipes) {
4726 struct intel_crtc_state *cstate;
4727
4728 cstate = intel_atomic_get_crtc_state(state, intel_crtc);
4729 if (IS_ERR(cstate))
4730 return PTR_ERR(cstate);
4731
Matt Roper734fa012016-05-12 15:11:40 -07004732 ret = skl_allocate_pipe_ddb(cstate, ddb);
Matt Roper98d39492016-05-12 07:06:03 -07004733 if (ret)
4734 return ret;
Lyude05a76d32016-08-17 15:55:57 -04004735
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004736 ret = skl_ddb_add_affected_planes(cstate);
Lyude05a76d32016-08-17 15:55:57 -04004737 if (ret)
4738 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07004739 }
4740
4741 return 0;
4742}
4743
Matt Roper2722efb2016-08-17 15:55:55 -04004744static void
4745skl_copy_wm_for_pipe(struct skl_wm_values *dst,
4746 struct skl_wm_values *src,
4747 enum pipe pipe)
4748{
Matt Roper2722efb2016-08-17 15:55:55 -04004749 memcpy(dst->ddb.y_plane[pipe], src->ddb.y_plane[pipe],
4750 sizeof(dst->ddb.y_plane[pipe]));
4751 memcpy(dst->ddb.plane[pipe], src->ddb.plane[pipe],
4752 sizeof(dst->ddb.plane[pipe]));
4753}
4754
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004755static void
4756skl_print_wm_changes(const struct drm_atomic_state *state)
4757{
4758 const struct drm_device *dev = state->dev;
4759 const struct drm_i915_private *dev_priv = to_i915(dev);
4760 const struct intel_atomic_state *intel_state =
4761 to_intel_atomic_state(state);
4762 const struct drm_crtc *crtc;
4763 const struct drm_crtc_state *cstate;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004764 const struct intel_plane *intel_plane;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004765 const struct skl_ddb_allocation *old_ddb = &dev_priv->wm.skl_hw.ddb;
4766 const struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
Maarten Lankhorst75704982016-11-01 12:04:10 +01004767 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004768
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004769 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst75704982016-11-01 12:04:10 +01004770 const struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4771 enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004772
Maarten Lankhorst75704982016-11-01 12:04:10 +01004773 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004774 enum plane_id plane_id = intel_plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004775 const struct skl_ddb_entry *old, *new;
4776
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004777 old = &old_ddb->plane[pipe][plane_id];
4778 new = &new_ddb->plane[pipe][plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004779
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004780 if (skl_ddb_entry_equal(old, new))
4781 continue;
4782
Maarten Lankhorst75704982016-11-01 12:04:10 +01004783 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] ddb (%d - %d) -> (%d - %d)\n",
4784 intel_plane->base.base.id,
4785 intel_plane->base.name,
4786 old->start, old->end,
4787 new->start, new->end);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004788 }
4789 }
4790}
4791
Matt Roper98d39492016-05-12 07:06:03 -07004792static int
4793skl_compute_wm(struct drm_atomic_state *state)
4794{
4795 struct drm_crtc *crtc;
4796 struct drm_crtc_state *cstate;
Matt Roper734fa012016-05-12 15:11:40 -07004797 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4798 struct skl_wm_values *results = &intel_state->wm_results;
4799 struct skl_pipe_wm *pipe_wm;
Matt Roper98d39492016-05-12 07:06:03 -07004800 bool changed = false;
Matt Roper734fa012016-05-12 15:11:40 -07004801 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07004802
4803 /*
4804 * If this transaction isn't actually touching any CRTC's, don't
4805 * bother with watermark calculation. Note that if we pass this
4806 * test, we're guaranteed to hold at least one CRTC state mutex,
4807 * which means we can safely use values like dev_priv->active_crtcs
4808 * since any racing commits that want to update them would need to
4809 * hold _all_ CRTC state mutexes.
4810 */
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004811 for_each_new_crtc_in_state(state, crtc, cstate, i)
Matt Roper98d39492016-05-12 07:06:03 -07004812 changed = true;
4813 if (!changed)
4814 return 0;
4815
Matt Roper734fa012016-05-12 15:11:40 -07004816 /* Clear all dirty flags */
4817 results->dirty_pipes = 0;
4818
Matt Roper98d39492016-05-12 07:06:03 -07004819 ret = skl_compute_ddb(state);
4820 if (ret)
4821 return ret;
4822
Matt Roper734fa012016-05-12 15:11:40 -07004823 /*
4824 * Calculate WM's for all pipes that are part of this transaction.
4825 * Note that the DDB allocation above may have added more CRTC's that
4826 * weren't otherwise being modified (and set bits in dirty_pipes) if
4827 * pipe allocations had to change.
4828 *
4829 * FIXME: Now that we're doing this in the atomic check phase, we
4830 * should allow skl_update_pipe_wm() to return failure in cases where
4831 * no suitable watermark values can be found.
4832 */
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004833 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Matt Roper734fa012016-05-12 15:11:40 -07004834 struct intel_crtc_state *intel_cstate =
4835 to_intel_crtc_state(cstate);
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004836 const struct skl_pipe_wm *old_pipe_wm =
4837 &to_intel_crtc_state(crtc->state)->wm.skl.optimal;
Matt Roper734fa012016-05-12 15:11:40 -07004838
4839 pipe_wm = &intel_cstate->wm.skl.optimal;
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004840 ret = skl_update_pipe_wm(cstate, old_pipe_wm, pipe_wm,
4841 &results->ddb, &changed);
Matt Roper734fa012016-05-12 15:11:40 -07004842 if (ret)
4843 return ret;
4844
4845 if (changed)
4846 results->dirty_pipes |= drm_crtc_mask(crtc);
4847
4848 if ((results->dirty_pipes & drm_crtc_mask(crtc)) == 0)
4849 /* This pipe's WM's did not change */
4850 continue;
4851
4852 intel_cstate->update_wm_pre = true;
Matt Roper734fa012016-05-12 15:11:40 -07004853 }
4854
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004855 skl_print_wm_changes(state);
4856
Matt Roper98d39492016-05-12 07:06:03 -07004857 return 0;
4858}
4859
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004860static void skl_atomic_update_crtc_wm(struct intel_atomic_state *state,
4861 struct intel_crtc_state *cstate)
4862{
4863 struct intel_crtc *crtc = to_intel_crtc(cstate->base.crtc);
4864 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
4865 struct skl_pipe_wm *pipe_wm = &cstate->wm.skl.optimal;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004866 const struct skl_ddb_allocation *ddb = &state->wm_results.ddb;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004867 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004868 enum plane_id plane_id;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004869
4870 if (!(state->wm_results.dirty_pipes & drm_crtc_mask(&crtc->base)))
4871 return;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004872
4873 I915_WRITE(PIPE_WM_LINETIME(pipe), pipe_wm->linetime);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004874
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004875 for_each_plane_id_on_crtc(crtc, plane_id) {
4876 if (plane_id != PLANE_CURSOR)
4877 skl_write_plane_wm(crtc, &pipe_wm->planes[plane_id],
4878 ddb, plane_id);
4879 else
4880 skl_write_cursor_wm(crtc, &pipe_wm->planes[plane_id],
4881 ddb);
4882 }
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004883}
4884
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004885static void skl_initial_wm(struct intel_atomic_state *state,
4886 struct intel_crtc_state *cstate)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004887{
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004888 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02004889 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004890 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004891 struct skl_wm_values *results = &state->wm_results;
Matt Roper2722efb2016-08-17 15:55:55 -04004892 struct skl_wm_values *hw_vals = &dev_priv->wm.skl_hw;
Lyude27082492016-08-24 07:48:10 +02004893 enum pipe pipe = intel_crtc->pipe;
Bob Paauweadda50b2015-07-21 10:42:53 -07004894
Ville Syrjälä432081b2016-10-31 22:37:03 +02004895 if ((results->dirty_pipes & drm_crtc_mask(&intel_crtc->base)) == 0)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004896 return;
4897
Matt Roper734fa012016-05-12 15:11:40 -07004898 mutex_lock(&dev_priv->wm.wm_mutex);
4899
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004900 if (cstate->base.active_changed)
4901 skl_atomic_update_crtc_wm(state, cstate);
Lyude27082492016-08-24 07:48:10 +02004902
4903 skl_copy_wm_for_pipe(hw_vals, results, pipe);
Matt Roper734fa012016-05-12 15:11:40 -07004904
4905 mutex_unlock(&dev_priv->wm.wm_mutex);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004906}
4907
Ville Syrjäläd8905652016-01-14 14:53:35 +02004908static void ilk_compute_wm_config(struct drm_device *dev,
4909 struct intel_wm_config *config)
4910{
4911 struct intel_crtc *crtc;
4912
4913 /* Compute the currently _active_ config */
4914 for_each_intel_crtc(dev, crtc) {
4915 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
4916
4917 if (!wm->pipe_enabled)
4918 continue;
4919
4920 config->sprites_enabled |= wm->sprites_enabled;
4921 config->sprites_scaled |= wm->sprites_scaled;
4922 config->num_pipes_active++;
4923 }
4924}
4925
Matt Ropered4a6a72016-02-23 17:20:13 -08004926static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03004927{
Chris Wilson91c8a322016-07-05 10:40:23 +01004928 struct drm_device *dev = &dev_priv->drm;
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004929 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02004930 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02004931 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02004932 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03004933 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07004934
Ville Syrjäläd8905652016-01-14 14:53:35 +02004935 ilk_compute_wm_config(dev, &config);
4936
4937 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
4938 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03004939
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03004940 /* 5/6 split only in single pipe config on IVB+ */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00004941 if (INTEL_GEN(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02004942 config.num_pipes_active == 1 && config.sprites_enabled) {
4943 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
4944 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03004945
Imre Deak820c1982013-12-17 14:46:36 +02004946 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03004947 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03004948 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03004949 }
4950
Ville Syrjälä198a1e92013-10-09 19:17:58 +03004951 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03004952 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03004953
Imre Deak820c1982013-12-17 14:46:36 +02004954 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03004955
Imre Deak820c1982013-12-17 14:46:36 +02004956 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03004957}
4958
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004959static void ilk_initial_watermarks(struct intel_atomic_state *state,
4960 struct intel_crtc_state *cstate)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004961{
Matt Ropered4a6a72016-02-23 17:20:13 -08004962 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
4963 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004964
Matt Ropered4a6a72016-02-23 17:20:13 -08004965 mutex_lock(&dev_priv->wm.wm_mutex);
Matt Ropere8f1f022016-05-12 07:05:55 -07004966 intel_crtc->wm.active.ilk = cstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08004967 ilk_program_watermarks(dev_priv);
4968 mutex_unlock(&dev_priv->wm.wm_mutex);
4969}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004970
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004971static void ilk_optimize_watermarks(struct intel_atomic_state *state,
4972 struct intel_crtc_state *cstate)
Matt Ropered4a6a72016-02-23 17:20:13 -08004973{
4974 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
4975 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
4976
4977 mutex_lock(&dev_priv->wm.wm_mutex);
4978 if (cstate->wm.need_postvbl_update) {
Matt Ropere8f1f022016-05-12 07:05:55 -07004979 intel_crtc->wm.active.ilk = cstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08004980 ilk_program_watermarks(dev_priv);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004981 }
Matt Ropered4a6a72016-02-23 17:20:13 -08004982 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004983}
4984
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004985static inline void skl_wm_level_from_reg_val(uint32_t val,
4986 struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00004987{
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004988 level->plane_en = val & PLANE_WM_EN;
4989 level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
4990 level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
4991 PLANE_WM_LINES_MASK;
Pradeep Bhat30789992014-11-04 17:06:45 +00004992}
4993
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004994void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc,
4995 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00004996{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004997 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Pradeep Bhat30789992014-11-04 17:06:45 +00004998 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Pradeep Bhat30789992014-11-04 17:06:45 +00004999 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005000 int level, max_level;
5001 enum plane_id plane_id;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005002 uint32_t val;
Pradeep Bhat30789992014-11-04 17:06:45 +00005003
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005004 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00005005
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005006 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
5007 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00005008
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005009 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005010 if (plane_id != PLANE_CURSOR)
5011 val = I915_READ(PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005012 else
5013 val = I915_READ(CUR_WM(pipe, level));
5014
5015 skl_wm_level_from_reg_val(val, &wm->wm[level]);
5016 }
5017
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005018 if (plane_id != PLANE_CURSOR)
5019 val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005020 else
5021 val = I915_READ(CUR_WM_TRANS(pipe));
5022
5023 skl_wm_level_from_reg_val(val, &wm->trans_wm);
5024 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005025
Matt Roper3ef00282015-03-09 10:19:24 -07005026 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00005027 return;
5028
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005029 out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00005030}
5031
5032void skl_wm_get_hw_state(struct drm_device *dev)
5033{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005034 struct drm_i915_private *dev_priv = to_i915(dev);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005035 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
Damien Lespiaua269c582014-11-04 17:06:49 +00005036 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00005037 struct drm_crtc *crtc;
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005038 struct intel_crtc *intel_crtc;
5039 struct intel_crtc_state *cstate;
Pradeep Bhat30789992014-11-04 17:06:45 +00005040
Damien Lespiaua269c582014-11-04 17:06:49 +00005041 skl_ddb_get_hw_state(dev_priv, ddb);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005042 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5043 intel_crtc = to_intel_crtc(crtc);
5044 cstate = to_intel_crtc_state(crtc->state);
5045
5046 skl_pipe_wm_get_hw_state(crtc, &cstate->wm.skl.optimal);
5047
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02005048 if (intel_crtc->active)
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005049 hw->dirty_pipes |= drm_crtc_mask(crtc);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005050 }
Matt Ropera1de91e2016-05-12 07:05:57 -07005051
Matt Roper279e99d2016-05-12 07:06:02 -07005052 if (dev_priv->active_crtcs) {
5053 /* Fully recompute DDB on first atomic commit */
5054 dev_priv->wm.distrust_bios_wm = true;
5055 } else {
5056 /* Easy/common case; just sanitize DDB now if everything off */
5057 memset(ddb, 0, sizeof(*ddb));
5058 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005059}
5060
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005061static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
5062{
5063 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005064 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02005065 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005066 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper4e0963c2015-09-24 15:53:15 -07005067 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Matt Ropere8f1f022016-05-12 07:05:55 -07005068 struct intel_pipe_wm *active = &cstate->wm.ilk.optimal;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005069 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005070 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005071 [PIPE_A] = WM0_PIPEA_ILK,
5072 [PIPE_B] = WM0_PIPEB_ILK,
5073 [PIPE_C] = WM0_PIPEC_IVB,
5074 };
5075
5076 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005077 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02005078 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005079
Ville Syrjälä15606532016-05-13 17:55:17 +03005080 memset(active, 0, sizeof(*active));
5081
Matt Roper3ef00282015-03-09 10:19:24 -07005082 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02005083
5084 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005085 u32 tmp = hw->wm_pipe[pipe];
5086
5087 /*
5088 * For active pipes LP0 watermark is marked as
5089 * enabled, and LP1+ watermaks as disabled since
5090 * we can't really reverse compute them in case
5091 * multiple pipes are active.
5092 */
5093 active->wm[0].enable = true;
5094 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
5095 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
5096 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
5097 active->linetime = hw->wm_linetime[pipe];
5098 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005099 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005100
5101 /*
5102 * For inactive pipes, all watermark levels
5103 * should be marked as enabled but zeroed,
5104 * which is what we'd compute them to.
5105 */
5106 for (level = 0; level <= max_level; level++)
5107 active->wm[level].enable = true;
5108 }
Matt Roper4e0963c2015-09-24 15:53:15 -07005109
5110 intel_crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005111}
5112
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005113#define _FW_WM(value, plane) \
5114 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
5115#define _FW_WM_VLV(value, plane) \
5116 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
5117
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005118static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
5119 struct g4x_wm_values *wm)
5120{
5121 uint32_t tmp;
5122
5123 tmp = I915_READ(DSPFW1);
5124 wm->sr.plane = _FW_WM(tmp, SR);
5125 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5126 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
5127 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
5128
5129 tmp = I915_READ(DSPFW2);
5130 wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
5131 wm->sr.fbc = _FW_WM(tmp, FBC_SR);
5132 wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
5133 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
5134 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5135 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
5136
5137 tmp = I915_READ(DSPFW3);
5138 wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
5139 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5140 wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
5141 wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
5142}
5143
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005144static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
5145 struct vlv_wm_values *wm)
5146{
5147 enum pipe pipe;
5148 uint32_t tmp;
5149
5150 for_each_pipe(dev_priv, pipe) {
5151 tmp = I915_READ(VLV_DDL(pipe));
5152
Ville Syrjälä1b313892016-11-28 19:37:08 +02005153 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005154 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005155 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005156 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005157 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005158 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005159 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005160 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
5161 }
5162
5163 tmp = I915_READ(DSPFW1);
5164 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005165 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5166 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
5167 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005168
5169 tmp = I915_READ(DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005170 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
5171 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5172 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005173
5174 tmp = I915_READ(DSPFW3);
5175 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5176
5177 if (IS_CHERRYVIEW(dev_priv)) {
5178 tmp = I915_READ(DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005179 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5180 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005181
5182 tmp = I915_READ(DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005183 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
5184 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005185
5186 tmp = I915_READ(DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005187 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
5188 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005189
5190 tmp = I915_READ(DSPHOWM);
5191 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005192 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
5193 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
5194 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
5195 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5196 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5197 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5198 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5199 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5200 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005201 } else {
5202 tmp = I915_READ(DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005203 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5204 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005205
5206 tmp = I915_READ(DSPHOWM);
5207 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005208 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5209 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5210 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5211 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5212 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5213 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005214 }
5215}
5216
5217#undef _FW_WM
5218#undef _FW_WM_VLV
5219
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005220void g4x_wm_get_hw_state(struct drm_device *dev)
5221{
5222 struct drm_i915_private *dev_priv = to_i915(dev);
5223 struct g4x_wm_values *wm = &dev_priv->wm.g4x;
5224 struct intel_crtc *crtc;
5225
5226 g4x_read_wm_values(dev_priv, wm);
5227
5228 wm->cxsr = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
5229
5230 for_each_intel_crtc(dev, crtc) {
5231 struct intel_crtc_state *crtc_state =
5232 to_intel_crtc_state(crtc->base.state);
5233 struct g4x_wm_state *active = &crtc->wm.active.g4x;
5234 struct g4x_pipe_wm *raw;
5235 enum pipe pipe = crtc->pipe;
5236 enum plane_id plane_id;
5237 int level, max_level;
5238
5239 active->cxsr = wm->cxsr;
5240 active->hpll_en = wm->hpll_en;
5241 active->fbc_en = wm->fbc_en;
5242
5243 active->sr = wm->sr;
5244 active->hpll = wm->hpll;
5245
5246 for_each_plane_id_on_crtc(crtc, plane_id) {
5247 active->wm.plane[plane_id] =
5248 wm->pipe[pipe].plane[plane_id];
5249 }
5250
5251 if (wm->cxsr && wm->hpll_en)
5252 max_level = G4X_WM_LEVEL_HPLL;
5253 else if (wm->cxsr)
5254 max_level = G4X_WM_LEVEL_SR;
5255 else
5256 max_level = G4X_WM_LEVEL_NORMAL;
5257
5258 level = G4X_WM_LEVEL_NORMAL;
5259 raw = &crtc_state->wm.g4x.raw[level];
5260 for_each_plane_id_on_crtc(crtc, plane_id)
5261 raw->plane[plane_id] = active->wm.plane[plane_id];
5262
5263 if (++level > max_level)
5264 goto out;
5265
5266 raw = &crtc_state->wm.g4x.raw[level];
5267 raw->plane[PLANE_PRIMARY] = active->sr.plane;
5268 raw->plane[PLANE_CURSOR] = active->sr.cursor;
5269 raw->plane[PLANE_SPRITE0] = 0;
5270 raw->fbc = active->sr.fbc;
5271
5272 if (++level > max_level)
5273 goto out;
5274
5275 raw = &crtc_state->wm.g4x.raw[level];
5276 raw->plane[PLANE_PRIMARY] = active->hpll.plane;
5277 raw->plane[PLANE_CURSOR] = active->hpll.cursor;
5278 raw->plane[PLANE_SPRITE0] = 0;
5279 raw->fbc = active->hpll.fbc;
5280
5281 out:
5282 for_each_plane_id_on_crtc(crtc, plane_id)
5283 g4x_raw_plane_wm_set(crtc_state, level,
5284 plane_id, USHRT_MAX);
5285 g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
5286
5287 crtc_state->wm.g4x.optimal = *active;
5288 crtc_state->wm.g4x.intermediate = *active;
5289
5290 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
5291 pipe_name(pipe),
5292 wm->pipe[pipe].plane[PLANE_PRIMARY],
5293 wm->pipe[pipe].plane[PLANE_CURSOR],
5294 wm->pipe[pipe].plane[PLANE_SPRITE0]);
5295 }
5296
5297 DRM_DEBUG_KMS("Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
5298 wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
5299 DRM_DEBUG_KMS("Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
5300 wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
5301 DRM_DEBUG_KMS("Initial SR=%s HPLL=%s FBC=%s\n",
5302 yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
5303}
5304
5305void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
5306{
5307 struct intel_plane *plane;
5308 struct intel_crtc *crtc;
5309
5310 mutex_lock(&dev_priv->wm.wm_mutex);
5311
5312 for_each_intel_plane(&dev_priv->drm, plane) {
5313 struct intel_crtc *crtc =
5314 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
5315 struct intel_crtc_state *crtc_state =
5316 to_intel_crtc_state(crtc->base.state);
5317 struct intel_plane_state *plane_state =
5318 to_intel_plane_state(plane->base.state);
5319 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
5320 enum plane_id plane_id = plane->id;
5321 int level;
5322
5323 if (plane_state->base.visible)
5324 continue;
5325
5326 for (level = 0; level < 3; level++) {
5327 struct g4x_pipe_wm *raw =
5328 &crtc_state->wm.g4x.raw[level];
5329
5330 raw->plane[plane_id] = 0;
5331 wm_state->wm.plane[plane_id] = 0;
5332 }
5333
5334 if (plane_id == PLANE_PRIMARY) {
5335 for (level = 0; level < 3; level++) {
5336 struct g4x_pipe_wm *raw =
5337 &crtc_state->wm.g4x.raw[level];
5338 raw->fbc = 0;
5339 }
5340
5341 wm_state->sr.fbc = 0;
5342 wm_state->hpll.fbc = 0;
5343 wm_state->fbc_en = false;
5344 }
5345 }
5346
5347 for_each_intel_crtc(&dev_priv->drm, crtc) {
5348 struct intel_crtc_state *crtc_state =
5349 to_intel_crtc_state(crtc->base.state);
5350
5351 crtc_state->wm.g4x.intermediate =
5352 crtc_state->wm.g4x.optimal;
5353 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
5354 }
5355
5356 g4x_program_watermarks(dev_priv);
5357
5358 mutex_unlock(&dev_priv->wm.wm_mutex);
5359}
5360
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005361void vlv_wm_get_hw_state(struct drm_device *dev)
5362{
5363 struct drm_i915_private *dev_priv = to_i915(dev);
5364 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02005365 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005366 u32 val;
5367
5368 vlv_read_wm_values(dev_priv, wm);
5369
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005370 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
5371 wm->level = VLV_WM_LEVEL_PM2;
5372
5373 if (IS_CHERRYVIEW(dev_priv)) {
5374 mutex_lock(&dev_priv->rps.hw_lock);
5375
5376 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5377 if (val & DSP_MAXFIFO_PM5_ENABLE)
5378 wm->level = VLV_WM_LEVEL_PM5;
5379
Ville Syrjälä58590c12015-09-08 21:05:12 +03005380 /*
5381 * If DDR DVFS is disabled in the BIOS, Punit
5382 * will never ack the request. So if that happens
5383 * assume we don't have to enable/disable DDR DVFS
5384 * dynamically. To test that just set the REQ_ACK
5385 * bit to poke the Punit, but don't change the
5386 * HIGH/LOW bits so that we don't actually change
5387 * the current state.
5388 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005389 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03005390 val |= FORCE_DDR_FREQ_REQ_ACK;
5391 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
5392
5393 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
5394 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
5395 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
5396 "assuming DDR DVFS is disabled\n");
5397 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
5398 } else {
5399 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
5400 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
5401 wm->level = VLV_WM_LEVEL_DDR_DVFS;
5402 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005403
5404 mutex_unlock(&dev_priv->rps.hw_lock);
5405 }
5406
Ville Syrjäläff32c542017-03-02 19:14:57 +02005407 for_each_intel_crtc(dev, crtc) {
5408 struct intel_crtc_state *crtc_state =
5409 to_intel_crtc_state(crtc->base.state);
5410 struct vlv_wm_state *active = &crtc->wm.active.vlv;
5411 const struct vlv_fifo_state *fifo_state =
5412 &crtc_state->wm.vlv.fifo_state;
5413 enum pipe pipe = crtc->pipe;
5414 enum plane_id plane_id;
5415 int level;
5416
5417 vlv_get_fifo_size(crtc_state);
5418
5419 active->num_levels = wm->level + 1;
5420 active->cxsr = wm->cxsr;
5421
Ville Syrjäläff32c542017-03-02 19:14:57 +02005422 for (level = 0; level < active->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03005423 struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02005424 &crtc_state->wm.vlv.raw[level];
5425
5426 active->sr[level].plane = wm->sr.plane;
5427 active->sr[level].cursor = wm->sr.cursor;
5428
5429 for_each_plane_id_on_crtc(crtc, plane_id) {
5430 active->wm[level].plane[plane_id] =
5431 wm->pipe[pipe].plane[plane_id];
5432
5433 raw->plane[plane_id] =
5434 vlv_invert_wm_value(active->wm[level].plane[plane_id],
5435 fifo_state->plane[plane_id]);
5436 }
5437 }
5438
5439 for_each_plane_id_on_crtc(crtc, plane_id)
5440 vlv_raw_plane_wm_set(crtc_state, level,
5441 plane_id, USHRT_MAX);
5442 vlv_invalidate_wms(crtc, active, level);
5443
5444 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02005445 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02005446
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005447 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 +02005448 pipe_name(pipe),
5449 wm->pipe[pipe].plane[PLANE_PRIMARY],
5450 wm->pipe[pipe].plane[PLANE_CURSOR],
5451 wm->pipe[pipe].plane[PLANE_SPRITE0],
5452 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02005453 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005454
5455 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
5456 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
5457}
5458
Ville Syrjälä602ae832017-03-02 19:15:02 +02005459void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
5460{
5461 struct intel_plane *plane;
5462 struct intel_crtc *crtc;
5463
5464 mutex_lock(&dev_priv->wm.wm_mutex);
5465
5466 for_each_intel_plane(&dev_priv->drm, plane) {
5467 struct intel_crtc *crtc =
5468 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
5469 struct intel_crtc_state *crtc_state =
5470 to_intel_crtc_state(crtc->base.state);
5471 struct intel_plane_state *plane_state =
5472 to_intel_plane_state(plane->base.state);
5473 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
5474 const struct vlv_fifo_state *fifo_state =
5475 &crtc_state->wm.vlv.fifo_state;
5476 enum plane_id plane_id = plane->id;
5477 int level;
5478
5479 if (plane_state->base.visible)
5480 continue;
5481
5482 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03005483 struct g4x_pipe_wm *raw =
Ville Syrjälä602ae832017-03-02 19:15:02 +02005484 &crtc_state->wm.vlv.raw[level];
5485
5486 raw->plane[plane_id] = 0;
5487
5488 wm_state->wm[level].plane[plane_id] =
5489 vlv_invert_wm_value(raw->plane[plane_id],
5490 fifo_state->plane[plane_id]);
5491 }
5492 }
5493
5494 for_each_intel_crtc(&dev_priv->drm, crtc) {
5495 struct intel_crtc_state *crtc_state =
5496 to_intel_crtc_state(crtc->base.state);
5497
5498 crtc_state->wm.vlv.intermediate =
5499 crtc_state->wm.vlv.optimal;
5500 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
5501 }
5502
5503 vlv_program_watermarks(dev_priv);
5504
5505 mutex_unlock(&dev_priv->wm.wm_mutex);
5506}
5507
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005508void ilk_wm_get_hw_state(struct drm_device *dev)
5509{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005510 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02005511 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005512 struct drm_crtc *crtc;
5513
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01005514 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005515 ilk_pipe_wm_get_hw_state(crtc);
5516
5517 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
5518 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
5519 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
5520
5521 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00005522 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjäläcfa76982014-03-07 18:32:08 +02005523 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
5524 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
5525 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005526
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005527 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02005528 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
5529 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01005530 else if (IS_IVYBRIDGE(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02005531 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
5532 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005533
5534 hw->enable_fbc_wm =
5535 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
5536}
5537
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005538/**
5539 * intel_update_watermarks - update FIFO watermark values based on current modes
5540 *
5541 * Calculate watermark values for the various WM regs based on current mode
5542 * and plane configuration.
5543 *
5544 * There are several cases to deal with here:
5545 * - normal (i.e. non-self-refresh)
5546 * - self-refresh (SR) mode
5547 * - lines are large relative to FIFO size (buffer can hold up to 2)
5548 * - lines are small relative to FIFO size (buffer can hold more than 2
5549 * lines), so need to account for TLB latency
5550 *
5551 * The normal calculation is:
5552 * watermark = dotclock * bytes per pixel * latency
5553 * where latency is platform & configuration dependent (we assume pessimal
5554 * values here).
5555 *
5556 * The SR calculation is:
5557 * watermark = (trunc(latency/line time)+1) * surface width *
5558 * bytes per pixel
5559 * where
5560 * line time = htotal / dotclock
5561 * surface width = hdisplay for normal plane and 64 for cursor
5562 * and latency is assumed to be high, as above.
5563 *
5564 * The final value programmed to the register should always be rounded up,
5565 * and include an extra 2 entries to account for clock crossings.
5566 *
5567 * We don't use the sprite, so we can ignore that. And on Crestline we have
5568 * to set the non-SR watermarks to 8.
5569 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02005570void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005571{
Ville Syrjälä432081b2016-10-31 22:37:03 +02005572 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005573
5574 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005575 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005576}
5577
Jani Nikulae2828912016-01-18 09:19:47 +02005578/*
Daniel Vetter92703882012-08-09 16:46:01 +02005579 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02005580 */
5581DEFINE_SPINLOCK(mchdev_lock);
5582
5583/* Global for IPS driver to get at the current i915 device. Protected by
5584 * mchdev_lock. */
5585static struct drm_i915_private *i915_mch_dev;
5586
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005587bool ironlake_set_drps(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005588{
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005589 u16 rgvswctl;
5590
Chris Wilson67520412017-03-02 13:28:01 +00005591 lockdep_assert_held(&mchdev_lock);
Daniel Vetter92703882012-08-09 16:46:01 +02005592
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005593 rgvswctl = I915_READ16(MEMSWCTL);
5594 if (rgvswctl & MEMCTL_CMD_STS) {
5595 DRM_DEBUG("gpu busy, RCS change rejected\n");
5596 return false; /* still busy with another command */
5597 }
5598
5599 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
5600 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
5601 I915_WRITE16(MEMSWCTL, rgvswctl);
5602 POSTING_READ16(MEMSWCTL);
5603
5604 rgvswctl |= MEMCTL_CMD_STS;
5605 I915_WRITE16(MEMSWCTL, rgvswctl);
5606
5607 return true;
5608}
5609
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005610static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005611{
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00005612 u32 rgvmodectl;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005613 u8 fmax, fmin, fstart, vstart;
5614
Daniel Vetter92703882012-08-09 16:46:01 +02005615 spin_lock_irq(&mchdev_lock);
5616
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00005617 rgvmodectl = I915_READ(MEMMODECTL);
5618
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005619 /* Enable temp reporting */
5620 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
5621 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
5622
5623 /* 100ms RC evaluation intervals */
5624 I915_WRITE(RCUPEI, 100000);
5625 I915_WRITE(RCDNEI, 100000);
5626
5627 /* Set max/min thresholds to 90ms and 80ms respectively */
5628 I915_WRITE(RCBMAXAVG, 90000);
5629 I915_WRITE(RCBMINAVG, 80000);
5630
5631 I915_WRITE(MEMIHYST, 1);
5632
5633 /* Set up min, max, and cur for interrupt handling */
5634 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
5635 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
5636 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
5637 MEMMODE_FSTART_SHIFT;
5638
Ville Syrjälä616847e2015-09-18 20:03:19 +03005639 vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005640 PXVFREQ_PX_SHIFT;
5641
Daniel Vetter20e4d402012-08-08 23:35:39 +02005642 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
5643 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005644
Daniel Vetter20e4d402012-08-08 23:35:39 +02005645 dev_priv->ips.max_delay = fstart;
5646 dev_priv->ips.min_delay = fmin;
5647 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005648
5649 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
5650 fmax, fmin, fstart);
5651
5652 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
5653
5654 /*
5655 * Interrupts will be enabled in ironlake_irq_postinstall
5656 */
5657
5658 I915_WRITE(VIDSTART, vstart);
5659 POSTING_READ(VIDSTART);
5660
5661 rgvmodectl |= MEMMODE_SWMODE_EN;
5662 I915_WRITE(MEMMODECTL, rgvmodectl);
5663
Daniel Vetter92703882012-08-09 16:46:01 +02005664 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005665 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005666 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005667
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005668 ironlake_set_drps(dev_priv, fstart);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005669
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03005670 dev_priv->ips.last_count1 = I915_READ(DMIEC) +
5671 I915_READ(DDREC) + I915_READ(CSIEC);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005672 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03005673 dev_priv->ips.last_count2 = I915_READ(GFXEC);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005674 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02005675
5676 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005677}
5678
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005679static void ironlake_disable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005680{
Daniel Vetter92703882012-08-09 16:46:01 +02005681 u16 rgvswctl;
5682
5683 spin_lock_irq(&mchdev_lock);
5684
5685 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005686
5687 /* Ack interrupts, disable EFC interrupt */
5688 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
5689 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
5690 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
5691 I915_WRITE(DEIIR, DE_PCU_EVENT);
5692 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
5693
5694 /* Go back to the starting frequency */
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005695 ironlake_set_drps(dev_priv, dev_priv->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005696 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005697 rgvswctl |= MEMCTL_CMD_STS;
5698 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005699 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005700
Daniel Vetter92703882012-08-09 16:46:01 +02005701 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005702}
5703
Daniel Vetteracbe9472012-07-26 11:50:05 +02005704/* There's a funny hw issue where the hw returns all 0 when reading from
5705 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
5706 * ourselves, instead of doing a rmw cycle (which might result in us clearing
5707 * all limits and the gpu stuck at whatever frequency it is at atm).
5708 */
Akash Goel74ef1172015-03-06 11:07:19 +05305709static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005710{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005711 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005712
Daniel Vetter20b46e52012-07-26 11:16:14 +02005713 /* Only set the down limit when we've reached the lowest level to avoid
5714 * getting more interrupts, otherwise leave this clear. This prevents a
5715 * race in the hw when coming out of rc6: There's a tiny window where
5716 * the hw runs at the minimal clock before selecting the desired
5717 * frequency, if the down threshold expires in that window we will not
5718 * receive a down interrupt. */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03005719 if (IS_GEN9(dev_priv)) {
Akash Goel74ef1172015-03-06 11:07:19 +05305720 limits = (dev_priv->rps.max_freq_softlimit) << 23;
5721 if (val <= dev_priv->rps.min_freq_softlimit)
5722 limits |= (dev_priv->rps.min_freq_softlimit) << 14;
5723 } else {
5724 limits = dev_priv->rps.max_freq_softlimit << 24;
5725 if (val <= dev_priv->rps.min_freq_softlimit)
5726 limits |= dev_priv->rps.min_freq_softlimit << 16;
5727 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02005728
5729 return limits;
5730}
5731
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005732static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
5733{
5734 int new_power;
Akash Goel8a586432015-03-06 11:07:18 +05305735 u32 threshold_up = 0, threshold_down = 0; /* in % */
5736 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005737
5738 new_power = dev_priv->rps.power;
5739 switch (dev_priv->rps.power) {
5740 case LOW_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01005741 if (val > dev_priv->rps.efficient_freq + 1 &&
5742 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005743 new_power = BETWEEN;
5744 break;
5745
5746 case BETWEEN:
Chris Wilsona72b5622016-07-02 15:35:59 +01005747 if (val <= dev_priv->rps.efficient_freq &&
5748 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005749 new_power = LOW_POWER;
Chris Wilsona72b5622016-07-02 15:35:59 +01005750 else if (val >= dev_priv->rps.rp0_freq &&
5751 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005752 new_power = HIGH_POWER;
5753 break;
5754
5755 case HIGH_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01005756 if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 &&
5757 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005758 new_power = BETWEEN;
5759 break;
5760 }
5761 /* Max/min bins are special */
Chris Wilsonaed242f2015-03-18 09:48:21 +00005762 if (val <= dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005763 new_power = LOW_POWER;
Chris Wilsonaed242f2015-03-18 09:48:21 +00005764 if (val >= dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005765 new_power = HIGH_POWER;
5766 if (new_power == dev_priv->rps.power)
5767 return;
5768
5769 /* Note the units here are not exactly 1us, but 1280ns. */
5770 switch (new_power) {
5771 case LOW_POWER:
5772 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05305773 ei_up = 16000;
5774 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005775
5776 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305777 ei_down = 32000;
5778 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005779 break;
5780
5781 case BETWEEN:
5782 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05305783 ei_up = 13000;
5784 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005785
5786 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305787 ei_down = 32000;
5788 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005789 break;
5790
5791 case HIGH_POWER:
5792 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05305793 ei_up = 10000;
5794 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005795
5796 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305797 ei_down = 32000;
5798 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005799 break;
5800 }
5801
Mika Kuoppala6067a272017-02-15 15:52:59 +02005802 /* When byt can survive without system hang with dynamic
5803 * sw freq adjustments, this restriction can be lifted.
5804 */
5805 if (IS_VALLEYVIEW(dev_priv))
5806 goto skip_hw_write;
5807
Akash Goel8a586432015-03-06 11:07:18 +05305808 I915_WRITE(GEN6_RP_UP_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01005809 GT_INTERVAL_FROM_US(dev_priv, ei_up));
Akash Goel8a586432015-03-06 11:07:18 +05305810 I915_WRITE(GEN6_RP_UP_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01005811 GT_INTERVAL_FROM_US(dev_priv,
5812 ei_up * threshold_up / 100));
Akash Goel8a586432015-03-06 11:07:18 +05305813
5814 I915_WRITE(GEN6_RP_DOWN_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01005815 GT_INTERVAL_FROM_US(dev_priv, ei_down));
Akash Goel8a586432015-03-06 11:07:18 +05305816 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01005817 GT_INTERVAL_FROM_US(dev_priv,
5818 ei_down * threshold_down / 100));
Akash Goel8a586432015-03-06 11:07:18 +05305819
Chris Wilsona72b5622016-07-02 15:35:59 +01005820 I915_WRITE(GEN6_RP_CONTROL,
5821 GEN6_RP_MEDIA_TURBO |
5822 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5823 GEN6_RP_MEDIA_IS_GFX |
5824 GEN6_RP_ENABLE |
5825 GEN6_RP_UP_BUSY_AVG |
5826 GEN6_RP_DOWN_IDLE_AVG);
Akash Goel8a586432015-03-06 11:07:18 +05305827
Mika Kuoppala6067a272017-02-15 15:52:59 +02005828skip_hw_write:
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005829 dev_priv->rps.power = new_power;
Chris Wilson8fb55192015-04-07 16:20:28 +01005830 dev_priv->rps.up_threshold = threshold_up;
5831 dev_priv->rps.down_threshold = threshold_down;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005832 dev_priv->rps.last_adj = 0;
5833}
5834
Chris Wilson2876ce72014-03-28 08:03:34 +00005835static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
5836{
5837 u32 mask = 0;
5838
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00005839 /* We use UP_EI_EXPIRED interupts for both up/down in manual mode */
Chris Wilson2876ce72014-03-28 08:03:34 +00005840 if (val > dev_priv->rps.min_freq_softlimit)
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00005841 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
Chris Wilson2876ce72014-03-28 08:03:34 +00005842 if (val < dev_priv->rps.max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00005843 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00005844
Chris Wilson7b3c29f2014-07-10 20:31:19 +01005845 mask &= dev_priv->pm_rps_events;
5846
Imre Deak59d02a12014-12-19 19:33:26 +02005847 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00005848}
5849
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06005850/* gen6_set_rps is called to update the frequency request, but should also be
5851 * called when the range (min_delay and max_delay) is modified so that we can
5852 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005853static int gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02005854{
Chris Wilsoneb64cad2014-03-27 08:24:20 +00005855 /* min/max delay may still have been modified so be sure to
5856 * write the limits value.
5857 */
5858 if (val != dev_priv->rps.cur_freq) {
5859 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06005860
Chris Wilsondc979972016-05-10 14:10:04 +01005861 if (IS_GEN9(dev_priv))
Akash Goel57041952015-03-06 11:07:17 +05305862 I915_WRITE(GEN6_RPNSWREQ,
5863 GEN9_FREQUENCY(val));
Chris Wilsondc979972016-05-10 14:10:04 +01005864 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00005865 I915_WRITE(GEN6_RPNSWREQ,
5866 HSW_FREQUENCY(val));
5867 else
5868 I915_WRITE(GEN6_RPNSWREQ,
5869 GEN6_FREQUENCY(val) |
5870 GEN6_OFFSET(0) |
5871 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06005872 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005873
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005874 /* Make sure we continue to get interrupts
5875 * until we hit the minimum or maximum frequencies.
5876 */
Akash Goel74ef1172015-03-06 11:07:19 +05305877 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00005878 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005879
Ben Widawskyb39fb292014-03-19 18:31:11 -07005880 dev_priv->rps.cur_freq = val;
Mika Kuoppala0f945922015-11-17 18:14:26 +02005881 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005882
5883 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005884}
5885
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005886static int valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005887{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005888 int err;
5889
Chris Wilsondc979972016-05-10 14:10:04 +01005890 if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005891 "Odd GPU freq value\n"))
5892 val &= ~1;
5893
Deepak Scd25dd52015-07-10 18:31:40 +05305894 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
5895
Chris Wilson8fb55192015-04-07 16:20:28 +01005896 if (val != dev_priv->rps.cur_freq) {
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005897 err = vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
5898 if (err)
5899 return err;
5900
Chris Wilsondb4c5e02017-02-10 15:03:46 +00005901 gen6_set_rps_thresholds(dev_priv, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01005902 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005903
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005904 dev_priv->rps.cur_freq = val;
5905 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005906
5907 return 0;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005908}
5909
Deepak Sa7f6e232015-05-09 18:04:44 +05305910/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05305911 *
5912 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05305913 * 1. Forcewake Media well.
5914 * 2. Request idle freq.
5915 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05305916*/
5917static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
5918{
Chris Wilsonaed242f2015-03-18 09:48:21 +00005919 u32 val = dev_priv->rps.idle_freq;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005920 int err;
Deepak S5549d252014-06-28 11:26:11 +05305921
Chris Wilsonaed242f2015-03-18 09:48:21 +00005922 if (dev_priv->rps.cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05305923 return;
5924
Chris Wilsonc9efef72017-01-02 15:28:45 +00005925 /* The punit delays the write of the frequency and voltage until it
5926 * determines the GPU is awake. During normal usage we don't want to
5927 * waste power changing the frequency if the GPU is sleeping (rc6).
5928 * However, the GPU and driver is now idle and we do not want to delay
5929 * switching to minimum voltage (reducing power whilst idle) as we do
5930 * not expect to be woken in the near future and so must flush the
5931 * change by waking the device.
5932 *
5933 * We choose to take the media powerwell (either would do to trick the
5934 * punit into committing the voltage change) as that takes a lot less
5935 * power than the render powerwell.
5936 */
Deepak Sa7f6e232015-05-09 18:04:44 +05305937 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005938 err = valleyview_set_rps(dev_priv, val);
Deepak Sa7f6e232015-05-09 18:04:44 +05305939 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005940
5941 if (err)
5942 DRM_ERROR("Failed to set RPS for idle\n");
Deepak S76c3552f2014-01-30 23:08:16 +05305943}
5944
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005945void gen6_rps_busy(struct drm_i915_private *dev_priv)
5946{
5947 mutex_lock(&dev_priv->rps.hw_lock);
5948 if (dev_priv->rps.enabled) {
Chris Wilsonbd648182017-02-10 15:03:48 +00005949 u8 freq;
5950
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00005951 if (dev_priv->pm_rps_events & GEN6_PM_RP_UP_EI_EXPIRED)
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005952 gen6_rps_reset_ei(dev_priv);
5953 I915_WRITE(GEN6_PMINTRMSK,
5954 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Michał Winiarski2b83c4c2016-06-20 11:58:27 +02005955
Chris Wilsonc33d2472016-07-04 08:08:36 +01005956 gen6_enable_rps_interrupts(dev_priv);
5957
Chris Wilsonbd648182017-02-10 15:03:48 +00005958 /* Use the user's desired frequency as a guide, but for better
5959 * performance, jump directly to RPe as our starting frequency.
5960 */
5961 freq = max(dev_priv->rps.cur_freq,
5962 dev_priv->rps.efficient_freq);
5963
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005964 if (intel_set_rps(dev_priv,
Chris Wilsonbd648182017-02-10 15:03:48 +00005965 clamp(freq,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005966 dev_priv->rps.min_freq_softlimit,
5967 dev_priv->rps.max_freq_softlimit)))
5968 DRM_DEBUG_DRIVER("Failed to set idle frequency\n");
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005969 }
5970 mutex_unlock(&dev_priv->rps.hw_lock);
5971}
5972
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005973void gen6_rps_idle(struct drm_i915_private *dev_priv)
5974{
Chris Wilsonc33d2472016-07-04 08:08:36 +01005975 /* Flush our bottom-half so that it does not race with us
5976 * setting the idle frequency and so that it is bounded by
5977 * our rpm wakeref. And then disable the interrupts to stop any
5978 * futher RPS reclocking whilst we are asleep.
5979 */
5980 gen6_disable_rps_interrupts(dev_priv);
5981
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005982 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01005983 if (dev_priv->rps.enabled) {
Chris Wilsondc979972016-05-10 14:10:04 +01005984 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Deepak S76c3552f2014-01-30 23:08:16 +05305985 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02005986 else
Chris Wilsondc979972016-05-10 14:10:04 +01005987 gen6_set_rps(dev_priv, dev_priv->rps.idle_freq);
Chris Wilsonc0951f02013-10-10 21:58:50 +01005988 dev_priv->rps.last_adj = 0;
Ville Syrjälä12c100b2016-05-23 17:42:48 +03005989 I915_WRITE(GEN6_PMINTRMSK,
5990 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Chris Wilsonc0951f02013-10-10 21:58:50 +01005991 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01005992 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005993
Chris Wilson8d3afd72015-05-21 21:01:47 +01005994 spin_lock(&dev_priv->rps.client_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005995 while (!list_empty(&dev_priv->rps.clients))
5996 list_del_init(dev_priv->rps.clients.next);
Chris Wilson8d3afd72015-05-21 21:01:47 +01005997 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005998}
5999
Chris Wilson1854d5c2015-04-07 16:20:32 +01006000void gen6_rps_boost(struct drm_i915_private *dev_priv,
Chris Wilsone61b9952015-04-27 13:41:24 +01006001 struct intel_rps_client *rps,
6002 unsigned long submitted)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006003{
Chris Wilson8d3afd72015-05-21 21:01:47 +01006004 /* This is intentionally racy! We peek at the state here, then
6005 * validate inside the RPS worker.
6006 */
Chris Wilson67d97da2016-07-04 08:08:31 +01006007 if (!(dev_priv->gt.awake &&
Chris Wilson8d3afd72015-05-21 21:01:47 +01006008 dev_priv->rps.enabled &&
Chris Wilson29ecd78d2016-07-13 09:10:35 +01006009 dev_priv->rps.cur_freq < dev_priv->rps.boost_freq))
Chris Wilson8d3afd72015-05-21 21:01:47 +01006010 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006011
Chris Wilsone61b9952015-04-27 13:41:24 +01006012 /* Force a RPS boost (and don't count it against the client) if
6013 * the GPU is severely congested.
6014 */
Chris Wilsond0bc54f2015-05-21 21:01:48 +01006015 if (rps && time_after(jiffies, submitted + DRM_I915_THROTTLE_JIFFIES))
Chris Wilsone61b9952015-04-27 13:41:24 +01006016 rps = NULL;
6017
Chris Wilson8d3afd72015-05-21 21:01:47 +01006018 spin_lock(&dev_priv->rps.client_lock);
6019 if (rps == NULL || list_empty(&rps->link)) {
6020 spin_lock_irq(&dev_priv->irq_lock);
6021 if (dev_priv->rps.interrupts_enabled) {
6022 dev_priv->rps.client_boost = true;
Chris Wilsonc33d2472016-07-04 08:08:36 +01006023 schedule_work(&dev_priv->rps.work);
Chris Wilson8d3afd72015-05-21 21:01:47 +01006024 }
6025 spin_unlock_irq(&dev_priv->irq_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01006026
Chris Wilson2e1b8732015-04-27 13:41:22 +01006027 if (rps != NULL) {
6028 list_add(&rps->link, &dev_priv->rps.clients);
6029 rps->boosts++;
Chris Wilson1854d5c2015-04-07 16:20:32 +01006030 } else
6031 dev_priv->rps.boosts++;
Chris Wilsonc0951f02013-10-10 21:58:50 +01006032 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01006033 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006034}
6035
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006036int intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006037{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006038 int err;
6039
Chris Wilsoncfd1c482017-02-20 09:47:07 +00006040 lockdep_assert_held(&dev_priv->rps.hw_lock);
6041 GEM_BUG_ON(val > dev_priv->rps.max_freq);
6042 GEM_BUG_ON(val < dev_priv->rps.min_freq);
6043
Chris Wilson76e4e4b2017-02-20 09:47:08 +00006044 if (!dev_priv->rps.enabled) {
6045 dev_priv->rps.cur_freq = val;
6046 return 0;
6047 }
6048
Chris Wilsondc979972016-05-10 14:10:04 +01006049 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006050 err = valleyview_set_rps(dev_priv, val);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006051 else
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006052 err = gen6_set_rps(dev_priv, val);
6053
6054 return err;
Jesse Barnes0a073b82013-04-17 15:54:58 -07006055}
6056
Chris Wilsondc979972016-05-10 14:10:04 +01006057static void gen9_disable_rc6(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00006058{
Zhe Wang20e49362014-11-04 17:07:05 +00006059 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00006060 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00006061}
6062
Chris Wilsondc979972016-05-10 14:10:04 +01006063static void gen9_disable_rps(struct drm_i915_private *dev_priv)
Akash Goel2030d682016-04-23 00:05:45 +05306064{
Akash Goel2030d682016-04-23 00:05:45 +05306065 I915_WRITE(GEN6_RP_CONTROL, 0);
6066}
6067
Chris Wilsondc979972016-05-10 14:10:04 +01006068static void gen6_disable_rps(struct drm_i915_private *dev_priv)
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006069{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006070 I915_WRITE(GEN6_RC_CONTROL, 0);
6071 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Akash Goel2030d682016-04-23 00:05:45 +05306072 I915_WRITE(GEN6_RP_CONTROL, 0);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006073}
6074
Chris Wilsondc979972016-05-10 14:10:04 +01006075static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306076{
Deepak S38807742014-05-23 21:00:15 +05306077 I915_WRITE(GEN6_RC_CONTROL, 0);
6078}
6079
Chris Wilsondc979972016-05-10 14:10:04 +01006080static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006081{
Deepak S98a2e5f2014-08-18 10:35:27 -07006082 /* we're doing forcewake before Disabling RC6,
6083 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02006084 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07006085
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006086 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006087
Mika Kuoppala59bad942015-01-16 11:34:40 +02006088 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006089}
6090
Chris Wilsondc979972016-05-10 14:10:04 +01006091static void intel_print_rc6_info(struct drm_i915_private *dev_priv, u32 mode)
Ben Widawskydc39fff2013-10-18 12:32:07 -07006092{
Chris Wilsondc979972016-05-10 14:10:04 +01006093 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Imre Deak91ca6892014-04-14 20:24:25 +03006094 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
6095 mode = GEN6_RC_CTL_RC6_ENABLE;
6096 else
6097 mode = 0;
6098 }
Chris Wilsondc979972016-05-10 14:10:04 +01006099 if (HAS_RC6p(dev_priv))
Imre Deakb99d49c2016-06-29 19:13:54 +03006100 DRM_DEBUG_DRIVER("Enabling RC6 states: "
6101 "RC6 %s RC6p %s RC6pp %s\n",
6102 onoff(mode & GEN6_RC_CTL_RC6_ENABLE),
6103 onoff(mode & GEN6_RC_CTL_RC6p_ENABLE),
6104 onoff(mode & GEN6_RC_CTL_RC6pp_ENABLE));
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07006105
6106 else
Imre Deakb99d49c2016-06-29 19:13:54 +03006107 DRM_DEBUG_DRIVER("Enabling RC6 states: RC6 %s\n",
6108 onoff(mode & GEN6_RC_CTL_RC6_ENABLE));
Ben Widawskydc39fff2013-10-18 12:32:07 -07006109}
6110
Chris Wilsondc979972016-05-10 14:10:04 +01006111static bool bxt_check_bios_rc6_setup(struct drm_i915_private *dev_priv)
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306112{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006113 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306114 bool enable_rc6 = true;
6115 unsigned long rc6_ctx_base;
Imre Deakfc619842016-06-29 19:13:55 +03006116 u32 rc_ctl;
6117 int rc_sw_target;
6118
6119 rc_ctl = I915_READ(GEN6_RC_CONTROL);
6120 rc_sw_target = (I915_READ(GEN6_RC_STATE) & RC_SW_TARGET_STATE_MASK) >>
6121 RC_SW_TARGET_STATE_SHIFT;
6122 DRM_DEBUG_DRIVER("BIOS enabled RC states: "
6123 "HW_CTRL %s HW_RC6 %s SW_TARGET_STATE %x\n",
6124 onoff(rc_ctl & GEN6_RC_CTL_HW_ENABLE),
6125 onoff(rc_ctl & GEN6_RC_CTL_RC6_ENABLE),
6126 rc_sw_target);
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306127
6128 if (!(I915_READ(RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006129 DRM_DEBUG_DRIVER("RC6 Base location not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306130 enable_rc6 = false;
6131 }
6132
6133 /*
6134 * The exact context size is not known for BXT, so assume a page size
6135 * for this check.
6136 */
6137 rc6_ctx_base = I915_READ(RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006138 if (!((rc6_ctx_base >= ggtt->stolen_reserved_base) &&
6139 (rc6_ctx_base + PAGE_SIZE <= ggtt->stolen_reserved_base +
6140 ggtt->stolen_reserved_size))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006141 DRM_DEBUG_DRIVER("RC6 Base address not as expected.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306142 enable_rc6 = false;
6143 }
6144
6145 if (!(((I915_READ(PWRCTX_MAXCNT_RCSUNIT) & IDLE_TIME_MASK) > 1) &&
6146 ((I915_READ(PWRCTX_MAXCNT_VCSUNIT0) & IDLE_TIME_MASK) > 1) &&
6147 ((I915_READ(PWRCTX_MAXCNT_BCSUNIT) & IDLE_TIME_MASK) > 1) &&
6148 ((I915_READ(PWRCTX_MAXCNT_VECSUNIT) & IDLE_TIME_MASK) > 1))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006149 DRM_DEBUG_DRIVER("Engine Idle wait time not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306150 enable_rc6 = false;
6151 }
6152
Imre Deakfc619842016-06-29 19:13:55 +03006153 if (!I915_READ(GEN8_PUSHBUS_CONTROL) ||
6154 !I915_READ(GEN8_PUSHBUS_ENABLE) ||
6155 !I915_READ(GEN8_PUSHBUS_SHIFT)) {
6156 DRM_DEBUG_DRIVER("Pushbus not setup properly.\n");
6157 enable_rc6 = false;
6158 }
6159
6160 if (!I915_READ(GEN6_GFXPAUSE)) {
6161 DRM_DEBUG_DRIVER("GFX pause not setup properly.\n");
6162 enable_rc6 = false;
6163 }
6164
6165 if (!I915_READ(GEN8_MISC_CTRL0)) {
6166 DRM_DEBUG_DRIVER("GPM control not setup properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306167 enable_rc6 = false;
6168 }
6169
6170 return enable_rc6;
6171}
6172
Chris Wilsondc979972016-05-10 14:10:04 +01006173int sanitize_rc6_option(struct drm_i915_private *dev_priv, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006174{
Daniel Vettere7d66d82015-06-15 23:23:54 +02006175 /* No RC6 before Ironlake and code is gone for ilk. */
Chris Wilsondc979972016-05-10 14:10:04 +01006176 if (INTEL_INFO(dev_priv)->gen < 6)
Imre Deake6069ca2014-04-18 16:01:02 +03006177 return 0;
6178
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306179 if (!enable_rc6)
6180 return 0;
6181
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02006182 if (IS_GEN9_LP(dev_priv) && !bxt_check_bios_rc6_setup(dev_priv)) {
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306183 DRM_INFO("RC6 disabled by BIOS\n");
6184 return 0;
6185 }
6186
Daniel Vetter456470e2012-08-08 23:35:40 +02006187 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03006188 if (enable_rc6 >= 0) {
6189 int mask;
6190
Chris Wilsondc979972016-05-10 14:10:04 +01006191 if (HAS_RC6p(dev_priv))
Imre Deake6069ca2014-04-18 16:01:02 +03006192 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
6193 INTEL_RC6pp_ENABLE;
6194 else
6195 mask = INTEL_RC6_ENABLE;
6196
6197 if ((enable_rc6 & mask) != enable_rc6)
Imre Deakb99d49c2016-06-29 19:13:54 +03006198 DRM_DEBUG_DRIVER("Adjusting RC6 mask to %d "
6199 "(requested %d, valid %d)\n",
6200 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03006201
6202 return enable_rc6 & mask;
6203 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006204
Chris Wilsondc979972016-05-10 14:10:04 +01006205 if (IS_IVYBRIDGE(dev_priv))
Ben Widawskycca84a12014-01-28 20:25:38 -08006206 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08006207
6208 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006209}
6210
Chris Wilsondc979972016-05-10 14:10:04 +01006211static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
Imre Deake6069ca2014-04-18 16:01:02 +03006212{
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006213 /* All of these values are in units of 50MHz */
Chris Wilson773ea9a2016-07-13 09:10:33 +01006214
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006215 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02006216 if (IS_GEN9_LP(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006217 u32 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07006218 dev_priv->rps.rp0_freq = (rp_state_cap >> 16) & 0xff;
6219 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
6220 dev_priv->rps.min_freq = (rp_state_cap >> 0) & 0xff;
6221 } else {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006222 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07006223 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
6224 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
6225 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
6226 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006227 /* hw_max = RP0 until we check for overclocking */
Chris Wilson773ea9a2016-07-13 09:10:33 +01006228 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006229
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006230 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01006231 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
Rodrigo Vivib976dc52017-01-23 10:32:37 -08006232 IS_GEN9_BC(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006233 u32 ddcc_status = 0;
6234
6235 if (sandybridge_pcode_read(dev_priv,
6236 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
6237 &ddcc_status) == 0)
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006238 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08006239 clamp_t(u8,
6240 ((ddcc_status >> 8) & 0xff),
6241 dev_priv->rps.min_freq,
6242 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006243 }
6244
Rodrigo Vivib976dc52017-01-23 10:32:37 -08006245 if (IS_GEN9_BC(dev_priv)) {
Akash Goelc5e06882015-06-29 14:50:19 +05306246 /* Store the frequency values in 16.66 MHZ units, which is
Chris Wilson773ea9a2016-07-13 09:10:33 +01006247 * the natural hardware unit for SKL
6248 */
Akash Goelc5e06882015-06-29 14:50:19 +05306249 dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
6250 dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
6251 dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
6252 dev_priv->rps.max_freq *= GEN9_FREQ_SCALER;
6253 dev_priv->rps.efficient_freq *= GEN9_FREQ_SCALER;
6254 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006255}
6256
Chris Wilson3a45b052016-07-13 09:10:32 +01006257static void reset_rps(struct drm_i915_private *dev_priv,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006258 int (*set)(struct drm_i915_private *, u8))
Chris Wilson3a45b052016-07-13 09:10:32 +01006259{
6260 u8 freq = dev_priv->rps.cur_freq;
6261
6262 /* force a reset */
6263 dev_priv->rps.power = -1;
6264 dev_priv->rps.cur_freq = -1;
6265
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006266 if (set(dev_priv, freq))
6267 DRM_ERROR("Failed to reset RPS to initial values\n");
Chris Wilson3a45b052016-07-13 09:10:32 +01006268}
6269
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006270/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Chris Wilsondc979972016-05-10 14:10:04 +01006271static void gen9_enable_rps(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00006272{
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006273 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
6274
Akash Goel0beb0592015-03-06 11:07:20 +05306275 /* Program defaults and thresholds for RPS*/
6276 I915_WRITE(GEN6_RC_VIDEO_FREQ,
6277 GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006278
Akash Goel0beb0592015-03-06 11:07:20 +05306279 /* 1 second timeout*/
6280 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
6281 GT_INTERVAL_FROM_US(dev_priv, 1000000));
6282
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006283 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006284
Akash Goel0beb0592015-03-06 11:07:20 +05306285 /* Leaning on the below call to gen6_set_rps to program/setup the
6286 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
6287 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
Chris Wilson3a45b052016-07-13 09:10:32 +01006288 reset_rps(dev_priv, gen6_set_rps);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006289
6290 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
6291}
6292
Chris Wilsondc979972016-05-10 14:10:04 +01006293static void gen9_enable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006294{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006295 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306296 enum intel_engine_id id;
Zhe Wang20e49362014-11-04 17:07:05 +00006297 uint32_t rc6_mask = 0;
Zhe Wang20e49362014-11-04 17:07:05 +00006298
6299 /* 1a: Software RC state - RC0 */
6300 I915_WRITE(GEN6_RC_STATE, 0);
6301
6302 /* 1b: Get forcewake during program sequence. Although the driver
6303 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006304 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00006305
6306 /* 2a: Disable RC states. */
6307 I915_WRITE(GEN6_RC_CONTROL, 0);
6308
6309 /* 2b: Program RC6 thresholds.*/
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05306310
6311 /* WaRsDoubleRc6WrlWithCoarsePowerGating: Doubling WRL only when CPG is enabled */
Chris Wilsondc979972016-05-10 14:10:04 +01006312 if (IS_SKYLAKE(dev_priv))
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05306313 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
6314 else
6315 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
Zhe Wang20e49362014-11-04 17:07:05 +00006316 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6317 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05306318 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006319 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05306320
Dave Gordon1a3d1892016-05-13 15:36:30 +01006321 if (HAS_GUC(dev_priv))
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05306322 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
6323
Zhe Wang20e49362014-11-04 17:07:05 +00006324 I915_WRITE(GEN6_RC_SLEEP, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00006325
Zhe Wang38c23522015-01-20 12:23:04 +00006326 /* 2c: Program Coarse Power Gating Policies. */
6327 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
6328 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
6329
Zhe Wang20e49362014-11-04 17:07:05 +00006330 /* 3a: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006331 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Zhe Wang20e49362014-11-04 17:07:05 +00006332 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Jani Nikula87ad3212016-01-14 12:53:34 +02006333 DRM_INFO("RC6 %s\n", onoff(rc6_mask & GEN6_RC_CTL_RC6_ENABLE));
Chris Wilson1c044f92017-01-25 17:26:01 +00006334 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
6335 I915_WRITE(GEN6_RC_CONTROL,
6336 GEN6_RC_CTL_HW_ENABLE | GEN6_RC_CTL_EI_MODE(1) | rc6_mask);
Zhe Wang20e49362014-11-04 17:07:05 +00006337
Sagar Kamblecb07bae2015-04-12 11:28:14 +05306338 /*
6339 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05306340 * WaRsDisableCoarsePowerGating:skl,bxt - Render/Media PG need to be disabled with RC6.
Sagar Kamblecb07bae2015-04-12 11:28:14 +05306341 */
Chris Wilsondc979972016-05-10 14:10:04 +01006342 if (NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05306343 I915_WRITE(GEN9_PG_ENABLE, 0);
6344 else
6345 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
6346 (GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE) : 0);
Zhe Wang38c23522015-01-20 12:23:04 +00006347
Mika Kuoppala59bad942015-01-16 11:34:40 +02006348 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00006349}
6350
Chris Wilsondc979972016-05-10 14:10:04 +01006351static void gen8_enable_rps(struct drm_i915_private *dev_priv)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006352{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006353 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306354 enum intel_engine_id id;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006355 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006356
6357 /* 1a: Software RC state - RC0 */
6358 I915_WRITE(GEN6_RC_STATE, 0);
6359
6360 /* 1c & 1d: Get forcewake during program sequence. Although the driver
6361 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006362 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006363
6364 /* 2a: Disable RC states. */
6365 I915_WRITE(GEN6_RC_CONTROL, 0);
6366
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006367 /* 2b: Program RC6 thresholds.*/
6368 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
6369 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6370 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05306371 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006372 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006373 I915_WRITE(GEN6_RC_SLEEP, 0);
Chris Wilsondc979972016-05-10 14:10:04 +01006374 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07006375 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
6376 else
6377 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006378
6379 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006380 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006381 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Chris Wilsondc979972016-05-10 14:10:04 +01006382 intel_print_rc6_info(dev_priv, rc6_mask);
6383 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07006384 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
6385 GEN7_RC_CTL_TO_MODE |
6386 rc6_mask);
6387 else
6388 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
6389 GEN6_RC_CTL_EI_MODE(1) |
6390 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006391
6392 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07006393 I915_WRITE(GEN6_RPNSWREQ,
6394 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
6395 I915_WRITE(GEN6_RC_VIDEO_FREQ,
6396 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02006397 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
6398 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006399
Daniel Vetter7526ed72014-09-29 15:07:19 +02006400 /* Docs recommend 900MHz, and 300 MHz respectively */
6401 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
6402 dev_priv->rps.max_freq_softlimit << 24 |
6403 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006404
Daniel Vetter7526ed72014-09-29 15:07:19 +02006405 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
6406 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
6407 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
6408 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006409
Daniel Vetter7526ed72014-09-29 15:07:19 +02006410 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006411
6412 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02006413 I915_WRITE(GEN6_RP_CONTROL,
6414 GEN6_RP_MEDIA_TURBO |
6415 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6416 GEN6_RP_MEDIA_IS_GFX |
6417 GEN6_RP_ENABLE |
6418 GEN6_RP_UP_BUSY_AVG |
6419 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006420
Daniel Vetter7526ed72014-09-29 15:07:19 +02006421 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006422
Chris Wilson3a45b052016-07-13 09:10:32 +01006423 reset_rps(dev_priv, gen6_set_rps);
Daniel Vetter7526ed72014-09-29 15:07:19 +02006424
Mika Kuoppala59bad942015-01-16 11:34:40 +02006425 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006426}
6427
Chris Wilsondc979972016-05-10 14:10:04 +01006428static void gen6_enable_rps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006429{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006430 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306431 enum intel_engine_id id;
Chris Wilson99ac9612016-07-13 09:10:34 +01006432 u32 rc6vids, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006433 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006434 int rc6_mode;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00006435 int ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006436
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006437 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02006438
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006439 /* Here begins a magic sequence of register writes to enable
6440 * auto-downclocking.
6441 *
6442 * Perhaps there might be some value in exposing these to
6443 * userspace...
6444 */
6445 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006446
6447 /* Clear the DBG now so we don't confuse earlier errors */
Ville Syrjälä297b32e2016-04-13 21:09:30 +03006448 gtfifodbg = I915_READ(GTFIFODBG);
6449 if (gtfifodbg) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006450 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
6451 I915_WRITE(GTFIFODBG, gtfifodbg);
6452 }
6453
Mika Kuoppala59bad942015-01-16 11:34:40 +02006454 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006455
6456 /* disable the counters and set deterministic thresholds */
6457 I915_WRITE(GEN6_RC_CONTROL, 0);
6458
6459 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
6460 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
6461 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
6462 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
6463 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
6464
Akash Goel3b3f1652016-10-13 22:44:48 +05306465 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006466 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006467
6468 I915_WRITE(GEN6_RC_SLEEP, 0);
6469 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Chris Wilsondc979972016-05-10 14:10:04 +01006470 if (IS_IVYBRIDGE(dev_priv))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07006471 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
6472 else
6473 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08006474 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006475 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
6476
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006477 /* Check if we are enabling RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006478 rc6_mode = intel_enable_rc6();
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006479 if (rc6_mode & INTEL_RC6_ENABLE)
6480 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
6481
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006482 /* We don't use those on Haswell */
Chris Wilsondc979972016-05-10 14:10:04 +01006483 if (!IS_HASWELL(dev_priv)) {
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006484 if (rc6_mode & INTEL_RC6p_ENABLE)
6485 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006486
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006487 if (rc6_mode & INTEL_RC6pp_ENABLE)
6488 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
6489 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006490
Chris Wilsondc979972016-05-10 14:10:04 +01006491 intel_print_rc6_info(dev_priv, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006492
6493 I915_WRITE(GEN6_RC_CONTROL,
6494 rc6_mask |
6495 GEN6_RC_CTL_EI_MODE(1) |
6496 GEN6_RC_CTL_HW_ENABLE);
6497
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006498 /* Power down if completely idle for over 50ms */
6499 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006500 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006501
Chris Wilson3a45b052016-07-13 09:10:32 +01006502 reset_rps(dev_priv, gen6_set_rps);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006503
Ben Widawsky31643d52012-09-26 10:34:01 -07006504 rc6vids = 0;
6505 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
Chris Wilsondc979972016-05-10 14:10:04 +01006506 if (IS_GEN6(dev_priv) && ret) {
Ben Widawsky31643d52012-09-26 10:34:01 -07006507 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
Chris Wilsondc979972016-05-10 14:10:04 +01006508 } else if (IS_GEN6(dev_priv) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
Ben Widawsky31643d52012-09-26 10:34:01 -07006509 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
6510 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
6511 rc6vids &= 0xffff00;
6512 rc6vids |= GEN6_ENCODE_RC6_VID(450);
6513 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
6514 if (ret)
6515 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
6516 }
6517
Mika Kuoppala59bad942015-01-16 11:34:40 +02006518 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006519}
6520
Chris Wilsonfb7404e2016-07-13 09:10:38 +01006521static void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006522{
6523 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01006524 unsigned int gpu_freq;
6525 unsigned int max_ia_freq, min_ring_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05306526 unsigned int max_gpu_freq, min_gpu_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006527 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03006528 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006529
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006530 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02006531
Ben Widawskyeda79642013-10-07 17:15:48 -03006532 policy = cpufreq_cpu_get(0);
6533 if (policy) {
6534 max_ia_freq = policy->cpuinfo.max_freq;
6535 cpufreq_cpu_put(policy);
6536 } else {
6537 /*
6538 * Default to measured freq if none found, PCU will ensure we
6539 * don't go over
6540 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006541 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03006542 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006543
6544 /* Convert from kHz to MHz */
6545 max_ia_freq /= 1000;
6546
Ben Widawsky153b4b952013-10-22 22:05:09 -07006547 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07006548 /* convert DDR frequency from units of 266.6MHz to bandwidth */
6549 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01006550
Rodrigo Vivib976dc52017-01-23 10:32:37 -08006551 if (IS_GEN9_BC(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05306552 /* Convert GT frequency to 50 HZ units */
6553 min_gpu_freq = dev_priv->rps.min_freq / GEN9_FREQ_SCALER;
6554 max_gpu_freq = dev_priv->rps.max_freq / GEN9_FREQ_SCALER;
6555 } else {
6556 min_gpu_freq = dev_priv->rps.min_freq;
6557 max_gpu_freq = dev_priv->rps.max_freq;
6558 }
6559
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006560 /*
6561 * For each potential GPU frequency, load a ring frequency we'd like
6562 * to use for memory access. We do this by specifying the IA frequency
6563 * the PCU should use as a reference to determine the ring frequency.
6564 */
Akash Goel4c8c7742015-06-29 14:50:20 +05306565 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
6566 int diff = max_gpu_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01006567 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006568
Rodrigo Vivib976dc52017-01-23 10:32:37 -08006569 if (IS_GEN9_BC(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05306570 /*
6571 * ring_freq = 2 * GT. ring_freq is in 100MHz units
6572 * No floor required for ring frequency on SKL.
6573 */
6574 ring_freq = gpu_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01006575 } else if (INTEL_INFO(dev_priv)->gen >= 8) {
Ben Widawsky46c764d2013-11-02 21:07:49 -07006576 /* max(2 * GT, DDR). NB: GT is 50MHz units */
6577 ring_freq = max(min_ring_freq, gpu_freq);
Chris Wilsondc979972016-05-10 14:10:04 +01006578 } else if (IS_HASWELL(dev_priv)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07006579 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01006580 ring_freq = max(min_ring_freq, ring_freq);
6581 /* leave ia_freq as the default, chosen by cpufreq */
6582 } else {
6583 /* On older processors, there is no separate ring
6584 * clock domain, so in order to boost the bandwidth
6585 * of the ring, we need to upclock the CPU (ia_freq).
6586 *
6587 * For GPU frequencies less than 750MHz,
6588 * just use the lowest ring freq.
6589 */
6590 if (gpu_freq < min_freq)
6591 ia_freq = 800;
6592 else
6593 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
6594 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
6595 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006596
Ben Widawsky42c05262012-09-26 10:34:00 -07006597 sandybridge_pcode_write(dev_priv,
6598 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01006599 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
6600 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
6601 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006602 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006603}
6604
Ville Syrjälä03af2042014-06-28 02:03:53 +03006605static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05306606{
6607 u32 val, rp0;
6608
Jani Nikula5b5929c2015-10-07 11:17:46 +03006609 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05306610
Imre Deak43b67992016-08-31 19:13:02 +03006611 switch (INTEL_INFO(dev_priv)->sseu.eu_total) {
Jani Nikula5b5929c2015-10-07 11:17:46 +03006612 case 8:
6613 /* (2 * 4) config */
6614 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
6615 break;
6616 case 12:
6617 /* (2 * 6) config */
6618 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
6619 break;
6620 case 16:
6621 /* (2 * 8) config */
6622 default:
6623 /* Setting (2 * 8) Min RP0 for any other combination */
6624 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
6625 break;
Deepak S095acd52015-01-17 11:05:59 +05306626 }
Jani Nikula5b5929c2015-10-07 11:17:46 +03006627
6628 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
6629
Deepak S2b6b3a02014-05-27 15:59:30 +05306630 return rp0;
6631}
6632
6633static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
6634{
6635 u32 val, rpe;
6636
6637 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
6638 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
6639
6640 return rpe;
6641}
6642
Deepak S7707df42014-07-12 18:46:14 +05306643static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
6644{
6645 u32 val, rp1;
6646
Jani Nikula5b5929c2015-10-07 11:17:46 +03006647 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
6648 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
6649
Deepak S7707df42014-07-12 18:46:14 +05306650 return rp1;
6651}
6652
Deepak S96676fe2016-08-12 18:46:41 +05306653static u32 cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
6654{
6655 u32 val, rpn;
6656
6657 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
6658 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
6659 FB_GFX_FREQ_FUSE_MASK);
6660
6661 return rpn;
6662}
6663
Deepak Sf8f2b002014-07-10 13:16:21 +05306664static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
6665{
6666 u32 val, rp1;
6667
6668 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
6669
6670 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
6671
6672 return rp1;
6673}
6674
Ville Syrjälä03af2042014-06-28 02:03:53 +03006675static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006676{
6677 u32 val, rp0;
6678
Jani Nikula64936252013-05-22 15:36:20 +03006679 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006680
6681 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
6682 /* Clamp to max */
6683 rp0 = min_t(u32, rp0, 0xea);
6684
6685 return rp0;
6686}
6687
6688static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
6689{
6690 u32 val, rpe;
6691
Jani Nikula64936252013-05-22 15:36:20 +03006692 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006693 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03006694 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006695 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
6696
6697 return rpe;
6698}
6699
Ville Syrjälä03af2042014-06-28 02:03:53 +03006700static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006701{
Imre Deak36146032014-12-04 18:39:35 +02006702 u32 val;
6703
6704 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
6705 /*
6706 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
6707 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
6708 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
6709 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
6710 * to make sure it matches what Punit accepts.
6711 */
6712 return max_t(u32, val, 0xc0);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006713}
6714
Imre Deakae484342014-03-31 15:10:44 +03006715/* Check that the pctx buffer wasn't move under us. */
6716static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
6717{
6718 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
6719
6720 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
6721 dev_priv->vlv_pctx->stolen->start);
6722}
6723
Deepak S38807742014-05-23 21:00:15 +05306724
6725/* Check that the pcbr address is not empty. */
6726static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
6727{
6728 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
6729
6730 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
6731}
6732
Chris Wilsondc979972016-05-10 14:10:04 +01006733static void cherryview_setup_pctx(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306734{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02006735 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006736 unsigned long pctx_paddr, paddr;
Deepak S38807742014-05-23 21:00:15 +05306737 u32 pcbr;
6738 int pctx_size = 32*1024;
6739
Deepak S38807742014-05-23 21:00:15 +05306740 pcbr = I915_READ(VLV_PCBR);
6741 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006742 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05306743 paddr = (dev_priv->mm.stolen_base +
Joonas Lahtinen62106b42016-03-18 10:42:57 +02006744 (ggtt->stolen_size - pctx_size));
Deepak S38807742014-05-23 21:00:15 +05306745
6746 pctx_paddr = (paddr & (~4095));
6747 I915_WRITE(VLV_PCBR, pctx_paddr);
6748 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006749
6750 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05306751}
6752
Chris Wilsondc979972016-05-10 14:10:04 +01006753static void valleyview_setup_pctx(struct drm_i915_private *dev_priv)
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006754{
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006755 struct drm_i915_gem_object *pctx;
6756 unsigned long pctx_paddr;
6757 u32 pcbr;
6758 int pctx_size = 24*1024;
6759
6760 pcbr = I915_READ(VLV_PCBR);
6761 if (pcbr) {
6762 /* BIOS set it up already, grab the pre-alloc'd space */
6763 int pcbr_offset;
6764
6765 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00006766 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006767 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02006768 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006769 pctx_size);
6770 goto out;
6771 }
6772
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006773 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
6774
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006775 /*
6776 * From the Gunit register HAS:
6777 * The Gfx driver is expected to program this register and ensure
6778 * proper allocation within Gfx stolen memory. For example, this
6779 * register should be programmed such than the PCBR range does not
6780 * overlap with other ranges, such as the frame buffer, protected
6781 * memory, or any other relevant ranges.
6782 */
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00006783 pctx = i915_gem_object_create_stolen(dev_priv, pctx_size);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006784 if (!pctx) {
6785 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
Tvrtko Ursulinee504892016-02-11 10:27:30 +00006786 goto out;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006787 }
6788
6789 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
6790 I915_WRITE(VLV_PCBR, pctx_paddr);
6791
6792out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006793 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006794 dev_priv->vlv_pctx = pctx;
6795}
6796
Chris Wilsondc979972016-05-10 14:10:04 +01006797static void valleyview_cleanup_pctx(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006798{
Imre Deakae484342014-03-31 15:10:44 +03006799 if (WARN_ON(!dev_priv->vlv_pctx))
6800 return;
6801
Chris Wilsonf0cd5182016-10-28 13:58:43 +01006802 i915_gem_object_put(dev_priv->vlv_pctx);
Imre Deakae484342014-03-31 15:10:44 +03006803 dev_priv->vlv_pctx = NULL;
6804}
6805
Ville Syrjäläc30fec62016-03-04 21:43:02 +02006806static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
6807{
6808 dev_priv->rps.gpll_ref_freq =
6809 vlv_get_cck_clock(dev_priv, "GPLL ref",
6810 CCK_GPLL_CLOCK_CONTROL,
6811 dev_priv->czclk_freq);
6812
6813 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
6814 dev_priv->rps.gpll_ref_freq);
6815}
6816
Chris Wilsondc979972016-05-10 14:10:04 +01006817static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03006818{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006819 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03006820
Chris Wilsondc979972016-05-10 14:10:04 +01006821 valleyview_setup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03006822
Ville Syrjäläc30fec62016-03-04 21:43:02 +02006823 vlv_init_gpll_ref_freq(dev_priv);
6824
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006825 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
6826 switch ((val >> 6) & 3) {
6827 case 0:
6828 case 1:
6829 dev_priv->mem_freq = 800;
6830 break;
6831 case 2:
6832 dev_priv->mem_freq = 1066;
6833 break;
6834 case 3:
6835 dev_priv->mem_freq = 1333;
6836 break;
6837 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02006838 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006839
Imre Deak4e805192014-04-14 20:24:41 +03006840 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
6841 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
6842 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006843 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03006844 dev_priv->rps.max_freq);
6845
6846 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
6847 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006848 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03006849 dev_priv->rps.efficient_freq);
6850
Deepak Sf8f2b002014-07-10 13:16:21 +05306851 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
6852 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006853 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05306854 dev_priv->rps.rp1_freq);
6855
Imre Deak4e805192014-04-14 20:24:41 +03006856 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
6857 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006858 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03006859 dev_priv->rps.min_freq);
Imre Deak4e805192014-04-14 20:24:41 +03006860}
6861
Chris Wilsondc979972016-05-10 14:10:04 +01006862static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306863{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006864 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05306865
Chris Wilsondc979972016-05-10 14:10:04 +01006866 cherryview_setup_pctx(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05306867
Ville Syrjäläc30fec62016-03-04 21:43:02 +02006868 vlv_init_gpll_ref_freq(dev_priv);
6869
Ville Syrjäläa5805162015-05-26 20:42:30 +03006870 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02006871 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006872 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02006873
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006874 switch ((val >> 2) & 0x7) {
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006875 case 3:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006876 dev_priv->mem_freq = 2000;
6877 break;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006878 default:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006879 dev_priv->mem_freq = 1600;
6880 break;
6881 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02006882 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006883
Deepak S2b6b3a02014-05-27 15:59:30 +05306884 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
6885 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
6886 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006887 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05306888 dev_priv->rps.max_freq);
6889
6890 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
6891 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006892 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05306893 dev_priv->rps.efficient_freq);
6894
Deepak S7707df42014-07-12 18:46:14 +05306895 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
6896 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006897 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05306898 dev_priv->rps.rp1_freq);
6899
Deepak S96676fe2016-08-12 18:46:41 +05306900 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05306901 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006902 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05306903 dev_priv->rps.min_freq);
6904
Ville Syrjälä1c147622014-08-18 14:42:43 +03006905 WARN_ONCE((dev_priv->rps.max_freq |
6906 dev_priv->rps.efficient_freq |
6907 dev_priv->rps.rp1_freq |
6908 dev_priv->rps.min_freq) & 1,
6909 "Odd GPU freq values\n");
Deepak S38807742014-05-23 21:00:15 +05306910}
6911
Chris Wilsondc979972016-05-10 14:10:04 +01006912static void valleyview_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03006913{
Chris Wilsondc979972016-05-10 14:10:04 +01006914 valleyview_cleanup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03006915}
6916
Chris Wilsondc979972016-05-10 14:10:04 +01006917static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306918{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006919 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306920 enum intel_engine_id id;
Deepak S2b6b3a02014-05-27 15:59:30 +05306921 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05306922
6923 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
6924
Ville Syrjälä297b32e2016-04-13 21:09:30 +03006925 gtfifodbg = I915_READ(GTFIFODBG) & ~(GT_FIFO_SBDEDICATE_FREE_ENTRY_CHV |
6926 GT_FIFO_FREE_ENTRIES_CHV);
Deepak S38807742014-05-23 21:00:15 +05306927 if (gtfifodbg) {
6928 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
6929 gtfifodbg);
6930 I915_WRITE(GTFIFODBG, gtfifodbg);
6931 }
6932
6933 cherryview_check_pctx(dev_priv);
6934
6935 /* 1a & 1b: Get forcewake during program sequence. Although the driver
6936 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006937 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05306938
Ville Syrjälä160614a2015-01-19 13:50:47 +02006939 /* Disable RC states. */
6940 I915_WRITE(GEN6_RC_CONTROL, 0);
6941
Deepak S38807742014-05-23 21:00:15 +05306942 /* 2a: Program RC6 thresholds.*/
6943 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
6944 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6945 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
6946
Akash Goel3b3f1652016-10-13 22:44:48 +05306947 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006948 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Deepak S38807742014-05-23 21:00:15 +05306949 I915_WRITE(GEN6_RC_SLEEP, 0);
6950
Deepak Sf4f71c72015-03-28 15:23:35 +05306951 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
6952 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
Deepak S38807742014-05-23 21:00:15 +05306953
6954 /* allows RC6 residency counter to work */
6955 I915_WRITE(VLV_COUNTER_CONTROL,
6956 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
6957 VLV_MEDIA_RC6_COUNT_EN |
6958 VLV_RENDER_RC6_COUNT_EN));
6959
6960 /* For now we assume BIOS is allocating and populating the PCBR */
6961 pcbr = I915_READ(VLV_PCBR);
6962
Deepak S38807742014-05-23 21:00:15 +05306963 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006964 if ((intel_enable_rc6() & INTEL_RC6_ENABLE) &&
6965 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02006966 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05306967
6968 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
6969
Deepak S2b6b3a02014-05-27 15:59:30 +05306970 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02006971 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05306972 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
6973 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
6974 I915_WRITE(GEN6_RP_UP_EI, 66000);
6975 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
6976
6977 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
6978
6979 /* 5: Enable RPS */
6980 I915_WRITE(GEN6_RP_CONTROL,
6981 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02006982 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05306983 GEN6_RP_ENABLE |
6984 GEN6_RP_UP_BUSY_AVG |
6985 GEN6_RP_DOWN_IDLE_AVG);
6986
Deepak S3ef62342015-04-29 08:36:24 +05306987 /* Setting Fixed Bias */
6988 val = VLV_OVERRIDE_EN |
6989 VLV_SOC_TDP_EN |
6990 CHV_BIAS_CPU_50_SOC_50;
6991 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
6992
Deepak S2b6b3a02014-05-27 15:59:30 +05306993 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
6994
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02006995 /* RPS code assumes GPLL is used */
6996 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
6997
Jani Nikula742f4912015-09-03 11:16:09 +03006998 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05306999 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7000
Chris Wilson3a45b052016-07-13 09:10:32 +01007001 reset_rps(dev_priv, valleyview_set_rps);
Deepak S2b6b3a02014-05-27 15:59:30 +05307002
Mika Kuoppala59bad942015-01-16 11:34:40 +02007003 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05307004}
7005
Chris Wilsondc979972016-05-10 14:10:04 +01007006static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07007007{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007008 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05307009 enum intel_engine_id id;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07007010 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07007011
7012 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
7013
Imre Deakae484342014-03-31 15:10:44 +03007014 valleyview_check_pctx(dev_priv);
7015
Ville Syrjälä297b32e2016-04-13 21:09:30 +03007016 gtfifodbg = I915_READ(GTFIFODBG);
7017 if (gtfifodbg) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07007018 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
7019 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007020 I915_WRITE(GTFIFODBG, gtfifodbg);
7021 }
7022
Deepak Sc8d9a592013-11-23 14:55:42 +05307023 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02007024 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007025
Ville Syrjälä160614a2015-01-19 13:50:47 +02007026 /* Disable RC states. */
7027 I915_WRITE(GEN6_RC_CONTROL, 0);
7028
Ville Syrjäläcad725f2015-01-19 13:50:48 +02007029 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007030 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
7031 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
7032 I915_WRITE(GEN6_RP_UP_EI, 66000);
7033 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
7034
7035 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7036
7037 I915_WRITE(GEN6_RP_CONTROL,
7038 GEN6_RP_MEDIA_TURBO |
7039 GEN6_RP_MEDIA_HW_NORMAL_MODE |
7040 GEN6_RP_MEDIA_IS_GFX |
7041 GEN6_RP_ENABLE |
7042 GEN6_RP_UP_BUSY_AVG |
7043 GEN6_RP_DOWN_IDLE_CONT);
7044
7045 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
7046 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
7047 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
7048
Akash Goel3b3f1652016-10-13 22:44:48 +05307049 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007050 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007051
Jesse Barnes2f0aa3042013-11-15 09:32:11 -08007052 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007053
7054 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07007055 I915_WRITE(VLV_COUNTER_CONTROL,
Mika Kuoppala6b7f6aa2017-03-15 18:12:59 +02007056 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
7057 VLV_MEDIA_RC0_COUNT_EN |
Deepak S31685c22014-07-03 17:33:01 -04007058 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07007059 VLV_MEDIA_RC6_COUNT_EN |
7060 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04007061
Chris Wilsondc979972016-05-10 14:10:04 +01007062 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08007063 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07007064
Chris Wilsondc979972016-05-10 14:10:04 +01007065 intel_print_rc6_info(dev_priv, rc6_mode);
Ben Widawskydc39fff2013-10-18 12:32:07 -07007066
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07007067 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007068
Deepak S3ef62342015-04-29 08:36:24 +05307069 /* Setting Fixed Bias */
7070 val = VLV_OVERRIDE_EN |
7071 VLV_SOC_TDP_EN |
7072 VLV_BIAS_CPU_125_SOC_875;
7073 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
7074
Jani Nikula64936252013-05-22 15:36:20 +03007075 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007076
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02007077 /* RPS code assumes GPLL is used */
7078 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
7079
Jani Nikula742f4912015-09-03 11:16:09 +03007080 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07007081 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7082
Chris Wilson3a45b052016-07-13 09:10:32 +01007083 reset_rps(dev_priv, valleyview_set_rps);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007084
Mika Kuoppala59bad942015-01-16 11:34:40 +02007085 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007086}
7087
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007088static unsigned long intel_pxfreq(u32 vidfreq)
7089{
7090 unsigned long freq;
7091 int div = (vidfreq & 0x3f0000) >> 16;
7092 int post = (vidfreq & 0x3000) >> 12;
7093 int pre = (vidfreq & 0x7);
7094
7095 if (!pre)
7096 return 0;
7097
7098 freq = ((div * 133333) / ((1<<post) * pre));
7099
7100 return freq;
7101}
7102
Daniel Vettereb48eb02012-04-26 23:28:12 +02007103static const struct cparams {
7104 u16 i;
7105 u16 t;
7106 u16 m;
7107 u16 c;
7108} cparams[] = {
7109 { 1, 1333, 301, 28664 },
7110 { 1, 1066, 294, 24460 },
7111 { 1, 800, 294, 25192 },
7112 { 0, 1333, 276, 27605 },
7113 { 0, 1066, 276, 27605 },
7114 { 0, 800, 231, 23784 },
7115};
7116
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007117static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007118{
7119 u64 total_count, diff, ret;
7120 u32 count1, count2, count3, m = 0, c = 0;
7121 unsigned long now = jiffies_to_msecs(jiffies), diff1;
7122 int i;
7123
Chris Wilson67520412017-03-02 13:28:01 +00007124 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007125
Daniel Vetter20e4d402012-08-08 23:35:39 +02007126 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007127
7128 /* Prevent division-by-zero if we are asking too fast.
7129 * Also, we don't get interesting results if we are polling
7130 * faster than once in 10ms, so just return the saved value
7131 * in such cases.
7132 */
7133 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02007134 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007135
7136 count1 = I915_READ(DMIEC);
7137 count2 = I915_READ(DDREC);
7138 count3 = I915_READ(CSIEC);
7139
7140 total_count = count1 + count2 + count3;
7141
7142 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02007143 if (total_count < dev_priv->ips.last_count1) {
7144 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007145 diff += total_count;
7146 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007147 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007148 }
7149
7150 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007151 if (cparams[i].i == dev_priv->ips.c_m &&
7152 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02007153 m = cparams[i].m;
7154 c = cparams[i].c;
7155 break;
7156 }
7157 }
7158
7159 diff = div_u64(diff, diff1);
7160 ret = ((m * diff) + c);
7161 ret = div_u64(ret, 10);
7162
Daniel Vetter20e4d402012-08-08 23:35:39 +02007163 dev_priv->ips.last_count1 = total_count;
7164 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007165
Daniel Vetter20e4d402012-08-08 23:35:39 +02007166 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007167
7168 return ret;
7169}
7170
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007171unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
7172{
7173 unsigned long val;
7174
Chris Wilsondc979972016-05-10 14:10:04 +01007175 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007176 return 0;
7177
7178 spin_lock_irq(&mchdev_lock);
7179
7180 val = __i915_chipset_val(dev_priv);
7181
7182 spin_unlock_irq(&mchdev_lock);
7183
7184 return val;
7185}
7186
Daniel Vettereb48eb02012-04-26 23:28:12 +02007187unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
7188{
7189 unsigned long m, x, b;
7190 u32 tsfs;
7191
7192 tsfs = I915_READ(TSFS);
7193
7194 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
7195 x = I915_READ8(TR1);
7196
7197 b = tsfs & TSFS_INTR_MASK;
7198
7199 return ((m * x) / 127) - b;
7200}
7201
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007202static int _pxvid_to_vd(u8 pxvid)
7203{
7204 if (pxvid == 0)
7205 return 0;
7206
7207 if (pxvid >= 8 && pxvid < 31)
7208 pxvid = 31;
7209
7210 return (pxvid + 2) * 125;
7211}
7212
7213static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007214{
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007215 const int vd = _pxvid_to_vd(pxvid);
7216 const int vm = vd - 1125;
7217
Chris Wilsondc979972016-05-10 14:10:04 +01007218 if (INTEL_INFO(dev_priv)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007219 return vm > 0 ? vm : 0;
7220
7221 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007222}
7223
Daniel Vetter02d71952012-08-09 16:44:54 +02007224static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007225{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00007226 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007227 u32 count;
7228
Chris Wilson67520412017-03-02 13:28:01 +00007229 lockdep_assert_held(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007230
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00007231 now = ktime_get_raw_ns();
7232 diffms = now - dev_priv->ips.last_time2;
7233 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007234
7235 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02007236 if (!diffms)
7237 return;
7238
7239 count = I915_READ(GFXEC);
7240
Daniel Vetter20e4d402012-08-08 23:35:39 +02007241 if (count < dev_priv->ips.last_count2) {
7242 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007243 diff += count;
7244 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007245 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007246 }
7247
Daniel Vetter20e4d402012-08-08 23:35:39 +02007248 dev_priv->ips.last_count2 = count;
7249 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007250
7251 /* More magic constants... */
7252 diff = diff * 1181;
7253 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02007254 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007255}
7256
Daniel Vetter02d71952012-08-09 16:44:54 +02007257void i915_update_gfx_val(struct drm_i915_private *dev_priv)
7258{
Chris Wilsondc979972016-05-10 14:10:04 +01007259 if (INTEL_INFO(dev_priv)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02007260 return;
7261
Daniel Vetter92703882012-08-09 16:46:01 +02007262 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007263
7264 __i915_update_gfx_val(dev_priv);
7265
Daniel Vetter92703882012-08-09 16:46:01 +02007266 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007267}
7268
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007269static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007270{
7271 unsigned long t, corr, state1, corr2, state2;
7272 u32 pxvid, ext_v;
7273
Chris Wilson67520412017-03-02 13:28:01 +00007274 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007275
Ville Syrjälä616847e2015-09-18 20:03:19 +03007276 pxvid = I915_READ(PXVFREQ(dev_priv->rps.cur_freq));
Daniel Vettereb48eb02012-04-26 23:28:12 +02007277 pxvid = (pxvid >> 24) & 0x7f;
7278 ext_v = pvid_to_extvid(dev_priv, pxvid);
7279
7280 state1 = ext_v;
7281
7282 t = i915_mch_val(dev_priv);
7283
7284 /* Revel in the empirically derived constants */
7285
7286 /* Correction factor in 1/100000 units */
7287 if (t > 80)
7288 corr = ((t * 2349) + 135940);
7289 else if (t >= 50)
7290 corr = ((t * 964) + 29317);
7291 else /* < 50 */
7292 corr = ((t * 301) + 1004);
7293
7294 corr = corr * ((150142 * state1) / 10000 - 78642);
7295 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02007296 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007297
7298 state2 = (corr2 * state1) / 10000;
7299 state2 /= 100; /* convert to mW */
7300
Daniel Vetter02d71952012-08-09 16:44:54 +02007301 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007302
Daniel Vetter20e4d402012-08-08 23:35:39 +02007303 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007304}
7305
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007306unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
7307{
7308 unsigned long val;
7309
Chris Wilsondc979972016-05-10 14:10:04 +01007310 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007311 return 0;
7312
7313 spin_lock_irq(&mchdev_lock);
7314
7315 val = __i915_gfx_val(dev_priv);
7316
7317 spin_unlock_irq(&mchdev_lock);
7318
7319 return val;
7320}
7321
Daniel Vettereb48eb02012-04-26 23:28:12 +02007322/**
7323 * i915_read_mch_val - return value for IPS use
7324 *
7325 * Calculate and return a value for the IPS driver to use when deciding whether
7326 * we have thermal and power headroom to increase CPU or GPU power budget.
7327 */
7328unsigned long i915_read_mch_val(void)
7329{
7330 struct drm_i915_private *dev_priv;
7331 unsigned long chipset_val, graphics_val, ret = 0;
7332
Daniel Vetter92703882012-08-09 16:46:01 +02007333 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007334 if (!i915_mch_dev)
7335 goto out_unlock;
7336 dev_priv = i915_mch_dev;
7337
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007338 chipset_val = __i915_chipset_val(dev_priv);
7339 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007340
7341 ret = chipset_val + graphics_val;
7342
7343out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007344 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007345
7346 return ret;
7347}
7348EXPORT_SYMBOL_GPL(i915_read_mch_val);
7349
7350/**
7351 * i915_gpu_raise - raise GPU frequency limit
7352 *
7353 * Raise the limit; IPS indicates we have thermal headroom.
7354 */
7355bool i915_gpu_raise(void)
7356{
7357 struct drm_i915_private *dev_priv;
7358 bool ret = true;
7359
Daniel Vetter92703882012-08-09 16:46:01 +02007360 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007361 if (!i915_mch_dev) {
7362 ret = false;
7363 goto out_unlock;
7364 }
7365 dev_priv = i915_mch_dev;
7366
Daniel Vetter20e4d402012-08-08 23:35:39 +02007367 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
7368 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007369
7370out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007371 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007372
7373 return ret;
7374}
7375EXPORT_SYMBOL_GPL(i915_gpu_raise);
7376
7377/**
7378 * i915_gpu_lower - lower GPU frequency limit
7379 *
7380 * IPS indicates we're close to a thermal limit, so throttle back the GPU
7381 * frequency maximum.
7382 */
7383bool i915_gpu_lower(void)
7384{
7385 struct drm_i915_private *dev_priv;
7386 bool ret = true;
7387
Daniel Vetter92703882012-08-09 16:46:01 +02007388 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007389 if (!i915_mch_dev) {
7390 ret = false;
7391 goto out_unlock;
7392 }
7393 dev_priv = i915_mch_dev;
7394
Daniel Vetter20e4d402012-08-08 23:35:39 +02007395 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
7396 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007397
7398out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007399 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007400
7401 return ret;
7402}
7403EXPORT_SYMBOL_GPL(i915_gpu_lower);
7404
7405/**
7406 * i915_gpu_busy - indicate GPU business to IPS
7407 *
7408 * Tell the IPS driver whether or not the GPU is busy.
7409 */
7410bool i915_gpu_busy(void)
7411{
Daniel Vettereb48eb02012-04-26 23:28:12 +02007412 bool ret = false;
7413
Daniel Vetter92703882012-08-09 16:46:01 +02007414 spin_lock_irq(&mchdev_lock);
Chris Wilsondcff85c2016-08-05 10:14:11 +01007415 if (i915_mch_dev)
7416 ret = i915_mch_dev->gt.awake;
Daniel Vetter92703882012-08-09 16:46:01 +02007417 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007418
7419 return ret;
7420}
7421EXPORT_SYMBOL_GPL(i915_gpu_busy);
7422
7423/**
7424 * i915_gpu_turbo_disable - disable graphics turbo
7425 *
7426 * Disable graphics turbo by resetting the max frequency and setting the
7427 * current frequency to the default.
7428 */
7429bool i915_gpu_turbo_disable(void)
7430{
7431 struct drm_i915_private *dev_priv;
7432 bool ret = true;
7433
Daniel Vetter92703882012-08-09 16:46:01 +02007434 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007435 if (!i915_mch_dev) {
7436 ret = false;
7437 goto out_unlock;
7438 }
7439 dev_priv = i915_mch_dev;
7440
Daniel Vetter20e4d402012-08-08 23:35:39 +02007441 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007442
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01007443 if (!ironlake_set_drps(dev_priv, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02007444 ret = false;
7445
7446out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007447 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007448
7449 return ret;
7450}
7451EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
7452
7453/**
7454 * Tells the intel_ips driver that the i915 driver is now loaded, if
7455 * IPS got loaded first.
7456 *
7457 * This awkward dance is so that neither module has to depend on the
7458 * other in order for IPS to do the appropriate communication of
7459 * GPU turbo limits to i915.
7460 */
7461static void
7462ips_ping_for_i915_load(void)
7463{
7464 void (*link)(void);
7465
7466 link = symbol_get(ips_link_to_i915_driver);
7467 if (link) {
7468 link();
7469 symbol_put(ips_link_to_i915_driver);
7470 }
7471}
7472
7473void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
7474{
Daniel Vetter02d71952012-08-09 16:44:54 +02007475 /* We only register the i915 ips part with intel-ips once everything is
7476 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02007477 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007478 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02007479 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007480
7481 ips_ping_for_i915_load();
7482}
7483
7484void intel_gpu_ips_teardown(void)
7485{
Daniel Vetter92703882012-08-09 16:46:01 +02007486 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007487 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02007488 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007489}
Deepak S76c3552f2014-01-30 23:08:16 +05307490
Chris Wilsondc979972016-05-10 14:10:04 +01007491static void intel_init_emon(struct drm_i915_private *dev_priv)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007492{
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007493 u32 lcfuse;
7494 u8 pxw[16];
7495 int i;
7496
7497 /* Disable to program */
7498 I915_WRITE(ECR, 0);
7499 POSTING_READ(ECR);
7500
7501 /* Program energy weights for various events */
7502 I915_WRITE(SDEW, 0x15040d00);
7503 I915_WRITE(CSIEW0, 0x007f0000);
7504 I915_WRITE(CSIEW1, 0x1e220004);
7505 I915_WRITE(CSIEW2, 0x04000004);
7506
7507 for (i = 0; i < 5; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007508 I915_WRITE(PEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007509 for (i = 0; i < 3; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007510 I915_WRITE(DEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007511
7512 /* Program P-state weights to account for frequency power adjustment */
7513 for (i = 0; i < 16; i++) {
Ville Syrjälä616847e2015-09-18 20:03:19 +03007514 u32 pxvidfreq = I915_READ(PXVFREQ(i));
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007515 unsigned long freq = intel_pxfreq(pxvidfreq);
7516 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
7517 PXVFREQ_PX_SHIFT;
7518 unsigned long val;
7519
7520 val = vid * vid;
7521 val *= (freq / 1000);
7522 val *= 255;
7523 val /= (127*127*900);
7524 if (val > 0xff)
7525 DRM_ERROR("bad pxval: %ld\n", val);
7526 pxw[i] = val;
7527 }
7528 /* Render standby states get 0 weight */
7529 pxw[14] = 0;
7530 pxw[15] = 0;
7531
7532 for (i = 0; i < 4; i++) {
7533 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
7534 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
Ville Syrjälä616847e2015-09-18 20:03:19 +03007535 I915_WRITE(PXW(i), val);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007536 }
7537
7538 /* Adjust magic regs to magic values (more experimental results) */
7539 I915_WRITE(OGW0, 0);
7540 I915_WRITE(OGW1, 0);
7541 I915_WRITE(EG0, 0x00007f00);
7542 I915_WRITE(EG1, 0x0000000e);
7543 I915_WRITE(EG2, 0x000e0000);
7544 I915_WRITE(EG3, 0x68000300);
7545 I915_WRITE(EG4, 0x42000000);
7546 I915_WRITE(EG5, 0x00140031);
7547 I915_WRITE(EG6, 0);
7548 I915_WRITE(EG7, 0);
7549
7550 for (i = 0; i < 8; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007551 I915_WRITE(PXWL(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007552
7553 /* Enable PMON + select events */
7554 I915_WRITE(ECR, 0x80000019);
7555
7556 lcfuse = I915_READ(LCFUSE02);
7557
Daniel Vetter20e4d402012-08-08 23:35:39 +02007558 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007559}
7560
Chris Wilsondc979972016-05-10 14:10:04 +01007561void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007562{
Imre Deakb268c692015-12-15 20:10:31 +02007563 /*
7564 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
7565 * requirement.
7566 */
7567 if (!i915.enable_rc6) {
7568 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
7569 intel_runtime_pm_get(dev_priv);
7570 }
Imre Deake6069ca2014-04-18 16:01:02 +03007571
Chris Wilsonb5163db2016-08-10 13:58:24 +01007572 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson773ea9a2016-07-13 09:10:33 +01007573 mutex_lock(&dev_priv->rps.hw_lock);
7574
7575 /* Initialize RPS limits (for userspace) */
Chris Wilsondc979972016-05-10 14:10:04 +01007576 if (IS_CHERRYVIEW(dev_priv))
7577 cherryview_init_gt_powersave(dev_priv);
7578 else if (IS_VALLEYVIEW(dev_priv))
7579 valleyview_init_gt_powersave(dev_priv);
Chris Wilson2a13ae72016-08-02 11:15:27 +01007580 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson773ea9a2016-07-13 09:10:33 +01007581 gen6_init_rps_frequencies(dev_priv);
7582
7583 /* Derive initial user preferences/limits from the hardware limits */
7584 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
7585 dev_priv->rps.cur_freq = dev_priv->rps.idle_freq;
7586
7587 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
7588 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
7589
7590 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
7591 dev_priv->rps.min_freq_softlimit =
7592 max_t(int,
7593 dev_priv->rps.efficient_freq,
7594 intel_freq_opcode(dev_priv, 450));
7595
Chris Wilson99ac9612016-07-13 09:10:34 +01007596 /* After setting max-softlimit, find the overclock max freq */
7597 if (IS_GEN6(dev_priv) ||
7598 IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
7599 u32 params = 0;
7600
7601 sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &params);
7602 if (params & BIT(31)) { /* OC supported */
7603 DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
7604 (dev_priv->rps.max_freq & 0xff) * 50,
7605 (params & 0xff) * 50);
7606 dev_priv->rps.max_freq = params & 0xff;
7607 }
7608 }
7609
Chris Wilson29ecd78d2016-07-13 09:10:35 +01007610 /* Finally allow us to boost to max by default */
7611 dev_priv->rps.boost_freq = dev_priv->rps.max_freq;
7612
Chris Wilson773ea9a2016-07-13 09:10:33 +01007613 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb5163db2016-08-10 13:58:24 +01007614 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson54b4f682016-07-21 21:16:19 +01007615
7616 intel_autoenable_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03007617}
7618
Chris Wilsondc979972016-05-10 14:10:04 +01007619void intel_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007620{
Ville Syrjälä8dac1e12016-08-02 14:07:33 +03007621 if (IS_VALLEYVIEW(dev_priv))
Chris Wilsondc979972016-05-10 14:10:04 +01007622 valleyview_cleanup_gt_powersave(dev_priv);
Imre Deakb268c692015-12-15 20:10:31 +02007623
7624 if (!i915.enable_rc6)
7625 intel_runtime_pm_put(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03007626}
7627
Chris Wilson54b4f682016-07-21 21:16:19 +01007628/**
7629 * intel_suspend_gt_powersave - suspend PM work and helper threads
7630 * @dev_priv: i915 device
7631 *
7632 * We don't want to disable RC6 or other features here, we just want
7633 * to make sure any work we've queued has finished and won't bother
7634 * us while we're suspended.
7635 */
7636void intel_suspend_gt_powersave(struct drm_i915_private *dev_priv)
7637{
7638 if (INTEL_GEN(dev_priv) < 6)
7639 return;
7640
7641 if (cancel_delayed_work_sync(&dev_priv->rps.autoenable_work))
7642 intel_runtime_pm_put(dev_priv);
7643
7644 /* gen6_rps_idle() will be called later to disable interrupts */
7645}
7646
Chris Wilsonb7137e02016-07-13 09:10:37 +01007647void intel_sanitize_gt_powersave(struct drm_i915_private *dev_priv)
7648{
7649 dev_priv->rps.enabled = true; /* force disabling */
7650 intel_disable_gt_powersave(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007651
7652 gen6_reset_rps_interrupts(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07007653}
7654
Chris Wilsondc979972016-05-10 14:10:04 +01007655void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
Daniel Vetter8090c6b2012-06-24 16:42:32 +02007656{
Chris Wilsonb7137e02016-07-13 09:10:37 +01007657 if (!READ_ONCE(dev_priv->rps.enabled))
7658 return;
Jesse Barnes1a01ab32012-11-02 11:14:00 -07007659
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007660 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007661
Chris Wilsonb7137e02016-07-13 09:10:37 +01007662 if (INTEL_GEN(dev_priv) >= 9) {
7663 gen9_disable_rc6(dev_priv);
7664 gen9_disable_rps(dev_priv);
7665 } else if (IS_CHERRYVIEW(dev_priv)) {
7666 cherryview_disable_rps(dev_priv);
7667 } else if (IS_VALLEYVIEW(dev_priv)) {
7668 valleyview_disable_rps(dev_priv);
7669 } else if (INTEL_GEN(dev_priv) >= 6) {
7670 gen6_disable_rps(dev_priv);
7671 } else if (IS_IRONLAKE_M(dev_priv)) {
7672 ironlake_disable_drps(dev_priv);
7673 }
7674
7675 dev_priv->rps.enabled = false;
7676 mutex_unlock(&dev_priv->rps.hw_lock);
7677}
7678
7679void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
7680{
Chris Wilson54b4f682016-07-21 21:16:19 +01007681 /* We shouldn't be disabling as we submit, so this should be less
7682 * racy than it appears!
7683 */
Chris Wilsonb7137e02016-07-13 09:10:37 +01007684 if (READ_ONCE(dev_priv->rps.enabled))
7685 return;
7686
7687 /* Powersaving is controlled by the host when inside a VM */
7688 if (intel_vgpu_active(dev_priv))
7689 return;
7690
7691 mutex_lock(&dev_priv->rps.hw_lock);
Imre Deak3cc134e2014-11-19 15:30:03 +02007692
Chris Wilsondc979972016-05-10 14:10:04 +01007693 if (IS_CHERRYVIEW(dev_priv)) {
7694 cherryview_enable_rps(dev_priv);
7695 } else if (IS_VALLEYVIEW(dev_priv)) {
7696 valleyview_enable_rps(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007697 } else if (INTEL_GEN(dev_priv) >= 9) {
Chris Wilsondc979972016-05-10 14:10:04 +01007698 gen9_enable_rc6(dev_priv);
7699 gen9_enable_rps(dev_priv);
Rodrigo Vivib976dc52017-01-23 10:32:37 -08007700 if (IS_GEN9_BC(dev_priv))
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007701 gen6_update_ring_freq(dev_priv);
Chris Wilsondc979972016-05-10 14:10:04 +01007702 } else if (IS_BROADWELL(dev_priv)) {
7703 gen8_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007704 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007705 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilsondc979972016-05-10 14:10:04 +01007706 gen6_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007707 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007708 } else if (IS_IRONLAKE_M(dev_priv)) {
7709 ironlake_enable_drps(dev_priv);
7710 intel_init_emon(dev_priv);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007711 }
Chris Wilsonaed242f2015-03-18 09:48:21 +00007712
7713 WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
7714 WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
7715
7716 WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
7717 WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
7718
Chris Wilson54b4f682016-07-21 21:16:19 +01007719 dev_priv->rps.enabled = true;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007720 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007721}
Imre Deakc6df39b2014-04-14 20:24:29 +03007722
Chris Wilson54b4f682016-07-21 21:16:19 +01007723static void __intel_autoenable_gt_powersave(struct work_struct *work)
7724{
7725 struct drm_i915_private *dev_priv =
7726 container_of(work, typeof(*dev_priv), rps.autoenable_work.work);
7727 struct intel_engine_cs *rcs;
7728 struct drm_i915_gem_request *req;
7729
7730 if (READ_ONCE(dev_priv->rps.enabled))
7731 goto out;
7732
Akash Goel3b3f1652016-10-13 22:44:48 +05307733 rcs = dev_priv->engine[RCS];
Chris Wilsone8a9c582016-12-18 15:37:20 +00007734 if (rcs->last_retired_context)
Chris Wilson54b4f682016-07-21 21:16:19 +01007735 goto out;
7736
7737 if (!rcs->init_context)
7738 goto out;
7739
7740 mutex_lock(&dev_priv->drm.struct_mutex);
7741
7742 req = i915_gem_request_alloc(rcs, dev_priv->kernel_context);
7743 if (IS_ERR(req))
7744 goto unlock;
7745
7746 if (!i915.enable_execlists && i915_switch_context(req) == 0)
7747 rcs->init_context(req);
7748
7749 /* Mark the device busy, calling intel_enable_gt_powersave() */
Chris Wilsone642c852017-03-17 11:47:09 +00007750 i915_add_request(req);
Chris Wilson54b4f682016-07-21 21:16:19 +01007751
7752unlock:
7753 mutex_unlock(&dev_priv->drm.struct_mutex);
7754out:
7755 intel_runtime_pm_put(dev_priv);
7756}
7757
7758void intel_autoenable_gt_powersave(struct drm_i915_private *dev_priv)
7759{
7760 if (READ_ONCE(dev_priv->rps.enabled))
7761 return;
7762
7763 if (IS_IRONLAKE_M(dev_priv)) {
7764 ironlake_enable_drps(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007765 intel_init_emon(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007766 } else if (INTEL_INFO(dev_priv)->gen >= 6) {
7767 /*
7768 * PCU communication is slow and this doesn't need to be
7769 * done at any specific time, so do this out of our fast path
7770 * to make resume and init faster.
7771 *
7772 * We depend on the HW RC6 power context save/restore
7773 * mechanism when entering D3 through runtime PM suspend. So
7774 * disable RPM until RPS/RC6 is properly setup. We can only
7775 * get here via the driver load/system resume/runtime resume
7776 * paths, so the _noresume version is enough (and in case of
7777 * runtime resume it's necessary).
7778 */
7779 if (queue_delayed_work(dev_priv->wq,
7780 &dev_priv->rps.autoenable_work,
7781 round_jiffies_up_relative(HZ)))
7782 intel_runtime_pm_get_noresume(dev_priv);
7783 }
7784}
7785
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007786static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007787{
Daniel Vetter3107bd42012-10-31 22:52:31 +01007788 /*
7789 * On Ibex Peak and Cougar Point, we need to disable clock
7790 * gating for the panel power sequencer or it will fail to
7791 * start up when no ports are active.
7792 */
7793 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
7794}
7795
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007796static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007797{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007798 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007799
Damien Lespiau055e3932014-08-18 13:49:10 +01007800 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007801 I915_WRITE(DSPCNTR(pipe),
7802 I915_READ(DSPCNTR(pipe)) |
7803 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007804
7805 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
7806 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007807 }
7808}
7809
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007810static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä017636c2013-12-05 15:51:37 +02007811{
Ville Syrjälä017636c2013-12-05 15:51:37 +02007812 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
7813 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
7814 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
7815
7816 /*
7817 * Don't touch WM1S_LP_EN here.
7818 * Doing so could cause underruns.
7819 */
7820}
7821
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007822static void ironlake_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007823{
Damien Lespiau231e54f2012-10-19 17:55:41 +01007824 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007825
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01007826 /*
7827 * Required for FBC
7828 * WaFbcDisableDpfcClockGating:ilk
7829 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007830 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
7831 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
7832 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007833
7834 I915_WRITE(PCH_3DCGDIS0,
7835 MARIUNIT_CLOCK_GATE_DISABLE |
7836 SVSMUNIT_CLOCK_GATE_DISABLE);
7837 I915_WRITE(PCH_3DCGDIS1,
7838 VFMUNIT_CLOCK_GATE_DISABLE);
7839
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007840 /*
7841 * According to the spec the following bits should be set in
7842 * order to enable memory self-refresh
7843 * The bit 22/21 of 0x42004
7844 * The bit 5 of 0x42020
7845 * The bit 15 of 0x45000
7846 */
7847 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7848 (I915_READ(ILK_DISPLAY_CHICKEN2) |
7849 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007850 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007851 I915_WRITE(DISP_ARB_CTL,
7852 (I915_READ(DISP_ARB_CTL) |
7853 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02007854
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007855 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007856
7857 /*
7858 * Based on the document from hardware guys the following bits
7859 * should be set unconditionally in order to enable FBC.
7860 * The bit 22 of 0x42000
7861 * The bit 22 of 0x42004
7862 * The bit 7,8,9 of 0x42020.
7863 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007864 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01007865 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007866 I915_WRITE(ILK_DISPLAY_CHICKEN1,
7867 I915_READ(ILK_DISPLAY_CHICKEN1) |
7868 ILK_FBCQ_DIS);
7869 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7870 I915_READ(ILK_DISPLAY_CHICKEN2) |
7871 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007872 }
7873
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007874 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
7875
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007876 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7877 I915_READ(ILK_DISPLAY_CHICKEN2) |
7878 ILK_ELPIN_409_SELECT);
7879 I915_WRITE(_3D_CHICKEN2,
7880 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
7881 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02007882
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007883 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02007884 I915_WRITE(CACHE_MODE_0,
7885 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01007886
Akash Goel4e046322014-04-04 17:14:38 +05307887 /* WaDisable_RenderCache_OperationalFlush:ilk */
7888 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7889
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007890 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03007891
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007892 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007893}
7894
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007895static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007896{
Daniel Vetter3107bd42012-10-31 22:52:31 +01007897 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007898 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01007899
7900 /*
7901 * On Ibex Peak and Cougar Point, we need to disable clock
7902 * gating for the panel power sequencer or it will fail to
7903 * start up when no ports are active.
7904 */
Jesse Barnescd664072013-10-02 10:34:19 -07007905 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
7906 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
7907 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007908 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
7909 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01007910 /* The below fixes the weird display corruption, a few pixels shifted
7911 * downward, on (only) LVDS of some HP laptops with IVY.
7912 */
Damien Lespiau055e3932014-08-18 13:49:10 +01007913 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007914 val = I915_READ(TRANS_CHICKEN2(pipe));
7915 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
7916 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007917 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007918 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007919 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
7920 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
7921 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007922 I915_WRITE(TRANS_CHICKEN2(pipe), val);
7923 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01007924 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01007925 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01007926 I915_WRITE(TRANS_CHICKEN1(pipe),
7927 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
7928 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007929}
7930
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007931static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007932{
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007933 uint32_t tmp;
7934
7935 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02007936 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
7937 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
7938 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007939}
7940
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007941static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007942{
Damien Lespiau231e54f2012-10-19 17:55:41 +01007943 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007944
Damien Lespiau231e54f2012-10-19 17:55:41 +01007945 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007946
7947 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7948 I915_READ(ILK_DISPLAY_CHICKEN2) |
7949 ILK_ELPIN_409_SELECT);
7950
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007951 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01007952 I915_WRITE(_3D_CHICKEN,
7953 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
7954
Akash Goel4e046322014-04-04 17:14:38 +05307955 /* WaDisable_RenderCache_OperationalFlush:snb */
7956 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7957
Ville Syrjälä8d85d272014-02-04 21:59:15 +02007958 /*
7959 * BSpec recoomends 8x4 when MSAA is used,
7960 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007961 *
7962 * Note that PS/WM thread counts depend on the WIZ hashing
7963 * disable bit, which we don't touch here, but it's good
7964 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02007965 */
7966 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007967 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02007968
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007969 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007970
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007971 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02007972 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007973
7974 I915_WRITE(GEN6_UCGCTL1,
7975 I915_READ(GEN6_UCGCTL1) |
7976 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
7977 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
7978
7979 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
7980 * gating disable must be set. Failure to set it results in
7981 * flickering pixels due to Z write ordering failures after
7982 * some amount of runtime in the Mesa "fire" demo, and Unigine
7983 * Sanctuary and Tropics, and apparently anything else with
7984 * alpha test or pixel discard.
7985 *
7986 * According to the spec, bit 11 (RCCUNIT) must also be set,
7987 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007988 *
Ville Syrjäläef593182014-01-22 21:32:47 +02007989 * WaDisableRCCUnitClockGating:snb
7990 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007991 */
7992 I915_WRITE(GEN6_UCGCTL2,
7993 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
7994 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
7995
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02007996 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02007997 I915_WRITE(_3D_CHICKEN3,
7998 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007999
8000 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02008001 * Bspec says:
8002 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
8003 * 3DSTATE_SF number of SF output attributes is more than 16."
8004 */
8005 I915_WRITE(_3D_CHICKEN3,
8006 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
8007
8008 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008009 * According to the spec the following bits should be
8010 * set in order to enable memory self-refresh and fbc:
8011 * The bit21 and bit22 of 0x42000
8012 * The bit21 and bit22 of 0x42004
8013 * The bit5 and bit7 of 0x42020
8014 * The bit14 of 0x70180
8015 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01008016 *
8017 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008018 */
8019 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8020 I915_READ(ILK_DISPLAY_CHICKEN1) |
8021 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
8022 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8023 I915_READ(ILK_DISPLAY_CHICKEN2) |
8024 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01008025 I915_WRITE(ILK_DSPCLK_GATE_D,
8026 I915_READ(ILK_DSPCLK_GATE_D) |
8027 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
8028 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008029
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008030 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07008031
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008032 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008033
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008034 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008035}
8036
8037static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
8038{
8039 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
8040
Ville Syrjälä3aad9052014-01-22 21:32:59 +02008041 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02008042 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02008043 *
8044 * This actually overrides the dispatch
8045 * mode for all thread types.
8046 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008047 reg &= ~GEN7_FF_SCHED_MASK;
8048 reg |= GEN7_FF_TS_SCHED_HW;
8049 reg |= GEN7_FF_VS_SCHED_HW;
8050 reg |= GEN7_FF_DS_SCHED_HW;
8051
8052 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
8053}
8054
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008055static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008056{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008057 /*
8058 * TODO: this bit should only be enabled when really needed, then
8059 * disabled when not needed anymore in order to save power.
8060 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008061 if (HAS_PCH_LPT_LP(dev_priv))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008062 I915_WRITE(SOUTH_DSPCLK_GATE_D,
8063 I915_READ(SOUTH_DSPCLK_GATE_D) |
8064 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03008065
8066 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03008067 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
8068 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03008069 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008070}
8071
Ville Syrjälä712bf362016-10-31 22:37:23 +02008072static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03008073{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008074 if (HAS_PCH_LPT_LP(dev_priv)) {
Imre Deak7d708ee2013-04-17 14:04:50 +03008075 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
8076
8077 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8078 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8079 }
8080}
8081
Imre Deak450174f2016-05-03 15:54:21 +03008082static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
8083 int general_prio_credits,
8084 int high_prio_credits)
8085{
8086 u32 misccpctl;
8087
8088 /* WaTempDisableDOPClkGating:bdw */
8089 misccpctl = I915_READ(GEN7_MISCCPCTL);
8090 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
8091
8092 I915_WRITE(GEN8_L3SQCREG1,
8093 L3_GENERAL_PRIO_CREDITS(general_prio_credits) |
8094 L3_HIGH_PRIO_CREDITS(high_prio_credits));
8095
8096 /*
8097 * Wait at least 100 clocks before re-enabling clock gating.
8098 * See the definition of L3SQCREG1 in BSpec.
8099 */
8100 POSTING_READ(GEN8_L3SQCREG1);
8101 udelay(1);
8102 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
8103}
8104
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008105static void kabylake_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008106{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008107 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008108
8109 /* WaDisableSDEUnitClockGating:kbl */
8110 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
8111 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8112 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03008113
8114 /* WaDisableGamClockGating:kbl */
8115 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
8116 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
8117 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008118
8119 /* WaFbcNukeOnHostModify:kbl */
8120 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8121 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008122}
8123
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008124static void skylake_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008125{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008126 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03008127
8128 /* WAC6entrylatency:skl */
8129 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
8130 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008131
8132 /* WaFbcNukeOnHostModify:skl */
8133 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8134 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008135}
8136
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008137static void broadwell_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008138{
Damien Lespiau07d27e22014-03-03 17:31:46 +00008139 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008140
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008141 ilk_init_lp_watermarks(dev_priv);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07008142
Ben Widawskyab57fff2013-12-12 15:28:04 -08008143 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07008144 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008145
Ben Widawskyab57fff2013-12-12 15:28:04 -08008146 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008147 I915_WRITE(CHICKEN_PAR1_1,
8148 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
8149
Ben Widawskyab57fff2013-12-12 15:28:04 -08008150 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01008151 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00008152 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02008153 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02008154 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008155 }
Ben Widawsky63801f22013-12-12 17:26:03 -08008156
Ben Widawskyab57fff2013-12-12 15:28:04 -08008157 /* WaVSRefCountFullforceMissDisable:bdw */
8158 /* WaDSRefCountFullforceMissDisable:bdw */
8159 I915_WRITE(GEN7_FF_THREAD_MODE,
8160 I915_READ(GEN7_FF_THREAD_MODE) &
8161 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02008162
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02008163 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
8164 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02008165
8166 /* WaDisableSDEUnitClockGating:bdw */
8167 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8168 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00008169
Imre Deak450174f2016-05-03 15:54:21 +03008170 /* WaProgramL3SqcReg1Default:bdw */
8171 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03008172
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008173 /*
8174 * WaGttCachingOffByDefault:bdw
8175 * GTT cache may not work with big pages, so if those
8176 * are ever enabled GTT cache may need to be disabled.
8177 */
8178 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
8179
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03008180 /* WaKVMNotificationOnConfigChange:bdw */
8181 I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
8182 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
8183
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008184 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00008185
8186 /* WaDisableDopClockGating:bdw
8187 *
8188 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
8189 * clock gating.
8190 */
8191 I915_WRITE(GEN6_UCGCTL1,
8192 I915_READ(GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008193}
8194
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008195static void haswell_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008196{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008197 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008198
Francisco Jerezf3fc4882013-10-02 15:53:16 -07008199 /* L3 caching of data atomics doesn't work -- disable it. */
8200 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
8201 I915_WRITE(HSW_ROW_CHICKEN3,
8202 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
8203
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008204 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008205 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8206 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8207 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8208
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02008209 /* WaVSRefCountFullforceMissDisable:hsw */
8210 I915_WRITE(GEN7_FF_THREAD_MODE,
8211 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008212
Akash Goel4e046322014-04-04 17:14:38 +05308213 /* WaDisable_RenderCache_OperationalFlush:hsw */
8214 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8215
Chia-I Wufe27c602014-01-28 13:29:33 +08008216 /* enable HiZ Raw Stall Optimization */
8217 I915_WRITE(CACHE_MODE_0_GEN7,
8218 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
8219
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008220 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008221 I915_WRITE(CACHE_MODE_1,
8222 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03008223
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008224 /*
8225 * BSpec recommends 8x4 when MSAA is used,
8226 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008227 *
8228 * Note that PS/WM thread counts depend on the WIZ hashing
8229 * disable bit, which we don't touch here, but it's good
8230 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008231 */
8232 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008233 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008234
Kenneth Graunke94411592014-12-31 16:23:00 -08008235 /* WaSampleCChickenBitEnable:hsw */
8236 I915_WRITE(HALF_SLICE_CHICKEN3,
8237 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
8238
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008239 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07008240 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
8241
Paulo Zanoni90a88642013-05-03 17:23:45 -03008242 /* WaRsPkgCStateDisplayPMReq:hsw */
8243 I915_WRITE(CHICKEN_PAR1_1,
8244 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03008245
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008246 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008247}
8248
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008249static void ivybridge_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008250{
Ben Widawsky20848222012-05-04 18:58:59 -07008251 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008252
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008253 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008254
Damien Lespiau231e54f2012-10-19 17:55:41 +01008255 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008256
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008257 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05008258 I915_WRITE(_3D_CHICKEN3,
8259 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
8260
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008261 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008262 I915_WRITE(IVB_CHICKEN3,
8263 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8264 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8265
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008266 /* WaDisablePSDDualDispatchEnable:ivb */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008267 if (IS_IVB_GT1(dev_priv))
Jesse Barnes12f33822012-10-25 12:15:45 -07008268 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
8269 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07008270
Akash Goel4e046322014-04-04 17:14:38 +05308271 /* WaDisable_RenderCache_OperationalFlush:ivb */
8272 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8273
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008274 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008275 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
8276 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
8277
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008278 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008279 I915_WRITE(GEN7_L3CNTLREG1,
8280 GEN7_WA_FOR_GEN7_L3_CONTROL);
8281 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07008282 GEN7_WA_L3_CHICKEN_MODE);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008283 if (IS_IVB_GT1(dev_priv))
Jesse Barnes8ab43972012-10-25 12:15:42 -07008284 I915_WRITE(GEN7_ROW_CHICKEN2,
8285 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02008286 else {
8287 /* must write both registers */
8288 I915_WRITE(GEN7_ROW_CHICKEN2,
8289 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07008290 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
8291 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02008292 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008293
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008294 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05008295 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
8296 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
8297
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02008298 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07008299 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008300 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008301 */
8302 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02008303 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07008304
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008305 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008306 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8307 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8308 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8309
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008310 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008311
8312 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02008313
Chris Wilson22721342014-03-04 09:41:43 +00008314 if (0) { /* causes HiZ corruption on ivb:gt1 */
8315 /* enable HiZ Raw Stall Optimization */
8316 I915_WRITE(CACHE_MODE_0_GEN7,
8317 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
8318 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08008319
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008320 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02008321 I915_WRITE(CACHE_MODE_1,
8322 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07008323
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008324 /*
8325 * BSpec recommends 8x4 when MSAA is used,
8326 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008327 *
8328 * Note that PS/WM thread counts depend on the WIZ hashing
8329 * disable bit, which we don't touch here, but it's good
8330 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008331 */
8332 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008333 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008334
Ben Widawsky20848222012-05-04 18:58:59 -07008335 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
8336 snpcr &= ~GEN6_MBC_SNPCR_MASK;
8337 snpcr |= GEN6_MBC_SNPCR_MED;
8338 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008339
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008340 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008341 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008342
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008343 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008344}
8345
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008346static void valleyview_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008347{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008348 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05008349 I915_WRITE(_3D_CHICKEN3,
8350 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
8351
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008352 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008353 I915_WRITE(IVB_CHICKEN3,
8354 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8355 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8356
Ville Syrjäläfad7d362014-01-22 21:32:39 +02008357 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008358 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07008359 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08008360 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
8361 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07008362
Akash Goel4e046322014-04-04 17:14:38 +05308363 /* WaDisable_RenderCache_OperationalFlush:vlv */
8364 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8365
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008366 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05008367 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
8368 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
8369
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008370 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07008371 I915_WRITE(GEN7_ROW_CHICKEN2,
8372 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
8373
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008374 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008375 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8376 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8377 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8378
Ville Syrjälä46680e02014-01-22 21:33:01 +02008379 gen7_setup_fixed_func_scheduler(dev_priv);
8380
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02008381 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07008382 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008383 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008384 */
8385 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02008386 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07008387
Akash Goelc98f5062014-03-24 23:00:07 +05308388 /* WaDisableL3Bank2xClockGate:vlv
8389 * Disabling L3 clock gating- MMIO 940c[25] = 1
8390 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
8391 I915_WRITE(GEN7_UCGCTL4,
8392 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07008393
Ville Syrjäläafd58e72014-01-22 21:33:03 +02008394 /*
8395 * BSpec says this must be set, even though
8396 * WaDisable4x2SubspanOptimization isn't listed for VLV.
8397 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02008398 I915_WRITE(CACHE_MODE_1,
8399 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07008400
8401 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02008402 * BSpec recommends 8x4 when MSAA is used,
8403 * however in practice 16x4 seems fastest.
8404 *
8405 * Note that PS/WM thread counts depend on the WIZ hashing
8406 * disable bit, which we don't touch here, but it's good
8407 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
8408 */
8409 I915_WRITE(GEN7_GT_MODE,
8410 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
8411
8412 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02008413 * WaIncreaseL3CreditsForVLVB0:vlv
8414 * This is the hardware default actually.
8415 */
8416 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
8417
8418 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008419 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07008420 * Disable clock gating on th GCFG unit to prevent a delay
8421 * in the reporting of vblank events.
8422 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02008423 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008424}
8425
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008426static void cherryview_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03008427{
Ville Syrjälä232ce332014-04-09 13:28:35 +03008428 /* WaVSRefCountFullforceMissDisable:chv */
8429 /* WaDSRefCountFullforceMissDisable:chv */
8430 I915_WRITE(GEN7_FF_THREAD_MODE,
8431 I915_READ(GEN7_FF_THREAD_MODE) &
8432 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03008433
8434 /* WaDisableSemaphoreAndSyncFlipWait:chv */
8435 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
8436 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03008437
8438 /* WaDisableCSUnitClockGating:chv */
8439 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
8440 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03008441
8442 /* WaDisableSDEUnitClockGating:chv */
8443 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8444 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008445
8446 /*
Imre Deak450174f2016-05-03 15:54:21 +03008447 * WaProgramL3SqcReg1Default:chv
8448 * See gfxspecs/Related Documents/Performance Guide/
8449 * LSQC Setting Recommendations.
8450 */
8451 gen8_set_l3sqc_credits(dev_priv, 38, 2);
8452
8453 /*
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008454 * GTT cache may not work with big pages, so if those
8455 * are ever enabled GTT cache may need to be disabled.
8456 */
8457 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03008458}
8459
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008460static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008461{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008462 uint32_t dspclk_gate;
8463
8464 I915_WRITE(RENCLK_GATE_D1, 0);
8465 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
8466 GS_UNIT_CLOCK_GATE_DISABLE |
8467 CL_UNIT_CLOCK_GATE_DISABLE);
8468 I915_WRITE(RAMCLK_GATE_D, 0);
8469 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
8470 OVRUNIT_CLOCK_GATE_DISABLE |
8471 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008472 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008473 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
8474 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02008475
8476 /* WaDisableRenderCachePipelinedFlush */
8477 I915_WRITE(CACHE_MODE_0,
8478 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03008479
Akash Goel4e046322014-04-04 17:14:38 +05308480 /* WaDisable_RenderCache_OperationalFlush:g4x */
8481 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8482
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008483 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008484}
8485
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008486static void crestline_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008487{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008488 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
8489 I915_WRITE(RENCLK_GATE_D2, 0);
8490 I915_WRITE(DSPCLK_GATE_D, 0);
8491 I915_WRITE(RAMCLK_GATE_D, 0);
8492 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03008493 I915_WRITE(MI_ARB_STATE,
8494 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05308495
8496 /* WaDisable_RenderCache_OperationalFlush:gen4 */
8497 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008498}
8499
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008500static void broadwater_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008501{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008502 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
8503 I965_RCC_CLOCK_GATE_DISABLE |
8504 I965_RCPB_CLOCK_GATE_DISABLE |
8505 I965_ISC_CLOCK_GATE_DISABLE |
8506 I965_FBC_CLOCK_GATE_DISABLE);
8507 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03008508 I915_WRITE(MI_ARB_STATE,
8509 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05308510
8511 /* WaDisable_RenderCache_OperationalFlush:gen4 */
8512 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008513}
8514
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008515static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008516{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008517 u32 dstate = I915_READ(D_STATE);
8518
8519 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
8520 DSTATE_DOT_CLOCK_GATING;
8521 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01008522
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008523 if (IS_PINEVIEW(dev_priv))
Chris Wilson13a86b82012-04-24 14:51:43 +01008524 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02008525
8526 /* IIR "flip pending" means done if this bit is set */
8527 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02008528
8529 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02008530 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02008531
8532 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
8533 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03008534
8535 I915_WRITE(MI_ARB_STATE,
8536 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008537}
8538
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008539static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008540{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008541 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02008542
8543 /* interrupts should cause a wake up from C3 */
8544 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
8545 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03008546
8547 I915_WRITE(MEM_MODE,
8548 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008549}
8550
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008551static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008552{
Ville Syrjälä10383922014-08-15 01:21:54 +03008553 I915_WRITE(MEM_MODE,
8554 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
8555 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008556}
8557
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008558void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008559{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008560 dev_priv->display.init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008561}
8562
Ville Syrjälä712bf362016-10-31 22:37:23 +02008563void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03008564{
Ville Syrjälä712bf362016-10-31 22:37:23 +02008565 if (HAS_PCH_LPT(dev_priv))
8566 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03008567}
8568
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008569static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02008570{
8571 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
8572}
8573
8574/**
8575 * intel_init_clock_gating_hooks - setup the clock gating hooks
8576 * @dev_priv: device private
8577 *
8578 * Setup the hooks that configure which clocks of a given platform can be
8579 * gated and also apply various GT and display specific workarounds for these
8580 * platforms. Note that some GT specific workarounds are applied separately
8581 * when GPU contexts or batchbuffers start their execution.
8582 */
8583void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
8584{
8585 if (IS_SKYLAKE(dev_priv))
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008586 dev_priv->display.init_clock_gating = skylake_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008587 else if (IS_KABYLAKE(dev_priv))
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008588 dev_priv->display.init_clock_gating = kabylake_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02008589 else if (IS_BROXTON(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02008590 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02008591 else if (IS_GEMINILAKE(dev_priv))
8592 dev_priv->display.init_clock_gating = glk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008593 else if (IS_BROADWELL(dev_priv))
8594 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
8595 else if (IS_CHERRYVIEW(dev_priv))
8596 dev_priv->display.init_clock_gating = cherryview_init_clock_gating;
8597 else if (IS_HASWELL(dev_priv))
8598 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
8599 else if (IS_IVYBRIDGE(dev_priv))
8600 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
8601 else if (IS_VALLEYVIEW(dev_priv))
8602 dev_priv->display.init_clock_gating = valleyview_init_clock_gating;
8603 else if (IS_GEN6(dev_priv))
8604 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
8605 else if (IS_GEN5(dev_priv))
8606 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
8607 else if (IS_G4X(dev_priv))
8608 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02008609 else if (IS_I965GM(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02008610 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02008611 else if (IS_I965G(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02008612 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
8613 else if (IS_GEN3(dev_priv))
8614 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
8615 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
8616 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
8617 else if (IS_GEN2(dev_priv))
8618 dev_priv->display.init_clock_gating = i830_init_clock_gating;
8619 else {
8620 MISSING_CASE(INTEL_DEVID(dev_priv));
8621 dev_priv->display.init_clock_gating = nop_init_clock_gating;
8622 }
8623}
8624
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008625/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008626void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008627{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02008628 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008629
Daniel Vetterc921aba2012-04-26 23:28:17 +02008630 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008631 if (IS_PINEVIEW(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02008632 i915_pineview_get_mem_freq(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008633 else if (IS_GEN5(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02008634 i915_ironlake_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02008635
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008636 /* For FIFO watermark updates */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008637 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008638 skl_setup_wm_latency(dev_priv);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01008639 dev_priv->display.initial_watermarks = skl_initial_wm;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01008640 dev_priv->display.atomic_update_watermarks = skl_atomic_update_crtc_wm;
Matt Roper98d39492016-05-12 07:06:03 -07008641 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008642 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008643 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03008644
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008645 if ((IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008646 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008647 (!IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008648 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07008649 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08008650 dev_priv->display.compute_intermediate_wm =
8651 ilk_compute_intermediate_wm;
8652 dev_priv->display.initial_watermarks =
8653 ilk_initial_watermarks;
8654 dev_priv->display.optimize_watermarks =
8655 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02008656 } else {
8657 DRM_DEBUG_KMS("Failed to read display plane latency. "
8658 "Disable CxSR\n");
8659 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02008660 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008661 vlv_setup_wm_latency(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02008662 dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
Ville Syrjälä4841da52017-03-02 19:14:59 +02008663 dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02008664 dev_priv->display.initial_watermarks = vlv_initial_watermarks;
Ville Syrjälä4841da52017-03-02 19:14:59 +02008665 dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
Ville Syrjäläff32c542017-03-02 19:14:57 +02008666 dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03008667 } else if (IS_G4X(dev_priv)) {
8668 g4x_setup_wm_latency(dev_priv);
8669 dev_priv->display.compute_pipe_wm = g4x_compute_pipe_wm;
8670 dev_priv->display.compute_intermediate_wm = g4x_compute_intermediate_wm;
8671 dev_priv->display.initial_watermarks = g4x_initial_watermarks;
8672 dev_priv->display.optimize_watermarks = g4x_optimize_watermarks;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008673 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008674 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008675 dev_priv->is_ddr3,
8676 dev_priv->fsb_freq,
8677 dev_priv->mem_freq)) {
8678 DRM_INFO("failed to find known CxSR latency "
8679 "(found ddr%s fsb freq %d, mem freq %d), "
8680 "disabling CxSR\n",
8681 (dev_priv->is_ddr3 == 1) ? "3" : "2",
8682 dev_priv->fsb_freq, dev_priv->mem_freq);
8683 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03008684 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008685 dev_priv->display.update_wm = NULL;
8686 } else
8687 dev_priv->display.update_wm = pineview_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008688 } else if (IS_GEN4(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008689 dev_priv->display.update_wm = i965_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008690 } else if (IS_GEN3(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008691 dev_priv->display.update_wm = i9xx_update_wm;
8692 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008693 } else if (IS_GEN2(dev_priv)) {
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008694 if (INTEL_INFO(dev_priv)->num_pipes == 1) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008695 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008696 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008697 } else {
8698 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008699 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008700 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008701 } else {
8702 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008703 }
8704}
8705
Lyude87660502016-08-17 15:55:53 -04008706static inline int gen6_check_mailbox_status(struct drm_i915_private *dev_priv)
8707{
8708 uint32_t flags =
8709 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
8710
8711 switch (flags) {
8712 case GEN6_PCODE_SUCCESS:
8713 return 0;
8714 case GEN6_PCODE_UNIMPLEMENTED_CMD:
8715 case GEN6_PCODE_ILLEGAL_CMD:
8716 return -ENXIO;
8717 case GEN6_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Chris Wilson7850d1c2016-08-26 11:59:26 +01008718 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Lyude87660502016-08-17 15:55:53 -04008719 return -EOVERFLOW;
8720 case GEN6_PCODE_TIMEOUT:
8721 return -ETIMEDOUT;
8722 default:
Michal Wajdeczkof0d66152017-03-28 08:45:12 +00008723 MISSING_CASE(flags);
Lyude87660502016-08-17 15:55:53 -04008724 return 0;
8725 }
8726}
8727
8728static inline int gen7_check_mailbox_status(struct drm_i915_private *dev_priv)
8729{
8730 uint32_t flags =
8731 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
8732
8733 switch (flags) {
8734 case GEN6_PCODE_SUCCESS:
8735 return 0;
8736 case GEN6_PCODE_ILLEGAL_CMD:
8737 return -ENXIO;
8738 case GEN7_PCODE_TIMEOUT:
8739 return -ETIMEDOUT;
8740 case GEN7_PCODE_ILLEGAL_DATA:
8741 return -EINVAL;
8742 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
8743 return -EOVERFLOW;
8744 default:
8745 MISSING_CASE(flags);
8746 return 0;
8747 }
8748}
8749
Tom O'Rourke151a49d2014-11-13 18:50:10 -08008750int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07008751{
Lyude87660502016-08-17 15:55:53 -04008752 int status;
8753
Jesse Barnes4fc688c2012-11-02 11:14:01 -07008754 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07008755
Chris Wilson3f5582d2016-06-30 15:32:45 +01008756 /* GEN6_PCODE_* are outside of the forcewake domain, we can
8757 * use te fw I915_READ variants to reduce the amount of work
8758 * required when reading/writing.
8759 */
8760
8761 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008762 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
8763 return -EAGAIN;
8764 }
8765
Chris Wilson3f5582d2016-06-30 15:32:45 +01008766 I915_WRITE_FW(GEN6_PCODE_DATA, *val);
8767 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
8768 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07008769
Chris Wilsone09a3032017-04-11 11:13:39 +01008770 if (__intel_wait_for_register_fw(dev_priv,
8771 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
8772 500, 0, NULL)) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008773 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
8774 return -ETIMEDOUT;
8775 }
8776
Chris Wilson3f5582d2016-06-30 15:32:45 +01008777 *val = I915_READ_FW(GEN6_PCODE_DATA);
8778 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07008779
Lyude87660502016-08-17 15:55:53 -04008780 if (INTEL_GEN(dev_priv) > 6)
8781 status = gen7_check_mailbox_status(dev_priv);
8782 else
8783 status = gen6_check_mailbox_status(dev_priv);
8784
8785 if (status) {
8786 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed: %d\n",
8787 status);
8788 return status;
8789 }
8790
Ben Widawsky42c05262012-09-26 10:34:00 -07008791 return 0;
8792}
8793
Chris Wilson3f5582d2016-06-30 15:32:45 +01008794int sandybridge_pcode_write(struct drm_i915_private *dev_priv,
Lyude87660502016-08-17 15:55:53 -04008795 u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07008796{
Lyude87660502016-08-17 15:55:53 -04008797 int status;
8798
Jesse Barnes4fc688c2012-11-02 11:14:01 -07008799 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07008800
Chris Wilson3f5582d2016-06-30 15:32:45 +01008801 /* GEN6_PCODE_* are outside of the forcewake domain, we can
8802 * use te fw I915_READ variants to reduce the amount of work
8803 * required when reading/writing.
8804 */
8805
8806 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008807 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
8808 return -EAGAIN;
8809 }
8810
Chris Wilson3f5582d2016-06-30 15:32:45 +01008811 I915_WRITE_FW(GEN6_PCODE_DATA, val);
Imre Deak8bf41b72016-11-28 17:29:27 +02008812 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
Chris Wilson3f5582d2016-06-30 15:32:45 +01008813 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07008814
Chris Wilsone09a3032017-04-11 11:13:39 +01008815 if (__intel_wait_for_register_fw(dev_priv,
8816 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
8817 500, 0, NULL)) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008818 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
8819 return -ETIMEDOUT;
8820 }
8821
Chris Wilson3f5582d2016-06-30 15:32:45 +01008822 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07008823
Lyude87660502016-08-17 15:55:53 -04008824 if (INTEL_GEN(dev_priv) > 6)
8825 status = gen7_check_mailbox_status(dev_priv);
8826 else
8827 status = gen6_check_mailbox_status(dev_priv);
8828
8829 if (status) {
8830 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed: %d\n",
8831 status);
8832 return status;
8833 }
8834
Ben Widawsky42c05262012-09-26 10:34:00 -07008835 return 0;
8836}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07008837
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008838static bool skl_pcode_try_request(struct drm_i915_private *dev_priv, u32 mbox,
8839 u32 request, u32 reply_mask, u32 reply,
8840 u32 *status)
8841{
8842 u32 val = request;
8843
8844 *status = sandybridge_pcode_read(dev_priv, mbox, &val);
8845
8846 return *status || ((val & reply_mask) == reply);
8847}
8848
8849/**
8850 * skl_pcode_request - send PCODE request until acknowledgment
8851 * @dev_priv: device private
8852 * @mbox: PCODE mailbox ID the request is targeted for
8853 * @request: request ID
8854 * @reply_mask: mask used to check for request acknowledgment
8855 * @reply: value used to check for request acknowledgment
8856 * @timeout_base_ms: timeout for polling with preemption enabled
8857 *
8858 * Keep resending the @request to @mbox until PCODE acknowledges it, PCODE
Imre Deak01299362017-02-24 16:32:10 +02008859 * reports an error or an overall timeout of @timeout_base_ms+50 ms expires.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008860 * The request is acknowledged once the PCODE reply dword equals @reply after
8861 * applying @reply_mask. Polling is first attempted with preemption enabled
Imre Deak01299362017-02-24 16:32:10 +02008862 * for @timeout_base_ms and if this times out for another 50 ms with
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008863 * preemption disabled.
8864 *
8865 * Returns 0 on success, %-ETIMEDOUT in case of a timeout, <0 in case of some
8866 * other error as reported by PCODE.
8867 */
8868int skl_pcode_request(struct drm_i915_private *dev_priv, u32 mbox, u32 request,
8869 u32 reply_mask, u32 reply, int timeout_base_ms)
8870{
8871 u32 status;
8872 int ret;
8873
8874 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
8875
8876#define COND skl_pcode_try_request(dev_priv, mbox, request, reply_mask, reply, \
8877 &status)
8878
8879 /*
8880 * Prime the PCODE by doing a request first. Normally it guarantees
8881 * that a subsequent request, at most @timeout_base_ms later, succeeds.
8882 * _wait_for() doesn't guarantee when its passed condition is evaluated
8883 * first, so send the first request explicitly.
8884 */
8885 if (COND) {
8886 ret = 0;
8887 goto out;
8888 }
8889 ret = _wait_for(COND, timeout_base_ms * 1000, 10);
8890 if (!ret)
8891 goto out;
8892
8893 /*
8894 * The above can time out if the number of requests was low (2 in the
8895 * worst case) _and_ PCODE was busy for some reason even after a
8896 * (queued) request and @timeout_base_ms delay. As a workaround retry
8897 * the poll with preemption disabled to maximize the number of
Imre Deak01299362017-02-24 16:32:10 +02008898 * requests. Increase the timeout from @timeout_base_ms to 50ms to
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008899 * account for interrupts that could reduce the number of these
Imre Deak01299362017-02-24 16:32:10 +02008900 * requests, and for any quirks of the PCODE firmware that delays
8901 * the request completion.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008902 */
8903 DRM_DEBUG_KMS("PCODE timeout, retrying with preemption disabled\n");
8904 WARN_ON_ONCE(timeout_base_ms > 3);
8905 preempt_disable();
Imre Deak01299362017-02-24 16:32:10 +02008906 ret = wait_for_atomic(COND, 50);
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008907 preempt_enable();
8908
8909out:
8910 return ret ? ret : status;
8911#undef COND
8912}
8913
Ville Syrjälädd06f882014-11-10 22:55:12 +02008914static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
8915{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008916 /*
8917 * N = val - 0xb7
8918 * Slow = Fast = GPLL ref * N
8919 */
8920 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * (val - 0xb7), 1000);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07008921}
8922
Fengguang Wub55dd642014-07-12 11:21:39 +02008923static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07008924{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008925 return DIV_ROUND_CLOSEST(1000 * val, dev_priv->rps.gpll_ref_freq) + 0xb7;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07008926}
8927
Fengguang Wub55dd642014-07-12 11:21:39 +02008928static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05308929{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008930 /*
8931 * N = val / 2
8932 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
8933 */
8934 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * val, 2 * 2 * 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05308935}
8936
Fengguang Wub55dd642014-07-12 11:21:39 +02008937static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05308938{
Ville Syrjälä1c147622014-08-18 14:42:43 +03008939 /* CHV needs even values */
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008940 return DIV_ROUND_CLOSEST(2 * 1000 * val, dev_priv->rps.gpll_ref_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05308941}
8942
Ville Syrjälä616bc822015-01-23 21:04:25 +02008943int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
8944{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008945 if (IS_GEN9(dev_priv))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02008946 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
8947 GEN9_FREQ_SCALER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008948 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008949 return chv_gpu_freq(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008950 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008951 return byt_gpu_freq(dev_priv, val);
8952 else
8953 return val * GT_FREQUENCY_MULTIPLIER;
8954}
8955
Ville Syrjälä616bc822015-01-23 21:04:25 +02008956int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
8957{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008958 if (IS_GEN9(dev_priv))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02008959 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
8960 GT_FREQUENCY_MULTIPLIER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008961 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008962 return chv_freq_opcode(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008963 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008964 return byt_freq_opcode(dev_priv, val);
8965 else
Mika Kuoppala500a3d22015-11-13 19:29:41 +02008966 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
Deepak S22b1b2f2014-07-12 14:54:33 +05308967}
8968
Chris Wilson6ad790c2015-04-07 16:20:31 +01008969struct request_boost {
8970 struct work_struct work;
Daniel Vettereed29a52015-05-21 14:21:25 +02008971 struct drm_i915_gem_request *req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01008972};
8973
8974static void __intel_rps_boost_work(struct work_struct *work)
8975{
8976 struct request_boost *boost = container_of(work, struct request_boost, work);
Chris Wilsone61b9952015-04-27 13:41:24 +01008977 struct drm_i915_gem_request *req = boost->req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01008978
Chris Wilsonf69a02c2016-07-01 17:23:16 +01008979 if (!i915_gem_request_completed(req))
Chris Wilsonc0336662016-05-06 15:40:21 +01008980 gen6_rps_boost(req->i915, NULL, req->emitted_jiffies);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008981
Chris Wilsone8a261e2016-07-20 13:31:49 +01008982 i915_gem_request_put(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008983 kfree(boost);
8984}
8985
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01008986void intel_queue_rps_boost_for_request(struct drm_i915_gem_request *req)
Chris Wilson6ad790c2015-04-07 16:20:31 +01008987{
8988 struct request_boost *boost;
8989
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01008990 if (req == NULL || INTEL_GEN(req->i915) < 6)
Chris Wilson6ad790c2015-04-07 16:20:31 +01008991 return;
8992
Chris Wilsonf69a02c2016-07-01 17:23:16 +01008993 if (i915_gem_request_completed(req))
Chris Wilsone61b9952015-04-27 13:41:24 +01008994 return;
8995
Chris Wilson6ad790c2015-04-07 16:20:31 +01008996 boost = kmalloc(sizeof(*boost), GFP_ATOMIC);
8997 if (boost == NULL)
8998 return;
8999
Chris Wilsone8a261e2016-07-20 13:31:49 +01009000 boost->req = i915_gem_request_get(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01009001
9002 INIT_WORK(&boost->work, __intel_rps_boost_work);
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01009003 queue_work(req->i915->wq, &boost->work);
Chris Wilson6ad790c2015-04-07 16:20:31 +01009004}
9005
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00009006void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01009007{
Daniel Vetterf742a552013-12-06 10:17:53 +01009008 mutex_init(&dev_priv->rps.hw_lock);
Chris Wilson8d3afd72015-05-21 21:01:47 +01009009 spin_lock_init(&dev_priv->rps.client_lock);
Daniel Vetterf742a552013-12-06 10:17:53 +01009010
Chris Wilson54b4f682016-07-21 21:16:19 +01009011 INIT_DELAYED_WORK(&dev_priv->rps.autoenable_work,
9012 __intel_autoenable_gt_powersave);
Chris Wilson1854d5c2015-04-07 16:20:32 +01009013 INIT_LIST_HEAD(&dev_priv->rps.clients);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03009014
Paulo Zanoni33688d92014-03-07 20:08:19 -03009015 dev_priv->pm.suspended = false;
Imre Deak1f814da2015-12-16 02:52:19 +02009016 atomic_set(&dev_priv->pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01009017}
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009018
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009019static u64 vlv_residency_raw(struct drm_i915_private *dev_priv,
9020 const i915_reg_t reg)
9021{
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009022 u32 lower, upper, tmp;
Chris Wilson71cc2b12017-03-24 16:54:18 +00009023 int loop = 2;
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009024
9025 /* The register accessed do not need forcewake. We borrow
9026 * uncore lock to prevent concurrent access to range reg.
9027 */
9028 spin_lock_irq(&dev_priv->uncore.lock);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009029
9030 /* vlv and chv residency counters are 40 bits in width.
9031 * With a control bit, we can choose between upper or lower
9032 * 32bit window into this counter.
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009033 *
9034 * Although we always use the counter in high-range mode elsewhere,
9035 * userspace may attempt to read the value before rc6 is initialised,
9036 * before we have set the default VLV_COUNTER_CONTROL value. So always
9037 * set the high bit to be safe.
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009038 */
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009039 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9040 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009041 upper = I915_READ_FW(reg);
9042 do {
9043 tmp = upper;
9044
9045 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9046 _MASKED_BIT_DISABLE(VLV_COUNT_RANGE_HIGH));
9047 lower = I915_READ_FW(reg);
9048
9049 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9050 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
9051 upper = I915_READ_FW(reg);
Chris Wilson71cc2b12017-03-24 16:54:18 +00009052 } while (upper != tmp && --loop);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009053
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009054 /* Everywhere else we always use VLV_COUNTER_CONTROL with the
9055 * VLV_COUNT_RANGE_HIGH bit set - so it is safe to leave it set
9056 * now.
9057 */
9058
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009059 spin_unlock_irq(&dev_priv->uncore.lock);
9060
9061 return lower | (u64)upper << 8;
9062}
9063
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009064u64 intel_rc6_residency_us(struct drm_i915_private *dev_priv,
9065 const i915_reg_t reg)
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009066{
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009067 u64 time_hw, units, div;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009068
9069 if (!intel_enable_rc6())
9070 return 0;
9071
9072 intel_runtime_pm_get(dev_priv);
9073
9074 /* On VLV and CHV, residency time is in CZ units rather than 1.28us */
9075 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009076 units = 1000;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009077 div = dev_priv->czclk_freq;
9078
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009079 time_hw = vlv_residency_raw(dev_priv, reg);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009080 } else if (IS_GEN9_LP(dev_priv)) {
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009081 units = 1000;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009082 div = 1200; /* 833.33ns */
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009083
9084 time_hw = I915_READ(reg);
9085 } else {
9086 units = 128000; /* 1.28us */
9087 div = 100000;
9088
9089 time_hw = I915_READ(reg);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009090 }
9091
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009092 intel_runtime_pm_put(dev_priv);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009093 return DIV_ROUND_UP_ULL(time_hw * units, div);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009094}