blob: 6e393b217450ab145ac07c4c05612fb99d8b214c [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{
Rodrigo Vivi82525c12017-06-08 08:50:00 -070061 /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl,cfl */
Mika Kuoppalab033bb62016-06-07 17:19:04 +030062 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
Rodrigo Vivi82525c12017-06-08 08:50:00 -070068 /* WaEnableChickenDCPR:skl,bxt,kbl,glk,cfl */
Mika Kuoppala590e8ff2016-06-07 17:19:13 +030069 I915_WRITE(GEN8_CHICKEN_DCPR_1,
70 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
Mika Kuoppala0f78dee2016-06-07 17:19:16 +030071
Rodrigo Vivi82525c12017-06-08 08:50:00 -070072 /* WaFbcTurnOffFbcWatermark:skl,bxt,kbl,cfl */
73 /* WaFbcWakeMemOn:skl,bxt,kbl,glk,cfl */
Mika Kuoppala303d4ea2016-06-07 17:19:17 +030074 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
75 DISP_FBC_WM_DIS |
76 DISP_FBC_MEMORY_WAKE);
Mika Kuoppalad1b4eef2016-06-07 17:19:19 +030077
Rodrigo Vivi82525c12017-06-08 08:50:00 -070078 /* WaFbcHighMemBwCorruptionAvoidance:skl,bxt,kbl,cfl */
Mika Kuoppalad1b4eef2016-06-07 17:19:19 +030079 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
80 ILK_DPFC_DISABLE_DUMMY0);
Praveen Paneri32087d12017-08-03 23:02:10 +053081
82 if (IS_SKYLAKE(dev_priv)) {
83 /* WaDisableDopClockGating */
84 I915_WRITE(GEN7_MISCCPCTL, I915_READ(GEN7_MISCCPCTL)
85 & ~GEN7_DOP_CLOCK_GATE_ENABLE);
86 }
Mika Kuoppalab033bb62016-06-07 17:19:04 +030087}
88
Ville Syrjälä46f16e62016-10-31 22:37:22 +020089static void bxt_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deaka82abe42015-03-27 14:00:04 +020090{
Ville Syrjälä46f16e62016-10-31 22:37:22 +020091 gen9_init_clock_gating(dev_priv);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +020092
Nick Hoatha7546152015-06-29 14:07:32 +010093 /* WaDisableSDEUnitClockGating:bxt */
94 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
95 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
96
Imre Deak32608ca2015-03-11 11:10:27 +020097 /*
98 * FIXME:
Ben Widawsky868434c2015-03-11 10:49:32 +020099 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
Imre Deak32608ca2015-03-11 11:10:27 +0200100 */
Imre Deak32608ca2015-03-11 11:10:27 +0200101 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
Ben Widawsky868434c2015-03-11 10:49:32 +0200102 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
Imre Deakd965e7ac2015-12-01 10:23:52 +0200103
104 /*
105 * Wa: Backlight PWM may stop in the asserted state, causing backlight
106 * to stay fully on.
107 */
Jani Nikula8aeaf642017-02-15 17:21:37 +0200108 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
109 PWM1_GATING_DIS | PWM2_GATING_DIS);
Imre Deaka82abe42015-03-27 14:00:04 +0200110}
111
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200112static void glk_init_clock_gating(struct drm_i915_private *dev_priv)
113{
114 gen9_init_clock_gating(dev_priv);
115
116 /*
117 * WaDisablePWMClockGating:glk
118 * Backlight PWM may stop in the asserted state, causing backlight
119 * to stay fully on.
120 */
121 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
122 PWM1_GATING_DIS | PWM2_GATING_DIS);
Ander Conselvan de Oliveiraf4f4b592017-02-22 08:34:29 +0200123
124 /* WaDDIIOTimeout:glk */
125 if (IS_GLK_REVID(dev_priv, 0, GLK_REVID_A1)) {
126 u32 val = I915_READ(CHICKEN_MISC_2);
127 val &= ~(GLK_CL0_PWR_DOWN |
128 GLK_CL1_PWR_DOWN |
129 GLK_CL2_PWR_DOWN);
130 I915_WRITE(CHICKEN_MISC_2, val);
131 }
132
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200133}
134
Ville Syrjälä148ac1f2016-10-31 22:37:16 +0200135static void i915_pineview_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200136{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200137 u32 tmp;
138
139 tmp = I915_READ(CLKCFG);
140
141 switch (tmp & CLKCFG_FSB_MASK) {
142 case CLKCFG_FSB_533:
143 dev_priv->fsb_freq = 533; /* 133*4 */
144 break;
145 case CLKCFG_FSB_800:
146 dev_priv->fsb_freq = 800; /* 200*4 */
147 break;
148 case CLKCFG_FSB_667:
149 dev_priv->fsb_freq = 667; /* 167*4 */
150 break;
151 case CLKCFG_FSB_400:
152 dev_priv->fsb_freq = 400; /* 100*4 */
153 break;
154 }
155
156 switch (tmp & CLKCFG_MEM_MASK) {
157 case CLKCFG_MEM_533:
158 dev_priv->mem_freq = 533;
159 break;
160 case CLKCFG_MEM_667:
161 dev_priv->mem_freq = 667;
162 break;
163 case CLKCFG_MEM_800:
164 dev_priv->mem_freq = 800;
165 break;
166 }
167
168 /* detect pineview DDR3 setting */
169 tmp = I915_READ(CSHRDDR3CTL);
170 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
171}
172
Ville Syrjälä148ac1f2016-10-31 22:37:16 +0200173static void i915_ironlake_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200174{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200175 u16 ddrpll, csipll;
176
177 ddrpll = I915_READ16(DDRMPLL1);
178 csipll = I915_READ16(CSIPLL0);
179
180 switch (ddrpll & 0xff) {
181 case 0xc:
182 dev_priv->mem_freq = 800;
183 break;
184 case 0x10:
185 dev_priv->mem_freq = 1066;
186 break;
187 case 0x14:
188 dev_priv->mem_freq = 1333;
189 break;
190 case 0x18:
191 dev_priv->mem_freq = 1600;
192 break;
193 default:
194 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
195 ddrpll & 0xff);
196 dev_priv->mem_freq = 0;
197 break;
198 }
199
Daniel Vetter20e4d402012-08-08 23:35:39 +0200200 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200201
202 switch (csipll & 0x3ff) {
203 case 0x00c:
204 dev_priv->fsb_freq = 3200;
205 break;
206 case 0x00e:
207 dev_priv->fsb_freq = 3733;
208 break;
209 case 0x010:
210 dev_priv->fsb_freq = 4266;
211 break;
212 case 0x012:
213 dev_priv->fsb_freq = 4800;
214 break;
215 case 0x014:
216 dev_priv->fsb_freq = 5333;
217 break;
218 case 0x016:
219 dev_priv->fsb_freq = 5866;
220 break;
221 case 0x018:
222 dev_priv->fsb_freq = 6400;
223 break;
224 default:
225 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
226 csipll & 0x3ff);
227 dev_priv->fsb_freq = 0;
228 break;
229 }
230
231 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200232 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200233 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200234 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200235 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200236 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200237 }
238}
239
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300240static const struct cxsr_latency cxsr_latency_table[] = {
241 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
242 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
243 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
244 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
245 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
246
247 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
248 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
249 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
250 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
251 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
252
253 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
254 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
255 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
256 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
257 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
258
259 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
260 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
261 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
262 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
263 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
264
265 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
266 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
267 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
268 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
269 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
270
271 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
272 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
273 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
274 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
275 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
276};
277
Tvrtko Ursulin44a655c2016-10-13 11:09:23 +0100278static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
279 bool is_ddr3,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300280 int fsb,
281 int mem)
282{
283 const struct cxsr_latency *latency;
284 int i;
285
286 if (fsb == 0 || mem == 0)
287 return NULL;
288
289 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
290 latency = &cxsr_latency_table[i];
291 if (is_desktop == latency->is_desktop &&
292 is_ddr3 == latency->is_ddr3 &&
293 fsb == latency->fsb_freq && mem == latency->mem_freq)
294 return latency;
295 }
296
297 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
298
299 return NULL;
300}
301
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200302static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
303{
304 u32 val;
305
306 mutex_lock(&dev_priv->rps.hw_lock);
307
308 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
309 if (enable)
310 val &= ~FORCE_DDR_HIGH_FREQ;
311 else
312 val |= FORCE_DDR_HIGH_FREQ;
313 val &= ~FORCE_DDR_LOW_FREQ;
314 val |= FORCE_DDR_FREQ_REQ_ACK;
315 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
316
317 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
318 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
319 DRM_ERROR("timed out waiting for Punit DDR DVFS request\n");
320
321 mutex_unlock(&dev_priv->rps.hw_lock);
322}
323
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200324static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
325{
326 u32 val;
327
328 mutex_lock(&dev_priv->rps.hw_lock);
329
330 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
331 if (enable)
332 val |= DSP_MAXFIFO_PM5_ENABLE;
333 else
334 val &= ~DSP_MAXFIFO_PM5_ENABLE;
335 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
336
337 mutex_unlock(&dev_priv->rps.hw_lock);
338}
339
Ville Syrjäläf4998962015-03-10 17:02:21 +0200340#define FW_WM(value, plane) \
341 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
342
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200343static bool _intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300344{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200345 bool was_enabled;
Imre Deak5209b1f2014-07-01 12:36:17 +0300346 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300347
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100348 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200349 was_enabled = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300350 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300351 POSTING_READ(FW_BLC_SELF_VLV);
Jani Nikulac0f86832016-12-07 12:13:04 +0200352 } else if (IS_G4X(dev_priv) || IS_I965GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200353 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300354 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300355 POSTING_READ(FW_BLC_SELF);
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +0200356 } else if (IS_PINEVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200357 val = I915_READ(DSPFW3);
358 was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
359 if (enable)
360 val |= PINEVIEW_SELF_REFRESH_EN;
361 else
362 val &= ~PINEVIEW_SELF_REFRESH_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300363 I915_WRITE(DSPFW3, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300364 POSTING_READ(DSPFW3);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100365 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200366 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300367 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
368 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
369 I915_WRITE(FW_BLC_SELF, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300370 POSTING_READ(FW_BLC_SELF);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100371 } else if (IS_I915GM(dev_priv)) {
Ville Syrjäläacb91352016-07-29 17:57:02 +0300372 /*
373 * FIXME can't find a bit like this for 915G, and
374 * and yet it does have the related watermark in
375 * FW_BLC_SELF. What's going on?
376 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200377 was_enabled = I915_READ(INSTPM) & INSTPM_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300378 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
379 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
380 I915_WRITE(INSTPM, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300381 POSTING_READ(INSTPM);
Imre Deak5209b1f2014-07-01 12:36:17 +0300382 } else {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200383 return false;
Imre Deak5209b1f2014-07-01 12:36:17 +0300384 }
385
Ville Syrjälä1489bba2017-03-02 19:15:07 +0200386 trace_intel_memory_cxsr(dev_priv, was_enabled, enable);
387
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200388 DRM_DEBUG_KMS("memory self-refresh is %s (was %s)\n",
389 enableddisabled(enable),
390 enableddisabled(was_enabled));
391
392 return was_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300393}
394
Ville Syrjälä62571fc2017-04-21 21:14:23 +0300395/**
396 * intel_set_memory_cxsr - Configure CxSR state
397 * @dev_priv: i915 device
398 * @enable: Allow vs. disallow CxSR
399 *
400 * Allow or disallow the system to enter a special CxSR
401 * (C-state self refresh) state. What typically happens in CxSR mode
402 * is that several display FIFOs may get combined into a single larger
403 * FIFO for a particular plane (so called max FIFO mode) to allow the
404 * system to defer memory fetches longer, and the memory will enter
405 * self refresh.
406 *
407 * Note that enabling CxSR does not guarantee that the system enter
408 * this special mode, nor does it guarantee that the system stays
409 * in that mode once entered. So this just allows/disallows the system
410 * to autonomously utilize the CxSR mode. Other factors such as core
411 * C-states will affect when/if the system actually enters/exits the
412 * CxSR mode.
413 *
414 * Note that on VLV/CHV this actually only controls the max FIFO mode,
415 * and the system is free to enter/exit memory self refresh at any time
416 * even when the use of CxSR has been disallowed.
417 *
418 * While the system is actually in the CxSR/max FIFO mode, some plane
419 * control registers will not get latched on vblank. Thus in order to
420 * guarantee the system will respond to changes in the plane registers
421 * we must always disallow CxSR prior to making changes to those registers.
422 * Unfortunately the system will re-evaluate the CxSR conditions at
423 * frame start which happens after vblank start (which is when the plane
424 * registers would get latched), so we can't proceed with the plane update
425 * during the same frame where we disallowed CxSR.
426 *
427 * Certain platforms also have a deeper HPLL SR mode. Fortunately the
428 * HPLL SR mode depends on CxSR itself, so we don't have to hand hold
429 * the hardware w.r.t. HPLL SR when writing to plane registers.
430 * Disallowing just CxSR is sufficient.
431 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200432bool intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200433{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200434 bool ret;
435
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200436 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200437 ret = _intel_set_memory_cxsr(dev_priv, enable);
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300438 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
439 dev_priv->wm.vlv.cxsr = enable;
440 else if (IS_G4X(dev_priv))
441 dev_priv->wm.g4x.cxsr = enable;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200442 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200443
444 return ret;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200445}
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200446
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300447/*
448 * Latency for FIFO fetches is dependent on several factors:
449 * - memory configuration (speed, channels)
450 * - chipset
451 * - current MCH state
452 * It can be fairly high in some situations, so here we assume a fairly
453 * pessimal value. It's a tradeoff between extra memory fetches (if we
454 * set this value too high, the FIFO will fetch frequently to stay full)
455 * and power consumption (set it too low to save power and we might see
456 * FIFO underruns and display "flicker").
457 *
458 * A value of 5us seems to be a good balance; safe for very low end
459 * platforms but not overly aggressive on lower latency configs.
460 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100461static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300462
Ville Syrjäläb5004722015-03-05 21:19:47 +0200463#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
464 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
465
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200466static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
Ville Syrjäläb5004722015-03-05 21:19:47 +0200467{
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200468 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200469 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200470 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200471 enum pipe pipe = crtc->pipe;
472 int sprite0_start, sprite1_start;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200473
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200474 switch (pipe) {
Ville Syrjäläb5004722015-03-05 21:19:47 +0200475 uint32_t dsparb, dsparb2, dsparb3;
476 case PIPE_A:
477 dsparb = I915_READ(DSPARB);
478 dsparb2 = I915_READ(DSPARB2);
479 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
480 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
481 break;
482 case PIPE_B:
483 dsparb = I915_READ(DSPARB);
484 dsparb2 = I915_READ(DSPARB2);
485 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
486 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
487 break;
488 case PIPE_C:
489 dsparb2 = I915_READ(DSPARB2);
490 dsparb3 = I915_READ(DSPARB3);
491 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
492 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
493 break;
494 default:
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200495 MISSING_CASE(pipe);
496 return;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200497 }
498
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200499 fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
500 fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
501 fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
502 fifo_state->plane[PLANE_CURSOR] = 63;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200503}
504
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200505static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300506{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300507 uint32_t dsparb = I915_READ(DSPARB);
508 int size;
509
510 size = dsparb & 0x7f;
511 if (plane)
512 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
513
514 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
515 plane ? "B" : "A", size);
516
517 return size;
518}
519
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200520static int i830_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300521{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300522 uint32_t dsparb = I915_READ(DSPARB);
523 int size;
524
525 size = dsparb & 0x1ff;
526 if (plane)
527 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
528 size >>= 1; /* Convert to cachelines */
529
530 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
531 plane ? "B" : "A", size);
532
533 return size;
534}
535
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200536static int i845_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300537{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300538 uint32_t dsparb = I915_READ(DSPARB);
539 int size;
540
541 size = dsparb & 0x7f;
542 size >>= 2; /* Convert to cachelines */
543
544 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
545 plane ? "B" : "A",
546 size);
547
548 return size;
549}
550
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300551/* Pineview has different values for various configs */
552static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300553 .fifo_size = PINEVIEW_DISPLAY_FIFO,
554 .max_wm = PINEVIEW_MAX_WM,
555 .default_wm = PINEVIEW_DFT_WM,
556 .guard_size = PINEVIEW_GUARD_WM,
557 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300558};
559static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300560 .fifo_size = PINEVIEW_DISPLAY_FIFO,
561 .max_wm = PINEVIEW_MAX_WM,
562 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
563 .guard_size = PINEVIEW_GUARD_WM,
564 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300565};
566static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300567 .fifo_size = PINEVIEW_CURSOR_FIFO,
568 .max_wm = PINEVIEW_CURSOR_MAX_WM,
569 .default_wm = PINEVIEW_CURSOR_DFT_WM,
570 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
571 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300572};
573static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300574 .fifo_size = PINEVIEW_CURSOR_FIFO,
575 .max_wm = PINEVIEW_CURSOR_MAX_WM,
576 .default_wm = PINEVIEW_CURSOR_DFT_WM,
577 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
578 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300579};
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300580static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300581 .fifo_size = I965_CURSOR_FIFO,
582 .max_wm = I965_CURSOR_MAX_WM,
583 .default_wm = I965_CURSOR_DFT_WM,
584 .guard_size = 2,
585 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300586};
587static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300588 .fifo_size = I945_FIFO_SIZE,
589 .max_wm = I915_MAX_WM,
590 .default_wm = 1,
591 .guard_size = 2,
592 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300593};
594static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300595 .fifo_size = I915_FIFO_SIZE,
596 .max_wm = I915_MAX_WM,
597 .default_wm = 1,
598 .guard_size = 2,
599 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300600};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300601static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300602 .fifo_size = I855GM_FIFO_SIZE,
603 .max_wm = I915_MAX_WM,
604 .default_wm = 1,
605 .guard_size = 2,
606 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300607};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300608static const struct intel_watermark_params i830_bc_wm_info = {
609 .fifo_size = I855GM_FIFO_SIZE,
610 .max_wm = I915_MAX_WM/2,
611 .default_wm = 1,
612 .guard_size = 2,
613 .cacheline_size = I830_FIFO_LINE_SIZE,
614};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200615static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300616 .fifo_size = I830_FIFO_SIZE,
617 .max_wm = I915_MAX_WM,
618 .default_wm = 1,
619 .guard_size = 2,
620 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300621};
622
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300623/**
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300624 * intel_wm_method1 - Method 1 / "small buffer" watermark formula
625 * @pixel_rate: Pipe pixel rate in kHz
626 * @cpp: Plane bytes per pixel
627 * @latency: Memory wakeup latency in 0.1us units
628 *
629 * Compute the watermark using the method 1 or "small buffer"
630 * formula. The caller may additonally add extra cachelines
631 * to account for TLB misses and clock crossings.
632 *
633 * This method is concerned with the short term drain rate
634 * of the FIFO, ie. it does not account for blanking periods
635 * which would effectively reduce the average drain rate across
636 * a longer period. The name "small" refers to the fact the
637 * FIFO is relatively small compared to the amount of data
638 * fetched.
639 *
640 * The FIFO level vs. time graph might look something like:
641 *
642 * |\ |\
643 * | \ | \
644 * __---__---__ (- plane active, _ blanking)
645 * -> time
646 *
647 * or perhaps like this:
648 *
649 * |\|\ |\|\
650 * __----__----__ (- plane active, _ blanking)
651 * -> time
652 *
653 * Returns:
654 * The watermark in bytes
655 */
656static unsigned int intel_wm_method1(unsigned int pixel_rate,
657 unsigned int cpp,
658 unsigned int latency)
659{
660 uint64_t ret;
661
662 ret = (uint64_t) pixel_rate * cpp * latency;
663 ret = DIV_ROUND_UP_ULL(ret, 10000);
664
665 return ret;
666}
667
668/**
669 * intel_wm_method2 - Method 2 / "large buffer" watermark formula
670 * @pixel_rate: Pipe pixel rate in kHz
671 * @htotal: Pipe horizontal total
672 * @width: Plane width in pixels
673 * @cpp: Plane bytes per pixel
674 * @latency: Memory wakeup latency in 0.1us units
675 *
676 * Compute the watermark using the method 2 or "large buffer"
677 * formula. The caller may additonally add extra cachelines
678 * to account for TLB misses and clock crossings.
679 *
680 * This method is concerned with the long term drain rate
681 * of the FIFO, ie. it does account for blanking periods
682 * which effectively reduce the average drain rate across
683 * a longer period. The name "large" refers to the fact the
684 * FIFO is relatively large compared to the amount of data
685 * fetched.
686 *
687 * The FIFO level vs. time graph might look something like:
688 *
689 * |\___ |\___
690 * | \___ | \___
691 * | \ | \
692 * __ --__--__--__--__--__--__ (- plane active, _ blanking)
693 * -> time
694 *
695 * Returns:
696 * The watermark in bytes
697 */
698static unsigned int intel_wm_method2(unsigned int pixel_rate,
699 unsigned int htotal,
700 unsigned int width,
701 unsigned int cpp,
702 unsigned int latency)
703{
704 unsigned int ret;
705
706 /*
707 * FIXME remove once all users are computing
708 * watermarks in the correct place.
709 */
710 if (WARN_ON_ONCE(htotal == 0))
711 htotal = 1;
712
713 ret = (latency * pixel_rate) / (htotal * 10000);
714 ret = (ret + 1) * width * cpp;
715
716 return ret;
717}
718
719/**
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300720 * intel_calculate_wm - calculate watermark level
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300721 * @pixel_rate: pixel clock
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300722 * @wm: chip FIFO params
Ville Syrjäläac484962016-01-20 21:05:26 +0200723 * @cpp: bytes per pixel
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300724 * @latency_ns: memory latency for the platform
725 *
726 * Calculate the watermark level (the level at which the display plane will
727 * start fetching from memory again). Each chip has a different display
728 * FIFO size and allocation, so the caller needs to figure that out and pass
729 * in the correct intel_watermark_params structure.
730 *
731 * As the pixel clock runs, the FIFO will be drained at a rate that depends
732 * on the pixel size. When it reaches the watermark level, it'll start
733 * fetching FIFO line sized based chunks from memory until the FIFO fills
734 * past the watermark point. If the FIFO drains completely, a FIFO underrun
735 * will occur, and a display engine hang could result.
736 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300737static unsigned int intel_calculate_wm(int pixel_rate,
738 const struct intel_watermark_params *wm,
739 int fifo_size, int cpp,
740 unsigned int latency_ns)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300741{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300742 int entries, wm_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300743
744 /*
745 * Note: we need to make sure we don't overflow for various clock &
746 * latency values.
747 * clocks go from a few thousand to several hundred thousand.
748 * latency is usually a few thousand
749 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300750 entries = intel_wm_method1(pixel_rate, cpp,
751 latency_ns / 100);
752 entries = DIV_ROUND_UP(entries, wm->cacheline_size) +
753 wm->guard_size;
754 DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300755
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300756 wm_size = fifo_size - entries;
757 DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300758
759 /* Don't promote wm_size to unsigned... */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300760 if (wm_size > wm->max_wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300761 wm_size = wm->max_wm;
762 if (wm_size <= 0)
763 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300764
765 /*
766 * Bspec seems to indicate that the value shouldn't be lower than
767 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
768 * Lets go for 8 which is the burst size since certain platforms
769 * already use a hardcoded 8 (which is what the spec says should be
770 * done).
771 */
772 if (wm_size <= 8)
773 wm_size = 8;
774
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300775 return wm_size;
776}
777
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300778static bool is_disabling(int old, int new, int threshold)
779{
780 return old >= threshold && new < threshold;
781}
782
783static bool is_enabling(int old, int new, int threshold)
784{
785 return old < threshold && new >= threshold;
786}
787
Ville Syrjälä6d5019b2017-04-21 21:14:20 +0300788static int intel_wm_num_levels(struct drm_i915_private *dev_priv)
789{
790 return dev_priv->wm.max_level + 1;
791}
792
Ville Syrjälä24304d812017-03-14 17:10:49 +0200793static bool intel_wm_plane_visible(const struct intel_crtc_state *crtc_state,
794 const struct intel_plane_state *plane_state)
795{
796 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
797
798 /* FIXME check the 'enable' instead */
799 if (!crtc_state->base.active)
800 return false;
801
802 /*
803 * Treat cursor with fb as always visible since cursor updates
804 * can happen faster than the vrefresh rate, and the current
805 * watermark code doesn't handle that correctly. Cursor updates
806 * which set/clear the fb or change the cursor size are going
807 * to get throttled by intel_legacy_cursor_update() to work
808 * around this problem with the watermark code.
809 */
810 if (plane->id == PLANE_CURSOR)
811 return plane_state->base.fb != NULL;
812 else
813 return plane_state->base.visible;
814}
815
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200816static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300817{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200818 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300819
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200820 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200821 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300822 if (enabled)
823 return NULL;
824 enabled = crtc;
825 }
826 }
827
828 return enabled;
829}
830
Ville Syrjälä432081b2016-10-31 22:37:03 +0200831static void pineview_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300832{
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200833 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200834 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300835 const struct cxsr_latency *latency;
836 u32 reg;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300837 unsigned int wm;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300838
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100839 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
840 dev_priv->is_ddr3,
841 dev_priv->fsb_freq,
842 dev_priv->mem_freq);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300843 if (!latency) {
844 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300845 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300846 return;
847 }
848
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200849 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300850 if (crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200851 const struct drm_display_mode *adjusted_mode =
852 &crtc->config->base.adjusted_mode;
853 const struct drm_framebuffer *fb =
854 crtc->base.primary->state->fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200855 int cpp = fb->format->cpp[0];
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300856 int clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300857
858 /* Display SR */
859 wm = intel_calculate_wm(clock, &pineview_display_wm,
860 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200861 cpp, latency->display_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300862 reg = I915_READ(DSPFW1);
863 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200864 reg |= FW_WM(wm, SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300865 I915_WRITE(DSPFW1, reg);
866 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
867
868 /* cursor SR */
869 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
870 pineview_display_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300871 4, latency->cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300872 reg = I915_READ(DSPFW3);
873 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200874 reg |= FW_WM(wm, CURSOR_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300875 I915_WRITE(DSPFW3, reg);
876
877 /* Display HPLL off SR */
878 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
879 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200880 cpp, latency->display_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300881 reg = I915_READ(DSPFW3);
882 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200883 reg |= FW_WM(wm, HPLL_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300884 I915_WRITE(DSPFW3, reg);
885
886 /* cursor HPLL off SR */
887 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
888 pineview_display_hplloff_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300889 4, latency->cursor_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300890 reg = I915_READ(DSPFW3);
891 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200892 reg |= FW_WM(wm, HPLL_CURSOR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300893 I915_WRITE(DSPFW3, reg);
894 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
895
Imre Deak5209b1f2014-07-01 12:36:17 +0300896 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300897 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300898 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300899 }
900}
901
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300902/*
903 * Documentation says:
904 * "If the line size is small, the TLB fetches can get in the way of the
905 * data fetches, causing some lag in the pixel data return which is not
906 * accounted for in the above formulas. The following adjustment only
907 * needs to be applied if eight whole lines fit in the buffer at once.
908 * The WM is adjusted upwards by the difference between the FIFO size
909 * and the size of 8 whole lines. This adjustment is always performed
910 * in the actual pixel depth regardless of whether FBC is enabled or not."
911 */
912static int g4x_tlb_miss_wa(int fifo_size, int width, int cpp)
913{
914 int tlb_miss = fifo_size * 64 - width * cpp * 8;
915
916 return max(0, tlb_miss);
917}
918
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300919static void g4x_write_wm_values(struct drm_i915_private *dev_priv,
920 const struct g4x_wm_values *wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300921{
Ville Syrjäläe93329a2017-04-21 21:14:31 +0300922 enum pipe pipe;
923
924 for_each_pipe(dev_priv, pipe)
925 trace_g4x_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
926
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300927 I915_WRITE(DSPFW1,
928 FW_WM(wm->sr.plane, SR) |
929 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
930 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
931 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
932 I915_WRITE(DSPFW2,
933 (wm->fbc_en ? DSPFW_FBC_SR_EN : 0) |
934 FW_WM(wm->sr.fbc, FBC_SR) |
935 FW_WM(wm->hpll.fbc, FBC_HPLL_SR) |
936 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEB) |
937 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
938 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
939 I915_WRITE(DSPFW3,
940 (wm->hpll_en ? DSPFW_HPLL_SR_EN : 0) |
941 FW_WM(wm->sr.cursor, CURSOR_SR) |
942 FW_WM(wm->hpll.cursor, HPLL_CURSOR) |
943 FW_WM(wm->hpll.plane, HPLL_SR));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300944
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300945 POSTING_READ(DSPFW1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300946}
947
Ville Syrjälä15665972015-03-10 16:16:28 +0200948#define FW_WM_VLV(value, plane) \
949 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
950
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200951static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200952 const struct vlv_wm_values *wm)
953{
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200954 enum pipe pipe;
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200955
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200956 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläc137d662017-03-02 19:15:06 +0200957 trace_vlv_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
958
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200959 I915_WRITE(VLV_DDL(pipe),
960 (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
961 (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
962 (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
963 (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
964 }
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200965
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +0200966 /*
967 * Zero the (unused) WM1 watermarks, and also clear all the
968 * high order bits so that there are no out of bounds values
969 * present in the registers during the reprogramming.
970 */
971 I915_WRITE(DSPHOWM, 0);
972 I915_WRITE(DSPHOWM1, 0);
973 I915_WRITE(DSPFW4, 0);
974 I915_WRITE(DSPFW5, 0);
975 I915_WRITE(DSPFW6, 0);
976
Ville Syrjäläae801522015-03-05 21:19:49 +0200977 I915_WRITE(DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +0200978 FW_WM(wm->sr.plane, SR) |
Ville Syrjälä1b313892016-11-28 19:37:08 +0200979 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
980 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
981 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200982 I915_WRITE(DSPFW2,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200983 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
984 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
985 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200986 I915_WRITE(DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +0200987 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +0200988
989 if (IS_CHERRYVIEW(dev_priv)) {
990 I915_WRITE(DSPFW7_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200991 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
992 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200993 I915_WRITE(DSPFW8_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200994 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
995 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
Ville Syrjäläae801522015-03-05 21:19:49 +0200996 I915_WRITE(DSPFW9_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200997 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
998 FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200999 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001000 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001001 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
1002 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
1003 FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
1004 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1005 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1006 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1007 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1008 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1009 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001010 } else {
1011 I915_WRITE(DSPFW7,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001012 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1013 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001014 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001015 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001016 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1017 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1018 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1019 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1020 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1021 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001022 }
1023
1024 POSTING_READ(DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001025}
1026
Ville Syrjälä15665972015-03-10 16:16:28 +02001027#undef FW_WM_VLV
1028
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001029static void g4x_setup_wm_latency(struct drm_i915_private *dev_priv)
1030{
1031 /* all latencies in usec */
1032 dev_priv->wm.pri_latency[G4X_WM_LEVEL_NORMAL] = 5;
1033 dev_priv->wm.pri_latency[G4X_WM_LEVEL_SR] = 12;
Ville Syrjälä79d94302017-04-21 21:14:30 +03001034 dev_priv->wm.pri_latency[G4X_WM_LEVEL_HPLL] = 35;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001035
Ville Syrjälä79d94302017-04-21 21:14:30 +03001036 dev_priv->wm.max_level = G4X_WM_LEVEL_HPLL;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001037}
1038
1039static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
1040{
1041 /*
1042 * DSPCNTR[13] supposedly controls whether the
1043 * primary plane can use the FIFO space otherwise
1044 * reserved for the sprite plane. It's not 100% clear
1045 * what the actual FIFO size is, but it looks like we
1046 * can happily set both primary and sprite watermarks
1047 * up to 127 cachelines. So that would seem to mean
1048 * that either DSPCNTR[13] doesn't do anything, or that
1049 * the total FIFO is >= 256 cachelines in size. Either
1050 * way, we don't seem to have to worry about this
1051 * repartitioning as the maximum watermark value the
1052 * register can hold for each plane is lower than the
1053 * minimum FIFO size.
1054 */
1055 switch (plane_id) {
1056 case PLANE_CURSOR:
1057 return 63;
1058 case PLANE_PRIMARY:
1059 return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
1060 case PLANE_SPRITE0:
1061 return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
1062 default:
1063 MISSING_CASE(plane_id);
1064 return 0;
1065 }
1066}
1067
1068static int g4x_fbc_fifo_size(int level)
1069{
1070 switch (level) {
1071 case G4X_WM_LEVEL_SR:
1072 return 7;
1073 case G4X_WM_LEVEL_HPLL:
1074 return 15;
1075 default:
1076 MISSING_CASE(level);
1077 return 0;
1078 }
1079}
1080
1081static uint16_t g4x_compute_wm(const struct intel_crtc_state *crtc_state,
1082 const struct intel_plane_state *plane_state,
1083 int level)
1084{
1085 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1086 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1087 const struct drm_display_mode *adjusted_mode =
1088 &crtc_state->base.adjusted_mode;
1089 int clock, htotal, cpp, width, wm;
1090 int latency = dev_priv->wm.pri_latency[level] * 10;
1091
1092 if (latency == 0)
1093 return USHRT_MAX;
1094
1095 if (!intel_wm_plane_visible(crtc_state, plane_state))
1096 return 0;
1097
1098 /*
1099 * Not 100% sure which way ELK should go here as the
1100 * spec only says CL/CTG should assume 32bpp and BW
1101 * doesn't need to. But as these things followed the
1102 * mobile vs. desktop lines on gen3 as well, let's
1103 * assume ELK doesn't need this.
1104 *
1105 * The spec also fails to list such a restriction for
1106 * the HPLL watermark, which seems a little strange.
1107 * Let's use 32bpp for the HPLL watermark as well.
1108 */
1109 if (IS_GM45(dev_priv) && plane->id == PLANE_PRIMARY &&
1110 level != G4X_WM_LEVEL_NORMAL)
1111 cpp = 4;
1112 else
1113 cpp = plane_state->base.fb->format->cpp[0];
1114
1115 clock = adjusted_mode->crtc_clock;
1116 htotal = adjusted_mode->crtc_htotal;
1117
1118 if (plane->id == PLANE_CURSOR)
1119 width = plane_state->base.crtc_w;
1120 else
1121 width = drm_rect_width(&plane_state->base.dst);
1122
1123 if (plane->id == PLANE_CURSOR) {
1124 wm = intel_wm_method2(clock, htotal, width, cpp, latency);
1125 } else if (plane->id == PLANE_PRIMARY &&
1126 level == G4X_WM_LEVEL_NORMAL) {
1127 wm = intel_wm_method1(clock, cpp, latency);
1128 } else {
1129 int small, large;
1130
1131 small = intel_wm_method1(clock, cpp, latency);
1132 large = intel_wm_method2(clock, htotal, width, cpp, latency);
1133
1134 wm = min(small, large);
1135 }
1136
1137 wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
1138 width, cpp);
1139
1140 wm = DIV_ROUND_UP(wm, 64) + 2;
1141
1142 return min_t(int, wm, USHRT_MAX);
1143}
1144
1145static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1146 int level, enum plane_id plane_id, u16 value)
1147{
1148 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1149 bool dirty = false;
1150
1151 for (; level < intel_wm_num_levels(dev_priv); level++) {
1152 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1153
1154 dirty |= raw->plane[plane_id] != value;
1155 raw->plane[plane_id] = value;
1156 }
1157
1158 return dirty;
1159}
1160
1161static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
1162 int level, u16 value)
1163{
1164 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1165 bool dirty = false;
1166
1167 /* NORMAL level doesn't have an FBC watermark */
1168 level = max(level, G4X_WM_LEVEL_SR);
1169
1170 for (; level < intel_wm_num_levels(dev_priv); level++) {
1171 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1172
1173 dirty |= raw->fbc != value;
1174 raw->fbc = value;
1175 }
1176
1177 return dirty;
1178}
1179
1180static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
1181 const struct intel_plane_state *pstate,
1182 uint32_t pri_val);
1183
1184static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1185 const struct intel_plane_state *plane_state)
1186{
1187 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1188 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
1189 enum plane_id plane_id = plane->id;
1190 bool dirty = false;
1191 int level;
1192
1193 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1194 dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1195 if (plane_id == PLANE_PRIMARY)
1196 dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
1197 goto out;
1198 }
1199
1200 for (level = 0; level < num_levels; level++) {
1201 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1202 int wm, max_wm;
1203
1204 wm = g4x_compute_wm(crtc_state, plane_state, level);
1205 max_wm = g4x_plane_fifo_size(plane_id, level);
1206
1207 if (wm > max_wm)
1208 break;
1209
1210 dirty |= raw->plane[plane_id] != wm;
1211 raw->plane[plane_id] = wm;
1212
1213 if (plane_id != PLANE_PRIMARY ||
1214 level == G4X_WM_LEVEL_NORMAL)
1215 continue;
1216
1217 wm = ilk_compute_fbc_wm(crtc_state, plane_state,
1218 raw->plane[plane_id]);
1219 max_wm = g4x_fbc_fifo_size(level);
1220
1221 /*
1222 * FBC wm is not mandatory as we
1223 * can always just disable its use.
1224 */
1225 if (wm > max_wm)
1226 wm = USHRT_MAX;
1227
1228 dirty |= raw->fbc != wm;
1229 raw->fbc = wm;
1230 }
1231
1232 /* mark watermarks as invalid */
1233 dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1234
1235 if (plane_id == PLANE_PRIMARY)
1236 dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
1237
1238 out:
1239 if (dirty) {
1240 DRM_DEBUG_KMS("%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
1241 plane->base.name,
1242 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
1243 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
1244 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
1245
1246 if (plane_id == PLANE_PRIMARY)
1247 DRM_DEBUG_KMS("FBC watermarks: SR=%d, HPLL=%d\n",
1248 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
1249 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
1250 }
1251
1252 return dirty;
1253}
1254
1255static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1256 enum plane_id plane_id, int level)
1257{
1258 const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1259
1260 return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
1261}
1262
1263static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
1264 int level)
1265{
1266 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1267
1268 if (level > dev_priv->wm.max_level)
1269 return false;
1270
1271 return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1272 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1273 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1274}
1275
1276/* mark all levels starting from 'level' as invalid */
1277static void g4x_invalidate_wms(struct intel_crtc *crtc,
1278 struct g4x_wm_state *wm_state, int level)
1279{
1280 if (level <= G4X_WM_LEVEL_NORMAL) {
1281 enum plane_id plane_id;
1282
1283 for_each_plane_id_on_crtc(crtc, plane_id)
1284 wm_state->wm.plane[plane_id] = USHRT_MAX;
1285 }
1286
1287 if (level <= G4X_WM_LEVEL_SR) {
1288 wm_state->cxsr = false;
1289 wm_state->sr.cursor = USHRT_MAX;
1290 wm_state->sr.plane = USHRT_MAX;
1291 wm_state->sr.fbc = USHRT_MAX;
1292 }
1293
1294 if (level <= G4X_WM_LEVEL_HPLL) {
1295 wm_state->hpll_en = false;
1296 wm_state->hpll.cursor = USHRT_MAX;
1297 wm_state->hpll.plane = USHRT_MAX;
1298 wm_state->hpll.fbc = USHRT_MAX;
1299 }
1300}
1301
1302static int g4x_compute_pipe_wm(struct intel_crtc_state *crtc_state)
1303{
1304 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1305 struct intel_atomic_state *state =
1306 to_intel_atomic_state(crtc_state->base.state);
1307 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
1308 int num_active_planes = hweight32(crtc_state->active_planes &
1309 ~BIT(PLANE_CURSOR));
1310 const struct g4x_pipe_wm *raw;
1311 struct intel_plane_state *plane_state;
1312 struct intel_plane *plane;
1313 enum plane_id plane_id;
1314 int i, level;
1315 unsigned int dirty = 0;
1316
1317 for_each_intel_plane_in_state(state, plane, plane_state, i) {
1318 const struct intel_plane_state *old_plane_state =
1319 to_intel_plane_state(plane->base.state);
1320
1321 if (plane_state->base.crtc != &crtc->base &&
1322 old_plane_state->base.crtc != &crtc->base)
1323 continue;
1324
1325 if (g4x_raw_plane_wm_compute(crtc_state, plane_state))
1326 dirty |= BIT(plane->id);
1327 }
1328
1329 if (!dirty)
1330 return 0;
1331
1332 level = G4X_WM_LEVEL_NORMAL;
1333 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1334 goto out;
1335
1336 raw = &crtc_state->wm.g4x.raw[level];
1337 for_each_plane_id_on_crtc(crtc, plane_id)
1338 wm_state->wm.plane[plane_id] = raw->plane[plane_id];
1339
1340 level = G4X_WM_LEVEL_SR;
1341
1342 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1343 goto out;
1344
1345 raw = &crtc_state->wm.g4x.raw[level];
1346 wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
1347 wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
1348 wm_state->sr.fbc = raw->fbc;
1349
1350 wm_state->cxsr = num_active_planes == BIT(PLANE_PRIMARY);
1351
1352 level = G4X_WM_LEVEL_HPLL;
1353
1354 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1355 goto out;
1356
1357 raw = &crtc_state->wm.g4x.raw[level];
1358 wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
1359 wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
1360 wm_state->hpll.fbc = raw->fbc;
1361
1362 wm_state->hpll_en = wm_state->cxsr;
1363
1364 level++;
1365
1366 out:
1367 if (level == G4X_WM_LEVEL_NORMAL)
1368 return -EINVAL;
1369
1370 /* invalidate the higher levels */
1371 g4x_invalidate_wms(crtc, wm_state, level);
1372
1373 /*
1374 * Determine if the FBC watermark(s) can be used. IF
1375 * this isn't the case we prefer to disable the FBC
1376 ( watermark(s) rather than disable the SR/HPLL
1377 * level(s) entirely.
1378 */
1379 wm_state->fbc_en = level > G4X_WM_LEVEL_NORMAL;
1380
1381 if (level >= G4X_WM_LEVEL_SR &&
1382 wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
1383 wm_state->fbc_en = false;
1384 else if (level >= G4X_WM_LEVEL_HPLL &&
1385 wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
1386 wm_state->fbc_en = false;
1387
1388 return 0;
1389}
1390
1391static int g4x_compute_intermediate_wm(struct drm_device *dev,
1392 struct intel_crtc *crtc,
1393 struct intel_crtc_state *crtc_state)
1394{
1395 struct g4x_wm_state *intermediate = &crtc_state->wm.g4x.intermediate;
1396 const struct g4x_wm_state *optimal = &crtc_state->wm.g4x.optimal;
1397 const struct g4x_wm_state *active = &crtc->wm.active.g4x;
1398 enum plane_id plane_id;
1399
1400 intermediate->cxsr = optimal->cxsr && active->cxsr &&
1401 !crtc_state->disable_cxsr;
1402 intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
1403 !crtc_state->disable_cxsr;
1404 intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
1405
1406 for_each_plane_id_on_crtc(crtc, plane_id) {
1407 intermediate->wm.plane[plane_id] =
1408 max(optimal->wm.plane[plane_id],
1409 active->wm.plane[plane_id]);
1410
1411 WARN_ON(intermediate->wm.plane[plane_id] >
1412 g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
1413 }
1414
1415 intermediate->sr.plane = max(optimal->sr.plane,
1416 active->sr.plane);
1417 intermediate->sr.cursor = max(optimal->sr.cursor,
1418 active->sr.cursor);
1419 intermediate->sr.fbc = max(optimal->sr.fbc,
1420 active->sr.fbc);
1421
1422 intermediate->hpll.plane = max(optimal->hpll.plane,
1423 active->hpll.plane);
1424 intermediate->hpll.cursor = max(optimal->hpll.cursor,
1425 active->hpll.cursor);
1426 intermediate->hpll.fbc = max(optimal->hpll.fbc,
1427 active->hpll.fbc);
1428
1429 WARN_ON((intermediate->sr.plane >
1430 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
1431 intermediate->sr.cursor >
1432 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
1433 intermediate->cxsr);
1434 WARN_ON((intermediate->sr.plane >
1435 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
1436 intermediate->sr.cursor >
1437 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
1438 intermediate->hpll_en);
1439
1440 WARN_ON(intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
1441 intermediate->fbc_en && intermediate->cxsr);
1442 WARN_ON(intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
1443 intermediate->fbc_en && intermediate->hpll_en);
1444
1445 /*
1446 * If our intermediate WM are identical to the final WM, then we can
1447 * omit the post-vblank programming; only update if it's different.
1448 */
1449 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
1450 crtc_state->wm.need_postvbl_update = true;
1451
1452 return 0;
1453}
1454
1455static void g4x_merge_wm(struct drm_i915_private *dev_priv,
1456 struct g4x_wm_values *wm)
1457{
1458 struct intel_crtc *crtc;
1459 int num_active_crtcs = 0;
1460
1461 wm->cxsr = true;
1462 wm->hpll_en = true;
1463 wm->fbc_en = true;
1464
1465 for_each_intel_crtc(&dev_priv->drm, crtc) {
1466 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1467
1468 if (!crtc->active)
1469 continue;
1470
1471 if (!wm_state->cxsr)
1472 wm->cxsr = false;
1473 if (!wm_state->hpll_en)
1474 wm->hpll_en = false;
1475 if (!wm_state->fbc_en)
1476 wm->fbc_en = false;
1477
1478 num_active_crtcs++;
1479 }
1480
1481 if (num_active_crtcs != 1) {
1482 wm->cxsr = false;
1483 wm->hpll_en = false;
1484 wm->fbc_en = false;
1485 }
1486
1487 for_each_intel_crtc(&dev_priv->drm, crtc) {
1488 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1489 enum pipe pipe = crtc->pipe;
1490
1491 wm->pipe[pipe] = wm_state->wm;
1492 if (crtc->active && wm->cxsr)
1493 wm->sr = wm_state->sr;
1494 if (crtc->active && wm->hpll_en)
1495 wm->hpll = wm_state->hpll;
1496 }
1497}
1498
1499static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
1500{
1501 struct g4x_wm_values *old_wm = &dev_priv->wm.g4x;
1502 struct g4x_wm_values new_wm = {};
1503
1504 g4x_merge_wm(dev_priv, &new_wm);
1505
1506 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
1507 return;
1508
1509 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
1510 _intel_set_memory_cxsr(dev_priv, false);
1511
1512 g4x_write_wm_values(dev_priv, &new_wm);
1513
1514 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
1515 _intel_set_memory_cxsr(dev_priv, true);
1516
1517 *old_wm = new_wm;
1518}
1519
1520static void g4x_initial_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 *crtc = to_intel_crtc(crtc_state->base.crtc);
1525
1526 mutex_lock(&dev_priv->wm.wm_mutex);
1527 crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
1528 g4x_program_watermarks(dev_priv);
1529 mutex_unlock(&dev_priv->wm.wm_mutex);
1530}
1531
1532static void g4x_optimize_watermarks(struct intel_atomic_state *state,
1533 struct intel_crtc_state *crtc_state)
1534{
1535 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1536 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
1537
1538 if (!crtc_state->wm.need_postvbl_update)
1539 return;
1540
1541 mutex_lock(&dev_priv->wm.wm_mutex);
1542 intel_crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
1543 g4x_program_watermarks(dev_priv);
1544 mutex_unlock(&dev_priv->wm.wm_mutex);
1545}
1546
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001547/* latency must be in 0.1us units. */
1548static unsigned int vlv_wm_method2(unsigned int pixel_rate,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001549 unsigned int htotal,
1550 unsigned int width,
Ville Syrjäläac484962016-01-20 21:05:26 +02001551 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001552 unsigned int latency)
1553{
1554 unsigned int ret;
1555
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001556 ret = intel_wm_method2(pixel_rate, htotal,
1557 width, cpp, latency);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001558 ret = DIV_ROUND_UP(ret, 64);
1559
1560 return ret;
1561}
1562
Ville Syrjäläbb726512016-10-31 22:37:24 +02001563static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001564{
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001565 /* all latencies in usec */
1566 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1567
Ville Syrjälä58590c12015-09-08 21:05:12 +03001568 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
1569
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001570 if (IS_CHERRYVIEW(dev_priv)) {
1571 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1572 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001573
1574 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001575 }
1576}
1577
Ville Syrjäläe339d672016-11-28 19:37:17 +02001578static uint16_t vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1579 const struct intel_plane_state *plane_state,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001580 int level)
1581{
Ville Syrjäläe339d672016-11-28 19:37:17 +02001582 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001583 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläe339d672016-11-28 19:37:17 +02001584 const struct drm_display_mode *adjusted_mode =
1585 &crtc_state->base.adjusted_mode;
Ville Syrjäläac484962016-01-20 21:05:26 +02001586 int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001587
1588 if (dev_priv->wm.pri_latency[level] == 0)
1589 return USHRT_MAX;
1590
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001591 if (!intel_wm_plane_visible(crtc_state, plane_state))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001592 return 0;
1593
Daniel Vetteref426c12017-01-04 11:41:10 +01001594 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjäläe339d672016-11-28 19:37:17 +02001595 clock = adjusted_mode->crtc_clock;
1596 htotal = adjusted_mode->crtc_htotal;
1597 width = crtc_state->pipe_src_w;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001598
Ville Syrjälä709f3fc2017-03-03 17:19:26 +02001599 if (plane->id == PLANE_CURSOR) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001600 /*
1601 * FIXME the formula gives values that are
1602 * too big for the cursor FIFO, and hence we
1603 * would never be able to use cursors. For
1604 * now just hardcode the watermark.
1605 */
1606 wm = 63;
1607 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +02001608 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001609 dev_priv->wm.pri_latency[level] * 10);
1610 }
1611
1612 return min_t(int, wm, USHRT_MAX);
1613}
1614
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001615static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1616{
1617 return (active_planes & (BIT(PLANE_SPRITE0) |
1618 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1619}
1620
Ville Syrjälä5012e602017-03-02 19:14:56 +02001621static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001622{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001623 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001624 const struct g4x_pipe_wm *raw =
Ville Syrjälä5012e602017-03-02 19:14:56 +02001625 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001626 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001627 unsigned int active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1628 int num_active_planes = hweight32(active_planes);
1629 const int fifo_size = 511;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001630 int fifo_extra, fifo_left = fifo_size;
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001631 int sprite0_fifo_extra = 0;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001632 unsigned int total_rate;
1633 enum plane_id plane_id;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001634
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001635 /*
1636 * When enabling sprite0 after sprite1 has already been enabled
1637 * we tend to get an underrun unless sprite0 already has some
1638 * FIFO space allcoated. Hence we always allocate at least one
1639 * cacheline for sprite0 whenever sprite1 is enabled.
1640 *
1641 * All other plane enable sequences appear immune to this problem.
1642 */
1643 if (vlv_need_sprite0_fifo_workaround(active_planes))
1644 sprite0_fifo_extra = 1;
1645
Ville Syrjälä5012e602017-03-02 19:14:56 +02001646 total_rate = raw->plane[PLANE_PRIMARY] +
1647 raw->plane[PLANE_SPRITE0] +
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001648 raw->plane[PLANE_SPRITE1] +
1649 sprite0_fifo_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001650
Ville Syrjälä5012e602017-03-02 19:14:56 +02001651 if (total_rate > fifo_size)
1652 return -EINVAL;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001653
Ville Syrjälä5012e602017-03-02 19:14:56 +02001654 if (total_rate == 0)
1655 total_rate = 1;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001656
Ville Syrjälä5012e602017-03-02 19:14:56 +02001657 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001658 unsigned int rate;
1659
Ville Syrjälä5012e602017-03-02 19:14:56 +02001660 if ((active_planes & BIT(plane_id)) == 0) {
1661 fifo_state->plane[plane_id] = 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001662 continue;
1663 }
1664
Ville Syrjälä5012e602017-03-02 19:14:56 +02001665 rate = raw->plane[plane_id];
1666 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1667 fifo_left -= fifo_state->plane[plane_id];
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001668 }
1669
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001670 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1671 fifo_left -= sprite0_fifo_extra;
1672
Ville Syrjälä5012e602017-03-02 19:14:56 +02001673 fifo_state->plane[PLANE_CURSOR] = 63;
1674
1675 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001676
1677 /* spread the remainder evenly */
Ville Syrjälä5012e602017-03-02 19:14:56 +02001678 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001679 int plane_extra;
1680
1681 if (fifo_left == 0)
1682 break;
1683
Ville Syrjälä5012e602017-03-02 19:14:56 +02001684 if ((active_planes & BIT(plane_id)) == 0)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001685 continue;
1686
1687 plane_extra = min(fifo_extra, fifo_left);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001688 fifo_state->plane[plane_id] += plane_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001689 fifo_left -= plane_extra;
1690 }
1691
Ville Syrjälä5012e602017-03-02 19:14:56 +02001692 WARN_ON(active_planes != 0 && fifo_left != 0);
1693
1694 /* give it all to the first plane if none are active */
1695 if (active_planes == 0) {
1696 WARN_ON(fifo_left != fifo_size);
1697 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1698 }
1699
1700 return 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001701}
1702
Ville Syrjäläff32c542017-03-02 19:14:57 +02001703/* mark all levels starting from 'level' as invalid */
1704static void vlv_invalidate_wms(struct intel_crtc *crtc,
1705 struct vlv_wm_state *wm_state, int level)
1706{
1707 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1708
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001709 for (; level < intel_wm_num_levels(dev_priv); level++) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02001710 enum plane_id plane_id;
1711
1712 for_each_plane_id_on_crtc(crtc, plane_id)
1713 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1714
1715 wm_state->sr[level].cursor = USHRT_MAX;
1716 wm_state->sr[level].plane = USHRT_MAX;
1717 }
1718}
1719
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001720static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1721{
1722 if (wm > fifo_size)
1723 return USHRT_MAX;
1724 else
1725 return fifo_size - wm;
1726}
1727
Ville Syrjäläff32c542017-03-02 19:14:57 +02001728/*
1729 * Starting from 'level' set all higher
1730 * levels to 'value' in the "raw" watermarks.
1731 */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001732static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001733 int level, enum plane_id plane_id, u16 value)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001734{
Ville Syrjäläff32c542017-03-02 19:14:57 +02001735 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001736 int num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001737 bool dirty = false;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001738
Ville Syrjäläff32c542017-03-02 19:14:57 +02001739 for (; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001740 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001741
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001742 dirty |= raw->plane[plane_id] != value;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001743 raw->plane[plane_id] = value;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001744 }
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001745
1746 return dirty;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001747}
1748
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001749static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1750 const struct intel_plane_state *plane_state)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001751{
1752 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1753 enum plane_id plane_id = plane->id;
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001754 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
Ville Syrjäläff32c542017-03-02 19:14:57 +02001755 int level;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001756 bool dirty = false;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001757
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001758 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001759 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1760 goto out;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001761 }
1762
1763 for (level = 0; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001764 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001765 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1766 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1767
Ville Syrjäläff32c542017-03-02 19:14:57 +02001768 if (wm > max_wm)
1769 break;
1770
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001771 dirty |= raw->plane[plane_id] != wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001772 raw->plane[plane_id] = wm;
1773 }
1774
1775 /* mark all higher levels as invalid */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001776 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001777
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001778out:
1779 if (dirty)
Ville Syrjälä57a65282017-04-21 21:14:22 +03001780 DRM_DEBUG_KMS("%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001781 plane->base.name,
1782 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1783 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1784 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
1785
1786 return dirty;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001787}
1788
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001789static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1790 enum plane_id plane_id, int level)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001791{
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001792 const struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02001793 &crtc_state->wm.vlv.raw[level];
1794 const struct vlv_fifo_state *fifo_state =
1795 &crtc_state->wm.vlv.fifo_state;
1796
1797 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1798}
1799
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001800static 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 +02001801{
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001802 return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1803 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1804 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1805 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001806}
1807
1808static int vlv_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001809{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001810 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001811 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001812 struct intel_atomic_state *state =
1813 to_intel_atomic_state(crtc_state->base.state);
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001814 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001815 const struct vlv_fifo_state *fifo_state =
1816 &crtc_state->wm.vlv.fifo_state;
1817 int num_active_planes = hweight32(crtc_state->active_planes &
1818 ~BIT(PLANE_CURSOR));
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001819 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->base);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001820 struct intel_plane_state *plane_state;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001821 struct intel_plane *plane;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001822 enum plane_id plane_id;
1823 int level, ret, i;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001824 unsigned int dirty = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001825
Ville Syrjäläff32c542017-03-02 19:14:57 +02001826 for_each_intel_plane_in_state(state, plane, plane_state, i) {
1827 const struct intel_plane_state *old_plane_state =
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001828 to_intel_plane_state(plane->base.state);
1829
Ville Syrjäläff32c542017-03-02 19:14:57 +02001830 if (plane_state->base.crtc != &crtc->base &&
1831 old_plane_state->base.crtc != &crtc->base)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001832 continue;
1833
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001834 if (vlv_raw_plane_wm_compute(crtc_state, plane_state))
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001835 dirty |= BIT(plane->id);
1836 }
1837
1838 /*
1839 * DSPARB registers may have been reset due to the
1840 * power well being turned off. Make sure we restore
1841 * them to a consistent state even if no primary/sprite
1842 * planes are initially active.
1843 */
1844 if (needs_modeset)
1845 crtc_state->fifo_changed = true;
1846
1847 if (!dirty)
1848 return 0;
1849
1850 /* cursor changes don't warrant a FIFO recompute */
1851 if (dirty & ~BIT(PLANE_CURSOR)) {
1852 const struct intel_crtc_state *old_crtc_state =
1853 to_intel_crtc_state(crtc->base.state);
1854 const struct vlv_fifo_state *old_fifo_state =
1855 &old_crtc_state->wm.vlv.fifo_state;
1856
1857 ret = vlv_compute_fifo(crtc_state);
1858 if (ret)
1859 return ret;
1860
1861 if (needs_modeset ||
1862 memcmp(old_fifo_state, fifo_state,
1863 sizeof(*fifo_state)) != 0)
1864 crtc_state->fifo_changed = true;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001865 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001866
Ville Syrjäläff32c542017-03-02 19:14:57 +02001867 /* initially allow all levels */
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001868 wm_state->num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001869 /*
1870 * Note that enabling cxsr with no primary/sprite planes
1871 * enabled can wedge the pipe. Hence we only allow cxsr
1872 * with exactly one enabled primary/sprite plane.
1873 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001874 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001875
Ville Syrjälä5012e602017-03-02 19:14:56 +02001876 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001877 const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001878 const int sr_fifo_size = INTEL_INFO(dev_priv)->num_pipes * 512 - 1;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001879
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001880 if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
Ville Syrjäläff32c542017-03-02 19:14:57 +02001881 break;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001882
Ville Syrjäläff32c542017-03-02 19:14:57 +02001883 for_each_plane_id_on_crtc(crtc, plane_id) {
1884 wm_state->wm[level].plane[plane_id] =
1885 vlv_invert_wm_value(raw->plane[plane_id],
1886 fifo_state->plane[plane_id]);
1887 }
1888
1889 wm_state->sr[level].plane =
1890 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
Ville Syrjälä5012e602017-03-02 19:14:56 +02001891 raw->plane[PLANE_SPRITE0],
Ville Syrjäläff32c542017-03-02 19:14:57 +02001892 raw->plane[PLANE_SPRITE1]),
1893 sr_fifo_size);
1894
1895 wm_state->sr[level].cursor =
1896 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1897 63);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001898 }
1899
Ville Syrjäläff32c542017-03-02 19:14:57 +02001900 if (level == 0)
1901 return -EINVAL;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001902
Ville Syrjäläff32c542017-03-02 19:14:57 +02001903 /* limit to only levels we can actually handle */
1904 wm_state->num_levels = level;
1905
1906 /* invalidate the higher levels */
1907 vlv_invalidate_wms(crtc, wm_state, level);
1908
1909 return 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001910}
1911
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001912#define VLV_FIFO(plane, value) \
1913 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1914
Ville Syrjäläff32c542017-03-02 19:14:57 +02001915static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
1916 struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001917{
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001918 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001919 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001920 const struct vlv_fifo_state *fifo_state =
1921 &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001922 int sprite0_start, sprite1_start, fifo_size;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001923
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001924 if (!crtc_state->fifo_changed)
1925 return;
1926
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001927 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
1928 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
1929 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001930
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001931 WARN_ON(fifo_state->plane[PLANE_CURSOR] != 63);
1932 WARN_ON(fifo_size != 511);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001933
Ville Syrjäläc137d662017-03-02 19:15:06 +02001934 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
1935
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001936 /*
1937 * uncore.lock serves a double purpose here. It allows us to
1938 * use the less expensive I915_{READ,WRITE}_FW() functions, and
1939 * it protects the DSPARB registers from getting clobbered by
1940 * parallel updates from multiple pipes.
1941 *
1942 * intel_pipe_update_start() has already disabled interrupts
1943 * for us, so a plain spin_lock() is sufficient here.
1944 */
1945 spin_lock(&dev_priv->uncore.lock);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001946
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001947 switch (crtc->pipe) {
1948 uint32_t dsparb, dsparb2, dsparb3;
1949 case PIPE_A:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001950 dsparb = I915_READ_FW(DSPARB);
1951 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001952
1953 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1954 VLV_FIFO(SPRITEB, 0xff));
1955 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1956 VLV_FIFO(SPRITEB, sprite1_start));
1957
1958 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1959 VLV_FIFO(SPRITEB_HI, 0x1));
1960 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1961 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1962
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001963 I915_WRITE_FW(DSPARB, dsparb);
1964 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001965 break;
1966 case PIPE_B:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001967 dsparb = I915_READ_FW(DSPARB);
1968 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001969
1970 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1971 VLV_FIFO(SPRITED, 0xff));
1972 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1973 VLV_FIFO(SPRITED, sprite1_start));
1974
1975 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
1976 VLV_FIFO(SPRITED_HI, 0xff));
1977 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
1978 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
1979
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001980 I915_WRITE_FW(DSPARB, dsparb);
1981 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001982 break;
1983 case PIPE_C:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001984 dsparb3 = I915_READ_FW(DSPARB3);
1985 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001986
1987 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
1988 VLV_FIFO(SPRITEF, 0xff));
1989 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
1990 VLV_FIFO(SPRITEF, sprite1_start));
1991
1992 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
1993 VLV_FIFO(SPRITEF_HI, 0xff));
1994 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
1995 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
1996
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001997 I915_WRITE_FW(DSPARB3, dsparb3);
1998 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001999 break;
2000 default:
2001 break;
2002 }
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002003
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002004 POSTING_READ_FW(DSPARB);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002005
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002006 spin_unlock(&dev_priv->uncore.lock);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002007}
2008
2009#undef VLV_FIFO
2010
Ville Syrjälä4841da52017-03-02 19:14:59 +02002011static int vlv_compute_intermediate_wm(struct drm_device *dev,
2012 struct intel_crtc *crtc,
2013 struct intel_crtc_state *crtc_state)
2014{
2015 struct vlv_wm_state *intermediate = &crtc_state->wm.vlv.intermediate;
2016 const struct vlv_wm_state *optimal = &crtc_state->wm.vlv.optimal;
2017 const struct vlv_wm_state *active = &crtc->wm.active.vlv;
2018 int level;
2019
2020 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002021 intermediate->cxsr = optimal->cxsr && active->cxsr &&
2022 !crtc_state->disable_cxsr;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002023
2024 for (level = 0; level < intermediate->num_levels; level++) {
2025 enum plane_id plane_id;
2026
2027 for_each_plane_id_on_crtc(crtc, plane_id) {
2028 intermediate->wm[level].plane[plane_id] =
2029 min(optimal->wm[level].plane[plane_id],
2030 active->wm[level].plane[plane_id]);
2031 }
2032
2033 intermediate->sr[level].plane = min(optimal->sr[level].plane,
2034 active->sr[level].plane);
2035 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
2036 active->sr[level].cursor);
2037 }
2038
2039 vlv_invalidate_wms(crtc, intermediate, level);
2040
2041 /*
2042 * If our intermediate WM are identical to the final WM, then we can
2043 * omit the post-vblank programming; only update if it's different.
2044 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002045 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
2046 crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002047
2048 return 0;
2049}
2050
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002051static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002052 struct vlv_wm_values *wm)
2053{
2054 struct intel_crtc *crtc;
2055 int num_active_crtcs = 0;
2056
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002057 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002058 wm->cxsr = true;
2059
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002060 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002061 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002062
2063 if (!crtc->active)
2064 continue;
2065
2066 if (!wm_state->cxsr)
2067 wm->cxsr = false;
2068
2069 num_active_crtcs++;
2070 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
2071 }
2072
2073 if (num_active_crtcs != 1)
2074 wm->cxsr = false;
2075
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03002076 if (num_active_crtcs > 1)
2077 wm->level = VLV_WM_LEVEL_PM2;
2078
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002079 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002080 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002081 enum pipe pipe = crtc->pipe;
2082
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002083 wm->pipe[pipe] = wm_state->wm[wm->level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02002084 if (crtc->active && wm->cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002085 wm->sr = wm_state->sr[wm->level];
2086
Ville Syrjälä1b313892016-11-28 19:37:08 +02002087 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
2088 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
2089 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
2090 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002091 }
2092}
2093
Ville Syrjäläff32c542017-03-02 19:14:57 +02002094static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002095{
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002096 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
2097 struct vlv_wm_values new_wm = {};
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002098
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002099 vlv_merge_wm(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002100
Ville Syrjäläff32c542017-03-02 19:14:57 +02002101 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002102 return;
2103
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002104 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002105 chv_set_memory_dvfs(dev_priv, false);
2106
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002107 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002108 chv_set_memory_pm5(dev_priv, false);
2109
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002110 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002111 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002112
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002113 vlv_write_wm_values(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002114
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002115 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002116 _intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002117
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002118 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002119 chv_set_memory_pm5(dev_priv, true);
2120
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002121 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002122 chv_set_memory_dvfs(dev_priv, true);
2123
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002124 *old_wm = new_wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03002125}
2126
Ville Syrjäläff32c542017-03-02 19:14:57 +02002127static void vlv_initial_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 *crtc = to_intel_crtc(crtc_state->base.crtc);
2132
2133 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002134 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
2135 vlv_program_watermarks(dev_priv);
2136 mutex_unlock(&dev_priv->wm.wm_mutex);
2137}
2138
2139static void vlv_optimize_watermarks(struct intel_atomic_state *state,
2140 struct intel_crtc_state *crtc_state)
2141{
2142 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2143 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2144
2145 if (!crtc_state->wm.need_postvbl_update)
2146 return;
2147
2148 mutex_lock(&dev_priv->wm.wm_mutex);
2149 intel_crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02002150 vlv_program_watermarks(dev_priv);
2151 mutex_unlock(&dev_priv->wm.wm_mutex);
2152}
2153
Ville Syrjälä432081b2016-10-31 22:37:03 +02002154static void i965_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002155{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002156 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002157 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002158 int srwm = 1;
2159 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03002160 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002161
2162 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002163 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002164 if (crtc) {
2165 /* self-refresh has much higher latency */
2166 static const int sr_latency_ns = 12000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002167 const struct drm_display_mode *adjusted_mode =
2168 &crtc->config->base.adjusted_mode;
2169 const struct drm_framebuffer *fb =
2170 crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002171 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002172 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002173 int hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002174 int cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002175 int entries;
2176
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002177 entries = intel_wm_method2(clock, htotal,
2178 hdisplay, cpp, sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002179 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
2180 srwm = I965_FIFO_SIZE - entries;
2181 if (srwm < 0)
2182 srwm = 1;
2183 srwm &= 0x1ff;
2184 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
2185 entries, srwm);
2186
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002187 entries = intel_wm_method2(clock, htotal,
2188 crtc->base.cursor->state->crtc_w, 4,
2189 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002190 entries = DIV_ROUND_UP(entries,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002191 i965_cursor_wm_info.cacheline_size) +
2192 i965_cursor_wm_info.guard_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002193
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002194 cursor_sr = i965_cursor_wm_info.fifo_size - entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002195 if (cursor_sr > i965_cursor_wm_info.max_wm)
2196 cursor_sr = i965_cursor_wm_info.max_wm;
2197
2198 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
2199 "cursor %d\n", srwm, cursor_sr);
2200
Imre Deak98584252014-06-13 14:54:20 +03002201 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002202 } else {
Imre Deak98584252014-06-13 14:54:20 +03002203 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002204 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03002205 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002206 }
2207
2208 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2209 srwm);
2210
2211 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002212 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
2213 FW_WM(8, CURSORB) |
2214 FW_WM(8, PLANEB) |
2215 FW_WM(8, PLANEA));
2216 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
2217 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002218 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002219 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03002220
2221 if (cxsr_enabled)
2222 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002223}
2224
Ville Syrjäläf4998962015-03-10 17:02:21 +02002225#undef FW_WM
2226
Ville Syrjälä432081b2016-10-31 22:37:03 +02002227static void i9xx_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002228{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002229 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002230 const struct intel_watermark_params *wm_info;
2231 uint32_t fwater_lo;
2232 uint32_t fwater_hi;
2233 int cwm, srwm = 1;
2234 int fifo_size;
2235 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002236 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002237
Ville Syrjäläa9097be2016-10-31 22:37:20 +02002238 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002239 wm_info = &i945_wm_info;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002240 else if (!IS_GEN2(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002241 wm_info = &i915_wm_info;
2242 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03002243 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002244
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02002245 fifo_size = dev_priv->display.get_fifo_size(dev_priv, 0);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02002246 crtc = intel_get_crtc_for_plane(dev_priv, 0);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002247 if (intel_crtc_active(crtc)) {
2248 const struct drm_display_mode *adjusted_mode =
2249 &crtc->config->base.adjusted_mode;
2250 const struct drm_framebuffer *fb =
2251 crtc->base.primary->state->fb;
2252 int cpp;
2253
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002254 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002255 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002256 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002257 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002258
Damien Lespiau241bfc32013-09-25 16:45:37 +01002259 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002260 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002261 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002262 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002263 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002264 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002265 if (planea_wm > (long)wm_info->max_wm)
2266 planea_wm = wm_info->max_wm;
2267 }
2268
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002269 if (IS_GEN2(dev_priv))
Ville Syrjälä9d539102014-08-15 01:21:53 +03002270 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002271
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02002272 fifo_size = dev_priv->display.get_fifo_size(dev_priv, 1);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02002273 crtc = intel_get_crtc_for_plane(dev_priv, 1);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002274 if (intel_crtc_active(crtc)) {
2275 const struct drm_display_mode *adjusted_mode =
2276 &crtc->config->base.adjusted_mode;
2277 const struct drm_framebuffer *fb =
2278 crtc->base.primary->state->fb;
2279 int cpp;
2280
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002281 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002282 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002283 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002284 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002285
Damien Lespiau241bfc32013-09-25 16:45:37 +01002286 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002287 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002288 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002289 if (enabled == NULL)
2290 enabled = crtc;
2291 else
2292 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002293 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002294 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002295 if (planeb_wm > (long)wm_info->max_wm)
2296 planeb_wm = wm_info->max_wm;
2297 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002298
2299 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
2300
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002301 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07002302 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002303
Ville Syrjäläefc26112016-10-31 22:37:04 +02002304 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002305
2306 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01002307 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002308 enabled = NULL;
2309 }
2310
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002311 /*
2312 * Overlay gets an aggressive default since video jitter is bad.
2313 */
2314 cwm = 2;
2315
2316 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03002317 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002318
2319 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02002320 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002321 /* self-refresh has much higher latency */
2322 static const int sr_latency_ns = 6000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002323 const struct drm_display_mode *adjusted_mode =
2324 &enabled->config->base.adjusted_mode;
2325 const struct drm_framebuffer *fb =
2326 enabled->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002327 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002328 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002329 int hdisplay = enabled->config->pipe_src_w;
2330 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002331 int entries;
2332
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002333 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002334 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002335 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002336 cpp = fb->format->cpp[0];
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002337
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002338 entries = intel_wm_method2(clock, htotal, hdisplay, cpp,
2339 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002340 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
2341 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
2342 srwm = wm_info->fifo_size - entries;
2343 if (srwm < 0)
2344 srwm = 1;
2345
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002346 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002347 I915_WRITE(FW_BLC_SELF,
2348 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03002349 else
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002350 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
2351 }
2352
2353 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
2354 planea_wm, planeb_wm, cwm, srwm);
2355
2356 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2357 fwater_hi = (cwm & 0x1f);
2358
2359 /* Set request length to 8 cachelines per fetch */
2360 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2361 fwater_hi = fwater_hi | (1 << 8);
2362
2363 I915_WRITE(FW_BLC, fwater_lo);
2364 I915_WRITE(FW_BLC2, fwater_hi);
2365
Imre Deak5209b1f2014-07-01 12:36:17 +03002366 if (enabled)
2367 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002368}
2369
Ville Syrjälä432081b2016-10-31 22:37:03 +02002370static void i845_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002371{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002372 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002373 struct intel_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002374 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002375 uint32_t fwater_lo;
2376 int planea_wm;
2377
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002378 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002379 if (crtc == NULL)
2380 return;
2381
Ville Syrjäläefc26112016-10-31 22:37:04 +02002382 adjusted_mode = &crtc->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002383 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02002384 &i845_wm_info,
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02002385 dev_priv->display.get_fifo_size(dev_priv, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01002386 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002387 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
2388 fwater_lo |= (3<<8) | planea_wm;
2389
2390 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
2391
2392 I915_WRITE(FW_BLC, fwater_lo);
2393}
2394
Ville Syrjälä37126462013-08-01 16:18:55 +03002395/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002396static unsigned int ilk_wm_method1(unsigned int pixel_rate,
2397 unsigned int cpp,
2398 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002399{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002400 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002401
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002402 ret = intel_wm_method1(pixel_rate, cpp, latency);
2403 ret = DIV_ROUND_UP(ret, 64) + 2;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002404
2405 return ret;
2406}
2407
Ville Syrjälä37126462013-08-01 16:18:55 +03002408/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002409static unsigned int ilk_wm_method2(unsigned int pixel_rate,
2410 unsigned int htotal,
2411 unsigned int width,
2412 unsigned int cpp,
2413 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002414{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002415 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002416
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002417 ret = intel_wm_method2(pixel_rate, htotal,
2418 width, cpp, latency);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002419 ret = DIV_ROUND_UP(ret, 64) + 2;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002420
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002421 return ret;
2422}
2423
Ville Syrjälä23297042013-07-05 11:57:17 +03002424static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Ville Syrjäläac484962016-01-20 21:05:26 +02002425 uint8_t cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002426{
Matt Roper15126882015-12-03 11:37:40 -08002427 /*
2428 * Neither of these should be possible since this function shouldn't be
2429 * called if the CRTC is off or the plane is invisible. But let's be
2430 * extra paranoid to avoid a potential divide-by-zero if we screw up
2431 * elsewhere in the driver.
2432 */
Ville Syrjäläac484962016-01-20 21:05:26 +02002433 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08002434 return 0;
2435 if (WARN_ON(!horiz_pixels))
2436 return 0;
2437
Ville Syrjäläac484962016-01-20 21:05:26 +02002438 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002439}
2440
Imre Deak820c1982013-12-17 14:46:36 +02002441struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002442 uint16_t pri;
2443 uint16_t spr;
2444 uint16_t cur;
2445 uint16_t fbc;
2446};
2447
Ville Syrjälä37126462013-08-01 16:18:55 +03002448/*
2449 * For both WM_PIPE and WM_LP.
2450 * mem_value must be in 0.1us units.
2451 */
Matt Roper7221fc32015-09-24 15:53:08 -07002452static uint32_t ilk_compute_pri_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002453 const struct intel_plane_state *pstate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03002454 uint32_t mem_value,
2455 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002456{
Paulo Zanonicca32e92013-05-31 11:45:06 -03002457 uint32_t method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002458 int cpp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002459
Ville Syrjälä24304d812017-03-14 17:10:49 +02002460 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002461 return 0;
2462
Ville Syrjälä353c8592016-12-14 23:30:57 +02002463 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002464
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002465 method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002466
2467 if (!is_lp)
2468 return method1;
2469
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002470 method2 = ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002471 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002472 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002473 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002474
2475 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002476}
2477
Ville Syrjälä37126462013-08-01 16:18:55 +03002478/*
2479 * For both WM_PIPE and WM_LP.
2480 * mem_value must be in 0.1us units.
2481 */
Matt Roper7221fc32015-09-24 15:53:08 -07002482static uint32_t ilk_compute_spr_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002483 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002484 uint32_t mem_value)
2485{
2486 uint32_t method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002487 int cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002488
Ville Syrjälä24304d812017-03-14 17:10:49 +02002489 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002490 return 0;
2491
Ville Syrjälä353c8592016-12-14 23:30:57 +02002492 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002493
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002494 method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
2495 method2 = ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002496 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002497 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002498 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002499 return min(method1, method2);
2500}
2501
Ville Syrjälä37126462013-08-01 16:18:55 +03002502/*
2503 * For both WM_PIPE and WM_LP.
2504 * mem_value must be in 0.1us units.
2505 */
Matt Roper7221fc32015-09-24 15:53:08 -07002506static uint32_t ilk_compute_cur_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002507 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002508 uint32_t mem_value)
2509{
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002510 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002511
Ville Syrjälä24304d812017-03-14 17:10:49 +02002512 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002513 return 0;
2514
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002515 cpp = pstate->base.fb->format->cpp[0];
2516
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002517 return ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002518 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002519 pstate->base.crtc_w, cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002520}
2521
Paulo Zanonicca32e92013-05-31 11:45:06 -03002522/* Only for WM_LP. */
Matt Roper7221fc32015-09-24 15:53:08 -07002523static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002524 const struct intel_plane_state *pstate,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03002525 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002526{
Ville Syrjälä83054942016-11-18 21:53:00 +02002527 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002528
Ville Syrjälä24304d812017-03-14 17:10:49 +02002529 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanonicca32e92013-05-31 11:45:06 -03002530 return 0;
2531
Ville Syrjälä353c8592016-12-14 23:30:57 +02002532 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002533
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002534 return ilk_wm_fbc(pri_val, drm_rect_width(&pstate->base.dst), cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002535}
2536
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002537static unsigned int
2538ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002539{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002540 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002541 return 3072;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002542 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002543 return 768;
2544 else
2545 return 512;
2546}
2547
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002548static unsigned int
2549ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2550 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002551{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002552 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002553 /* BDW primary/sprite plane watermarks */
2554 return level == 0 ? 255 : 2047;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002555 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002556 /* IVB/HSW primary/sprite plane watermarks */
2557 return level == 0 ? 127 : 1023;
2558 else if (!is_sprite)
2559 /* ILK/SNB primary plane watermarks */
2560 return level == 0 ? 127 : 511;
2561 else
2562 /* ILK/SNB sprite plane watermarks */
2563 return level == 0 ? 63 : 255;
2564}
2565
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002566static unsigned int
2567ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002568{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002569 if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002570 return level == 0 ? 63 : 255;
2571 else
2572 return level == 0 ? 31 : 63;
2573}
2574
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002575static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002576{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002577 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002578 return 31;
2579 else
2580 return 15;
2581}
2582
Ville Syrjälä158ae642013-08-07 13:28:19 +03002583/* Calculate the maximum primary/sprite plane watermark */
2584static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
2585 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002586 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002587 enum intel_ddb_partitioning ddb_partitioning,
2588 bool is_sprite)
2589{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002590 struct drm_i915_private *dev_priv = to_i915(dev);
2591 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002592
2593 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002594 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002595 return 0;
2596
2597 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002598 if (level == 0 || config->num_pipes_active > 1) {
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002599 fifo_size /= INTEL_INFO(dev_priv)->num_pipes;
Ville Syrjälä158ae642013-08-07 13:28:19 +03002600
2601 /*
2602 * For some reason the non self refresh
2603 * FIFO size is only half of the self
2604 * refresh FIFO size on ILK/SNB.
2605 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002606 if (INTEL_GEN(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002607 fifo_size /= 2;
2608 }
2609
Ville Syrjälä240264f2013-08-07 13:29:12 +03002610 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002611 /* level 0 is always calculated with 1:1 split */
2612 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2613 if (is_sprite)
2614 fifo_size *= 5;
2615 fifo_size /= 6;
2616 } else {
2617 fifo_size /= 2;
2618 }
2619 }
2620
2621 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002622 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002623}
2624
2625/* Calculate the maximum cursor plane watermark */
2626static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002627 int level,
2628 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002629{
2630 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002631 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002632 return 64;
2633
2634 /* otherwise just report max that registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002635 return ilk_cursor_wm_reg_max(to_i915(dev), level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002636}
2637
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002638static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002639 int level,
2640 const struct intel_wm_config *config,
2641 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002642 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002643{
Ville Syrjälä240264f2013-08-07 13:29:12 +03002644 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
2645 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
2646 max->cur = ilk_cursor_wm_max(dev, level, config);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002647 max->fbc = ilk_fbc_wm_reg_max(to_i915(dev));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002648}
2649
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002650static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002651 int level,
2652 struct ilk_wm_maximums *max)
2653{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002654 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2655 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2656 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2657 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002658}
2659
Ville Syrjäläd9395652013-10-09 19:18:10 +03002660static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002661 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002662 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002663{
2664 bool ret;
2665
2666 /* already determined to be invalid? */
2667 if (!result->enable)
2668 return false;
2669
2670 result->enable = result->pri_val <= max->pri &&
2671 result->spr_val <= max->spr &&
2672 result->cur_val <= max->cur;
2673
2674 ret = result->enable;
2675
2676 /*
2677 * HACK until we can pre-compute everything,
2678 * and thus fail gracefully if LP0 watermarks
2679 * are exceeded...
2680 */
2681 if (level == 0 && !result->enable) {
2682 if (result->pri_val > max->pri)
2683 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2684 level, result->pri_val, max->pri);
2685 if (result->spr_val > max->spr)
2686 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2687 level, result->spr_val, max->spr);
2688 if (result->cur_val > max->cur)
2689 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2690 level, result->cur_val, max->cur);
2691
2692 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
2693 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
2694 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
2695 result->enable = true;
2696 }
2697
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002698 return ret;
2699}
2700
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002701static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Matt Roper43d59ed2015-09-24 15:53:07 -07002702 const struct intel_crtc *intel_crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002703 int level,
Matt Roper7221fc32015-09-24 15:53:08 -07002704 struct intel_crtc_state *cstate,
Matt Roper86c8bbb2015-09-24 15:53:16 -07002705 struct intel_plane_state *pristate,
2706 struct intel_plane_state *sprstate,
2707 struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002708 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002709{
2710 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2711 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2712 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2713
2714 /* WM1+ latency values stored in 0.5us units */
2715 if (level > 0) {
2716 pri_latency *= 5;
2717 spr_latency *= 5;
2718 cur_latency *= 5;
2719 }
2720
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002721 if (pristate) {
2722 result->pri_val = ilk_compute_pri_wm(cstate, pristate,
2723 pri_latency, level);
2724 result->fbc_val = ilk_compute_fbc_wm(cstate, pristate, result->pri_val);
2725 }
2726
2727 if (sprstate)
2728 result->spr_val = ilk_compute_spr_wm(cstate, sprstate, spr_latency);
2729
2730 if (curstate)
2731 result->cur_val = ilk_compute_cur_wm(cstate, curstate, cur_latency);
2732
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002733 result->enable = true;
2734}
2735
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002736static uint32_t
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002737hsw_compute_linetime_wm(const struct intel_crtc_state *cstate)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002738{
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002739 const struct intel_atomic_state *intel_state =
2740 to_intel_atomic_state(cstate->base.state);
Matt Roperee91a152015-12-03 11:37:39 -08002741 const struct drm_display_mode *adjusted_mode =
2742 &cstate->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002743 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002744
Matt Roperee91a152015-12-03 11:37:39 -08002745 if (!cstate->base.active)
2746 return 0;
2747 if (WARN_ON(adjusted_mode->crtc_clock == 0))
2748 return 0;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002749 if (WARN_ON(intel_state->cdclk.logical.cdclk == 0))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002750 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002751
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002752 /* The WM are computed with base on how long it takes to fill a single
2753 * row at the given clock rate, multiplied by 8.
2754 * */
Ville Syrjälä124abe02015-09-08 13:40:45 +03002755 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2756 adjusted_mode->crtc_clock);
2757 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002758 intel_state->cdclk.logical.cdclk);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002759
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002760 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2761 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002762}
2763
Ville Syrjäläbb726512016-10-31 22:37:24 +02002764static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
2765 uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002766{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002767 if (IS_GEN9(dev_priv)) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002768 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002769 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002770 int level, max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002771
2772 /* read the first set of memory latencies[0:3] */
2773 val = 0; /* data0 to be programmed to 0 for first set */
2774 mutex_lock(&dev_priv->rps.hw_lock);
2775 ret = sandybridge_pcode_read(dev_priv,
2776 GEN9_PCODE_READ_MEM_LATENCY,
2777 &val);
2778 mutex_unlock(&dev_priv->rps.hw_lock);
2779
2780 if (ret) {
2781 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2782 return;
2783 }
2784
2785 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2786 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2787 GEN9_MEM_LATENCY_LEVEL_MASK;
2788 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2789 GEN9_MEM_LATENCY_LEVEL_MASK;
2790 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2791 GEN9_MEM_LATENCY_LEVEL_MASK;
2792
2793 /* read the second set of memory latencies[4:7] */
2794 val = 1; /* data0 to be programmed to 1 for second set */
2795 mutex_lock(&dev_priv->rps.hw_lock);
2796 ret = sandybridge_pcode_read(dev_priv,
2797 GEN9_PCODE_READ_MEM_LATENCY,
2798 &val);
2799 mutex_unlock(&dev_priv->rps.hw_lock);
2800 if (ret) {
2801 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2802 return;
2803 }
2804
2805 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2806 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2807 GEN9_MEM_LATENCY_LEVEL_MASK;
2808 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2809 GEN9_MEM_LATENCY_LEVEL_MASK;
2810 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2811 GEN9_MEM_LATENCY_LEVEL_MASK;
2812
Vandana Kannan367294b2014-11-04 17:06:46 +00002813 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002814 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2815 * need to be disabled. We make sure to sanitize the values out
2816 * of the punit to satisfy this requirement.
2817 */
2818 for (level = 1; level <= max_level; level++) {
2819 if (wm[level] == 0) {
2820 for (i = level + 1; i <= max_level; i++)
2821 wm[i] = 0;
2822 break;
2823 }
2824 }
2825
2826 /*
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02002827 * WaWmMemoryReadLatency:skl,glk
Damien Lespiau6f972352015-02-09 19:33:07 +00002828 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002829 * punit doesn't take into account the read latency so we need
Paulo Zanoni0727e402016-09-22 18:00:30 -03002830 * to add 2us to the various latency levels we retrieve from the
2831 * punit when level 0 response data us 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002832 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002833 if (wm[0] == 0) {
2834 wm[0] += 2;
2835 for (level = 1; level <= max_level; level++) {
2836 if (wm[level] == 0)
2837 break;
Vandana Kannan367294b2014-11-04 17:06:46 +00002838 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002839 }
Paulo Zanoni0727e402016-09-22 18:00:30 -03002840 }
2841
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002842 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002843 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2844
2845 wm[0] = (sskpd >> 56) & 0xFF;
2846 if (wm[0] == 0)
2847 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002848 wm[1] = (sskpd >> 4) & 0xFF;
2849 wm[2] = (sskpd >> 12) & 0xFF;
2850 wm[3] = (sskpd >> 20) & 0x1FF;
2851 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002852 } else if (INTEL_GEN(dev_priv) >= 6) {
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002853 uint32_t sskpd = I915_READ(MCH_SSKPD);
2854
2855 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2856 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2857 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2858 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002859 } else if (INTEL_GEN(dev_priv) >= 5) {
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002860 uint32_t mltr = I915_READ(MLTR_ILK);
2861
2862 /* ILK primary LP0 latency is 700 ns */
2863 wm[0] = 7;
2864 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2865 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002866 }
2867}
2868
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002869static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
2870 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002871{
2872 /* ILK sprite LP0 latency is 1300 ns */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002873 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002874 wm[0] = 13;
2875}
2876
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002877static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
2878 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002879{
2880 /* ILK cursor LP0 latency is 1300 ns */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002881 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002882 wm[0] = 13;
2883
2884 /* WaDoubleCursorLP3Latency:ivb */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002885 if (IS_IVYBRIDGE(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002886 wm[3] *= 2;
2887}
2888
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002889int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002890{
2891 /* how many WM levels are we expecting */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002892 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002893 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002894 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002895 return 4;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002896 else if (INTEL_GEN(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002897 return 3;
2898 else
2899 return 2;
2900}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002901
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002902static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002903 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002904 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002905{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002906 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002907
2908 for (level = 0; level <= max_level; level++) {
2909 unsigned int latency = wm[level];
2910
2911 if (latency == 0) {
2912 DRM_ERROR("%s WM%d latency not provided\n",
2913 name, level);
2914 continue;
2915 }
2916
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002917 /*
2918 * - latencies are in us on gen9.
2919 * - before then, WM1+ latency values are in 0.5us units
2920 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002921 if (IS_GEN9(dev_priv))
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002922 latency *= 10;
2923 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002924 latency *= 5;
2925
2926 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2927 name, level, wm[level],
2928 latency / 10, latency % 10);
2929 }
2930}
2931
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002932static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2933 uint16_t wm[5], uint16_t min)
2934{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002935 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002936
2937 if (wm[0] >= min)
2938 return false;
2939
2940 wm[0] = max(wm[0], min);
2941 for (level = 1; level <= max_level; level++)
2942 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2943
2944 return true;
2945}
2946
Ville Syrjäläbb726512016-10-31 22:37:24 +02002947static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002948{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002949 bool changed;
2950
2951 /*
2952 * The BIOS provided WM memory latency values are often
2953 * inadequate for high resolution displays. Adjust them.
2954 */
2955 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2956 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2957 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2958
2959 if (!changed)
2960 return;
2961
2962 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002963 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
2964 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
2965 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002966}
2967
Ville Syrjäläbb726512016-10-31 22:37:24 +02002968static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002969{
Ville Syrjäläbb726512016-10-31 22:37:24 +02002970 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002971
2972 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2973 sizeof(dev_priv->wm.pri_latency));
2974 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2975 sizeof(dev_priv->wm.pri_latency));
2976
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002977 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002978 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002979
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002980 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
2981 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
2982 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002983
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002984 if (IS_GEN6(dev_priv))
Ville Syrjäläbb726512016-10-31 22:37:24 +02002985 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002986}
2987
Ville Syrjäläbb726512016-10-31 22:37:24 +02002988static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002989{
Ville Syrjäläbb726512016-10-31 22:37:24 +02002990 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002991 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002992}
2993
Matt Ropered4a6a72016-02-23 17:20:13 -08002994static bool ilk_validate_pipe_wm(struct drm_device *dev,
2995 struct intel_pipe_wm *pipe_wm)
2996{
2997 /* LP0 watermark maximums depend on this pipe alone */
2998 const struct intel_wm_config config = {
2999 .num_pipes_active = 1,
3000 .sprites_enabled = pipe_wm->sprites_enabled,
3001 .sprites_scaled = pipe_wm->sprites_scaled,
3002 };
3003 struct ilk_wm_maximums max;
3004
3005 /* LP0 watermarks always use 1/2 DDB partitioning */
3006 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
3007
3008 /* At least LP0 must be valid */
3009 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
3010 DRM_DEBUG_KMS("LP0 watermark invalid\n");
3011 return false;
3012 }
3013
3014 return true;
3015}
3016
Matt Roper261a27d2015-10-08 15:28:25 -07003017/* Compute new watermarks for the pipe */
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003018static int ilk_compute_pipe_wm(struct intel_crtc_state *cstate)
Matt Roper261a27d2015-10-08 15:28:25 -07003019{
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003020 struct drm_atomic_state *state = cstate->base.state;
3021 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07003022 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003023 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003024 const struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper43d59ed2015-09-24 15:53:07 -07003025 struct intel_plane *intel_plane;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003026 struct intel_plane_state *pristate = NULL;
Matt Roper43d59ed2015-09-24 15:53:07 -07003027 struct intel_plane_state *sprstate = NULL;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003028 struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003029 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02003030 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003031
Matt Ropere8f1f022016-05-12 07:05:55 -07003032 pipe_wm = &cstate->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003033
Matt Roper43d59ed2015-09-24 15:53:07 -07003034 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003035 struct intel_plane_state *ps;
3036
3037 ps = intel_atomic_get_existing_plane_state(state,
3038 intel_plane);
3039 if (!ps)
3040 continue;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003041
3042 if (intel_plane->base.type == DRM_PLANE_TYPE_PRIMARY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003043 pristate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003044 else if (intel_plane->base.type == DRM_PLANE_TYPE_OVERLAY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003045 sprstate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003046 else if (intel_plane->base.type == DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003047 curstate = ps;
Matt Roper43d59ed2015-09-24 15:53:07 -07003048 }
3049
Matt Ropered4a6a72016-02-23 17:20:13 -08003050 pipe_wm->pipe_enabled = cstate->base.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003051 if (sprstate) {
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003052 pipe_wm->sprites_enabled = sprstate->base.visible;
3053 pipe_wm->sprites_scaled = sprstate->base.visible &&
3054 (drm_rect_width(&sprstate->base.dst) != drm_rect_width(&sprstate->base.src) >> 16 ||
3055 drm_rect_height(&sprstate->base.dst) != drm_rect_height(&sprstate->base.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003056 }
3057
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003058 usable_level = max_level;
3059
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003060 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003061 if (INTEL_GEN(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003062 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003063
3064 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08003065 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003066 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003067
Matt Roper86c8bbb2015-09-24 15:53:16 -07003068 ilk_compute_wm_level(dev_priv, intel_crtc, 0, cstate,
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003069 pristate, sprstate, curstate, &pipe_wm->raw_wm[0]);
3070
3071 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
3072 pipe_wm->wm[0] = pipe_wm->raw_wm[0];
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003073
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003074 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjälä532f7a72016-04-29 17:31:17 +03003075 pipe_wm->linetime = hsw_compute_linetime_wm(cstate);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003076
Matt Ropered4a6a72016-02-23 17:20:13 -08003077 if (!ilk_validate_pipe_wm(dev, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01003078 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003079
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003080 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003081
3082 for (level = 1; level <= max_level; level++) {
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003083 struct intel_wm_level *wm = &pipe_wm->raw_wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003084
Matt Roper86c8bbb2015-09-24 15:53:16 -07003085 ilk_compute_wm_level(dev_priv, intel_crtc, level, cstate,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003086 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003087
3088 /*
3089 * Disable any watermark level that exceeds the
3090 * register maximums since such watermarks are
3091 * always invalid.
3092 */
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003093 if (level > usable_level)
3094 continue;
3095
3096 if (ilk_validate_wm_level(level, &max, wm))
3097 pipe_wm->wm[level] = *wm;
3098 else
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003099 usable_level = level;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003100 }
3101
Matt Roper86c8bbb2015-09-24 15:53:16 -07003102 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003103}
3104
3105/*
Matt Ropered4a6a72016-02-23 17:20:13 -08003106 * Build a set of 'intermediate' watermark values that satisfy both the old
3107 * state and the new state. These can be programmed to the hardware
3108 * immediately.
3109 */
3110static int ilk_compute_intermediate_wm(struct drm_device *dev,
3111 struct intel_crtc *intel_crtc,
3112 struct intel_crtc_state *newstate)
3113{
Matt Ropere8f1f022016-05-12 07:05:55 -07003114 struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08003115 struct intel_pipe_wm *b = &intel_crtc->wm.active.ilk;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003116 int level, max_level = ilk_wm_max_level(to_i915(dev));
Matt Ropered4a6a72016-02-23 17:20:13 -08003117
3118 /*
3119 * Start with the final, target watermarks, then combine with the
3120 * currently active watermarks to get values that are safe both before
3121 * and after the vblank.
3122 */
Matt Ropere8f1f022016-05-12 07:05:55 -07003123 *a = newstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08003124 a->pipe_enabled |= b->pipe_enabled;
3125 a->sprites_enabled |= b->sprites_enabled;
3126 a->sprites_scaled |= b->sprites_scaled;
3127
3128 for (level = 0; level <= max_level; level++) {
3129 struct intel_wm_level *a_wm = &a->wm[level];
3130 const struct intel_wm_level *b_wm = &b->wm[level];
3131
3132 a_wm->enable &= b_wm->enable;
3133 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
3134 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
3135 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
3136 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
3137 }
3138
3139 /*
3140 * We need to make sure that these merged watermark values are
3141 * actually a valid configuration themselves. If they're not,
3142 * there's no safe way to transition from the old state to
3143 * the new state, so we need to fail the atomic transaction.
3144 */
3145 if (!ilk_validate_pipe_wm(dev, a))
3146 return -EINVAL;
3147
3148 /*
3149 * If our intermediate WM are identical to the final WM, then we can
3150 * omit the post-vblank programming; only update if it's different.
3151 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02003152 if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) != 0)
3153 newstate->wm.need_postvbl_update = true;
Matt Ropered4a6a72016-02-23 17:20:13 -08003154
3155 return 0;
3156}
3157
3158/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003159 * Merge the watermarks from all active pipes for a specific level.
3160 */
3161static void ilk_merge_wm_level(struct drm_device *dev,
3162 int level,
3163 struct intel_wm_level *ret_wm)
3164{
3165 const struct intel_crtc *intel_crtc;
3166
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003167 ret_wm->enable = true;
3168
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003169 for_each_intel_crtc(dev, intel_crtc) {
Matt Ropered4a6a72016-02-23 17:20:13 -08003170 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02003171 const struct intel_wm_level *wm = &active->wm[level];
3172
3173 if (!active->pipe_enabled)
3174 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003175
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003176 /*
3177 * The watermark values may have been used in the past,
3178 * so we must maintain them in the registers for some
3179 * time even if the level is now disabled.
3180 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003181 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003182 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003183
3184 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
3185 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
3186 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
3187 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
3188 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003189}
3190
3191/*
3192 * Merge all low power watermarks for all active pipes.
3193 */
3194static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003195 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02003196 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003197 struct intel_pipe_wm *merged)
3198{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003199 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003200 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003201 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003202
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003203 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003204 if ((INTEL_GEN(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003205 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03003206 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003207
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003208 /* ILK: FBC WM must be disabled always */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003209 merged->fbc_wm_enabled = INTEL_GEN(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003210
3211 /* merge each WM1+ level */
3212 for (level = 1; level <= max_level; level++) {
3213 struct intel_wm_level *wm = &merged->wm[level];
3214
3215 ilk_merge_wm_level(dev, level, wm);
3216
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003217 if (level > last_enabled_level)
3218 wm->enable = false;
3219 else if (!ilk_validate_wm_level(level, max, wm))
3220 /* make sure all following levels get disabled */
3221 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003222
3223 /*
3224 * The spec says it is preferred to disable
3225 * FBC WMs instead of disabling a WM level.
3226 */
3227 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003228 if (wm->enable)
3229 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003230 wm->fbc_val = 0;
3231 }
3232 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003233
3234 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
3235 /*
3236 * FIXME this is racy. FBC might get enabled later.
3237 * What we should check here is whether FBC can be
3238 * enabled sometime later.
3239 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003240 if (IS_GEN5(dev_priv) && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03003241 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003242 for (level = 2; level <= max_level; level++) {
3243 struct intel_wm_level *wm = &merged->wm[level];
3244
3245 wm->enable = false;
3246 }
3247 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003248}
3249
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003250static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
3251{
3252 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
3253 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
3254}
3255
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003256/* The value we need to program into the WM_LPx latency field */
3257static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
3258{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003259 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003260
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003261 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003262 return 2 * level;
3263 else
3264 return dev_priv->wm.pri_latency[level];
3265}
3266
Imre Deak820c1982013-12-17 14:46:36 +02003267static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03003268 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03003269 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02003270 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003271{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003272 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003273 struct intel_crtc *intel_crtc;
3274 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003275
Ville Syrjälä0362c782013-10-09 19:17:57 +03003276 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03003277 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003278
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003279 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03003280 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03003281 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003282
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003283 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003284
Ville Syrjälä0362c782013-10-09 19:17:57 +03003285 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03003286
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003287 /*
3288 * Maintain the watermark values even if the level is
3289 * disabled. Doing otherwise could cause underruns.
3290 */
3291 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003292 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07003293 (r->pri_val << WM1_LP_SR_SHIFT) |
3294 r->cur_val;
3295
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003296 if (r->enable)
3297 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
3298
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003299 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07003300 results->wm_lp[wm_lp - 1] |=
3301 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
3302 else
3303 results->wm_lp[wm_lp - 1] |=
3304 r->fbc_val << WM1_LP_FBC_SHIFT;
3305
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003306 /*
3307 * Always set WM1S_LP_EN when spr_val != 0, even if the
3308 * level is disabled. Doing otherwise could cause underruns.
3309 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003310 if (INTEL_GEN(dev_priv) <= 6 && r->spr_val) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003311 WARN_ON(wm_lp != 1);
3312 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
3313 } else
3314 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003315 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003316
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003317 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003318 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003319 enum pipe pipe = intel_crtc->pipe;
Matt Ropered4a6a72016-02-23 17:20:13 -08003320 const struct intel_wm_level *r =
3321 &intel_crtc->wm.active.ilk.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003322
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003323 if (WARN_ON(!r->enable))
3324 continue;
3325
Matt Ropered4a6a72016-02-23 17:20:13 -08003326 results->wm_linetime[pipe] = intel_crtc->wm.active.ilk.linetime;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003327
3328 results->wm_pipe[pipe] =
3329 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
3330 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
3331 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003332 }
3333}
3334
Paulo Zanoni861f3382013-05-31 10:19:21 -03003335/* Find the result with the highest level enabled. Check for enable_fbc_wm in
3336 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02003337static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003338 struct intel_pipe_wm *r1,
3339 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003340{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003341 int level, max_level = ilk_wm_max_level(to_i915(dev));
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003342 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003343
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003344 for (level = 1; level <= max_level; level++) {
3345 if (r1->wm[level].enable)
3346 level1 = level;
3347 if (r2->wm[level].enable)
3348 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003349 }
3350
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003351 if (level1 == level2) {
3352 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003353 return r2;
3354 else
3355 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003356 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03003357 return r1;
3358 } else {
3359 return r2;
3360 }
3361}
3362
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003363/* dirty bits used to track which watermarks need changes */
3364#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
3365#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
3366#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
3367#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
3368#define WM_DIRTY_FBC (1 << 24)
3369#define WM_DIRTY_DDB (1 << 25)
3370
Damien Lespiau055e3932014-08-18 13:49:10 +01003371static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02003372 const struct ilk_wm_values *old,
3373 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003374{
3375 unsigned int dirty = 0;
3376 enum pipe pipe;
3377 int wm_lp;
3378
Damien Lespiau055e3932014-08-18 13:49:10 +01003379 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003380 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
3381 dirty |= WM_DIRTY_LINETIME(pipe);
3382 /* Must disable LP1+ watermarks too */
3383 dirty |= WM_DIRTY_LP_ALL;
3384 }
3385
3386 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
3387 dirty |= WM_DIRTY_PIPE(pipe);
3388 /* Must disable LP1+ watermarks too */
3389 dirty |= WM_DIRTY_LP_ALL;
3390 }
3391 }
3392
3393 if (old->enable_fbc_wm != new->enable_fbc_wm) {
3394 dirty |= WM_DIRTY_FBC;
3395 /* Must disable LP1+ watermarks too */
3396 dirty |= WM_DIRTY_LP_ALL;
3397 }
3398
3399 if (old->partitioning != new->partitioning) {
3400 dirty |= WM_DIRTY_DDB;
3401 /* Must disable LP1+ watermarks too */
3402 dirty |= WM_DIRTY_LP_ALL;
3403 }
3404
3405 /* LP1+ watermarks already deemed dirty, no need to continue */
3406 if (dirty & WM_DIRTY_LP_ALL)
3407 return dirty;
3408
3409 /* Find the lowest numbered LP1+ watermark in need of an update... */
3410 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3411 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
3412 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
3413 break;
3414 }
3415
3416 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
3417 for (; wm_lp <= 3; wm_lp++)
3418 dirty |= WM_DIRTY_LP(wm_lp);
3419
3420 return dirty;
3421}
3422
Ville Syrjälä8553c182013-12-05 15:51:39 +02003423static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
3424 unsigned int dirty)
3425{
Imre Deak820c1982013-12-17 14:46:36 +02003426 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02003427 bool changed = false;
3428
3429 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
3430 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
3431 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
3432 changed = true;
3433 }
3434 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
3435 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
3436 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
3437 changed = true;
3438 }
3439 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
3440 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
3441 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
3442 changed = true;
3443 }
3444
3445 /*
3446 * Don't touch WM1S_LP_EN here.
3447 * Doing so could cause underruns.
3448 */
3449
3450 return changed;
3451}
3452
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003453/*
3454 * The spec says we shouldn't write when we don't need, because every write
3455 * causes WMs to be re-evaluated, expending some power.
3456 */
Imre Deak820c1982013-12-17 14:46:36 +02003457static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
3458 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003459{
Imre Deak820c1982013-12-17 14:46:36 +02003460 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003461 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003462 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003463
Damien Lespiau055e3932014-08-18 13:49:10 +01003464 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003465 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003466 return;
3467
Ville Syrjälä8553c182013-12-05 15:51:39 +02003468 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003469
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003470 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003471 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003472 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003473 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003474 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003475 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
3476
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003477 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003478 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003479 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003480 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003481 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003482 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
3483
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003484 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003485 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003486 val = I915_READ(WM_MISC);
3487 if (results->partitioning == INTEL_DDB_PART_1_2)
3488 val &= ~WM_MISC_DATA_PARTITION_5_6;
3489 else
3490 val |= WM_MISC_DATA_PARTITION_5_6;
3491 I915_WRITE(WM_MISC, val);
3492 } else {
3493 val = I915_READ(DISP_ARB_CTL2);
3494 if (results->partitioning == INTEL_DDB_PART_1_2)
3495 val &= ~DISP_DATA_PARTITION_5_6;
3496 else
3497 val |= DISP_DATA_PARTITION_5_6;
3498 I915_WRITE(DISP_ARB_CTL2, val);
3499 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003500 }
3501
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003502 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03003503 val = I915_READ(DISP_ARB_CTL);
3504 if (results->enable_fbc_wm)
3505 val &= ~DISP_FBC_WM_DIS;
3506 else
3507 val |= DISP_FBC_WM_DIS;
3508 I915_WRITE(DISP_ARB_CTL, val);
3509 }
3510
Imre Deak954911e2013-12-17 14:46:34 +02003511 if (dirty & WM_DIRTY_LP(1) &&
3512 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
3513 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
3514
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003515 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003516 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
3517 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
3518 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
3519 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
3520 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003521
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003522 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003523 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003524 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003525 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003526 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003527 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003528
3529 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003530}
3531
Matt Ropered4a6a72016-02-23 17:20:13 -08003532bool ilk_disable_lp_wm(struct drm_device *dev)
Ville Syrjälä8553c182013-12-05 15:51:39 +02003533{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003534 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003535
3536 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3537}
3538
Lyude656d1b82016-08-17 15:55:54 -04003539#define SKL_SAGV_BLOCK_TIME 30 /* µs */
Damien Lespiaub9cec072014-11-04 17:06:43 +00003540
Matt Roper024c9042015-09-24 15:53:11 -07003541/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003542 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3543 * so assume we'll always need it in order to avoid underruns.
3544 */
3545static bool skl_needs_memory_bw_wa(struct intel_atomic_state *state)
3546{
3547 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3548
Rodrigo Vivib976dc52017-01-23 10:32:37 -08003549 if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv))
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003550 return true;
3551
3552 return false;
3553}
3554
Paulo Zanoni56feca92016-09-22 18:00:28 -03003555static bool
3556intel_has_sagv(struct drm_i915_private *dev_priv)
3557{
Rodrigo Vivi82525c12017-06-08 08:50:00 -07003558 if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv))
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003559 return true;
3560
3561 if (IS_SKYLAKE(dev_priv) &&
3562 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED)
3563 return true;
3564
3565 return false;
Paulo Zanoni56feca92016-09-22 18:00:28 -03003566}
3567
Lyude656d1b82016-08-17 15:55:54 -04003568/*
3569 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3570 * depending on power and performance requirements. The display engine access
3571 * to system memory is blocked during the adjustment time. Because of the
3572 * blocking time, having this enabled can cause full system hangs and/or pipe
3573 * underruns if we don't meet all of the following requirements:
3574 *
3575 * - <= 1 pipe enabled
3576 * - All planes can enable watermarks for latencies >= SAGV engine block time
3577 * - We're not using an interlaced display configuration
3578 */
3579int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003580intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003581{
3582 int ret;
3583
Paulo Zanoni56feca92016-09-22 18:00:28 -03003584 if (!intel_has_sagv(dev_priv))
3585 return 0;
3586
3587 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003588 return 0;
3589
3590 DRM_DEBUG_KMS("Enabling the SAGV\n");
3591 mutex_lock(&dev_priv->rps.hw_lock);
3592
3593 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3594 GEN9_SAGV_ENABLE);
3595
3596 /* We don't need to wait for the SAGV when enabling */
3597 mutex_unlock(&dev_priv->rps.hw_lock);
3598
3599 /*
3600 * Some skl systems, pre-release machines in particular,
3601 * don't actually have an SAGV.
3602 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003603 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003604 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003605 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003606 return 0;
3607 } else if (ret < 0) {
3608 DRM_ERROR("Failed to enable the SAGV\n");
3609 return ret;
3610 }
3611
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003612 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003613 return 0;
3614}
3615
Lyude656d1b82016-08-17 15:55:54 -04003616int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003617intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003618{
Imre Deakb3b8e992016-12-05 18:27:38 +02003619 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003620
Paulo Zanoni56feca92016-09-22 18:00:28 -03003621 if (!intel_has_sagv(dev_priv))
3622 return 0;
3623
3624 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003625 return 0;
3626
3627 DRM_DEBUG_KMS("Disabling the SAGV\n");
3628 mutex_lock(&dev_priv->rps.hw_lock);
3629
3630 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003631 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3632 GEN9_SAGV_DISABLE,
3633 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3634 1);
Lyude656d1b82016-08-17 15:55:54 -04003635 mutex_unlock(&dev_priv->rps.hw_lock);
3636
Lyude656d1b82016-08-17 15:55:54 -04003637 /*
3638 * Some skl systems, pre-release machines in particular,
3639 * don't actually have an SAGV.
3640 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003641 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003642 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003643 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003644 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003645 } else if (ret < 0) {
3646 DRM_ERROR("Failed to disable the SAGV (%d)\n", ret);
3647 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003648 }
3649
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003650 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003651 return 0;
3652}
3653
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003654bool intel_can_enable_sagv(struct drm_atomic_state *state)
Lyude656d1b82016-08-17 15:55:54 -04003655{
3656 struct drm_device *dev = state->dev;
3657 struct drm_i915_private *dev_priv = to_i915(dev);
3658 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003659 struct intel_crtc *crtc;
3660 struct intel_plane *plane;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003661 struct intel_crtc_state *cstate;
Lyude656d1b82016-08-17 15:55:54 -04003662 enum pipe pipe;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003663 int level, latency;
Lyude656d1b82016-08-17 15:55:54 -04003664
Paulo Zanoni56feca92016-09-22 18:00:28 -03003665 if (!intel_has_sagv(dev_priv))
3666 return false;
3667
Lyude656d1b82016-08-17 15:55:54 -04003668 /*
3669 * SKL workaround: bspec recommends we disable the SAGV when we have
3670 * more then one pipe enabled
3671 *
3672 * If there are no active CRTCs, no additional checks need be performed
3673 */
3674 if (hweight32(intel_state->active_crtcs) == 0)
3675 return true;
3676 else if (hweight32(intel_state->active_crtcs) > 1)
3677 return false;
3678
3679 /* Since we're now guaranteed to only have one active CRTC... */
3680 pipe = ffs(intel_state->active_crtcs) - 1;
Ville Syrjälä98187832016-10-31 22:37:10 +02003681 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003682 cstate = to_intel_crtc_state(crtc->base.state);
Lyude656d1b82016-08-17 15:55:54 -04003683
Paulo Zanonic89cadd2016-10-10 17:30:59 -03003684 if (crtc->base.state->adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003685 return false;
3686
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003687 for_each_intel_plane_on_crtc(dev, crtc, plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003688 struct skl_plane_wm *wm =
3689 &cstate->wm.skl.optimal.planes[plane->id];
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003690
Lyude656d1b82016-08-17 15:55:54 -04003691 /* Skip this plane if it's not enabled */
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003692 if (!wm->wm[0].plane_en)
Lyude656d1b82016-08-17 15:55:54 -04003693 continue;
3694
3695 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003696 for (level = ilk_wm_max_level(dev_priv);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003697 !wm->wm[level].plane_en; --level)
Lyude656d1b82016-08-17 15:55:54 -04003698 { }
3699
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003700 latency = dev_priv->wm.skl_latency[level];
3701
3702 if (skl_needs_memory_bw_wa(intel_state) &&
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003703 plane->base.state->fb->modifier ==
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003704 I915_FORMAT_MOD_X_TILED)
3705 latency += 15;
3706
Lyude656d1b82016-08-17 15:55:54 -04003707 /*
3708 * If any of the planes on this pipe don't enable wm levels
3709 * that incur memory latencies higher then 30µs we can't enable
3710 * the SAGV
3711 */
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003712 if (latency < SKL_SAGV_BLOCK_TIME)
Lyude656d1b82016-08-17 15:55:54 -04003713 return false;
3714 }
3715
3716 return true;
3717}
3718
Damien Lespiaub9cec072014-11-04 17:06:43 +00003719static void
3720skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
Matt Roper024c9042015-09-24 15:53:11 -07003721 const struct intel_crtc_state *cstate,
Matt Roperc107acf2016-05-12 07:06:01 -07003722 struct skl_ddb_entry *alloc, /* out */
3723 int *num_active /* out */)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003724{
Matt Roperc107acf2016-05-12 07:06:01 -07003725 struct drm_atomic_state *state = cstate->base.state;
3726 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
3727 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper024c9042015-09-24 15:53:11 -07003728 struct drm_crtc *for_crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003729 unsigned int pipe_size, ddb_size;
3730 int nth_active_pipe;
Matt Roperc107acf2016-05-12 07:06:01 -07003731
Matt Ropera6d3460e2016-05-12 07:06:04 -07003732 if (WARN_ON(!state) || !cstate->base.active) {
Damien Lespiaub9cec072014-11-04 17:06:43 +00003733 alloc->start = 0;
3734 alloc->end = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003735 *num_active = hweight32(dev_priv->active_crtcs);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003736 return;
3737 }
3738
Matt Ropera6d3460e2016-05-12 07:06:04 -07003739 if (intel_state->active_pipe_changes)
3740 *num_active = hweight32(intel_state->active_crtcs);
3741 else
3742 *num_active = hweight32(dev_priv->active_crtcs);
3743
Deepak M6f3fff62016-09-15 15:01:10 +05303744 ddb_size = INTEL_INFO(dev_priv)->ddb_size;
3745 WARN_ON(ddb_size == 0);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003746
3747 ddb_size -= 4; /* 4 blocks for bypass path allocation */
3748
Matt Roperc107acf2016-05-12 07:06:01 -07003749 /*
Matt Ropera6d3460e2016-05-12 07:06:04 -07003750 * If the state doesn't change the active CRTC's, then there's
3751 * no need to recalculate; the existing pipe allocation limits
3752 * should remain unchanged. Note that we're safe from racing
3753 * commits since any racing commit that changes the active CRTC
3754 * list would need to grab _all_ crtc locks, including the one
3755 * we currently hold.
Matt Roperc107acf2016-05-12 07:06:01 -07003756 */
Matt Ropera6d3460e2016-05-12 07:06:04 -07003757 if (!intel_state->active_pipe_changes) {
Maarten Lankhorst512b5522016-11-08 13:55:34 +01003758 /*
3759 * alloc may be cleared by clear_intel_crtc_state,
3760 * copy from old state to be sure
3761 */
3762 *alloc = to_intel_crtc_state(for_crtc->state)->wm.skl.ddb;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003763 return;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003764 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07003765
3766 nth_active_pipe = hweight32(intel_state->active_crtcs &
3767 (drm_crtc_mask(for_crtc) - 1));
3768 pipe_size = ddb_size / hweight32(intel_state->active_crtcs);
3769 alloc->start = nth_active_pipe * ddb_size / *num_active;
3770 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003771}
3772
Matt Roperc107acf2016-05-12 07:06:01 -07003773static unsigned int skl_cursor_allocation(int num_active)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003774{
Matt Roperc107acf2016-05-12 07:06:01 -07003775 if (num_active == 1)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003776 return 32;
3777
3778 return 8;
3779}
3780
Damien Lespiaua269c582014-11-04 17:06:49 +00003781static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
3782{
3783 entry->start = reg & 0x3ff;
3784 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00003785 if (entry->end)
3786 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00003787}
3788
Damien Lespiau08db6652014-11-04 17:06:52 +00003789void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
3790 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00003791{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003792 struct intel_crtc *crtc;
Damien Lespiaua269c582014-11-04 17:06:49 +00003793
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003794 memset(ddb, 0, sizeof(*ddb));
3795
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003796 for_each_intel_crtc(&dev_priv->drm, crtc) {
Imre Deak4d800032016-02-17 16:31:29 +02003797 enum intel_display_power_domain power_domain;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003798 enum plane_id plane_id;
3799 enum pipe pipe = crtc->pipe;
Imre Deak4d800032016-02-17 16:31:29 +02003800
3801 power_domain = POWER_DOMAIN_PIPE(pipe);
3802 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003803 continue;
3804
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003805 for_each_plane_id_on_crtc(crtc, plane_id) {
3806 u32 val;
Damien Lespiaua269c582014-11-04 17:06:49 +00003807
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003808 if (plane_id != PLANE_CURSOR)
3809 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
3810 else
3811 val = I915_READ(CUR_BUF_CFG(pipe));
3812
3813 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane_id], val);
3814 }
Imre Deak4d800032016-02-17 16:31:29 +02003815
3816 intel_display_power_put(dev_priv, power_domain);
Damien Lespiaua269c582014-11-04 17:06:49 +00003817 }
3818}
3819
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003820/*
3821 * Determines the downscale amount of a plane for the purposes of watermark calculations.
3822 * The bspec defines downscale amount as:
3823 *
3824 * """
3825 * Horizontal down scale amount = maximum[1, Horizontal source size /
3826 * Horizontal destination size]
3827 * Vertical down scale amount = maximum[1, Vertical source size /
3828 * Vertical destination size]
3829 * Total down scale amount = Horizontal down scale amount *
3830 * Vertical down scale amount
3831 * """
3832 *
3833 * Return value is provided in 16.16 fixed point form to retain fractional part.
3834 * Caller should take care of dividing & rounding off the value.
3835 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303836static uint_fixed_16_16_t
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003837skl_plane_downscale_amount(const struct intel_crtc_state *cstate,
3838 const struct intel_plane_state *pstate)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003839{
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003840 struct intel_plane *plane = to_intel_plane(pstate->base.plane);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003841 uint32_t src_w, src_h, dst_w, dst_h;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303842 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
3843 uint_fixed_16_16_t downscale_h, downscale_w;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003844
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003845 if (WARN_ON(!intel_wm_plane_visible(cstate, pstate)))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303846 return u32_to_fixed16(0);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003847
3848 /* n.b., src is 16.16 fixed point, dst is whole integer */
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003849 if (plane->id == PLANE_CURSOR) {
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03003850 /*
3851 * Cursors only support 0/180 degree rotation,
3852 * hence no need to account for rotation here.
3853 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303854 src_w = pstate->base.src_w >> 16;
3855 src_h = pstate->base.src_h >> 16;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003856 dst_w = pstate->base.crtc_w;
3857 dst_h = pstate->base.crtc_h;
3858 } else {
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03003859 /*
3860 * Src coordinates are already rotated by 270 degrees for
3861 * the 90/270 degree plane rotation cases (to match the
3862 * GTT mapping), hence no need to account for rotation here.
3863 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303864 src_w = drm_rect_width(&pstate->base.src) >> 16;
3865 src_h = drm_rect_height(&pstate->base.src) >> 16;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003866 dst_w = drm_rect_width(&pstate->base.dst);
3867 dst_h = drm_rect_height(&pstate->base.dst);
3868 }
3869
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303870 fp_w_ratio = div_fixed16(src_w, dst_w);
3871 fp_h_ratio = div_fixed16(src_h, dst_h);
3872 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
3873 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003874
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303875 return mul_fixed16(downscale_w, downscale_h);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003876}
3877
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303878static uint_fixed_16_16_t
3879skl_pipe_downscale_amount(const struct intel_crtc_state *crtc_state)
3880{
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303881 uint_fixed_16_16_t pipe_downscale = u32_to_fixed16(1);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303882
3883 if (!crtc_state->base.enable)
3884 return pipe_downscale;
3885
3886 if (crtc_state->pch_pfit.enabled) {
3887 uint32_t src_w, src_h, dst_w, dst_h;
3888 uint32_t pfit_size = crtc_state->pch_pfit.size;
3889 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
3890 uint_fixed_16_16_t downscale_h, downscale_w;
3891
3892 src_w = crtc_state->pipe_src_w;
3893 src_h = crtc_state->pipe_src_h;
3894 dst_w = pfit_size >> 16;
3895 dst_h = pfit_size & 0xffff;
3896
3897 if (!dst_w || !dst_h)
3898 return pipe_downscale;
3899
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303900 fp_w_ratio = div_fixed16(src_w, dst_w);
3901 fp_h_ratio = div_fixed16(src_h, dst_h);
3902 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
3903 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303904
3905 pipe_downscale = mul_fixed16(downscale_w, downscale_h);
3906 }
3907
3908 return pipe_downscale;
3909}
3910
3911int skl_check_pipe_max_pixel_rate(struct intel_crtc *intel_crtc,
3912 struct intel_crtc_state *cstate)
3913{
3914 struct drm_crtc_state *crtc_state = &cstate->base;
3915 struct drm_atomic_state *state = crtc_state->state;
3916 struct drm_plane *plane;
3917 const struct drm_plane_state *pstate;
3918 struct intel_plane_state *intel_pstate;
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02003919 int crtc_clock, dotclk;
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303920 uint32_t pipe_max_pixel_rate;
3921 uint_fixed_16_16_t pipe_downscale;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303922 uint_fixed_16_16_t max_downscale = u32_to_fixed16(1);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303923
3924 if (!cstate->base.enable)
3925 return 0;
3926
3927 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, crtc_state) {
3928 uint_fixed_16_16_t plane_downscale;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303929 uint_fixed_16_16_t fp_9_div_8 = div_fixed16(9, 8);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303930 int bpp;
3931
3932 if (!intel_wm_plane_visible(cstate,
3933 to_intel_plane_state(pstate)))
3934 continue;
3935
3936 if (WARN_ON(!pstate->fb))
3937 return -EINVAL;
3938
3939 intel_pstate = to_intel_plane_state(pstate);
3940 plane_downscale = skl_plane_downscale_amount(cstate,
3941 intel_pstate);
3942 bpp = pstate->fb->format->cpp[0] * 8;
3943 if (bpp == 64)
3944 plane_downscale = mul_fixed16(plane_downscale,
3945 fp_9_div_8);
3946
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303947 max_downscale = max_fixed16(plane_downscale, max_downscale);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303948 }
3949 pipe_downscale = skl_pipe_downscale_amount(cstate);
3950
3951 pipe_downscale = mul_fixed16(pipe_downscale, max_downscale);
3952
3953 crtc_clock = crtc_state->adjusted_mode.crtc_clock;
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02003954 dotclk = to_intel_atomic_state(state)->cdclk.logical.cdclk;
3955
3956 if (IS_GEMINILAKE(to_i915(intel_crtc->base.dev)))
3957 dotclk *= 2;
3958
3959 pipe_max_pixel_rate = div_round_up_u32_fixed16(dotclk, pipe_downscale);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303960
3961 if (pipe_max_pixel_rate < crtc_clock) {
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02003962 DRM_DEBUG_KMS("Max supported pixel clock with scaling exceeded\n");
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303963 return -EINVAL;
3964 }
3965
3966 return 0;
3967}
3968
Damien Lespiaub9cec072014-11-04 17:06:43 +00003969static unsigned int
Matt Roper024c9042015-09-24 15:53:11 -07003970skl_plane_relative_data_rate(const struct intel_crtc_state *cstate,
3971 const struct drm_plane_state *pstate,
3972 int y)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003973{
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003974 struct intel_plane *plane = to_intel_plane(pstate->plane);
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003975 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303976 uint32_t data_rate;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003977 uint32_t width = 0, height = 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02003978 struct drm_framebuffer *fb;
3979 u32 format;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303980 uint_fixed_16_16_t down_scale_amount;
Matt Ropera1de91e2016-05-12 07:05:57 -07003981
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003982 if (!intel_pstate->base.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07003983 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02003984
3985 fb = pstate->fb;
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003986 format = fb->format->format;
Ville Syrjälä83054942016-11-18 21:53:00 +02003987
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003988 if (plane->id == PLANE_CURSOR)
Matt Ropera1de91e2016-05-12 07:05:57 -07003989 return 0;
3990 if (y && format != DRM_FORMAT_NV12)
3991 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003992
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03003993 /*
3994 * Src coordinates are already rotated by 270 degrees for
3995 * the 90/270 degree plane rotation cases (to match the
3996 * GTT mapping), hence no need to account for rotation here.
3997 */
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003998 width = drm_rect_width(&intel_pstate->base.src) >> 16;
3999 height = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004000
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004001 /* for planar format */
Matt Ropera1de91e2016-05-12 07:05:57 -07004002 if (format == DRM_FORMAT_NV12) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004003 if (y) /* y-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004004 data_rate = width * height *
Ville Syrjälä353c8592016-12-14 23:30:57 +02004005 fb->format->cpp[0];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004006 else /* uv-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004007 data_rate = (width / 2) * (height / 2) *
Ville Syrjälä353c8592016-12-14 23:30:57 +02004008 fb->format->cpp[1];
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004009 } else {
4010 /* for packed formats */
Ville Syrjälä353c8592016-12-14 23:30:57 +02004011 data_rate = width * height * fb->format->cpp[0];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004012 }
4013
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004014 down_scale_amount = skl_plane_downscale_amount(cstate, intel_pstate);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004015
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304016 return mul_round_up_u32_fixed16(data_rate, down_scale_amount);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004017}
4018
4019/*
4020 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
4021 * a 8192x4096@32bpp framebuffer:
4022 * 3 * 4096 * 8192 * 4 < 2^32
4023 */
4024static unsigned int
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004025skl_get_total_relative_data_rate(struct intel_crtc_state *intel_cstate,
4026 unsigned *plane_data_rate,
4027 unsigned *plane_y_data_rate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004028{
Matt Roper9c74d822016-05-12 07:05:58 -07004029 struct drm_crtc_state *cstate = &intel_cstate->base;
4030 struct drm_atomic_state *state = cstate->state;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02004031 struct drm_plane *plane;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02004032 const struct drm_plane_state *pstate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004033 unsigned int total_data_rate = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07004034
4035 if (WARN_ON(!state))
4036 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004037
Matt Ropera1de91e2016-05-12 07:05:57 -07004038 /* Calculate and cache data rate for each plane */
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02004039 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, cstate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004040 enum plane_id plane_id = to_intel_plane(plane)->id;
4041 unsigned int rate;
Matt Roper024c9042015-09-24 15:53:11 -07004042
Matt Ropera6d3460e2016-05-12 07:06:04 -07004043 /* packed/uv */
4044 rate = skl_plane_relative_data_rate(intel_cstate,
4045 pstate, 0);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004046 plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004047
4048 total_data_rate += rate;
Matt Roper9c74d822016-05-12 07:05:58 -07004049
Matt Ropera6d3460e2016-05-12 07:06:04 -07004050 /* y-plane */
4051 rate = skl_plane_relative_data_rate(intel_cstate,
4052 pstate, 1);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004053 plane_y_data_rate[plane_id] = rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07004054
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004055 total_data_rate += rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004056 }
4057
4058 return total_data_rate;
4059}
4060
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004061static uint16_t
4062skl_ddb_min_alloc(const struct drm_plane_state *pstate,
4063 const int y)
4064{
4065 struct drm_framebuffer *fb = pstate->fb;
4066 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
4067 uint32_t src_w, src_h;
4068 uint32_t min_scanlines = 8;
4069 uint8_t plane_bpp;
4070
4071 if (WARN_ON(!fb))
4072 return 0;
4073
4074 /* For packed formats, no y-plane, return 0 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004075 if (y && fb->format->format != DRM_FORMAT_NV12)
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004076 return 0;
4077
4078 /* For Non Y-tile return 8-blocks */
Ville Syrjäläbae781b2016-11-16 13:33:16 +02004079 if (fb->modifier != I915_FORMAT_MOD_Y_TILED &&
4080 fb->modifier != I915_FORMAT_MOD_Yf_TILED)
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004081 return 8;
4082
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004083 /*
4084 * Src coordinates are already rotated by 270 degrees for
4085 * the 90/270 degree plane rotation cases (to match the
4086 * GTT mapping), hence no need to account for rotation here.
4087 */
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004088 src_w = drm_rect_width(&intel_pstate->base.src) >> 16;
4089 src_h = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004090
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004091 /* Halve UV plane width and height for NV12 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004092 if (fb->format->format == DRM_FORMAT_NV12 && !y) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004093 src_w /= 2;
4094 src_h /= 2;
4095 }
4096
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004097 if (fb->format->format == DRM_FORMAT_NV12 && !y)
Ville Syrjälä353c8592016-12-14 23:30:57 +02004098 plane_bpp = fb->format->cpp[1];
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004099 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02004100 plane_bpp = fb->format->cpp[0];
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004101
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03004102 if (drm_rotation_90_or_270(pstate->rotation)) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004103 switch (plane_bpp) {
4104 case 1:
4105 min_scanlines = 32;
4106 break;
4107 case 2:
4108 min_scanlines = 16;
4109 break;
4110 case 4:
4111 min_scanlines = 8;
4112 break;
4113 case 8:
4114 min_scanlines = 4;
4115 break;
4116 default:
4117 WARN(1, "Unsupported pixel depth %u for rotation",
4118 plane_bpp);
4119 min_scanlines = 32;
4120 }
4121 }
4122
4123 return DIV_ROUND_UP((4 * src_w * plane_bpp), 512) * min_scanlines/4 + 3;
4124}
4125
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004126static void
4127skl_ddb_calc_min(const struct intel_crtc_state *cstate, int num_active,
4128 uint16_t *minimum, uint16_t *y_minimum)
4129{
4130 const struct drm_plane_state *pstate;
4131 struct drm_plane *plane;
4132
4133 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, &cstate->base) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004134 enum plane_id plane_id = to_intel_plane(plane)->id;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004135
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004136 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004137 continue;
4138
4139 if (!pstate->visible)
4140 continue;
4141
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004142 minimum[plane_id] = skl_ddb_min_alloc(pstate, 0);
4143 y_minimum[plane_id] = skl_ddb_min_alloc(pstate, 1);
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004144 }
4145
4146 minimum[PLANE_CURSOR] = skl_cursor_allocation(num_active);
4147}
4148
Matt Roperc107acf2016-05-12 07:06:01 -07004149static int
Matt Roper024c9042015-09-24 15:53:11 -07004150skl_allocate_pipe_ddb(struct intel_crtc_state *cstate,
Damien Lespiaub9cec072014-11-04 17:06:43 +00004151 struct skl_ddb_allocation *ddb /* out */)
4152{
Matt Roperc107acf2016-05-12 07:06:01 -07004153 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07004154 struct drm_crtc *crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004155 struct drm_device *dev = crtc->dev;
4156 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4157 enum pipe pipe = intel_crtc->pipe;
Lyudece0ba282016-09-15 10:46:35 -04004158 struct skl_ddb_entry *alloc = &cstate->wm.skl.ddb;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004159 uint16_t alloc_size, start;
Maarten Lankhorstfefdd812016-10-26 15:41:33 +02004160 uint16_t minimum[I915_MAX_PLANES] = {};
4161 uint16_t y_minimum[I915_MAX_PLANES] = {};
Damien Lespiaub9cec072014-11-04 17:06:43 +00004162 unsigned int total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004163 enum plane_id plane_id;
Matt Roperc107acf2016-05-12 07:06:01 -07004164 int num_active;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004165 unsigned plane_data_rate[I915_MAX_PLANES] = {};
4166 unsigned plane_y_data_rate[I915_MAX_PLANES] = {};
Kumar, Mahesh5ba6faa2017-05-17 17:28:26 +05304167 uint16_t total_min_blocks = 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004168
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004169 /* Clear the partitioning for disabled planes. */
4170 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
4171 memset(ddb->y_plane[pipe], 0, sizeof(ddb->y_plane[pipe]));
4172
Matt Ropera6d3460e2016-05-12 07:06:04 -07004173 if (WARN_ON(!state))
4174 return 0;
4175
Matt Roperc107acf2016-05-12 07:06:01 -07004176 if (!cstate->base.active) {
Lyudece0ba282016-09-15 10:46:35 -04004177 alloc->start = alloc->end = 0;
Matt Roperc107acf2016-05-12 07:06:01 -07004178 return 0;
4179 }
4180
Matt Ropera6d3460e2016-05-12 07:06:04 -07004181 skl_ddb_get_pipe_allocation_limits(dev, cstate, alloc, &num_active);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00004182 alloc_size = skl_ddb_entry_size(alloc);
Kumar, Mahesh336031e2017-05-17 17:28:25 +05304183 if (alloc_size == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004184 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004185
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004186 skl_ddb_calc_min(cstate, num_active, minimum, y_minimum);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004187
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004188 /*
4189 * 1. Allocate the mininum required blocks for each active plane
4190 * and allocate the cursor, it doesn't require extra allocation
4191 * proportional to the data rate.
4192 */
Damien Lespiaub9cec072014-11-04 17:06:43 +00004193
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004194 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Kumar, Mahesh5ba6faa2017-05-17 17:28:26 +05304195 total_min_blocks += minimum[plane_id];
4196 total_min_blocks += y_minimum[plane_id];
Damien Lespiau80958152015-02-09 13:35:10 +00004197 }
4198
Kumar, Mahesh5ba6faa2017-05-17 17:28:26 +05304199 if (total_min_blocks > alloc_size) {
4200 DRM_DEBUG_KMS("Requested display configuration exceeds system DDB limitations");
4201 DRM_DEBUG_KMS("minimum required %d/%d\n", total_min_blocks,
4202 alloc_size);
4203 return -EINVAL;
4204 }
4205
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004206 alloc_size -= total_min_blocks;
4207 ddb->plane[pipe][PLANE_CURSOR].start = alloc->end - minimum[PLANE_CURSOR];
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004208 ddb->plane[pipe][PLANE_CURSOR].end = alloc->end;
4209
Damien Lespiaub9cec072014-11-04 17:06:43 +00004210 /*
Damien Lespiau80958152015-02-09 13:35:10 +00004211 * 2. Distribute the remaining space in proportion to the amount of
4212 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00004213 *
4214 * FIXME: we may not allocate every single block here.
4215 */
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004216 total_data_rate = skl_get_total_relative_data_rate(cstate,
4217 plane_data_rate,
4218 plane_y_data_rate);
Matt Ropera1de91e2016-05-12 07:05:57 -07004219 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004220 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004221
Damien Lespiau34bb56a2014-11-04 17:07:01 +00004222 start = alloc->start;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004223 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004224 unsigned int data_rate, y_data_rate;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004225 uint16_t plane_blocks, y_plane_blocks = 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004226
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004227 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004228 continue;
4229
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004230 data_rate = plane_data_rate[plane_id];
Damien Lespiaub9cec072014-11-04 17:06:43 +00004231
4232 /*
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004233 * allocation for (packed formats) or (uv-plane part of planar format):
Damien Lespiaub9cec072014-11-04 17:06:43 +00004234 * promote the expression to 64 bits to avoid overflowing, the
4235 * result is < available as data_rate / total_data_rate < 1
4236 */
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004237 plane_blocks = minimum[plane_id];
4238 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
4239 total_data_rate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004240
Matt Roperc107acf2016-05-12 07:06:01 -07004241 /* Leave disabled planes at (0,0) */
4242 if (data_rate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004243 ddb->plane[pipe][plane_id].start = start;
4244 ddb->plane[pipe][plane_id].end = start + plane_blocks;
Matt Roperc107acf2016-05-12 07:06:01 -07004245 }
Damien Lespiaub9cec072014-11-04 17:06:43 +00004246
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004247 start += plane_blocks;
4248
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004249 /*
4250 * allocation for y_plane part of planar format:
4251 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004252 y_data_rate = plane_y_data_rate[plane_id];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004253
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004254 y_plane_blocks = y_minimum[plane_id];
4255 y_plane_blocks += div_u64((uint64_t)alloc_size * y_data_rate,
4256 total_data_rate);
4257
Matt Roperc107acf2016-05-12 07:06:01 -07004258 if (y_data_rate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004259 ddb->y_plane[pipe][plane_id].start = start;
4260 ddb->y_plane[pipe][plane_id].end = start + y_plane_blocks;
Matt Roperc107acf2016-05-12 07:06:01 -07004261 }
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004262
4263 start += y_plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004264 }
4265
Matt Roperc107acf2016-05-12 07:06:01 -07004266 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004267}
4268
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004269/*
4270 * 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 +02004271 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004272 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
4273 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
4274*/
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304275static uint_fixed_16_16_t skl_wm_method1(uint32_t pixel_rate, uint8_t cpp,
4276 uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004277{
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304278 uint32_t wm_intermediate_val;
4279 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004280
4281 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304282 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004283
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304284 wm_intermediate_val = latency * pixel_rate * cpp;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304285 ret = div_fixed16(wm_intermediate_val, 1000 * 512);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004286 return ret;
4287}
4288
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304289static uint_fixed_16_16_t skl_wm_method2(uint32_t pixel_rate,
4290 uint32_t pipe_htotal,
4291 uint32_t latency,
4292 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004293{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004294 uint32_t wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304295 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004296
4297 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304298 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004299
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004300 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304301 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
4302 pipe_htotal * 1000);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304303 ret = mul_u32_fixed16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004304 return ret;
4305}
4306
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304307static uint_fixed_16_16_t
4308intel_get_linetime_us(struct intel_crtc_state *cstate)
4309{
4310 uint32_t pixel_rate;
4311 uint32_t crtc_htotal;
4312 uint_fixed_16_16_t linetime_us;
4313
4314 if (!cstate->base.active)
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304315 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304316
4317 pixel_rate = cstate->pixel_rate;
4318
4319 if (WARN_ON(pixel_rate == 0))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304320 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304321
4322 crtc_htotal = cstate->base.adjusted_mode.crtc_htotal;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304323 linetime_us = div_fixed16(crtc_htotal * 1000, pixel_rate);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304324
4325 return linetime_us;
4326}
4327
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304328static uint32_t
4329skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *cstate,
4330 const struct intel_plane_state *pstate)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004331{
4332 uint64_t adjusted_pixel_rate;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304333 uint_fixed_16_16_t downscale_amount;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004334
4335 /* Shouldn't reach here on disabled planes... */
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004336 if (WARN_ON(!intel_wm_plane_visible(cstate, pstate)))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004337 return 0;
4338
4339 /*
4340 * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
4341 * with additional adjustments for plane-specific scaling.
4342 */
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02004343 adjusted_pixel_rate = cstate->pixel_rate;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004344 downscale_amount = skl_plane_downscale_amount(cstate, pstate);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004345
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304346 return mul_round_up_u32_fixed16(adjusted_pixel_rate,
4347 downscale_amount);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004348}
4349
Matt Roper55994c22016-05-12 07:06:08 -07004350static int skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
4351 struct intel_crtc_state *cstate,
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304352 const struct intel_plane_state *intel_pstate,
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004353 uint16_t ddb_allocation,
Matt Roper55994c22016-05-12 07:06:08 -07004354 int level,
4355 uint16_t *out_blocks, /* out */
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004356 uint8_t *out_lines, /* out */
4357 bool *enabled /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004358{
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004359 struct intel_plane *plane = to_intel_plane(intel_pstate->base.plane);
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304360 const struct drm_plane_state *pstate = &intel_pstate->base;
4361 const struct drm_framebuffer *fb = pstate->fb;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004362 uint32_t latency = dev_priv->wm.skl_latency[level];
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304363 uint_fixed_16_16_t method1, method2;
4364 uint_fixed_16_16_t plane_blocks_per_line;
4365 uint_fixed_16_16_t selected_result;
4366 uint32_t interm_pbpl;
4367 uint32_t plane_bytes_per_line;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004368 uint32_t res_blocks, res_lines;
Ville Syrjäläac484962016-01-20 21:05:26 +02004369 uint8_t cpp;
Kumar, Mahesh129eaa92017-07-05 20:01:48 +05304370 uint32_t width = 0;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004371 uint32_t plane_pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304372 uint_fixed_16_16_t y_tile_minimum;
4373 uint32_t y_min_scanlines;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03004374 struct intel_atomic_state *state =
4375 to_intel_atomic_state(cstate->base.state);
4376 bool apply_memory_bw_wa = skl_needs_memory_bw_wa(state);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304377 bool y_tiled, x_tiled;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004378
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004379 if (latency == 0 ||
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004380 !intel_wm_plane_visible(cstate, intel_pstate)) {
4381 *enabled = false;
Matt Roper55994c22016-05-12 07:06:08 -07004382 return 0;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004383 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004384
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304385 y_tiled = fb->modifier == I915_FORMAT_MOD_Y_TILED ||
4386 fb->modifier == I915_FORMAT_MOD_Yf_TILED;
4387 x_tiled = fb->modifier == I915_FORMAT_MOD_X_TILED;
4388
Rodrigo Vivi82525c12017-06-08 08:50:00 -07004389 /* Display WA #1141: kbl,cfl */
4390 if ((IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv)) &&
4391 dev_priv->ipc_enabled)
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05304392 latency += 4;
4393
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304394 if (apply_memory_bw_wa && x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03004395 latency += 15;
4396
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004397 if (plane->id == PLANE_CURSOR) {
4398 width = intel_pstate->base.crtc_w;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004399 } else {
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004400 /*
4401 * Src coordinates are already rotated by 270 degrees for
4402 * the 90/270 degree plane rotation cases (to match the
4403 * GTT mapping), hence no need to account for rotation here.
4404 */
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004405 width = drm_rect_width(&intel_pstate->base.src) >> 16;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004406 }
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004407
Kumar, Maheshb064be02017-07-05 20:01:49 +05304408 cpp = (fb->format->format == DRM_FORMAT_NV12) ? fb->format->cpp[1] :
4409 fb->format->cpp[0];
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004410 plane_pixel_rate = skl_adjusted_plane_pixel_rate(cstate, intel_pstate);
4411
Dave Airlie61d0a042016-10-25 16:35:20 +10004412 if (drm_rotation_90_or_270(pstate->rotation)) {
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004413
4414 switch (cpp) {
4415 case 1:
4416 y_min_scanlines = 16;
4417 break;
4418 case 2:
4419 y_min_scanlines = 8;
4420 break;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004421 case 4:
4422 y_min_scanlines = 4;
4423 break;
Paulo Zanoni86a462b2016-09-22 18:00:35 -03004424 default:
4425 MISSING_CASE(cpp);
4426 return -EINVAL;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004427 }
4428 } else {
4429 y_min_scanlines = 4;
4430 }
4431
Paulo Zanoni2ef32de2016-11-08 18:22:11 -02004432 if (apply_memory_bw_wa)
4433 y_min_scanlines *= 2;
4434
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03004435 plane_bytes_per_line = width * cpp;
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304436 if (y_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304437 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line *
4438 y_min_scanlines, 512);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304439 plane_blocks_per_line = div_fixed16(interm_pbpl,
Kumar, Maheshafbc95c2017-05-17 17:28:20 +05304440 y_min_scanlines);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304441 } else if (x_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304442 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line, 512);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304443 plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304444 } else {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304445 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line, 512) + 1;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304446 plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03004447 }
4448
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004449 method1 = skl_wm_method1(plane_pixel_rate, cpp, latency);
4450 method2 = skl_wm_method2(plane_pixel_rate,
Matt Roper024c9042015-09-24 15:53:11 -07004451 cstate->base.adjusted_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004452 latency,
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03004453 plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004454
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304455 y_tile_minimum = mul_u32_fixed16(y_min_scanlines,
4456 plane_blocks_per_line);
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004457
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304458 if (y_tiled) {
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304459 selected_result = max_fixed16(method2, y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004460 } else {
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304461 uint32_t linetime_us;
4462
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304463 linetime_us = fixed16_to_u32_round_up(
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304464 intel_get_linetime_us(cstate));
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03004465 if ((cpp * cstate->base.adjusted_mode.crtc_htotal / 512 < 1) &&
4466 (plane_bytes_per_line / 512 < 1))
4467 selected_result = method2;
Maarten Lankhorst54d20ed2017-07-17 14:02:30 +02004468 else if (ddb_allocation >=
4469 fixed16_to_u32_round_up(plane_blocks_per_line))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304470 selected_result = min_fixed16(method1, method2);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304471 else if (latency >= linetime_us)
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304472 selected_result = min_fixed16(method1, method2);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004473 else
4474 selected_result = method1;
4475 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004476
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304477 res_blocks = fixed16_to_u32_round_up(selected_result) + 1;
Kumar, Maheshd273ecc2017-05-17 17:28:22 +05304478 res_lines = div_round_up_fixed16(selected_result,
4479 plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00004480
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004481 if (level >= 1 && level <= 7) {
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304482 if (y_tiled) {
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304483 res_blocks += fixed16_to_u32_round_up(y_tile_minimum);
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004484 res_lines += y_min_scanlines;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004485 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004486 res_blocks++;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004487 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004488 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004489
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004490 if (res_blocks >= ddb_allocation || res_lines > 31) {
4491 *enabled = false;
Matt Roper6b6bada2016-05-12 07:06:10 -07004492
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004493 /*
4494 * If there are no valid level 0 watermarks, then we can't
4495 * support this display configuration.
4496 */
4497 if (level) {
4498 return 0;
4499 } else {
4500 struct drm_plane *plane = pstate->plane;
4501
4502 DRM_DEBUG_KMS("Requested display configuration exceeds system watermark limitations\n");
4503 DRM_DEBUG_KMS("[PLANE:%d:%s] blocks required = %u/%u, lines required = %u/31\n",
4504 plane->base.id, plane->name,
4505 res_blocks, ddb_allocation, res_lines);
4506 return -EINVAL;
4507 }
Matt Roper55994c22016-05-12 07:06:08 -07004508 }
Damien Lespiaue6d66172014-11-04 17:06:55 +00004509
4510 *out_blocks = res_blocks;
4511 *out_lines = res_lines;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004512 *enabled = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004513
Matt Roper55994c22016-05-12 07:06:08 -07004514 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004515}
4516
Matt Roperf4a96752016-05-12 07:06:06 -07004517static int
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304518skl_compute_wm_levels(const struct drm_i915_private *dev_priv,
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004519 struct skl_ddb_allocation *ddb,
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304520 struct intel_crtc_state *cstate,
4521 const struct intel_plane_state *intel_pstate,
4522 struct skl_plane_wm *wm)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004523{
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004524 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
4525 struct drm_plane *plane = intel_pstate->base.plane;
4526 struct intel_plane *intel_plane = to_intel_plane(plane);
4527 uint16_t ddb_blocks;
4528 enum pipe pipe = intel_crtc->pipe;
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304529 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Roper55994c22016-05-12 07:06:08 -07004530 int ret;
Lyudea62163e2016-10-04 14:28:20 -04004531
Kumar, Mahesh7b751192017-05-17 17:28:24 +05304532 if (WARN_ON(!intel_pstate->base.fb))
4533 return -EINVAL;
Matt Roper024c9042015-09-24 15:53:11 -07004534
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004535 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][intel_plane->id]);
4536
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304537 for (level = 0; level <= max_level; level++) {
4538 struct skl_wm_level *result = &wm->wm[level];
4539
4540 ret = skl_compute_plane_wm(dev_priv,
4541 cstate,
4542 intel_pstate,
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004543 ddb_blocks,
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304544 level,
4545 &result->plane_res_b,
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004546 &result->plane_res_l,
4547 &result->plane_en);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304548 if (ret)
4549 return ret;
4550 }
Matt Roperf4a96752016-05-12 07:06:06 -07004551
4552 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004553}
4554
Damien Lespiau407b50f2014-11-04 17:06:57 +00004555static uint32_t
Matt Roper024c9042015-09-24 15:53:11 -07004556skl_compute_linetime_wm(struct intel_crtc_state *cstate)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004557{
Mahesh Kumara3a89862016-12-01 21:19:34 +05304558 struct drm_atomic_state *state = cstate->base.state;
4559 struct drm_i915_private *dev_priv = to_i915(state->dev);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304560 uint_fixed_16_16_t linetime_us;
Mahesh Kumara3a89862016-12-01 21:19:34 +05304561 uint32_t linetime_wm;
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03004562
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304563 linetime_us = intel_get_linetime_us(cstate);
4564
4565 if (is_fixed16_zero(linetime_us))
Damien Lespiau407b50f2014-11-04 17:06:57 +00004566 return 0;
4567
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304568 linetime_wm = fixed16_to_u32_round_up(mul_u32_fixed16(8, linetime_us));
Mahesh Kumara3a89862016-12-01 21:19:34 +05304569
4570 /* Display WA #1135: bxt. */
4571 if (IS_BROXTON(dev_priv) && dev_priv->ipc_enabled)
4572 linetime_wm = DIV_ROUND_UP(linetime_wm, 2);
4573
4574 return linetime_wm;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004575}
4576
Matt Roper024c9042015-09-24 15:53:11 -07004577static void skl_compute_transition_wm(struct intel_crtc_state *cstate,
Damien Lespiau9414f562014-11-04 17:06:58 +00004578 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004579{
Matt Roper024c9042015-09-24 15:53:11 -07004580 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004581 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00004582
4583 /* Until we know more, just disable transition WMs */
Lyudea62163e2016-10-04 14:28:20 -04004584 trans_wm->plane_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004585}
4586
Matt Roper55994c22016-05-12 07:06:08 -07004587static int skl_build_pipe_wm(struct intel_crtc_state *cstate,
4588 struct skl_ddb_allocation *ddb,
4589 struct skl_pipe_wm *pipe_wm)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004590{
Matt Roper024c9042015-09-24 15:53:11 -07004591 struct drm_device *dev = cstate->base.crtc->dev;
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304592 struct drm_crtc_state *crtc_state = &cstate->base;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004593 const struct drm_i915_private *dev_priv = to_i915(dev);
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304594 struct drm_plane *plane;
4595 const struct drm_plane_state *pstate;
Lyudea62163e2016-10-04 14:28:20 -04004596 struct skl_plane_wm *wm;
Matt Roper55994c22016-05-12 07:06:08 -07004597 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004598
Lyudea62163e2016-10-04 14:28:20 -04004599 /*
4600 * We'll only calculate watermarks for planes that are actually
4601 * enabled, so make sure all other planes are set as disabled.
4602 */
4603 memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
4604
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304605 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, crtc_state) {
4606 const struct intel_plane_state *intel_pstate =
4607 to_intel_plane_state(pstate);
4608 enum plane_id plane_id = to_intel_plane(plane)->id;
4609
4610 wm = &pipe_wm->planes[plane_id];
Lyudea62163e2016-10-04 14:28:20 -04004611
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004612 ret = skl_compute_wm_levels(dev_priv, ddb, cstate,
4613 intel_pstate, wm);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304614 if (ret)
4615 return ret;
Lyudea62163e2016-10-04 14:28:20 -04004616 skl_compute_transition_wm(cstate, &wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004617 }
Matt Roper024c9042015-09-24 15:53:11 -07004618 pipe_wm->linetime = skl_compute_linetime_wm(cstate);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004619
Matt Roper55994c22016-05-12 07:06:08 -07004620 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004621}
4622
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004623static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
4624 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00004625 const struct skl_ddb_entry *entry)
4626{
4627 if (entry->end)
4628 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
4629 else
4630 I915_WRITE(reg, 0);
4631}
4632
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004633static void skl_write_wm_level(struct drm_i915_private *dev_priv,
4634 i915_reg_t reg,
4635 const struct skl_wm_level *level)
4636{
4637 uint32_t val = 0;
4638
4639 if (level->plane_en) {
4640 val |= PLANE_WM_EN;
4641 val |= level->plane_res_b;
4642 val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
4643 }
4644
4645 I915_WRITE(reg, val);
4646}
4647
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004648static void skl_write_plane_wm(struct intel_crtc *intel_crtc,
4649 const struct skl_plane_wm *wm,
4650 const struct skl_ddb_allocation *ddb,
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004651 enum plane_id plane_id)
Lyude62e0fb82016-08-22 12:50:08 -04004652{
4653 struct drm_crtc *crtc = &intel_crtc->base;
4654 struct drm_device *dev = crtc->dev;
4655 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004656 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004657 enum pipe pipe = intel_crtc->pipe;
4658
4659 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004660 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004661 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004662 }
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004663 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004664 &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004665
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004666 skl_ddb_entry_write(dev_priv, PLANE_BUF_CFG(pipe, plane_id),
4667 &ddb->plane[pipe][plane_id]);
4668 skl_ddb_entry_write(dev_priv, PLANE_NV12_BUF_CFG(pipe, plane_id),
4669 &ddb->y_plane[pipe][plane_id]);
Lyude62e0fb82016-08-22 12:50:08 -04004670}
4671
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004672static void skl_write_cursor_wm(struct intel_crtc *intel_crtc,
4673 const struct skl_plane_wm *wm,
4674 const struct skl_ddb_allocation *ddb)
Lyude62e0fb82016-08-22 12:50:08 -04004675{
4676 struct drm_crtc *crtc = &intel_crtc->base;
4677 struct drm_device *dev = crtc->dev;
4678 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004679 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004680 enum pipe pipe = intel_crtc->pipe;
4681
4682 for (level = 0; level <= max_level; level++) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004683 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
4684 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004685 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004686 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004687
4688 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004689 &ddb->plane[pipe][PLANE_CURSOR]);
Lyude62e0fb82016-08-22 12:50:08 -04004690}
4691
cpaul@redhat.com45ece232016-10-14 17:31:56 -04004692bool skl_wm_level_equals(const struct skl_wm_level *l1,
4693 const struct skl_wm_level *l2)
4694{
4695 if (l1->plane_en != l2->plane_en)
4696 return false;
4697
4698 /* If both planes aren't enabled, the rest shouldn't matter */
4699 if (!l1->plane_en)
4700 return true;
4701
4702 return (l1->plane_res_l == l2->plane_res_l &&
4703 l1->plane_res_b == l2->plane_res_b);
4704}
4705
Lyude27082492016-08-24 07:48:10 +02004706static inline bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
4707 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004708{
Lyude27082492016-08-24 07:48:10 +02004709 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004710}
4711
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01004712bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry **entries,
4713 const struct skl_ddb_entry *ddb,
4714 int ignore)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004715{
Lyudece0ba282016-09-15 10:46:35 -04004716 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004717
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01004718 for (i = 0; i < I915_MAX_PIPES; i++)
4719 if (i != ignore && entries[i] &&
4720 skl_ddb_entries_overlap(ddb, entries[i]))
Lyude27082492016-08-24 07:48:10 +02004721 return true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004722
Lyude27082492016-08-24 07:48:10 +02004723 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004724}
4725
Matt Roper55994c22016-05-12 07:06:08 -07004726static int skl_update_pipe_wm(struct drm_crtc_state *cstate,
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004727 const struct skl_pipe_wm *old_pipe_wm,
Matt Roper55994c22016-05-12 07:06:08 -07004728 struct skl_pipe_wm *pipe_wm, /* out */
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004729 struct skl_ddb_allocation *ddb, /* out */
Matt Roper55994c22016-05-12 07:06:08 -07004730 bool *changed /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004731{
Matt Roperf4a96752016-05-12 07:06:06 -07004732 struct intel_crtc_state *intel_cstate = to_intel_crtc_state(cstate);
Matt Roper55994c22016-05-12 07:06:08 -07004733 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004734
Matt Roper55994c22016-05-12 07:06:08 -07004735 ret = skl_build_pipe_wm(intel_cstate, ddb, pipe_wm);
4736 if (ret)
4737 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004738
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004739 if (!memcmp(old_pipe_wm, pipe_wm, sizeof(*pipe_wm)))
Matt Roper55994c22016-05-12 07:06:08 -07004740 *changed = false;
4741 else
4742 *changed = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004743
Matt Roper55994c22016-05-12 07:06:08 -07004744 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004745}
4746
Matt Roper9b613022016-06-27 16:42:44 -07004747static uint32_t
4748pipes_modified(struct drm_atomic_state *state)
4749{
4750 struct drm_crtc *crtc;
4751 struct drm_crtc_state *cstate;
4752 uint32_t i, ret = 0;
4753
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004754 for_each_new_crtc_in_state(state, crtc, cstate, i)
Matt Roper9b613022016-06-27 16:42:44 -07004755 ret |= drm_crtc_mask(crtc);
4756
4757 return ret;
4758}
4759
Jani Nikulabb7791b2016-10-04 12:29:17 +03004760static int
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004761skl_ddb_add_affected_planes(struct intel_crtc_state *cstate)
4762{
4763 struct drm_atomic_state *state = cstate->base.state;
4764 struct drm_device *dev = state->dev;
4765 struct drm_crtc *crtc = cstate->base.crtc;
4766 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4767 struct drm_i915_private *dev_priv = to_i915(dev);
4768 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4769 struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
4770 struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
4771 struct drm_plane_state *plane_state;
4772 struct drm_plane *plane;
4773 enum pipe pipe = intel_crtc->pipe;
4774
4775 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
4776
4777 drm_for_each_plane_mask(plane, dev, cstate->base.plane_mask) {
4778 enum plane_id plane_id = to_intel_plane(plane)->id;
4779
4780 if (skl_ddb_entry_equal(&cur_ddb->plane[pipe][plane_id],
4781 &new_ddb->plane[pipe][plane_id]) &&
4782 skl_ddb_entry_equal(&cur_ddb->y_plane[pipe][plane_id],
4783 &new_ddb->y_plane[pipe][plane_id]))
4784 continue;
4785
4786 plane_state = drm_atomic_get_plane_state(state, plane);
4787 if (IS_ERR(plane_state))
4788 return PTR_ERR(plane_state);
4789 }
4790
4791 return 0;
4792}
4793
4794static int
4795skl_compute_ddb(struct drm_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07004796{
4797 struct drm_device *dev = state->dev;
4798 struct drm_i915_private *dev_priv = to_i915(dev);
4799 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4800 struct intel_crtc *intel_crtc;
Matt Roper734fa012016-05-12 15:11:40 -07004801 struct skl_ddb_allocation *ddb = &intel_state->wm_results.ddb;
Matt Roper9b613022016-06-27 16:42:44 -07004802 uint32_t realloc_pipes = pipes_modified(state);
Matt Roper98d39492016-05-12 07:06:03 -07004803 int ret;
4804
4805 /*
4806 * If this is our first atomic update following hardware readout,
4807 * we can't trust the DDB that the BIOS programmed for us. Let's
4808 * pretend that all pipes switched active status so that we'll
4809 * ensure a full DDB recompute.
4810 */
Matt Roper1b54a882016-06-17 13:42:18 -07004811 if (dev_priv->wm.distrust_bios_wm) {
4812 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
4813 state->acquire_ctx);
4814 if (ret)
4815 return ret;
4816
Matt Roper98d39492016-05-12 07:06:03 -07004817 intel_state->active_pipe_changes = ~0;
4818
Matt Roper1b54a882016-06-17 13:42:18 -07004819 /*
4820 * We usually only initialize intel_state->active_crtcs if we
4821 * we're doing a modeset; make sure this field is always
4822 * initialized during the sanitization process that happens
4823 * on the first commit too.
4824 */
4825 if (!intel_state->modeset)
4826 intel_state->active_crtcs = dev_priv->active_crtcs;
4827 }
4828
Matt Roper98d39492016-05-12 07:06:03 -07004829 /*
4830 * If the modeset changes which CRTC's are active, we need to
4831 * recompute the DDB allocation for *all* active pipes, even
4832 * those that weren't otherwise being modified in any way by this
4833 * atomic commit. Due to the shrinking of the per-pipe allocations
4834 * when new active CRTC's are added, it's possible for a pipe that
4835 * we were already using and aren't changing at all here to suddenly
4836 * become invalid if its DDB needs exceeds its new allocation.
4837 *
4838 * Note that if we wind up doing a full DDB recompute, we can't let
4839 * any other display updates race with this transaction, so we need
4840 * to grab the lock on *all* CRTC's.
4841 */
Matt Roper734fa012016-05-12 15:11:40 -07004842 if (intel_state->active_pipe_changes) {
Matt Roper98d39492016-05-12 07:06:03 -07004843 realloc_pipes = ~0;
Matt Roper734fa012016-05-12 15:11:40 -07004844 intel_state->wm_results.dirty_pipes = ~0;
4845 }
Matt Roper98d39492016-05-12 07:06:03 -07004846
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004847 /*
4848 * We're not recomputing for the pipes not included in the commit, so
4849 * make sure we start with the current state.
4850 */
4851 memcpy(ddb, &dev_priv->wm.skl_hw.ddb, sizeof(*ddb));
4852
Matt Roper98d39492016-05-12 07:06:03 -07004853 for_each_intel_crtc_mask(dev, intel_crtc, realloc_pipes) {
4854 struct intel_crtc_state *cstate;
4855
4856 cstate = intel_atomic_get_crtc_state(state, intel_crtc);
4857 if (IS_ERR(cstate))
4858 return PTR_ERR(cstate);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004859
4860 ret = skl_allocate_pipe_ddb(cstate, ddb);
4861 if (ret)
4862 return ret;
4863
4864 ret = skl_ddb_add_affected_planes(cstate);
4865 if (ret)
4866 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07004867 }
4868
4869 return 0;
4870}
4871
Matt Roper2722efb2016-08-17 15:55:55 -04004872static void
4873skl_copy_wm_for_pipe(struct skl_wm_values *dst,
4874 struct skl_wm_values *src,
4875 enum pipe pipe)
4876{
Matt Roper2722efb2016-08-17 15:55:55 -04004877 memcpy(dst->ddb.y_plane[pipe], src->ddb.y_plane[pipe],
4878 sizeof(dst->ddb.y_plane[pipe]));
4879 memcpy(dst->ddb.plane[pipe], src->ddb.plane[pipe],
4880 sizeof(dst->ddb.plane[pipe]));
4881}
4882
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004883static void
4884skl_print_wm_changes(const struct drm_atomic_state *state)
4885{
4886 const struct drm_device *dev = state->dev;
4887 const struct drm_i915_private *dev_priv = to_i915(dev);
4888 const struct intel_atomic_state *intel_state =
4889 to_intel_atomic_state(state);
4890 const struct drm_crtc *crtc;
4891 const struct drm_crtc_state *cstate;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004892 const struct intel_plane *intel_plane;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004893 const struct skl_ddb_allocation *old_ddb = &dev_priv->wm.skl_hw.ddb;
4894 const struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
Maarten Lankhorst75704982016-11-01 12:04:10 +01004895 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004896
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004897 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst75704982016-11-01 12:04:10 +01004898 const struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4899 enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004900
Maarten Lankhorst75704982016-11-01 12:04:10 +01004901 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004902 enum plane_id plane_id = intel_plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004903 const struct skl_ddb_entry *old, *new;
4904
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004905 old = &old_ddb->plane[pipe][plane_id];
4906 new = &new_ddb->plane[pipe][plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004907
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004908 if (skl_ddb_entry_equal(old, new))
4909 continue;
4910
Maarten Lankhorst75704982016-11-01 12:04:10 +01004911 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] ddb (%d - %d) -> (%d - %d)\n",
4912 intel_plane->base.base.id,
4913 intel_plane->base.name,
4914 old->start, old->end,
4915 new->start, new->end);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004916 }
4917 }
4918}
4919
Matt Roper98d39492016-05-12 07:06:03 -07004920static int
4921skl_compute_wm(struct drm_atomic_state *state)
4922{
4923 struct drm_crtc *crtc;
4924 struct drm_crtc_state *cstate;
Matt Roper734fa012016-05-12 15:11:40 -07004925 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4926 struct skl_wm_values *results = &intel_state->wm_results;
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02004927 struct drm_device *dev = state->dev;
Matt Roper734fa012016-05-12 15:11:40 -07004928 struct skl_pipe_wm *pipe_wm;
Matt Roper98d39492016-05-12 07:06:03 -07004929 bool changed = false;
Matt Roper734fa012016-05-12 15:11:40 -07004930 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07004931
4932 /*
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02004933 * When we distrust bios wm we always need to recompute to set the
4934 * expected DDB allocations for each CRTC.
4935 */
4936 if (to_i915(dev)->wm.distrust_bios_wm)
4937 changed = true;
4938
4939 /*
Matt Roper98d39492016-05-12 07:06:03 -07004940 * If this transaction isn't actually touching any CRTC's, don't
4941 * bother with watermark calculation. Note that if we pass this
4942 * test, we're guaranteed to hold at least one CRTC state mutex,
4943 * which means we can safely use values like dev_priv->active_crtcs
4944 * since any racing commits that want to update them would need to
4945 * hold _all_ CRTC state mutexes.
4946 */
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004947 for_each_new_crtc_in_state(state, crtc, cstate, i)
Matt Roper98d39492016-05-12 07:06:03 -07004948 changed = true;
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02004949
Matt Roper98d39492016-05-12 07:06:03 -07004950 if (!changed)
4951 return 0;
4952
Matt Roper734fa012016-05-12 15:11:40 -07004953 /* Clear all dirty flags */
4954 results->dirty_pipes = 0;
4955
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004956 ret = skl_compute_ddb(state);
Matt Roper98d39492016-05-12 07:06:03 -07004957 if (ret)
4958 return ret;
4959
Matt Roper734fa012016-05-12 15:11:40 -07004960 /*
4961 * Calculate WM's for all pipes that are part of this transaction.
4962 * Note that the DDB allocation above may have added more CRTC's that
4963 * weren't otherwise being modified (and set bits in dirty_pipes) if
4964 * pipe allocations had to change.
4965 *
4966 * FIXME: Now that we're doing this in the atomic check phase, we
4967 * should allow skl_update_pipe_wm() to return failure in cases where
4968 * no suitable watermark values can be found.
4969 */
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004970 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Matt Roper734fa012016-05-12 15:11:40 -07004971 struct intel_crtc_state *intel_cstate =
4972 to_intel_crtc_state(cstate);
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004973 const struct skl_pipe_wm *old_pipe_wm =
4974 &to_intel_crtc_state(crtc->state)->wm.skl.optimal;
Matt Roper734fa012016-05-12 15:11:40 -07004975
4976 pipe_wm = &intel_cstate->wm.skl.optimal;
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004977 ret = skl_update_pipe_wm(cstate, old_pipe_wm, pipe_wm,
4978 &results->ddb, &changed);
Matt Roper734fa012016-05-12 15:11:40 -07004979 if (ret)
4980 return ret;
4981
4982 if (changed)
4983 results->dirty_pipes |= drm_crtc_mask(crtc);
4984
4985 if ((results->dirty_pipes & drm_crtc_mask(crtc)) == 0)
4986 /* This pipe's WM's did not change */
4987 continue;
4988
4989 intel_cstate->update_wm_pre = true;
Matt Roper734fa012016-05-12 15:11:40 -07004990 }
4991
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004992 skl_print_wm_changes(state);
4993
Matt Roper98d39492016-05-12 07:06:03 -07004994 return 0;
4995}
4996
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004997static void skl_atomic_update_crtc_wm(struct intel_atomic_state *state,
4998 struct intel_crtc_state *cstate)
4999{
5000 struct intel_crtc *crtc = to_intel_crtc(cstate->base.crtc);
5001 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5002 struct skl_pipe_wm *pipe_wm = &cstate->wm.skl.optimal;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005003 const struct skl_ddb_allocation *ddb = &state->wm_results.ddb;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005004 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005005 enum plane_id plane_id;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005006
5007 if (!(state->wm_results.dirty_pipes & drm_crtc_mask(&crtc->base)))
5008 return;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005009
5010 I915_WRITE(PIPE_WM_LINETIME(pipe), pipe_wm->linetime);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005011
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005012 for_each_plane_id_on_crtc(crtc, plane_id) {
5013 if (plane_id != PLANE_CURSOR)
5014 skl_write_plane_wm(crtc, &pipe_wm->planes[plane_id],
5015 ddb, plane_id);
5016 else
5017 skl_write_cursor_wm(crtc, &pipe_wm->planes[plane_id],
5018 ddb);
5019 }
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005020}
5021
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005022static void skl_initial_wm(struct intel_atomic_state *state,
5023 struct intel_crtc_state *cstate)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005024{
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005025 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02005026 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005027 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005028 struct skl_wm_values *results = &state->wm_results;
Matt Roper2722efb2016-08-17 15:55:55 -04005029 struct skl_wm_values *hw_vals = &dev_priv->wm.skl_hw;
Lyude27082492016-08-24 07:48:10 +02005030 enum pipe pipe = intel_crtc->pipe;
Bob Paauweadda50b2015-07-21 10:42:53 -07005031
Ville Syrjälä432081b2016-10-31 22:37:03 +02005032 if ((results->dirty_pipes & drm_crtc_mask(&intel_crtc->base)) == 0)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005033 return;
5034
Matt Roper734fa012016-05-12 15:11:40 -07005035 mutex_lock(&dev_priv->wm.wm_mutex);
5036
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005037 if (cstate->base.active_changed)
5038 skl_atomic_update_crtc_wm(state, cstate);
Lyude27082492016-08-24 07:48:10 +02005039
5040 skl_copy_wm_for_pipe(hw_vals, results, pipe);
Matt Roper734fa012016-05-12 15:11:40 -07005041
5042 mutex_unlock(&dev_priv->wm.wm_mutex);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005043}
5044
Ville Syrjäläd8905652016-01-14 14:53:35 +02005045static void ilk_compute_wm_config(struct drm_device *dev,
5046 struct intel_wm_config *config)
5047{
5048 struct intel_crtc *crtc;
5049
5050 /* Compute the currently _active_ config */
5051 for_each_intel_crtc(dev, crtc) {
5052 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
5053
5054 if (!wm->pipe_enabled)
5055 continue;
5056
5057 config->sprites_enabled |= wm->sprites_enabled;
5058 config->sprites_scaled |= wm->sprites_scaled;
5059 config->num_pipes_active++;
5060 }
5061}
5062
Matt Ropered4a6a72016-02-23 17:20:13 -08005063static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03005064{
Chris Wilson91c8a322016-07-05 10:40:23 +01005065 struct drm_device *dev = &dev_priv->drm;
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005066 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02005067 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02005068 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02005069 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005070 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07005071
Ville Syrjäläd8905652016-01-14 14:53:35 +02005072 ilk_compute_wm_config(dev, &config);
5073
5074 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
5075 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03005076
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005077 /* 5/6 split only in single pipe config on IVB+ */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00005078 if (INTEL_GEN(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02005079 config.num_pipes_active == 1 && config.sprites_enabled) {
5080 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
5081 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005082
Imre Deak820c1982013-12-17 14:46:36 +02005083 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03005084 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005085 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005086 }
5087
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005088 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005089 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005090
Imre Deak820c1982013-12-17 14:46:36 +02005091 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03005092
Imre Deak820c1982013-12-17 14:46:36 +02005093 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03005094}
5095
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005096static void ilk_initial_watermarks(struct intel_atomic_state *state,
5097 struct intel_crtc_state *cstate)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005098{
Matt Ropered4a6a72016-02-23 17:20:13 -08005099 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
5100 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005101
Matt Ropered4a6a72016-02-23 17:20:13 -08005102 mutex_lock(&dev_priv->wm.wm_mutex);
Matt Ropere8f1f022016-05-12 07:05:55 -07005103 intel_crtc->wm.active.ilk = cstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08005104 ilk_program_watermarks(dev_priv);
5105 mutex_unlock(&dev_priv->wm.wm_mutex);
5106}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005107
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005108static void ilk_optimize_watermarks(struct intel_atomic_state *state,
5109 struct intel_crtc_state *cstate)
Matt Ropered4a6a72016-02-23 17:20:13 -08005110{
5111 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
5112 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
5113
5114 mutex_lock(&dev_priv->wm.wm_mutex);
5115 if (cstate->wm.need_postvbl_update) {
Matt Ropere8f1f022016-05-12 07:05:55 -07005116 intel_crtc->wm.active.ilk = cstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08005117 ilk_program_watermarks(dev_priv);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005118 }
Matt Ropered4a6a72016-02-23 17:20:13 -08005119 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005120}
5121
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005122static inline void skl_wm_level_from_reg_val(uint32_t val,
5123 struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00005124{
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005125 level->plane_en = val & PLANE_WM_EN;
5126 level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
5127 level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
5128 PLANE_WM_LINES_MASK;
Pradeep Bhat30789992014-11-04 17:06:45 +00005129}
5130
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005131void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc,
5132 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00005133{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005134 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Pradeep Bhat30789992014-11-04 17:06:45 +00005135 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Pradeep Bhat30789992014-11-04 17:06:45 +00005136 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005137 int level, max_level;
5138 enum plane_id plane_id;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005139 uint32_t val;
Pradeep Bhat30789992014-11-04 17:06:45 +00005140
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005141 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00005142
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005143 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
5144 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00005145
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005146 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005147 if (plane_id != PLANE_CURSOR)
5148 val = I915_READ(PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005149 else
5150 val = I915_READ(CUR_WM(pipe, level));
5151
5152 skl_wm_level_from_reg_val(val, &wm->wm[level]);
5153 }
5154
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005155 if (plane_id != PLANE_CURSOR)
5156 val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005157 else
5158 val = I915_READ(CUR_WM_TRANS(pipe));
5159
5160 skl_wm_level_from_reg_val(val, &wm->trans_wm);
5161 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005162
Matt Roper3ef00282015-03-09 10:19:24 -07005163 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00005164 return;
5165
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005166 out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00005167}
5168
5169void skl_wm_get_hw_state(struct drm_device *dev)
5170{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005171 struct drm_i915_private *dev_priv = to_i915(dev);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005172 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
Damien Lespiaua269c582014-11-04 17:06:49 +00005173 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00005174 struct drm_crtc *crtc;
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005175 struct intel_crtc *intel_crtc;
5176 struct intel_crtc_state *cstate;
Pradeep Bhat30789992014-11-04 17:06:45 +00005177
Damien Lespiaua269c582014-11-04 17:06:49 +00005178 skl_ddb_get_hw_state(dev_priv, ddb);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005179 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5180 intel_crtc = to_intel_crtc(crtc);
5181 cstate = to_intel_crtc_state(crtc->state);
5182
5183 skl_pipe_wm_get_hw_state(crtc, &cstate->wm.skl.optimal);
5184
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02005185 if (intel_crtc->active)
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005186 hw->dirty_pipes |= drm_crtc_mask(crtc);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005187 }
Matt Ropera1de91e2016-05-12 07:05:57 -07005188
Matt Roper279e99d2016-05-12 07:06:02 -07005189 if (dev_priv->active_crtcs) {
5190 /* Fully recompute DDB on first atomic commit */
5191 dev_priv->wm.distrust_bios_wm = true;
5192 } else {
5193 /* Easy/common case; just sanitize DDB now if everything off */
5194 memset(ddb, 0, sizeof(*ddb));
5195 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005196}
5197
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005198static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
5199{
5200 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005201 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02005202 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005203 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper4e0963c2015-09-24 15:53:15 -07005204 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Matt Ropere8f1f022016-05-12 07:05:55 -07005205 struct intel_pipe_wm *active = &cstate->wm.ilk.optimal;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005206 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005207 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005208 [PIPE_A] = WM0_PIPEA_ILK,
5209 [PIPE_B] = WM0_PIPEB_ILK,
5210 [PIPE_C] = WM0_PIPEC_IVB,
5211 };
5212
5213 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005214 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02005215 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005216
Ville Syrjälä15606532016-05-13 17:55:17 +03005217 memset(active, 0, sizeof(*active));
5218
Matt Roper3ef00282015-03-09 10:19:24 -07005219 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02005220
5221 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005222 u32 tmp = hw->wm_pipe[pipe];
5223
5224 /*
5225 * For active pipes LP0 watermark is marked as
5226 * enabled, and LP1+ watermaks as disabled since
5227 * we can't really reverse compute them in case
5228 * multiple pipes are active.
5229 */
5230 active->wm[0].enable = true;
5231 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
5232 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
5233 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
5234 active->linetime = hw->wm_linetime[pipe];
5235 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005236 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005237
5238 /*
5239 * For inactive pipes, all watermark levels
5240 * should be marked as enabled but zeroed,
5241 * which is what we'd compute them to.
5242 */
5243 for (level = 0; level <= max_level; level++)
5244 active->wm[level].enable = true;
5245 }
Matt Roper4e0963c2015-09-24 15:53:15 -07005246
5247 intel_crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005248}
5249
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005250#define _FW_WM(value, plane) \
5251 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
5252#define _FW_WM_VLV(value, plane) \
5253 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
5254
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005255static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
5256 struct g4x_wm_values *wm)
5257{
5258 uint32_t tmp;
5259
5260 tmp = I915_READ(DSPFW1);
5261 wm->sr.plane = _FW_WM(tmp, SR);
5262 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5263 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
5264 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
5265
5266 tmp = I915_READ(DSPFW2);
5267 wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
5268 wm->sr.fbc = _FW_WM(tmp, FBC_SR);
5269 wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
5270 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
5271 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5272 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
5273
5274 tmp = I915_READ(DSPFW3);
5275 wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
5276 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5277 wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
5278 wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
5279}
5280
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005281static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
5282 struct vlv_wm_values *wm)
5283{
5284 enum pipe pipe;
5285 uint32_t tmp;
5286
5287 for_each_pipe(dev_priv, pipe) {
5288 tmp = I915_READ(VLV_DDL(pipe));
5289
Ville Syrjälä1b313892016-11-28 19:37:08 +02005290 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005291 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005292 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005293 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005294 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005295 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005296 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005297 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
5298 }
5299
5300 tmp = I915_READ(DSPFW1);
5301 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005302 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5303 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
5304 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005305
5306 tmp = I915_READ(DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005307 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
5308 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5309 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005310
5311 tmp = I915_READ(DSPFW3);
5312 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5313
5314 if (IS_CHERRYVIEW(dev_priv)) {
5315 tmp = I915_READ(DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005316 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5317 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005318
5319 tmp = I915_READ(DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005320 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
5321 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005322
5323 tmp = I915_READ(DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005324 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
5325 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005326
5327 tmp = I915_READ(DSPHOWM);
5328 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005329 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
5330 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
5331 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
5332 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5333 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5334 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5335 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5336 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5337 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005338 } else {
5339 tmp = I915_READ(DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005340 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5341 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005342
5343 tmp = I915_READ(DSPHOWM);
5344 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005345 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5346 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5347 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5348 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5349 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5350 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005351 }
5352}
5353
5354#undef _FW_WM
5355#undef _FW_WM_VLV
5356
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005357void g4x_wm_get_hw_state(struct drm_device *dev)
5358{
5359 struct drm_i915_private *dev_priv = to_i915(dev);
5360 struct g4x_wm_values *wm = &dev_priv->wm.g4x;
5361 struct intel_crtc *crtc;
5362
5363 g4x_read_wm_values(dev_priv, wm);
5364
5365 wm->cxsr = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
5366
5367 for_each_intel_crtc(dev, crtc) {
5368 struct intel_crtc_state *crtc_state =
5369 to_intel_crtc_state(crtc->base.state);
5370 struct g4x_wm_state *active = &crtc->wm.active.g4x;
5371 struct g4x_pipe_wm *raw;
5372 enum pipe pipe = crtc->pipe;
5373 enum plane_id plane_id;
5374 int level, max_level;
5375
5376 active->cxsr = wm->cxsr;
5377 active->hpll_en = wm->hpll_en;
5378 active->fbc_en = wm->fbc_en;
5379
5380 active->sr = wm->sr;
5381 active->hpll = wm->hpll;
5382
5383 for_each_plane_id_on_crtc(crtc, plane_id) {
5384 active->wm.plane[plane_id] =
5385 wm->pipe[pipe].plane[plane_id];
5386 }
5387
5388 if (wm->cxsr && wm->hpll_en)
5389 max_level = G4X_WM_LEVEL_HPLL;
5390 else if (wm->cxsr)
5391 max_level = G4X_WM_LEVEL_SR;
5392 else
5393 max_level = G4X_WM_LEVEL_NORMAL;
5394
5395 level = G4X_WM_LEVEL_NORMAL;
5396 raw = &crtc_state->wm.g4x.raw[level];
5397 for_each_plane_id_on_crtc(crtc, plane_id)
5398 raw->plane[plane_id] = active->wm.plane[plane_id];
5399
5400 if (++level > max_level)
5401 goto out;
5402
5403 raw = &crtc_state->wm.g4x.raw[level];
5404 raw->plane[PLANE_PRIMARY] = active->sr.plane;
5405 raw->plane[PLANE_CURSOR] = active->sr.cursor;
5406 raw->plane[PLANE_SPRITE0] = 0;
5407 raw->fbc = active->sr.fbc;
5408
5409 if (++level > max_level)
5410 goto out;
5411
5412 raw = &crtc_state->wm.g4x.raw[level];
5413 raw->plane[PLANE_PRIMARY] = active->hpll.plane;
5414 raw->plane[PLANE_CURSOR] = active->hpll.cursor;
5415 raw->plane[PLANE_SPRITE0] = 0;
5416 raw->fbc = active->hpll.fbc;
5417
5418 out:
5419 for_each_plane_id_on_crtc(crtc, plane_id)
5420 g4x_raw_plane_wm_set(crtc_state, level,
5421 plane_id, USHRT_MAX);
5422 g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
5423
5424 crtc_state->wm.g4x.optimal = *active;
5425 crtc_state->wm.g4x.intermediate = *active;
5426
5427 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
5428 pipe_name(pipe),
5429 wm->pipe[pipe].plane[PLANE_PRIMARY],
5430 wm->pipe[pipe].plane[PLANE_CURSOR],
5431 wm->pipe[pipe].plane[PLANE_SPRITE0]);
5432 }
5433
5434 DRM_DEBUG_KMS("Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
5435 wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
5436 DRM_DEBUG_KMS("Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
5437 wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
5438 DRM_DEBUG_KMS("Initial SR=%s HPLL=%s FBC=%s\n",
5439 yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
5440}
5441
5442void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
5443{
5444 struct intel_plane *plane;
5445 struct intel_crtc *crtc;
5446
5447 mutex_lock(&dev_priv->wm.wm_mutex);
5448
5449 for_each_intel_plane(&dev_priv->drm, plane) {
5450 struct intel_crtc *crtc =
5451 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
5452 struct intel_crtc_state *crtc_state =
5453 to_intel_crtc_state(crtc->base.state);
5454 struct intel_plane_state *plane_state =
5455 to_intel_plane_state(plane->base.state);
5456 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
5457 enum plane_id plane_id = plane->id;
5458 int level;
5459
5460 if (plane_state->base.visible)
5461 continue;
5462
5463 for (level = 0; level < 3; level++) {
5464 struct g4x_pipe_wm *raw =
5465 &crtc_state->wm.g4x.raw[level];
5466
5467 raw->plane[plane_id] = 0;
5468 wm_state->wm.plane[plane_id] = 0;
5469 }
5470
5471 if (plane_id == PLANE_PRIMARY) {
5472 for (level = 0; level < 3; level++) {
5473 struct g4x_pipe_wm *raw =
5474 &crtc_state->wm.g4x.raw[level];
5475 raw->fbc = 0;
5476 }
5477
5478 wm_state->sr.fbc = 0;
5479 wm_state->hpll.fbc = 0;
5480 wm_state->fbc_en = false;
5481 }
5482 }
5483
5484 for_each_intel_crtc(&dev_priv->drm, crtc) {
5485 struct intel_crtc_state *crtc_state =
5486 to_intel_crtc_state(crtc->base.state);
5487
5488 crtc_state->wm.g4x.intermediate =
5489 crtc_state->wm.g4x.optimal;
5490 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
5491 }
5492
5493 g4x_program_watermarks(dev_priv);
5494
5495 mutex_unlock(&dev_priv->wm.wm_mutex);
5496}
5497
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005498void vlv_wm_get_hw_state(struct drm_device *dev)
5499{
5500 struct drm_i915_private *dev_priv = to_i915(dev);
5501 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02005502 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005503 u32 val;
5504
5505 vlv_read_wm_values(dev_priv, wm);
5506
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005507 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
5508 wm->level = VLV_WM_LEVEL_PM2;
5509
5510 if (IS_CHERRYVIEW(dev_priv)) {
5511 mutex_lock(&dev_priv->rps.hw_lock);
5512
5513 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5514 if (val & DSP_MAXFIFO_PM5_ENABLE)
5515 wm->level = VLV_WM_LEVEL_PM5;
5516
Ville Syrjälä58590c12015-09-08 21:05:12 +03005517 /*
5518 * If DDR DVFS is disabled in the BIOS, Punit
5519 * will never ack the request. So if that happens
5520 * assume we don't have to enable/disable DDR DVFS
5521 * dynamically. To test that just set the REQ_ACK
5522 * bit to poke the Punit, but don't change the
5523 * HIGH/LOW bits so that we don't actually change
5524 * the current state.
5525 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005526 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03005527 val |= FORCE_DDR_FREQ_REQ_ACK;
5528 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
5529
5530 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
5531 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
5532 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
5533 "assuming DDR DVFS is disabled\n");
5534 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
5535 } else {
5536 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
5537 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
5538 wm->level = VLV_WM_LEVEL_DDR_DVFS;
5539 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005540
5541 mutex_unlock(&dev_priv->rps.hw_lock);
5542 }
5543
Ville Syrjäläff32c542017-03-02 19:14:57 +02005544 for_each_intel_crtc(dev, crtc) {
5545 struct intel_crtc_state *crtc_state =
5546 to_intel_crtc_state(crtc->base.state);
5547 struct vlv_wm_state *active = &crtc->wm.active.vlv;
5548 const struct vlv_fifo_state *fifo_state =
5549 &crtc_state->wm.vlv.fifo_state;
5550 enum pipe pipe = crtc->pipe;
5551 enum plane_id plane_id;
5552 int level;
5553
5554 vlv_get_fifo_size(crtc_state);
5555
5556 active->num_levels = wm->level + 1;
5557 active->cxsr = wm->cxsr;
5558
Ville Syrjäläff32c542017-03-02 19:14:57 +02005559 for (level = 0; level < active->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03005560 struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02005561 &crtc_state->wm.vlv.raw[level];
5562
5563 active->sr[level].plane = wm->sr.plane;
5564 active->sr[level].cursor = wm->sr.cursor;
5565
5566 for_each_plane_id_on_crtc(crtc, plane_id) {
5567 active->wm[level].plane[plane_id] =
5568 wm->pipe[pipe].plane[plane_id];
5569
5570 raw->plane[plane_id] =
5571 vlv_invert_wm_value(active->wm[level].plane[plane_id],
5572 fifo_state->plane[plane_id]);
5573 }
5574 }
5575
5576 for_each_plane_id_on_crtc(crtc, plane_id)
5577 vlv_raw_plane_wm_set(crtc_state, level,
5578 plane_id, USHRT_MAX);
5579 vlv_invalidate_wms(crtc, active, level);
5580
5581 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02005582 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02005583
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005584 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 +02005585 pipe_name(pipe),
5586 wm->pipe[pipe].plane[PLANE_PRIMARY],
5587 wm->pipe[pipe].plane[PLANE_CURSOR],
5588 wm->pipe[pipe].plane[PLANE_SPRITE0],
5589 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02005590 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005591
5592 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
5593 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
5594}
5595
Ville Syrjälä602ae832017-03-02 19:15:02 +02005596void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
5597{
5598 struct intel_plane *plane;
5599 struct intel_crtc *crtc;
5600
5601 mutex_lock(&dev_priv->wm.wm_mutex);
5602
5603 for_each_intel_plane(&dev_priv->drm, plane) {
5604 struct intel_crtc *crtc =
5605 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
5606 struct intel_crtc_state *crtc_state =
5607 to_intel_crtc_state(crtc->base.state);
5608 struct intel_plane_state *plane_state =
5609 to_intel_plane_state(plane->base.state);
5610 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
5611 const struct vlv_fifo_state *fifo_state =
5612 &crtc_state->wm.vlv.fifo_state;
5613 enum plane_id plane_id = plane->id;
5614 int level;
5615
5616 if (plane_state->base.visible)
5617 continue;
5618
5619 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03005620 struct g4x_pipe_wm *raw =
Ville Syrjälä602ae832017-03-02 19:15:02 +02005621 &crtc_state->wm.vlv.raw[level];
5622
5623 raw->plane[plane_id] = 0;
5624
5625 wm_state->wm[level].plane[plane_id] =
5626 vlv_invert_wm_value(raw->plane[plane_id],
5627 fifo_state->plane[plane_id]);
5628 }
5629 }
5630
5631 for_each_intel_crtc(&dev_priv->drm, crtc) {
5632 struct intel_crtc_state *crtc_state =
5633 to_intel_crtc_state(crtc->base.state);
5634
5635 crtc_state->wm.vlv.intermediate =
5636 crtc_state->wm.vlv.optimal;
5637 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
5638 }
5639
5640 vlv_program_watermarks(dev_priv);
5641
5642 mutex_unlock(&dev_priv->wm.wm_mutex);
5643}
5644
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005645void ilk_wm_get_hw_state(struct drm_device *dev)
5646{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005647 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02005648 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005649 struct drm_crtc *crtc;
5650
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01005651 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005652 ilk_pipe_wm_get_hw_state(crtc);
5653
5654 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
5655 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
5656 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
5657
5658 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00005659 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjäläcfa76982014-03-07 18:32:08 +02005660 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
5661 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
5662 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005663
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005664 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02005665 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
5666 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01005667 else if (IS_IVYBRIDGE(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02005668 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
5669 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005670
5671 hw->enable_fbc_wm =
5672 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
5673}
5674
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005675/**
5676 * intel_update_watermarks - update FIFO watermark values based on current modes
5677 *
5678 * Calculate watermark values for the various WM regs based on current mode
5679 * and plane configuration.
5680 *
5681 * There are several cases to deal with here:
5682 * - normal (i.e. non-self-refresh)
5683 * - self-refresh (SR) mode
5684 * - lines are large relative to FIFO size (buffer can hold up to 2)
5685 * - lines are small relative to FIFO size (buffer can hold more than 2
5686 * lines), so need to account for TLB latency
5687 *
5688 * The normal calculation is:
5689 * watermark = dotclock * bytes per pixel * latency
5690 * where latency is platform & configuration dependent (we assume pessimal
5691 * values here).
5692 *
5693 * The SR calculation is:
5694 * watermark = (trunc(latency/line time)+1) * surface width *
5695 * bytes per pixel
5696 * where
5697 * line time = htotal / dotclock
5698 * surface width = hdisplay for normal plane and 64 for cursor
5699 * and latency is assumed to be high, as above.
5700 *
5701 * The final value programmed to the register should always be rounded up,
5702 * and include an extra 2 entries to account for clock crossings.
5703 *
5704 * We don't use the sprite, so we can ignore that. And on Crestline we have
5705 * to set the non-SR watermarks to 8.
5706 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02005707void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005708{
Ville Syrjälä432081b2016-10-31 22:37:03 +02005709 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005710
5711 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005712 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005713}
5714
Jani Nikulae2828912016-01-18 09:19:47 +02005715/*
Daniel Vetter92703882012-08-09 16:46:01 +02005716 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02005717 */
5718DEFINE_SPINLOCK(mchdev_lock);
5719
5720/* Global for IPS driver to get at the current i915 device. Protected by
5721 * mchdev_lock. */
5722static struct drm_i915_private *i915_mch_dev;
5723
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005724bool ironlake_set_drps(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005725{
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005726 u16 rgvswctl;
5727
Chris Wilson67520412017-03-02 13:28:01 +00005728 lockdep_assert_held(&mchdev_lock);
Daniel Vetter92703882012-08-09 16:46:01 +02005729
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005730 rgvswctl = I915_READ16(MEMSWCTL);
5731 if (rgvswctl & MEMCTL_CMD_STS) {
5732 DRM_DEBUG("gpu busy, RCS change rejected\n");
5733 return false; /* still busy with another command */
5734 }
5735
5736 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
5737 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
5738 I915_WRITE16(MEMSWCTL, rgvswctl);
5739 POSTING_READ16(MEMSWCTL);
5740
5741 rgvswctl |= MEMCTL_CMD_STS;
5742 I915_WRITE16(MEMSWCTL, rgvswctl);
5743
5744 return true;
5745}
5746
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005747static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005748{
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00005749 u32 rgvmodectl;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005750 u8 fmax, fmin, fstart, vstart;
5751
Daniel Vetter92703882012-08-09 16:46:01 +02005752 spin_lock_irq(&mchdev_lock);
5753
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00005754 rgvmodectl = I915_READ(MEMMODECTL);
5755
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005756 /* Enable temp reporting */
5757 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
5758 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
5759
5760 /* 100ms RC evaluation intervals */
5761 I915_WRITE(RCUPEI, 100000);
5762 I915_WRITE(RCDNEI, 100000);
5763
5764 /* Set max/min thresholds to 90ms and 80ms respectively */
5765 I915_WRITE(RCBMAXAVG, 90000);
5766 I915_WRITE(RCBMINAVG, 80000);
5767
5768 I915_WRITE(MEMIHYST, 1);
5769
5770 /* Set up min, max, and cur for interrupt handling */
5771 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
5772 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
5773 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
5774 MEMMODE_FSTART_SHIFT;
5775
Ville Syrjälä616847e2015-09-18 20:03:19 +03005776 vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005777 PXVFREQ_PX_SHIFT;
5778
Daniel Vetter20e4d402012-08-08 23:35:39 +02005779 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
5780 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005781
Daniel Vetter20e4d402012-08-08 23:35:39 +02005782 dev_priv->ips.max_delay = fstart;
5783 dev_priv->ips.min_delay = fmin;
5784 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005785
5786 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
5787 fmax, fmin, fstart);
5788
5789 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
5790
5791 /*
5792 * Interrupts will be enabled in ironlake_irq_postinstall
5793 */
5794
5795 I915_WRITE(VIDSTART, vstart);
5796 POSTING_READ(VIDSTART);
5797
5798 rgvmodectl |= MEMMODE_SWMODE_EN;
5799 I915_WRITE(MEMMODECTL, rgvmodectl);
5800
Daniel Vetter92703882012-08-09 16:46:01 +02005801 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005802 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005803 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005804
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005805 ironlake_set_drps(dev_priv, fstart);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005806
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03005807 dev_priv->ips.last_count1 = I915_READ(DMIEC) +
5808 I915_READ(DDREC) + I915_READ(CSIEC);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005809 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03005810 dev_priv->ips.last_count2 = I915_READ(GFXEC);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005811 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02005812
5813 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005814}
5815
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005816static void ironlake_disable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005817{
Daniel Vetter92703882012-08-09 16:46:01 +02005818 u16 rgvswctl;
5819
5820 spin_lock_irq(&mchdev_lock);
5821
5822 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005823
5824 /* Ack interrupts, disable EFC interrupt */
5825 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
5826 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
5827 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
5828 I915_WRITE(DEIIR, DE_PCU_EVENT);
5829 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
5830
5831 /* Go back to the starting frequency */
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005832 ironlake_set_drps(dev_priv, dev_priv->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005833 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005834 rgvswctl |= MEMCTL_CMD_STS;
5835 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005836 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005837
Daniel Vetter92703882012-08-09 16:46:01 +02005838 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005839}
5840
Daniel Vetteracbe9472012-07-26 11:50:05 +02005841/* There's a funny hw issue where the hw returns all 0 when reading from
5842 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
5843 * ourselves, instead of doing a rmw cycle (which might result in us clearing
5844 * all limits and the gpu stuck at whatever frequency it is at atm).
5845 */
Akash Goel74ef1172015-03-06 11:07:19 +05305846static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005847{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005848 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005849
Daniel Vetter20b46e52012-07-26 11:16:14 +02005850 /* Only set the down limit when we've reached the lowest level to avoid
5851 * getting more interrupts, otherwise leave this clear. This prevents a
5852 * race in the hw when coming out of rc6: There's a tiny window where
5853 * the hw runs at the minimal clock before selecting the desired
5854 * frequency, if the down threshold expires in that window we will not
5855 * receive a down interrupt. */
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07005856 if (INTEL_GEN(dev_priv) >= 9) {
Akash Goel74ef1172015-03-06 11:07:19 +05305857 limits = (dev_priv->rps.max_freq_softlimit) << 23;
5858 if (val <= dev_priv->rps.min_freq_softlimit)
5859 limits |= (dev_priv->rps.min_freq_softlimit) << 14;
5860 } else {
5861 limits = dev_priv->rps.max_freq_softlimit << 24;
5862 if (val <= dev_priv->rps.min_freq_softlimit)
5863 limits |= dev_priv->rps.min_freq_softlimit << 16;
5864 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02005865
5866 return limits;
5867}
5868
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005869static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
5870{
5871 int new_power;
Akash Goel8a586432015-03-06 11:07:18 +05305872 u32 threshold_up = 0, threshold_down = 0; /* in % */
5873 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005874
5875 new_power = dev_priv->rps.power;
5876 switch (dev_priv->rps.power) {
5877 case LOW_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01005878 if (val > dev_priv->rps.efficient_freq + 1 &&
5879 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005880 new_power = BETWEEN;
5881 break;
5882
5883 case BETWEEN:
Chris Wilsona72b5622016-07-02 15:35:59 +01005884 if (val <= dev_priv->rps.efficient_freq &&
5885 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005886 new_power = LOW_POWER;
Chris Wilsona72b5622016-07-02 15:35:59 +01005887 else if (val >= dev_priv->rps.rp0_freq &&
5888 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005889 new_power = HIGH_POWER;
5890 break;
5891
5892 case HIGH_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01005893 if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 &&
5894 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005895 new_power = BETWEEN;
5896 break;
5897 }
5898 /* Max/min bins are special */
Chris Wilsonaed242f2015-03-18 09:48:21 +00005899 if (val <= dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005900 new_power = LOW_POWER;
Chris Wilsonaed242f2015-03-18 09:48:21 +00005901 if (val >= dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005902 new_power = HIGH_POWER;
5903 if (new_power == dev_priv->rps.power)
5904 return;
5905
5906 /* Note the units here are not exactly 1us, but 1280ns. */
5907 switch (new_power) {
5908 case LOW_POWER:
5909 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05305910 ei_up = 16000;
5911 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005912
5913 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305914 ei_down = 32000;
5915 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005916 break;
5917
5918 case BETWEEN:
5919 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05305920 ei_up = 13000;
5921 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005922
5923 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305924 ei_down = 32000;
5925 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005926 break;
5927
5928 case HIGH_POWER:
5929 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05305930 ei_up = 10000;
5931 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005932
5933 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305934 ei_down = 32000;
5935 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005936 break;
5937 }
5938
Mika Kuoppala6067a272017-02-15 15:52:59 +02005939 /* When byt can survive without system hang with dynamic
5940 * sw freq adjustments, this restriction can be lifted.
5941 */
5942 if (IS_VALLEYVIEW(dev_priv))
5943 goto skip_hw_write;
5944
Akash Goel8a586432015-03-06 11:07:18 +05305945 I915_WRITE(GEN6_RP_UP_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01005946 GT_INTERVAL_FROM_US(dev_priv, ei_up));
Akash Goel8a586432015-03-06 11:07:18 +05305947 I915_WRITE(GEN6_RP_UP_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01005948 GT_INTERVAL_FROM_US(dev_priv,
5949 ei_up * threshold_up / 100));
Akash Goel8a586432015-03-06 11:07:18 +05305950
5951 I915_WRITE(GEN6_RP_DOWN_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01005952 GT_INTERVAL_FROM_US(dev_priv, ei_down));
Akash Goel8a586432015-03-06 11:07:18 +05305953 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01005954 GT_INTERVAL_FROM_US(dev_priv,
5955 ei_down * threshold_down / 100));
Akash Goel8a586432015-03-06 11:07:18 +05305956
Chris Wilsona72b5622016-07-02 15:35:59 +01005957 I915_WRITE(GEN6_RP_CONTROL,
5958 GEN6_RP_MEDIA_TURBO |
5959 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5960 GEN6_RP_MEDIA_IS_GFX |
5961 GEN6_RP_ENABLE |
5962 GEN6_RP_UP_BUSY_AVG |
5963 GEN6_RP_DOWN_IDLE_AVG);
Akash Goel8a586432015-03-06 11:07:18 +05305964
Mika Kuoppala6067a272017-02-15 15:52:59 +02005965skip_hw_write:
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005966 dev_priv->rps.power = new_power;
Chris Wilson8fb55192015-04-07 16:20:28 +01005967 dev_priv->rps.up_threshold = threshold_up;
5968 dev_priv->rps.down_threshold = threshold_down;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005969 dev_priv->rps.last_adj = 0;
5970}
5971
Chris Wilson2876ce72014-03-28 08:03:34 +00005972static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
5973{
5974 u32 mask = 0;
5975
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00005976 /* We use UP_EI_EXPIRED interupts for both up/down in manual mode */
Chris Wilson2876ce72014-03-28 08:03:34 +00005977 if (val > dev_priv->rps.min_freq_softlimit)
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00005978 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
Chris Wilson2876ce72014-03-28 08:03:34 +00005979 if (val < dev_priv->rps.max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00005980 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00005981
Chris Wilson7b3c29f2014-07-10 20:31:19 +01005982 mask &= dev_priv->pm_rps_events;
5983
Imre Deak59d02a12014-12-19 19:33:26 +02005984 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00005985}
5986
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06005987/* gen6_set_rps is called to update the frequency request, but should also be
5988 * called when the range (min_delay and max_delay) is modified so that we can
5989 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005990static int gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02005991{
Chris Wilsoneb64cad2014-03-27 08:24:20 +00005992 /* min/max delay may still have been modified so be sure to
5993 * write the limits value.
5994 */
5995 if (val != dev_priv->rps.cur_freq) {
5996 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06005997
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07005998 if (INTEL_GEN(dev_priv) >= 9)
Akash Goel57041952015-03-06 11:07:17 +05305999 I915_WRITE(GEN6_RPNSWREQ,
6000 GEN9_FREQUENCY(val));
Chris Wilsondc979972016-05-10 14:10:04 +01006001 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006002 I915_WRITE(GEN6_RPNSWREQ,
6003 HSW_FREQUENCY(val));
6004 else
6005 I915_WRITE(GEN6_RPNSWREQ,
6006 GEN6_FREQUENCY(val) |
6007 GEN6_OFFSET(0) |
6008 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006009 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006010
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006011 /* Make sure we continue to get interrupts
6012 * until we hit the minimum or maximum frequencies.
6013 */
Akash Goel74ef1172015-03-06 11:07:19 +05306014 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00006015 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006016
Ben Widawskyb39fb292014-03-19 18:31:11 -07006017 dev_priv->rps.cur_freq = val;
Mika Kuoppala0f945922015-11-17 18:14:26 +02006018 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006019
6020 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006021}
6022
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006023static int valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006024{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006025 int err;
6026
Chris Wilsondc979972016-05-10 14:10:04 +01006027 if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006028 "Odd GPU freq value\n"))
6029 val &= ~1;
6030
Deepak Scd25dd52015-07-10 18:31:40 +05306031 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
6032
Chris Wilson8fb55192015-04-07 16:20:28 +01006033 if (val != dev_priv->rps.cur_freq) {
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006034 err = vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
6035 if (err)
6036 return err;
6037
Chris Wilsondb4c5e02017-02-10 15:03:46 +00006038 gen6_set_rps_thresholds(dev_priv, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01006039 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006040
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006041 dev_priv->rps.cur_freq = val;
6042 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006043
6044 return 0;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006045}
6046
Deepak Sa7f6e232015-05-09 18:04:44 +05306047/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05306048 *
6049 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05306050 * 1. Forcewake Media well.
6051 * 2. Request idle freq.
6052 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05306053*/
6054static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
6055{
Chris Wilsonaed242f2015-03-18 09:48:21 +00006056 u32 val = dev_priv->rps.idle_freq;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006057 int err;
Deepak S5549d252014-06-28 11:26:11 +05306058
Chris Wilsonaed242f2015-03-18 09:48:21 +00006059 if (dev_priv->rps.cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05306060 return;
6061
Chris Wilsonc9efef72017-01-02 15:28:45 +00006062 /* The punit delays the write of the frequency and voltage until it
6063 * determines the GPU is awake. During normal usage we don't want to
6064 * waste power changing the frequency if the GPU is sleeping (rc6).
6065 * However, the GPU and driver is now idle and we do not want to delay
6066 * switching to minimum voltage (reducing power whilst idle) as we do
6067 * not expect to be woken in the near future and so must flush the
6068 * change by waking the device.
6069 *
6070 * We choose to take the media powerwell (either would do to trick the
6071 * punit into committing the voltage change) as that takes a lot less
6072 * power than the render powerwell.
6073 */
Deepak Sa7f6e232015-05-09 18:04:44 +05306074 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006075 err = valleyview_set_rps(dev_priv, val);
Deepak Sa7f6e232015-05-09 18:04:44 +05306076 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006077
6078 if (err)
6079 DRM_ERROR("Failed to set RPS for idle\n");
Deepak S76c3552f2014-01-30 23:08:16 +05306080}
6081
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006082void gen6_rps_busy(struct drm_i915_private *dev_priv)
6083{
6084 mutex_lock(&dev_priv->rps.hw_lock);
6085 if (dev_priv->rps.enabled) {
Chris Wilsonbd648182017-02-10 15:03:48 +00006086 u8 freq;
6087
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006088 if (dev_priv->pm_rps_events & GEN6_PM_RP_UP_EI_EXPIRED)
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006089 gen6_rps_reset_ei(dev_priv);
6090 I915_WRITE(GEN6_PMINTRMSK,
6091 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Michał Winiarski2b83c4c2016-06-20 11:58:27 +02006092
Chris Wilsonc33d2472016-07-04 08:08:36 +01006093 gen6_enable_rps_interrupts(dev_priv);
6094
Chris Wilsonbd648182017-02-10 15:03:48 +00006095 /* Use the user's desired frequency as a guide, but for better
6096 * performance, jump directly to RPe as our starting frequency.
6097 */
6098 freq = max(dev_priv->rps.cur_freq,
6099 dev_priv->rps.efficient_freq);
6100
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006101 if (intel_set_rps(dev_priv,
Chris Wilsonbd648182017-02-10 15:03:48 +00006102 clamp(freq,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006103 dev_priv->rps.min_freq_softlimit,
6104 dev_priv->rps.max_freq_softlimit)))
6105 DRM_DEBUG_DRIVER("Failed to set idle frequency\n");
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006106 }
6107 mutex_unlock(&dev_priv->rps.hw_lock);
6108}
6109
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006110void gen6_rps_idle(struct drm_i915_private *dev_priv)
6111{
Chris Wilsonc33d2472016-07-04 08:08:36 +01006112 /* Flush our bottom-half so that it does not race with us
6113 * setting the idle frequency and so that it is bounded by
6114 * our rpm wakeref. And then disable the interrupts to stop any
6115 * futher RPS reclocking whilst we are asleep.
6116 */
6117 gen6_disable_rps_interrupts(dev_priv);
6118
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006119 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01006120 if (dev_priv->rps.enabled) {
Chris Wilsondc979972016-05-10 14:10:04 +01006121 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Deepak S76c3552f2014-01-30 23:08:16 +05306122 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02006123 else
Chris Wilsondc979972016-05-10 14:10:04 +01006124 gen6_set_rps(dev_priv, dev_priv->rps.idle_freq);
Chris Wilsonc0951f02013-10-10 21:58:50 +01006125 dev_priv->rps.last_adj = 0;
Ville Syrjälä12c100b2016-05-23 17:42:48 +03006126 I915_WRITE(GEN6_PMINTRMSK,
6127 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Chris Wilsonc0951f02013-10-10 21:58:50 +01006128 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01006129 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006130}
6131
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006132void gen6_rps_boost(struct drm_i915_gem_request *rq,
6133 struct intel_rps_client *rps)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006134{
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006135 struct drm_i915_private *i915 = rq->i915;
6136 bool boost;
6137
Chris Wilson8d3afd72015-05-21 21:01:47 +01006138 /* This is intentionally racy! We peek at the state here, then
6139 * validate inside the RPS worker.
6140 */
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006141 if (!i915->rps.enabled)
Chris Wilson8d3afd72015-05-21 21:01:47 +01006142 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006143
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006144 boost = false;
6145 spin_lock_irq(&rq->lock);
6146 if (!rq->waitboost && !i915_gem_request_completed(rq)) {
6147 atomic_inc(&i915->rps.num_waiters);
6148 rq->waitboost = true;
6149 boost = true;
Chris Wilsonc0951f02013-10-10 21:58:50 +01006150 }
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006151 spin_unlock_irq(&rq->lock);
6152 if (!boost)
6153 return;
6154
6155 if (READ_ONCE(i915->rps.cur_freq) < i915->rps.boost_freq)
6156 schedule_work(&i915->rps.work);
6157
6158 atomic_inc(rps ? &rps->boosts : &i915->rps.boosts);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006159}
6160
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006161int intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006162{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006163 int err;
6164
Chris Wilsoncfd1c482017-02-20 09:47:07 +00006165 lockdep_assert_held(&dev_priv->rps.hw_lock);
6166 GEM_BUG_ON(val > dev_priv->rps.max_freq);
6167 GEM_BUG_ON(val < dev_priv->rps.min_freq);
6168
Chris Wilson76e4e4b2017-02-20 09:47:08 +00006169 if (!dev_priv->rps.enabled) {
6170 dev_priv->rps.cur_freq = val;
6171 return 0;
6172 }
6173
Chris Wilsondc979972016-05-10 14:10:04 +01006174 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006175 err = valleyview_set_rps(dev_priv, val);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006176 else
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006177 err = gen6_set_rps(dev_priv, val);
6178
6179 return err;
Jesse Barnes0a073b82013-04-17 15:54:58 -07006180}
6181
Chris Wilsondc979972016-05-10 14:10:04 +01006182static void gen9_disable_rc6(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00006183{
Zhe Wang20e49362014-11-04 17:07:05 +00006184 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00006185 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00006186}
6187
Chris Wilsondc979972016-05-10 14:10:04 +01006188static void gen9_disable_rps(struct drm_i915_private *dev_priv)
Akash Goel2030d682016-04-23 00:05:45 +05306189{
Akash Goel2030d682016-04-23 00:05:45 +05306190 I915_WRITE(GEN6_RP_CONTROL, 0);
6191}
6192
Chris Wilsondc979972016-05-10 14:10:04 +01006193static void gen6_disable_rps(struct drm_i915_private *dev_priv)
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006194{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006195 I915_WRITE(GEN6_RC_CONTROL, 0);
6196 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Akash Goel2030d682016-04-23 00:05:45 +05306197 I915_WRITE(GEN6_RP_CONTROL, 0);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006198}
6199
Chris Wilsondc979972016-05-10 14:10:04 +01006200static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306201{
Deepak S38807742014-05-23 21:00:15 +05306202 I915_WRITE(GEN6_RC_CONTROL, 0);
6203}
6204
Chris Wilsondc979972016-05-10 14:10:04 +01006205static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006206{
Deepak S98a2e5f2014-08-18 10:35:27 -07006207 /* we're doing forcewake before Disabling RC6,
6208 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02006209 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07006210
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006211 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006212
Mika Kuoppala59bad942015-01-16 11:34:40 +02006213 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006214}
6215
Chris Wilsondc979972016-05-10 14:10:04 +01006216static void intel_print_rc6_info(struct drm_i915_private *dev_priv, u32 mode)
Ben Widawskydc39fff2013-10-18 12:32:07 -07006217{
Chris Wilsondc979972016-05-10 14:10:04 +01006218 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Imre Deak91ca6892014-04-14 20:24:25 +03006219 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
6220 mode = GEN6_RC_CTL_RC6_ENABLE;
6221 else
6222 mode = 0;
6223 }
Chris Wilsondc979972016-05-10 14:10:04 +01006224 if (HAS_RC6p(dev_priv))
Imre Deakb99d49c2016-06-29 19:13:54 +03006225 DRM_DEBUG_DRIVER("Enabling RC6 states: "
6226 "RC6 %s RC6p %s RC6pp %s\n",
6227 onoff(mode & GEN6_RC_CTL_RC6_ENABLE),
6228 onoff(mode & GEN6_RC_CTL_RC6p_ENABLE),
6229 onoff(mode & GEN6_RC_CTL_RC6pp_ENABLE));
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07006230
6231 else
Imre Deakb99d49c2016-06-29 19:13:54 +03006232 DRM_DEBUG_DRIVER("Enabling RC6 states: RC6 %s\n",
6233 onoff(mode & GEN6_RC_CTL_RC6_ENABLE));
Ben Widawskydc39fff2013-10-18 12:32:07 -07006234}
6235
Chris Wilsondc979972016-05-10 14:10:04 +01006236static bool bxt_check_bios_rc6_setup(struct drm_i915_private *dev_priv)
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306237{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006238 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306239 bool enable_rc6 = true;
6240 unsigned long rc6_ctx_base;
Imre Deakfc619842016-06-29 19:13:55 +03006241 u32 rc_ctl;
6242 int rc_sw_target;
6243
6244 rc_ctl = I915_READ(GEN6_RC_CONTROL);
6245 rc_sw_target = (I915_READ(GEN6_RC_STATE) & RC_SW_TARGET_STATE_MASK) >>
6246 RC_SW_TARGET_STATE_SHIFT;
6247 DRM_DEBUG_DRIVER("BIOS enabled RC states: "
6248 "HW_CTRL %s HW_RC6 %s SW_TARGET_STATE %x\n",
6249 onoff(rc_ctl & GEN6_RC_CTL_HW_ENABLE),
6250 onoff(rc_ctl & GEN6_RC_CTL_RC6_ENABLE),
6251 rc_sw_target);
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306252
6253 if (!(I915_READ(RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006254 DRM_DEBUG_DRIVER("RC6 Base location not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306255 enable_rc6 = false;
6256 }
6257
6258 /*
6259 * The exact context size is not known for BXT, so assume a page size
6260 * for this check.
6261 */
6262 rc6_ctx_base = I915_READ(RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006263 if (!((rc6_ctx_base >= ggtt->stolen_reserved_base) &&
6264 (rc6_ctx_base + PAGE_SIZE <= ggtt->stolen_reserved_base +
6265 ggtt->stolen_reserved_size))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006266 DRM_DEBUG_DRIVER("RC6 Base address not as expected.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306267 enable_rc6 = false;
6268 }
6269
6270 if (!(((I915_READ(PWRCTX_MAXCNT_RCSUNIT) & IDLE_TIME_MASK) > 1) &&
6271 ((I915_READ(PWRCTX_MAXCNT_VCSUNIT0) & IDLE_TIME_MASK) > 1) &&
6272 ((I915_READ(PWRCTX_MAXCNT_BCSUNIT) & IDLE_TIME_MASK) > 1) &&
6273 ((I915_READ(PWRCTX_MAXCNT_VECSUNIT) & IDLE_TIME_MASK) > 1))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006274 DRM_DEBUG_DRIVER("Engine Idle wait time not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306275 enable_rc6 = false;
6276 }
6277
Imre Deakfc619842016-06-29 19:13:55 +03006278 if (!I915_READ(GEN8_PUSHBUS_CONTROL) ||
6279 !I915_READ(GEN8_PUSHBUS_ENABLE) ||
6280 !I915_READ(GEN8_PUSHBUS_SHIFT)) {
6281 DRM_DEBUG_DRIVER("Pushbus not setup properly.\n");
6282 enable_rc6 = false;
6283 }
6284
6285 if (!I915_READ(GEN6_GFXPAUSE)) {
6286 DRM_DEBUG_DRIVER("GFX pause not setup properly.\n");
6287 enable_rc6 = false;
6288 }
6289
6290 if (!I915_READ(GEN8_MISC_CTRL0)) {
6291 DRM_DEBUG_DRIVER("GPM control not setup properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306292 enable_rc6 = false;
6293 }
6294
6295 return enable_rc6;
6296}
6297
Chris Wilsondc979972016-05-10 14:10:04 +01006298int sanitize_rc6_option(struct drm_i915_private *dev_priv, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006299{
Daniel Vettere7d66d82015-06-15 23:23:54 +02006300 /* No RC6 before Ironlake and code is gone for ilk. */
Chris Wilsondc979972016-05-10 14:10:04 +01006301 if (INTEL_INFO(dev_priv)->gen < 6)
Imre Deake6069ca2014-04-18 16:01:02 +03006302 return 0;
6303
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306304 if (!enable_rc6)
6305 return 0;
6306
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02006307 if (IS_GEN9_LP(dev_priv) && !bxt_check_bios_rc6_setup(dev_priv)) {
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306308 DRM_INFO("RC6 disabled by BIOS\n");
6309 return 0;
6310 }
6311
Daniel Vetter456470e2012-08-08 23:35:40 +02006312 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03006313 if (enable_rc6 >= 0) {
6314 int mask;
6315
Chris Wilsondc979972016-05-10 14:10:04 +01006316 if (HAS_RC6p(dev_priv))
Imre Deake6069ca2014-04-18 16:01:02 +03006317 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
6318 INTEL_RC6pp_ENABLE;
6319 else
6320 mask = INTEL_RC6_ENABLE;
6321
6322 if ((enable_rc6 & mask) != enable_rc6)
Imre Deakb99d49c2016-06-29 19:13:54 +03006323 DRM_DEBUG_DRIVER("Adjusting RC6 mask to %d "
6324 "(requested %d, valid %d)\n",
6325 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03006326
6327 return enable_rc6 & mask;
6328 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006329
Chris Wilsondc979972016-05-10 14:10:04 +01006330 if (IS_IVYBRIDGE(dev_priv))
Ben Widawskycca84a12014-01-28 20:25:38 -08006331 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08006332
6333 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006334}
6335
Chris Wilsondc979972016-05-10 14:10:04 +01006336static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
Imre Deake6069ca2014-04-18 16:01:02 +03006337{
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006338 /* All of these values are in units of 50MHz */
Chris Wilson773ea9a2016-07-13 09:10:33 +01006339
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006340 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02006341 if (IS_GEN9_LP(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006342 u32 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07006343 dev_priv->rps.rp0_freq = (rp_state_cap >> 16) & 0xff;
6344 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
6345 dev_priv->rps.min_freq = (rp_state_cap >> 0) & 0xff;
6346 } else {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006347 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07006348 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
6349 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
6350 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
6351 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006352 /* hw_max = RP0 until we check for overclocking */
Chris Wilson773ea9a2016-07-13 09:10:33 +01006353 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006354
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006355 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01006356 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006357 IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006358 u32 ddcc_status = 0;
6359
6360 if (sandybridge_pcode_read(dev_priv,
6361 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
6362 &ddcc_status) == 0)
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006363 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08006364 clamp_t(u8,
6365 ((ddcc_status >> 8) & 0xff),
6366 dev_priv->rps.min_freq,
6367 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006368 }
6369
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006370 if (IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Akash Goelc5e06882015-06-29 14:50:19 +05306371 /* Store the frequency values in 16.66 MHZ units, which is
Chris Wilson773ea9a2016-07-13 09:10:33 +01006372 * the natural hardware unit for SKL
6373 */
Akash Goelc5e06882015-06-29 14:50:19 +05306374 dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
6375 dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
6376 dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
6377 dev_priv->rps.max_freq *= GEN9_FREQ_SCALER;
6378 dev_priv->rps.efficient_freq *= GEN9_FREQ_SCALER;
6379 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006380}
6381
Chris Wilson3a45b052016-07-13 09:10:32 +01006382static void reset_rps(struct drm_i915_private *dev_priv,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006383 int (*set)(struct drm_i915_private *, u8))
Chris Wilson3a45b052016-07-13 09:10:32 +01006384{
6385 u8 freq = dev_priv->rps.cur_freq;
6386
6387 /* force a reset */
6388 dev_priv->rps.power = -1;
6389 dev_priv->rps.cur_freq = -1;
6390
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006391 if (set(dev_priv, freq))
6392 DRM_ERROR("Failed to reset RPS to initial values\n");
Chris Wilson3a45b052016-07-13 09:10:32 +01006393}
6394
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006395/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Chris Wilsondc979972016-05-10 14:10:04 +01006396static void gen9_enable_rps(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00006397{
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006398 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
6399
Akash Goel0beb0592015-03-06 11:07:20 +05306400 /* Program defaults and thresholds for RPS*/
6401 I915_WRITE(GEN6_RC_VIDEO_FREQ,
6402 GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006403
Akash Goel0beb0592015-03-06 11:07:20 +05306404 /* 1 second timeout*/
6405 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
6406 GT_INTERVAL_FROM_US(dev_priv, 1000000));
6407
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006408 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006409
Akash Goel0beb0592015-03-06 11:07:20 +05306410 /* Leaning on the below call to gen6_set_rps to program/setup the
6411 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
6412 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
Chris Wilson3a45b052016-07-13 09:10:32 +01006413 reset_rps(dev_priv, gen6_set_rps);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006414
6415 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
6416}
6417
Chris Wilsondc979972016-05-10 14:10:04 +01006418static void gen9_enable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006419{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006420 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306421 enum intel_engine_id id;
Zhe Wang20e49362014-11-04 17:07:05 +00006422 uint32_t rc6_mask = 0;
Zhe Wang20e49362014-11-04 17:07:05 +00006423
6424 /* 1a: Software RC state - RC0 */
6425 I915_WRITE(GEN6_RC_STATE, 0);
6426
6427 /* 1b: Get forcewake during program sequence. Although the driver
6428 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006429 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00006430
6431 /* 2a: Disable RC states. */
6432 I915_WRITE(GEN6_RC_CONTROL, 0);
6433
6434 /* 2b: Program RC6 thresholds.*/
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05306435
6436 /* WaRsDoubleRc6WrlWithCoarsePowerGating: Doubling WRL only when CPG is enabled */
Chris Wilsondc979972016-05-10 14:10:04 +01006437 if (IS_SKYLAKE(dev_priv))
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05306438 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
6439 else
6440 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
Zhe Wang20e49362014-11-04 17:07:05 +00006441 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6442 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05306443 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006444 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05306445
Dave Gordon1a3d1892016-05-13 15:36:30 +01006446 if (HAS_GUC(dev_priv))
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05306447 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
6448
Zhe Wang20e49362014-11-04 17:07:05 +00006449 I915_WRITE(GEN6_RC_SLEEP, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00006450
Zhe Wang38c23522015-01-20 12:23:04 +00006451 /* 2c: Program Coarse Power Gating Policies. */
6452 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
6453 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
6454
Zhe Wang20e49362014-11-04 17:07:05 +00006455 /* 3a: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006456 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Zhe Wang20e49362014-11-04 17:07:05 +00006457 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Jani Nikula87ad3212016-01-14 12:53:34 +02006458 DRM_INFO("RC6 %s\n", onoff(rc6_mask & GEN6_RC_CTL_RC6_ENABLE));
Chris Wilson1c044f92017-01-25 17:26:01 +00006459 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
6460 I915_WRITE(GEN6_RC_CONTROL,
6461 GEN6_RC_CTL_HW_ENABLE | GEN6_RC_CTL_EI_MODE(1) | rc6_mask);
Zhe Wang20e49362014-11-04 17:07:05 +00006462
Sagar Kamblecb07bae2015-04-12 11:28:14 +05306463 /*
6464 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05306465 * WaRsDisableCoarsePowerGating:skl,bxt - Render/Media PG need to be disabled with RC6.
Sagar Kamblecb07bae2015-04-12 11:28:14 +05306466 */
Chris Wilsondc979972016-05-10 14:10:04 +01006467 if (NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05306468 I915_WRITE(GEN9_PG_ENABLE, 0);
6469 else
6470 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
6471 (GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE) : 0);
Zhe Wang38c23522015-01-20 12:23:04 +00006472
Mika Kuoppala59bad942015-01-16 11:34:40 +02006473 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00006474}
6475
Chris Wilsondc979972016-05-10 14:10:04 +01006476static void gen8_enable_rps(struct drm_i915_private *dev_priv)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006477{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006478 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306479 enum intel_engine_id id;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006480 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006481
6482 /* 1a: Software RC state - RC0 */
6483 I915_WRITE(GEN6_RC_STATE, 0);
6484
6485 /* 1c & 1d: Get forcewake during program sequence. Although the driver
6486 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006487 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006488
6489 /* 2a: Disable RC states. */
6490 I915_WRITE(GEN6_RC_CONTROL, 0);
6491
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006492 /* 2b: Program RC6 thresholds.*/
6493 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
6494 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6495 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05306496 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006497 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006498 I915_WRITE(GEN6_RC_SLEEP, 0);
Chris Wilsondc979972016-05-10 14:10:04 +01006499 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07006500 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
6501 else
6502 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006503
6504 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006505 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006506 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Chris Wilsondc979972016-05-10 14:10:04 +01006507 intel_print_rc6_info(dev_priv, rc6_mask);
6508 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07006509 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
6510 GEN7_RC_CTL_TO_MODE |
6511 rc6_mask);
6512 else
6513 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
6514 GEN6_RC_CTL_EI_MODE(1) |
6515 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006516
6517 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07006518 I915_WRITE(GEN6_RPNSWREQ,
6519 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
6520 I915_WRITE(GEN6_RC_VIDEO_FREQ,
6521 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02006522 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
6523 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006524
Daniel Vetter7526ed72014-09-29 15:07:19 +02006525 /* Docs recommend 900MHz, and 300 MHz respectively */
6526 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
6527 dev_priv->rps.max_freq_softlimit << 24 |
6528 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006529
Daniel Vetter7526ed72014-09-29 15:07:19 +02006530 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
6531 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
6532 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
6533 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006534
Daniel Vetter7526ed72014-09-29 15:07:19 +02006535 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006536
6537 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02006538 I915_WRITE(GEN6_RP_CONTROL,
6539 GEN6_RP_MEDIA_TURBO |
6540 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6541 GEN6_RP_MEDIA_IS_GFX |
6542 GEN6_RP_ENABLE |
6543 GEN6_RP_UP_BUSY_AVG |
6544 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006545
Daniel Vetter7526ed72014-09-29 15:07:19 +02006546 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006547
Chris Wilson3a45b052016-07-13 09:10:32 +01006548 reset_rps(dev_priv, gen6_set_rps);
Daniel Vetter7526ed72014-09-29 15:07:19 +02006549
Mika Kuoppala59bad942015-01-16 11:34:40 +02006550 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006551}
6552
Chris Wilsondc979972016-05-10 14:10:04 +01006553static void gen6_enable_rps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006554{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006555 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306556 enum intel_engine_id id;
Chris Wilson99ac9612016-07-13 09:10:34 +01006557 u32 rc6vids, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006558 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006559 int rc6_mode;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00006560 int ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006561
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006562 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02006563
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006564 /* Here begins a magic sequence of register writes to enable
6565 * auto-downclocking.
6566 *
6567 * Perhaps there might be some value in exposing these to
6568 * userspace...
6569 */
6570 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006571
6572 /* Clear the DBG now so we don't confuse earlier errors */
Ville Syrjälä297b32e2016-04-13 21:09:30 +03006573 gtfifodbg = I915_READ(GTFIFODBG);
6574 if (gtfifodbg) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006575 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
6576 I915_WRITE(GTFIFODBG, gtfifodbg);
6577 }
6578
Mika Kuoppala59bad942015-01-16 11:34:40 +02006579 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006580
6581 /* disable the counters and set deterministic thresholds */
6582 I915_WRITE(GEN6_RC_CONTROL, 0);
6583
6584 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
6585 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
6586 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
6587 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
6588 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
6589
Akash Goel3b3f1652016-10-13 22:44:48 +05306590 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006591 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006592
6593 I915_WRITE(GEN6_RC_SLEEP, 0);
6594 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Chris Wilsondc979972016-05-10 14:10:04 +01006595 if (IS_IVYBRIDGE(dev_priv))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07006596 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
6597 else
6598 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08006599 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006600 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
6601
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006602 /* Check if we are enabling RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006603 rc6_mode = intel_enable_rc6();
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006604 if (rc6_mode & INTEL_RC6_ENABLE)
6605 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
6606
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006607 /* We don't use those on Haswell */
Chris Wilsondc979972016-05-10 14:10:04 +01006608 if (!IS_HASWELL(dev_priv)) {
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006609 if (rc6_mode & INTEL_RC6p_ENABLE)
6610 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006611
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006612 if (rc6_mode & INTEL_RC6pp_ENABLE)
6613 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
6614 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006615
Chris Wilsondc979972016-05-10 14:10:04 +01006616 intel_print_rc6_info(dev_priv, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006617
6618 I915_WRITE(GEN6_RC_CONTROL,
6619 rc6_mask |
6620 GEN6_RC_CTL_EI_MODE(1) |
6621 GEN6_RC_CTL_HW_ENABLE);
6622
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006623 /* Power down if completely idle for over 50ms */
6624 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006625 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006626
Chris Wilson3a45b052016-07-13 09:10:32 +01006627 reset_rps(dev_priv, gen6_set_rps);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006628
Ben Widawsky31643d52012-09-26 10:34:01 -07006629 rc6vids = 0;
6630 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
Chris Wilsondc979972016-05-10 14:10:04 +01006631 if (IS_GEN6(dev_priv) && ret) {
Ben Widawsky31643d52012-09-26 10:34:01 -07006632 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
Chris Wilsondc979972016-05-10 14:10:04 +01006633 } else if (IS_GEN6(dev_priv) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
Ben Widawsky31643d52012-09-26 10:34:01 -07006634 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
6635 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
6636 rc6vids &= 0xffff00;
6637 rc6vids |= GEN6_ENCODE_RC6_VID(450);
6638 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
6639 if (ret)
6640 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
6641 }
6642
Mika Kuoppala59bad942015-01-16 11:34:40 +02006643 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006644}
6645
Chris Wilsonfb7404e2016-07-13 09:10:38 +01006646static void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006647{
6648 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01006649 unsigned int gpu_freq;
6650 unsigned int max_ia_freq, min_ring_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05306651 unsigned int max_gpu_freq, min_gpu_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006652 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03006653 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006654
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006655 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02006656
Ben Widawskyeda79642013-10-07 17:15:48 -03006657 policy = cpufreq_cpu_get(0);
6658 if (policy) {
6659 max_ia_freq = policy->cpuinfo.max_freq;
6660 cpufreq_cpu_put(policy);
6661 } else {
6662 /*
6663 * Default to measured freq if none found, PCU will ensure we
6664 * don't go over
6665 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006666 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03006667 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006668
6669 /* Convert from kHz to MHz */
6670 max_ia_freq /= 1000;
6671
Ben Widawsky153b4b952013-10-22 22:05:09 -07006672 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07006673 /* convert DDR frequency from units of 266.6MHz to bandwidth */
6674 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01006675
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006676 if (IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05306677 /* Convert GT frequency to 50 HZ units */
6678 min_gpu_freq = dev_priv->rps.min_freq / GEN9_FREQ_SCALER;
6679 max_gpu_freq = dev_priv->rps.max_freq / GEN9_FREQ_SCALER;
6680 } else {
6681 min_gpu_freq = dev_priv->rps.min_freq;
6682 max_gpu_freq = dev_priv->rps.max_freq;
6683 }
6684
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006685 /*
6686 * For each potential GPU frequency, load a ring frequency we'd like
6687 * to use for memory access. We do this by specifying the IA frequency
6688 * the PCU should use as a reference to determine the ring frequency.
6689 */
Akash Goel4c8c7742015-06-29 14:50:20 +05306690 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
6691 int diff = max_gpu_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01006692 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006693
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006694 if (IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05306695 /*
6696 * ring_freq = 2 * GT. ring_freq is in 100MHz units
6697 * No floor required for ring frequency on SKL.
6698 */
6699 ring_freq = gpu_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01006700 } else if (INTEL_INFO(dev_priv)->gen >= 8) {
Ben Widawsky46c764d2013-11-02 21:07:49 -07006701 /* max(2 * GT, DDR). NB: GT is 50MHz units */
6702 ring_freq = max(min_ring_freq, gpu_freq);
Chris Wilsondc979972016-05-10 14:10:04 +01006703 } else if (IS_HASWELL(dev_priv)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07006704 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01006705 ring_freq = max(min_ring_freq, ring_freq);
6706 /* leave ia_freq as the default, chosen by cpufreq */
6707 } else {
6708 /* On older processors, there is no separate ring
6709 * clock domain, so in order to boost the bandwidth
6710 * of the ring, we need to upclock the CPU (ia_freq).
6711 *
6712 * For GPU frequencies less than 750MHz,
6713 * just use the lowest ring freq.
6714 */
6715 if (gpu_freq < min_freq)
6716 ia_freq = 800;
6717 else
6718 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
6719 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
6720 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006721
Ben Widawsky42c05262012-09-26 10:34:00 -07006722 sandybridge_pcode_write(dev_priv,
6723 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01006724 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
6725 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
6726 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006727 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006728}
6729
Ville Syrjälä03af2042014-06-28 02:03:53 +03006730static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05306731{
6732 u32 val, rp0;
6733
Jani Nikula5b5929c2015-10-07 11:17:46 +03006734 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05306735
Imre Deak43b67992016-08-31 19:13:02 +03006736 switch (INTEL_INFO(dev_priv)->sseu.eu_total) {
Jani Nikula5b5929c2015-10-07 11:17:46 +03006737 case 8:
6738 /* (2 * 4) config */
6739 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
6740 break;
6741 case 12:
6742 /* (2 * 6) config */
6743 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
6744 break;
6745 case 16:
6746 /* (2 * 8) config */
6747 default:
6748 /* Setting (2 * 8) Min RP0 for any other combination */
6749 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
6750 break;
Deepak S095acd52015-01-17 11:05:59 +05306751 }
Jani Nikula5b5929c2015-10-07 11:17:46 +03006752
6753 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
6754
Deepak S2b6b3a02014-05-27 15:59:30 +05306755 return rp0;
6756}
6757
6758static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
6759{
6760 u32 val, rpe;
6761
6762 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
6763 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
6764
6765 return rpe;
6766}
6767
Deepak S7707df42014-07-12 18:46:14 +05306768static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
6769{
6770 u32 val, rp1;
6771
Jani Nikula5b5929c2015-10-07 11:17:46 +03006772 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
6773 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
6774
Deepak S7707df42014-07-12 18:46:14 +05306775 return rp1;
6776}
6777
Deepak S96676fe2016-08-12 18:46:41 +05306778static u32 cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
6779{
6780 u32 val, rpn;
6781
6782 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
6783 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
6784 FB_GFX_FREQ_FUSE_MASK);
6785
6786 return rpn;
6787}
6788
Deepak Sf8f2b002014-07-10 13:16:21 +05306789static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
6790{
6791 u32 val, rp1;
6792
6793 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
6794
6795 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
6796
6797 return rp1;
6798}
6799
Ville Syrjälä03af2042014-06-28 02:03:53 +03006800static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006801{
6802 u32 val, rp0;
6803
Jani Nikula64936252013-05-22 15:36:20 +03006804 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006805
6806 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
6807 /* Clamp to max */
6808 rp0 = min_t(u32, rp0, 0xea);
6809
6810 return rp0;
6811}
6812
6813static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
6814{
6815 u32 val, rpe;
6816
Jani Nikula64936252013-05-22 15:36:20 +03006817 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006818 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03006819 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006820 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
6821
6822 return rpe;
6823}
6824
Ville Syrjälä03af2042014-06-28 02:03:53 +03006825static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006826{
Imre Deak36146032014-12-04 18:39:35 +02006827 u32 val;
6828
6829 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
6830 /*
6831 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
6832 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
6833 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
6834 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
6835 * to make sure it matches what Punit accepts.
6836 */
6837 return max_t(u32, val, 0xc0);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006838}
6839
Imre Deakae484342014-03-31 15:10:44 +03006840/* Check that the pctx buffer wasn't move under us. */
6841static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
6842{
6843 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
6844
6845 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
6846 dev_priv->vlv_pctx->stolen->start);
6847}
6848
Deepak S38807742014-05-23 21:00:15 +05306849
6850/* Check that the pcbr address is not empty. */
6851static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
6852{
6853 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
6854
6855 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
6856}
6857
Chris Wilsondc979972016-05-10 14:10:04 +01006858static void cherryview_setup_pctx(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306859{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02006860 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006861 unsigned long pctx_paddr, paddr;
Deepak S38807742014-05-23 21:00:15 +05306862 u32 pcbr;
6863 int pctx_size = 32*1024;
6864
Deepak S38807742014-05-23 21:00:15 +05306865 pcbr = I915_READ(VLV_PCBR);
6866 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006867 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05306868 paddr = (dev_priv->mm.stolen_base +
Joonas Lahtinen62106b42016-03-18 10:42:57 +02006869 (ggtt->stolen_size - pctx_size));
Deepak S38807742014-05-23 21:00:15 +05306870
6871 pctx_paddr = (paddr & (~4095));
6872 I915_WRITE(VLV_PCBR, pctx_paddr);
6873 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006874
6875 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05306876}
6877
Chris Wilsondc979972016-05-10 14:10:04 +01006878static void valleyview_setup_pctx(struct drm_i915_private *dev_priv)
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006879{
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006880 struct drm_i915_gem_object *pctx;
6881 unsigned long pctx_paddr;
6882 u32 pcbr;
6883 int pctx_size = 24*1024;
6884
6885 pcbr = I915_READ(VLV_PCBR);
6886 if (pcbr) {
6887 /* BIOS set it up already, grab the pre-alloc'd space */
6888 int pcbr_offset;
6889
6890 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00006891 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006892 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02006893 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006894 pctx_size);
6895 goto out;
6896 }
6897
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006898 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
6899
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006900 /*
6901 * From the Gunit register HAS:
6902 * The Gfx driver is expected to program this register and ensure
6903 * proper allocation within Gfx stolen memory. For example, this
6904 * register should be programmed such than the PCBR range does not
6905 * overlap with other ranges, such as the frame buffer, protected
6906 * memory, or any other relevant ranges.
6907 */
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00006908 pctx = i915_gem_object_create_stolen(dev_priv, pctx_size);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006909 if (!pctx) {
6910 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
Tvrtko Ursulinee504892016-02-11 10:27:30 +00006911 goto out;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006912 }
6913
6914 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
6915 I915_WRITE(VLV_PCBR, pctx_paddr);
6916
6917out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006918 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006919 dev_priv->vlv_pctx = pctx;
6920}
6921
Chris Wilsondc979972016-05-10 14:10:04 +01006922static void valleyview_cleanup_pctx(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006923{
Imre Deakae484342014-03-31 15:10:44 +03006924 if (WARN_ON(!dev_priv->vlv_pctx))
6925 return;
6926
Chris Wilsonf0cd5182016-10-28 13:58:43 +01006927 i915_gem_object_put(dev_priv->vlv_pctx);
Imre Deakae484342014-03-31 15:10:44 +03006928 dev_priv->vlv_pctx = NULL;
6929}
6930
Ville Syrjäläc30fec62016-03-04 21:43:02 +02006931static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
6932{
6933 dev_priv->rps.gpll_ref_freq =
6934 vlv_get_cck_clock(dev_priv, "GPLL ref",
6935 CCK_GPLL_CLOCK_CONTROL,
6936 dev_priv->czclk_freq);
6937
6938 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
6939 dev_priv->rps.gpll_ref_freq);
6940}
6941
Chris Wilsondc979972016-05-10 14:10:04 +01006942static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03006943{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006944 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03006945
Chris Wilsondc979972016-05-10 14:10:04 +01006946 valleyview_setup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03006947
Ville Syrjäläc30fec62016-03-04 21:43:02 +02006948 vlv_init_gpll_ref_freq(dev_priv);
6949
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006950 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
6951 switch ((val >> 6) & 3) {
6952 case 0:
6953 case 1:
6954 dev_priv->mem_freq = 800;
6955 break;
6956 case 2:
6957 dev_priv->mem_freq = 1066;
6958 break;
6959 case 3:
6960 dev_priv->mem_freq = 1333;
6961 break;
6962 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02006963 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006964
Imre Deak4e805192014-04-14 20:24:41 +03006965 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
6966 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
6967 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006968 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03006969 dev_priv->rps.max_freq);
6970
6971 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
6972 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006973 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03006974 dev_priv->rps.efficient_freq);
6975
Deepak Sf8f2b002014-07-10 13:16:21 +05306976 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
6977 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006978 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05306979 dev_priv->rps.rp1_freq);
6980
Imre Deak4e805192014-04-14 20:24:41 +03006981 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
6982 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006983 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03006984 dev_priv->rps.min_freq);
Imre Deak4e805192014-04-14 20:24:41 +03006985}
6986
Chris Wilsondc979972016-05-10 14:10:04 +01006987static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306988{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006989 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05306990
Chris Wilsondc979972016-05-10 14:10:04 +01006991 cherryview_setup_pctx(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05306992
Ville Syrjäläc30fec62016-03-04 21:43:02 +02006993 vlv_init_gpll_ref_freq(dev_priv);
6994
Ville Syrjäläa5805162015-05-26 20:42:30 +03006995 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02006996 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006997 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02006998
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006999 switch ((val >> 2) & 0x7) {
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007000 case 3:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007001 dev_priv->mem_freq = 2000;
7002 break;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03007003 default:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007004 dev_priv->mem_freq = 1600;
7005 break;
7006 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02007007 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007008
Deepak S2b6b3a02014-05-27 15:59:30 +05307009 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
7010 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
7011 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007012 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05307013 dev_priv->rps.max_freq);
7014
7015 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
7016 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007017 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05307018 dev_priv->rps.efficient_freq);
7019
Deepak S7707df42014-07-12 18:46:14 +05307020 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
7021 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007022 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05307023 dev_priv->rps.rp1_freq);
7024
Deepak S96676fe2016-08-12 18:46:41 +05307025 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307026 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007027 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05307028 dev_priv->rps.min_freq);
7029
Ville Syrjälä1c147622014-08-18 14:42:43 +03007030 WARN_ONCE((dev_priv->rps.max_freq |
7031 dev_priv->rps.efficient_freq |
7032 dev_priv->rps.rp1_freq |
7033 dev_priv->rps.min_freq) & 1,
7034 "Odd GPU freq values\n");
Deepak S38807742014-05-23 21:00:15 +05307035}
7036
Chris Wilsondc979972016-05-10 14:10:04 +01007037static void valleyview_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03007038{
Chris Wilsondc979972016-05-10 14:10:04 +01007039 valleyview_cleanup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007040}
7041
Chris Wilsondc979972016-05-10 14:10:04 +01007042static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05307043{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007044 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05307045 enum intel_engine_id id;
Deepak S2b6b3a02014-05-27 15:59:30 +05307046 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05307047
7048 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
7049
Ville Syrjälä297b32e2016-04-13 21:09:30 +03007050 gtfifodbg = I915_READ(GTFIFODBG) & ~(GT_FIFO_SBDEDICATE_FREE_ENTRY_CHV |
7051 GT_FIFO_FREE_ENTRIES_CHV);
Deepak S38807742014-05-23 21:00:15 +05307052 if (gtfifodbg) {
7053 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
7054 gtfifodbg);
7055 I915_WRITE(GTFIFODBG, gtfifodbg);
7056 }
7057
7058 cherryview_check_pctx(dev_priv);
7059
7060 /* 1a & 1b: Get forcewake during program sequence. Although the driver
7061 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02007062 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05307063
Ville Syrjälä160614a2015-01-19 13:50:47 +02007064 /* Disable RC states. */
7065 I915_WRITE(GEN6_RC_CONTROL, 0);
7066
Deepak S38807742014-05-23 21:00:15 +05307067 /* 2a: Program RC6 thresholds.*/
7068 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
7069 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
7070 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
7071
Akash Goel3b3f1652016-10-13 22:44:48 +05307072 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007073 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Deepak S38807742014-05-23 21:00:15 +05307074 I915_WRITE(GEN6_RC_SLEEP, 0);
7075
Deepak Sf4f71c72015-03-28 15:23:35 +05307076 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
7077 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
Deepak S38807742014-05-23 21:00:15 +05307078
7079 /* allows RC6 residency counter to work */
7080 I915_WRITE(VLV_COUNTER_CONTROL,
7081 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
7082 VLV_MEDIA_RC6_COUNT_EN |
7083 VLV_RENDER_RC6_COUNT_EN));
7084
7085 /* For now we assume BIOS is allocating and populating the PCBR */
7086 pcbr = I915_READ(VLV_PCBR);
7087
Deepak S38807742014-05-23 21:00:15 +05307088 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01007089 if ((intel_enable_rc6() & INTEL_RC6_ENABLE) &&
7090 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02007091 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05307092
7093 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
7094
Deepak S2b6b3a02014-05-27 15:59:30 +05307095 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02007096 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05307097 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
7098 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
7099 I915_WRITE(GEN6_RP_UP_EI, 66000);
7100 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
7101
7102 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7103
7104 /* 5: Enable RPS */
7105 I915_WRITE(GEN6_RP_CONTROL,
7106 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02007107 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05307108 GEN6_RP_ENABLE |
7109 GEN6_RP_UP_BUSY_AVG |
7110 GEN6_RP_DOWN_IDLE_AVG);
7111
Deepak S3ef62342015-04-29 08:36:24 +05307112 /* Setting Fixed Bias */
7113 val = VLV_OVERRIDE_EN |
7114 VLV_SOC_TDP_EN |
7115 CHV_BIAS_CPU_50_SOC_50;
7116 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
7117
Deepak S2b6b3a02014-05-27 15:59:30 +05307118 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
7119
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02007120 /* RPS code assumes GPLL is used */
7121 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
7122
Jani Nikula742f4912015-09-03 11:16:09 +03007123 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05307124 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7125
Chris Wilson3a45b052016-07-13 09:10:32 +01007126 reset_rps(dev_priv, valleyview_set_rps);
Deepak S2b6b3a02014-05-27 15:59:30 +05307127
Mika Kuoppala59bad942015-01-16 11:34:40 +02007128 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05307129}
7130
Chris Wilsondc979972016-05-10 14:10:04 +01007131static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07007132{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007133 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05307134 enum intel_engine_id id;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07007135 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07007136
7137 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
7138
Imre Deakae484342014-03-31 15:10:44 +03007139 valleyview_check_pctx(dev_priv);
7140
Ville Syrjälä297b32e2016-04-13 21:09:30 +03007141 gtfifodbg = I915_READ(GTFIFODBG);
7142 if (gtfifodbg) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07007143 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
7144 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007145 I915_WRITE(GTFIFODBG, gtfifodbg);
7146 }
7147
Deepak Sc8d9a592013-11-23 14:55:42 +05307148 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02007149 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007150
Ville Syrjälä160614a2015-01-19 13:50:47 +02007151 /* Disable RC states. */
7152 I915_WRITE(GEN6_RC_CONTROL, 0);
7153
Ville Syrjäläcad725f2015-01-19 13:50:48 +02007154 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007155 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
7156 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
7157 I915_WRITE(GEN6_RP_UP_EI, 66000);
7158 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
7159
7160 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7161
7162 I915_WRITE(GEN6_RP_CONTROL,
7163 GEN6_RP_MEDIA_TURBO |
7164 GEN6_RP_MEDIA_HW_NORMAL_MODE |
7165 GEN6_RP_MEDIA_IS_GFX |
7166 GEN6_RP_ENABLE |
7167 GEN6_RP_UP_BUSY_AVG |
7168 GEN6_RP_DOWN_IDLE_CONT);
7169
7170 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
7171 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
7172 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
7173
Akash Goel3b3f1652016-10-13 22:44:48 +05307174 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007175 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007176
Jesse Barnes2f0aa3042013-11-15 09:32:11 -08007177 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007178
7179 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07007180 I915_WRITE(VLV_COUNTER_CONTROL,
Mika Kuoppala6b7f6aa2017-03-15 18:12:59 +02007181 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
7182 VLV_MEDIA_RC0_COUNT_EN |
Deepak S31685c22014-07-03 17:33:01 -04007183 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07007184 VLV_MEDIA_RC6_COUNT_EN |
7185 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04007186
Chris Wilsondc979972016-05-10 14:10:04 +01007187 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08007188 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07007189
Chris Wilsondc979972016-05-10 14:10:04 +01007190 intel_print_rc6_info(dev_priv, rc6_mode);
Ben Widawskydc39fff2013-10-18 12:32:07 -07007191
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07007192 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007193
Deepak S3ef62342015-04-29 08:36:24 +05307194 /* Setting Fixed Bias */
7195 val = VLV_OVERRIDE_EN |
7196 VLV_SOC_TDP_EN |
7197 VLV_BIAS_CPU_125_SOC_875;
7198 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
7199
Jani Nikula64936252013-05-22 15:36:20 +03007200 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007201
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02007202 /* RPS code assumes GPLL is used */
7203 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
7204
Jani Nikula742f4912015-09-03 11:16:09 +03007205 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07007206 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7207
Chris Wilson3a45b052016-07-13 09:10:32 +01007208 reset_rps(dev_priv, valleyview_set_rps);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007209
Mika Kuoppala59bad942015-01-16 11:34:40 +02007210 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007211}
7212
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007213static unsigned long intel_pxfreq(u32 vidfreq)
7214{
7215 unsigned long freq;
7216 int div = (vidfreq & 0x3f0000) >> 16;
7217 int post = (vidfreq & 0x3000) >> 12;
7218 int pre = (vidfreq & 0x7);
7219
7220 if (!pre)
7221 return 0;
7222
7223 freq = ((div * 133333) / ((1<<post) * pre));
7224
7225 return freq;
7226}
7227
Daniel Vettereb48eb02012-04-26 23:28:12 +02007228static const struct cparams {
7229 u16 i;
7230 u16 t;
7231 u16 m;
7232 u16 c;
7233} cparams[] = {
7234 { 1, 1333, 301, 28664 },
7235 { 1, 1066, 294, 24460 },
7236 { 1, 800, 294, 25192 },
7237 { 0, 1333, 276, 27605 },
7238 { 0, 1066, 276, 27605 },
7239 { 0, 800, 231, 23784 },
7240};
7241
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007242static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007243{
7244 u64 total_count, diff, ret;
7245 u32 count1, count2, count3, m = 0, c = 0;
7246 unsigned long now = jiffies_to_msecs(jiffies), diff1;
7247 int i;
7248
Chris Wilson67520412017-03-02 13:28:01 +00007249 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007250
Daniel Vetter20e4d402012-08-08 23:35:39 +02007251 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007252
7253 /* Prevent division-by-zero if we are asking too fast.
7254 * Also, we don't get interesting results if we are polling
7255 * faster than once in 10ms, so just return the saved value
7256 * in such cases.
7257 */
7258 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02007259 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007260
7261 count1 = I915_READ(DMIEC);
7262 count2 = I915_READ(DDREC);
7263 count3 = I915_READ(CSIEC);
7264
7265 total_count = count1 + count2 + count3;
7266
7267 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02007268 if (total_count < dev_priv->ips.last_count1) {
7269 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007270 diff += total_count;
7271 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007272 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007273 }
7274
7275 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007276 if (cparams[i].i == dev_priv->ips.c_m &&
7277 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02007278 m = cparams[i].m;
7279 c = cparams[i].c;
7280 break;
7281 }
7282 }
7283
7284 diff = div_u64(diff, diff1);
7285 ret = ((m * diff) + c);
7286 ret = div_u64(ret, 10);
7287
Daniel Vetter20e4d402012-08-08 23:35:39 +02007288 dev_priv->ips.last_count1 = total_count;
7289 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007290
Daniel Vetter20e4d402012-08-08 23:35:39 +02007291 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007292
7293 return ret;
7294}
7295
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007296unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
7297{
7298 unsigned long val;
7299
Chris Wilsondc979972016-05-10 14:10:04 +01007300 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007301 return 0;
7302
7303 spin_lock_irq(&mchdev_lock);
7304
7305 val = __i915_chipset_val(dev_priv);
7306
7307 spin_unlock_irq(&mchdev_lock);
7308
7309 return val;
7310}
7311
Daniel Vettereb48eb02012-04-26 23:28:12 +02007312unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
7313{
7314 unsigned long m, x, b;
7315 u32 tsfs;
7316
7317 tsfs = I915_READ(TSFS);
7318
7319 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
7320 x = I915_READ8(TR1);
7321
7322 b = tsfs & TSFS_INTR_MASK;
7323
7324 return ((m * x) / 127) - b;
7325}
7326
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007327static int _pxvid_to_vd(u8 pxvid)
7328{
7329 if (pxvid == 0)
7330 return 0;
7331
7332 if (pxvid >= 8 && pxvid < 31)
7333 pxvid = 31;
7334
7335 return (pxvid + 2) * 125;
7336}
7337
7338static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007339{
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007340 const int vd = _pxvid_to_vd(pxvid);
7341 const int vm = vd - 1125;
7342
Chris Wilsondc979972016-05-10 14:10:04 +01007343 if (INTEL_INFO(dev_priv)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007344 return vm > 0 ? vm : 0;
7345
7346 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007347}
7348
Daniel Vetter02d71952012-08-09 16:44:54 +02007349static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007350{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00007351 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007352 u32 count;
7353
Chris Wilson67520412017-03-02 13:28:01 +00007354 lockdep_assert_held(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007355
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00007356 now = ktime_get_raw_ns();
7357 diffms = now - dev_priv->ips.last_time2;
7358 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007359
7360 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02007361 if (!diffms)
7362 return;
7363
7364 count = I915_READ(GFXEC);
7365
Daniel Vetter20e4d402012-08-08 23:35:39 +02007366 if (count < dev_priv->ips.last_count2) {
7367 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007368 diff += count;
7369 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007370 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007371 }
7372
Daniel Vetter20e4d402012-08-08 23:35:39 +02007373 dev_priv->ips.last_count2 = count;
7374 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007375
7376 /* More magic constants... */
7377 diff = diff * 1181;
7378 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02007379 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007380}
7381
Daniel Vetter02d71952012-08-09 16:44:54 +02007382void i915_update_gfx_val(struct drm_i915_private *dev_priv)
7383{
Chris Wilsondc979972016-05-10 14:10:04 +01007384 if (INTEL_INFO(dev_priv)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02007385 return;
7386
Daniel Vetter92703882012-08-09 16:46:01 +02007387 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007388
7389 __i915_update_gfx_val(dev_priv);
7390
Daniel Vetter92703882012-08-09 16:46:01 +02007391 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007392}
7393
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007394static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007395{
7396 unsigned long t, corr, state1, corr2, state2;
7397 u32 pxvid, ext_v;
7398
Chris Wilson67520412017-03-02 13:28:01 +00007399 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007400
Ville Syrjälä616847e2015-09-18 20:03:19 +03007401 pxvid = I915_READ(PXVFREQ(dev_priv->rps.cur_freq));
Daniel Vettereb48eb02012-04-26 23:28:12 +02007402 pxvid = (pxvid >> 24) & 0x7f;
7403 ext_v = pvid_to_extvid(dev_priv, pxvid);
7404
7405 state1 = ext_v;
7406
7407 t = i915_mch_val(dev_priv);
7408
7409 /* Revel in the empirically derived constants */
7410
7411 /* Correction factor in 1/100000 units */
7412 if (t > 80)
7413 corr = ((t * 2349) + 135940);
7414 else if (t >= 50)
7415 corr = ((t * 964) + 29317);
7416 else /* < 50 */
7417 corr = ((t * 301) + 1004);
7418
7419 corr = corr * ((150142 * state1) / 10000 - 78642);
7420 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02007421 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007422
7423 state2 = (corr2 * state1) / 10000;
7424 state2 /= 100; /* convert to mW */
7425
Daniel Vetter02d71952012-08-09 16:44:54 +02007426 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007427
Daniel Vetter20e4d402012-08-08 23:35:39 +02007428 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007429}
7430
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007431unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
7432{
7433 unsigned long val;
7434
Chris Wilsondc979972016-05-10 14:10:04 +01007435 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007436 return 0;
7437
7438 spin_lock_irq(&mchdev_lock);
7439
7440 val = __i915_gfx_val(dev_priv);
7441
7442 spin_unlock_irq(&mchdev_lock);
7443
7444 return val;
7445}
7446
Daniel Vettereb48eb02012-04-26 23:28:12 +02007447/**
7448 * i915_read_mch_val - return value for IPS use
7449 *
7450 * Calculate and return a value for the IPS driver to use when deciding whether
7451 * we have thermal and power headroom to increase CPU or GPU power budget.
7452 */
7453unsigned long i915_read_mch_val(void)
7454{
7455 struct drm_i915_private *dev_priv;
7456 unsigned long chipset_val, graphics_val, ret = 0;
7457
Daniel Vetter92703882012-08-09 16:46:01 +02007458 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007459 if (!i915_mch_dev)
7460 goto out_unlock;
7461 dev_priv = i915_mch_dev;
7462
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007463 chipset_val = __i915_chipset_val(dev_priv);
7464 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007465
7466 ret = chipset_val + graphics_val;
7467
7468out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007469 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007470
7471 return ret;
7472}
7473EXPORT_SYMBOL_GPL(i915_read_mch_val);
7474
7475/**
7476 * i915_gpu_raise - raise GPU frequency limit
7477 *
7478 * Raise the limit; IPS indicates we have thermal headroom.
7479 */
7480bool i915_gpu_raise(void)
7481{
7482 struct drm_i915_private *dev_priv;
7483 bool ret = true;
7484
Daniel Vetter92703882012-08-09 16:46:01 +02007485 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007486 if (!i915_mch_dev) {
7487 ret = false;
7488 goto out_unlock;
7489 }
7490 dev_priv = i915_mch_dev;
7491
Daniel Vetter20e4d402012-08-08 23:35:39 +02007492 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
7493 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007494
7495out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007496 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007497
7498 return ret;
7499}
7500EXPORT_SYMBOL_GPL(i915_gpu_raise);
7501
7502/**
7503 * i915_gpu_lower - lower GPU frequency limit
7504 *
7505 * IPS indicates we're close to a thermal limit, so throttle back the GPU
7506 * frequency maximum.
7507 */
7508bool i915_gpu_lower(void)
7509{
7510 struct drm_i915_private *dev_priv;
7511 bool ret = true;
7512
Daniel Vetter92703882012-08-09 16:46:01 +02007513 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007514 if (!i915_mch_dev) {
7515 ret = false;
7516 goto out_unlock;
7517 }
7518 dev_priv = i915_mch_dev;
7519
Daniel Vetter20e4d402012-08-08 23:35:39 +02007520 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
7521 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007522
7523out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007524 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007525
7526 return ret;
7527}
7528EXPORT_SYMBOL_GPL(i915_gpu_lower);
7529
7530/**
7531 * i915_gpu_busy - indicate GPU business to IPS
7532 *
7533 * Tell the IPS driver whether or not the GPU is busy.
7534 */
7535bool i915_gpu_busy(void)
7536{
Daniel Vettereb48eb02012-04-26 23:28:12 +02007537 bool ret = false;
7538
Daniel Vetter92703882012-08-09 16:46:01 +02007539 spin_lock_irq(&mchdev_lock);
Chris Wilsondcff85c2016-08-05 10:14:11 +01007540 if (i915_mch_dev)
7541 ret = i915_mch_dev->gt.awake;
Daniel Vetter92703882012-08-09 16:46:01 +02007542 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007543
7544 return ret;
7545}
7546EXPORT_SYMBOL_GPL(i915_gpu_busy);
7547
7548/**
7549 * i915_gpu_turbo_disable - disable graphics turbo
7550 *
7551 * Disable graphics turbo by resetting the max frequency and setting the
7552 * current frequency to the default.
7553 */
7554bool i915_gpu_turbo_disable(void)
7555{
7556 struct drm_i915_private *dev_priv;
7557 bool ret = true;
7558
Daniel Vetter92703882012-08-09 16:46:01 +02007559 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007560 if (!i915_mch_dev) {
7561 ret = false;
7562 goto out_unlock;
7563 }
7564 dev_priv = i915_mch_dev;
7565
Daniel Vetter20e4d402012-08-08 23:35:39 +02007566 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007567
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01007568 if (!ironlake_set_drps(dev_priv, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02007569 ret = false;
7570
7571out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007572 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007573
7574 return ret;
7575}
7576EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
7577
7578/**
7579 * Tells the intel_ips driver that the i915 driver is now loaded, if
7580 * IPS got loaded first.
7581 *
7582 * This awkward dance is so that neither module has to depend on the
7583 * other in order for IPS to do the appropriate communication of
7584 * GPU turbo limits to i915.
7585 */
7586static void
7587ips_ping_for_i915_load(void)
7588{
7589 void (*link)(void);
7590
7591 link = symbol_get(ips_link_to_i915_driver);
7592 if (link) {
7593 link();
7594 symbol_put(ips_link_to_i915_driver);
7595 }
7596}
7597
7598void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
7599{
Daniel Vetter02d71952012-08-09 16:44:54 +02007600 /* We only register the i915 ips part with intel-ips once everything is
7601 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02007602 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007603 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02007604 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007605
7606 ips_ping_for_i915_load();
7607}
7608
7609void intel_gpu_ips_teardown(void)
7610{
Daniel Vetter92703882012-08-09 16:46:01 +02007611 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007612 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02007613 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007614}
Deepak S76c3552f2014-01-30 23:08:16 +05307615
Chris Wilsondc979972016-05-10 14:10:04 +01007616static void intel_init_emon(struct drm_i915_private *dev_priv)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007617{
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007618 u32 lcfuse;
7619 u8 pxw[16];
7620 int i;
7621
7622 /* Disable to program */
7623 I915_WRITE(ECR, 0);
7624 POSTING_READ(ECR);
7625
7626 /* Program energy weights for various events */
7627 I915_WRITE(SDEW, 0x15040d00);
7628 I915_WRITE(CSIEW0, 0x007f0000);
7629 I915_WRITE(CSIEW1, 0x1e220004);
7630 I915_WRITE(CSIEW2, 0x04000004);
7631
7632 for (i = 0; i < 5; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007633 I915_WRITE(PEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007634 for (i = 0; i < 3; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007635 I915_WRITE(DEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007636
7637 /* Program P-state weights to account for frequency power adjustment */
7638 for (i = 0; i < 16; i++) {
Ville Syrjälä616847e2015-09-18 20:03:19 +03007639 u32 pxvidfreq = I915_READ(PXVFREQ(i));
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007640 unsigned long freq = intel_pxfreq(pxvidfreq);
7641 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
7642 PXVFREQ_PX_SHIFT;
7643 unsigned long val;
7644
7645 val = vid * vid;
7646 val *= (freq / 1000);
7647 val *= 255;
7648 val /= (127*127*900);
7649 if (val > 0xff)
7650 DRM_ERROR("bad pxval: %ld\n", val);
7651 pxw[i] = val;
7652 }
7653 /* Render standby states get 0 weight */
7654 pxw[14] = 0;
7655 pxw[15] = 0;
7656
7657 for (i = 0; i < 4; i++) {
7658 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
7659 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
Ville Syrjälä616847e2015-09-18 20:03:19 +03007660 I915_WRITE(PXW(i), val);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007661 }
7662
7663 /* Adjust magic regs to magic values (more experimental results) */
7664 I915_WRITE(OGW0, 0);
7665 I915_WRITE(OGW1, 0);
7666 I915_WRITE(EG0, 0x00007f00);
7667 I915_WRITE(EG1, 0x0000000e);
7668 I915_WRITE(EG2, 0x000e0000);
7669 I915_WRITE(EG3, 0x68000300);
7670 I915_WRITE(EG4, 0x42000000);
7671 I915_WRITE(EG5, 0x00140031);
7672 I915_WRITE(EG6, 0);
7673 I915_WRITE(EG7, 0);
7674
7675 for (i = 0; i < 8; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007676 I915_WRITE(PXWL(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007677
7678 /* Enable PMON + select events */
7679 I915_WRITE(ECR, 0x80000019);
7680
7681 lcfuse = I915_READ(LCFUSE02);
7682
Daniel Vetter20e4d402012-08-08 23:35:39 +02007683 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007684}
7685
Chris Wilsondc979972016-05-10 14:10:04 +01007686void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007687{
Imre Deakb268c692015-12-15 20:10:31 +02007688 /*
7689 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
7690 * requirement.
7691 */
7692 if (!i915.enable_rc6) {
7693 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
7694 intel_runtime_pm_get(dev_priv);
7695 }
Imre Deake6069ca2014-04-18 16:01:02 +03007696
Chris Wilsonb5163db2016-08-10 13:58:24 +01007697 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson773ea9a2016-07-13 09:10:33 +01007698 mutex_lock(&dev_priv->rps.hw_lock);
7699
7700 /* Initialize RPS limits (for userspace) */
Chris Wilsondc979972016-05-10 14:10:04 +01007701 if (IS_CHERRYVIEW(dev_priv))
7702 cherryview_init_gt_powersave(dev_priv);
7703 else if (IS_VALLEYVIEW(dev_priv))
7704 valleyview_init_gt_powersave(dev_priv);
Chris Wilson2a13ae72016-08-02 11:15:27 +01007705 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson773ea9a2016-07-13 09:10:33 +01007706 gen6_init_rps_frequencies(dev_priv);
7707
7708 /* Derive initial user preferences/limits from the hardware limits */
7709 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
7710 dev_priv->rps.cur_freq = dev_priv->rps.idle_freq;
7711
7712 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
7713 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
7714
7715 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
7716 dev_priv->rps.min_freq_softlimit =
7717 max_t(int,
7718 dev_priv->rps.efficient_freq,
7719 intel_freq_opcode(dev_priv, 450));
7720
Chris Wilson99ac9612016-07-13 09:10:34 +01007721 /* After setting max-softlimit, find the overclock max freq */
7722 if (IS_GEN6(dev_priv) ||
7723 IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
7724 u32 params = 0;
7725
7726 sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &params);
7727 if (params & BIT(31)) { /* OC supported */
7728 DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
7729 (dev_priv->rps.max_freq & 0xff) * 50,
7730 (params & 0xff) * 50);
7731 dev_priv->rps.max_freq = params & 0xff;
7732 }
7733 }
7734
Chris Wilson29ecd78d2016-07-13 09:10:35 +01007735 /* Finally allow us to boost to max by default */
7736 dev_priv->rps.boost_freq = dev_priv->rps.max_freq;
7737
Chris Wilson773ea9a2016-07-13 09:10:33 +01007738 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb5163db2016-08-10 13:58:24 +01007739 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson54b4f682016-07-21 21:16:19 +01007740
7741 intel_autoenable_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03007742}
7743
Chris Wilsondc979972016-05-10 14:10:04 +01007744void intel_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007745{
Ville Syrjälä8dac1e12016-08-02 14:07:33 +03007746 if (IS_VALLEYVIEW(dev_priv))
Chris Wilsondc979972016-05-10 14:10:04 +01007747 valleyview_cleanup_gt_powersave(dev_priv);
Imre Deakb268c692015-12-15 20:10:31 +02007748
7749 if (!i915.enable_rc6)
7750 intel_runtime_pm_put(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03007751}
7752
Chris Wilson54b4f682016-07-21 21:16:19 +01007753/**
7754 * intel_suspend_gt_powersave - suspend PM work and helper threads
7755 * @dev_priv: i915 device
7756 *
7757 * We don't want to disable RC6 or other features here, we just want
7758 * to make sure any work we've queued has finished and won't bother
7759 * us while we're suspended.
7760 */
7761void intel_suspend_gt_powersave(struct drm_i915_private *dev_priv)
7762{
7763 if (INTEL_GEN(dev_priv) < 6)
7764 return;
7765
7766 if (cancel_delayed_work_sync(&dev_priv->rps.autoenable_work))
7767 intel_runtime_pm_put(dev_priv);
7768
7769 /* gen6_rps_idle() will be called later to disable interrupts */
7770}
7771
Chris Wilsonb7137e02016-07-13 09:10:37 +01007772void intel_sanitize_gt_powersave(struct drm_i915_private *dev_priv)
7773{
7774 dev_priv->rps.enabled = true; /* force disabling */
7775 intel_disable_gt_powersave(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007776
7777 gen6_reset_rps_interrupts(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07007778}
7779
Chris Wilsondc979972016-05-10 14:10:04 +01007780void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
Daniel Vetter8090c6b2012-06-24 16:42:32 +02007781{
Chris Wilsonb7137e02016-07-13 09:10:37 +01007782 if (!READ_ONCE(dev_priv->rps.enabled))
7783 return;
Jesse Barnes1a01ab32012-11-02 11:14:00 -07007784
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007785 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007786
Chris Wilsonb7137e02016-07-13 09:10:37 +01007787 if (INTEL_GEN(dev_priv) >= 9) {
7788 gen9_disable_rc6(dev_priv);
7789 gen9_disable_rps(dev_priv);
7790 } else if (IS_CHERRYVIEW(dev_priv)) {
7791 cherryview_disable_rps(dev_priv);
7792 } else if (IS_VALLEYVIEW(dev_priv)) {
7793 valleyview_disable_rps(dev_priv);
7794 } else if (INTEL_GEN(dev_priv) >= 6) {
7795 gen6_disable_rps(dev_priv);
7796 } else if (IS_IRONLAKE_M(dev_priv)) {
7797 ironlake_disable_drps(dev_priv);
7798 }
7799
7800 dev_priv->rps.enabled = false;
7801 mutex_unlock(&dev_priv->rps.hw_lock);
7802}
7803
7804void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
7805{
Chris Wilson54b4f682016-07-21 21:16:19 +01007806 /* We shouldn't be disabling as we submit, so this should be less
7807 * racy than it appears!
7808 */
Chris Wilsonb7137e02016-07-13 09:10:37 +01007809 if (READ_ONCE(dev_priv->rps.enabled))
7810 return;
7811
7812 /* Powersaving is controlled by the host when inside a VM */
7813 if (intel_vgpu_active(dev_priv))
7814 return;
7815
7816 mutex_lock(&dev_priv->rps.hw_lock);
Imre Deak3cc134e2014-11-19 15:30:03 +02007817
Chris Wilsondc979972016-05-10 14:10:04 +01007818 if (IS_CHERRYVIEW(dev_priv)) {
7819 cherryview_enable_rps(dev_priv);
7820 } else if (IS_VALLEYVIEW(dev_priv)) {
7821 valleyview_enable_rps(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007822 } else if (INTEL_GEN(dev_priv) >= 9) {
Chris Wilsondc979972016-05-10 14:10:04 +01007823 gen9_enable_rc6(dev_priv);
7824 gen9_enable_rps(dev_priv);
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07007825 if (IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv))
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007826 gen6_update_ring_freq(dev_priv);
Chris Wilsondc979972016-05-10 14:10:04 +01007827 } else if (IS_BROADWELL(dev_priv)) {
7828 gen8_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007829 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007830 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilsondc979972016-05-10 14:10:04 +01007831 gen6_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007832 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007833 } else if (IS_IRONLAKE_M(dev_priv)) {
7834 ironlake_enable_drps(dev_priv);
7835 intel_init_emon(dev_priv);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007836 }
Chris Wilsonaed242f2015-03-18 09:48:21 +00007837
7838 WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
7839 WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
7840
7841 WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
7842 WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
7843
Chris Wilson54b4f682016-07-21 21:16:19 +01007844 dev_priv->rps.enabled = true;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007845 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007846}
Imre Deakc6df39b2014-04-14 20:24:29 +03007847
Chris Wilson54b4f682016-07-21 21:16:19 +01007848static void __intel_autoenable_gt_powersave(struct work_struct *work)
7849{
7850 struct drm_i915_private *dev_priv =
7851 container_of(work, typeof(*dev_priv), rps.autoenable_work.work);
7852 struct intel_engine_cs *rcs;
7853 struct drm_i915_gem_request *req;
7854
7855 if (READ_ONCE(dev_priv->rps.enabled))
7856 goto out;
7857
Akash Goel3b3f1652016-10-13 22:44:48 +05307858 rcs = dev_priv->engine[RCS];
Chris Wilsone8a9c582016-12-18 15:37:20 +00007859 if (rcs->last_retired_context)
Chris Wilson54b4f682016-07-21 21:16:19 +01007860 goto out;
7861
7862 if (!rcs->init_context)
7863 goto out;
7864
7865 mutex_lock(&dev_priv->drm.struct_mutex);
7866
7867 req = i915_gem_request_alloc(rcs, dev_priv->kernel_context);
7868 if (IS_ERR(req))
7869 goto unlock;
7870
7871 if (!i915.enable_execlists && i915_switch_context(req) == 0)
7872 rcs->init_context(req);
7873
7874 /* Mark the device busy, calling intel_enable_gt_powersave() */
Chris Wilsone642c852017-03-17 11:47:09 +00007875 i915_add_request(req);
Chris Wilson54b4f682016-07-21 21:16:19 +01007876
7877unlock:
7878 mutex_unlock(&dev_priv->drm.struct_mutex);
7879out:
7880 intel_runtime_pm_put(dev_priv);
7881}
7882
7883void intel_autoenable_gt_powersave(struct drm_i915_private *dev_priv)
7884{
7885 if (READ_ONCE(dev_priv->rps.enabled))
7886 return;
7887
7888 if (IS_IRONLAKE_M(dev_priv)) {
7889 ironlake_enable_drps(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007890 intel_init_emon(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007891 } else if (INTEL_INFO(dev_priv)->gen >= 6) {
7892 /*
7893 * PCU communication is slow and this doesn't need to be
7894 * done at any specific time, so do this out of our fast path
7895 * to make resume and init faster.
7896 *
7897 * We depend on the HW RC6 power context save/restore
7898 * mechanism when entering D3 through runtime PM suspend. So
7899 * disable RPM until RPS/RC6 is properly setup. We can only
7900 * get here via the driver load/system resume/runtime resume
7901 * paths, so the _noresume version is enough (and in case of
7902 * runtime resume it's necessary).
7903 */
7904 if (queue_delayed_work(dev_priv->wq,
7905 &dev_priv->rps.autoenable_work,
7906 round_jiffies_up_relative(HZ)))
7907 intel_runtime_pm_get_noresume(dev_priv);
7908 }
7909}
7910
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007911static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007912{
Daniel Vetter3107bd42012-10-31 22:52:31 +01007913 /*
7914 * On Ibex Peak and Cougar Point, we need to disable clock
7915 * gating for the panel power sequencer or it will fail to
7916 * start up when no ports are active.
7917 */
7918 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
7919}
7920
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007921static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007922{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007923 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007924
Damien Lespiau055e3932014-08-18 13:49:10 +01007925 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007926 I915_WRITE(DSPCNTR(pipe),
7927 I915_READ(DSPCNTR(pipe)) |
7928 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007929
7930 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
7931 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007932 }
7933}
7934
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007935static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä017636c2013-12-05 15:51:37 +02007936{
Ville Syrjälä017636c2013-12-05 15:51:37 +02007937 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
7938 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
7939 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
7940
7941 /*
7942 * Don't touch WM1S_LP_EN here.
7943 * Doing so could cause underruns.
7944 */
7945}
7946
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007947static void ironlake_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007948{
Damien Lespiau231e54f2012-10-19 17:55:41 +01007949 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007950
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01007951 /*
7952 * Required for FBC
7953 * WaFbcDisableDpfcClockGating:ilk
7954 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007955 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
7956 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
7957 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007958
7959 I915_WRITE(PCH_3DCGDIS0,
7960 MARIUNIT_CLOCK_GATE_DISABLE |
7961 SVSMUNIT_CLOCK_GATE_DISABLE);
7962 I915_WRITE(PCH_3DCGDIS1,
7963 VFMUNIT_CLOCK_GATE_DISABLE);
7964
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007965 /*
7966 * According to the spec the following bits should be set in
7967 * order to enable memory self-refresh
7968 * The bit 22/21 of 0x42004
7969 * The bit 5 of 0x42020
7970 * The bit 15 of 0x45000
7971 */
7972 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7973 (I915_READ(ILK_DISPLAY_CHICKEN2) |
7974 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007975 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007976 I915_WRITE(DISP_ARB_CTL,
7977 (I915_READ(DISP_ARB_CTL) |
7978 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02007979
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007980 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007981
7982 /*
7983 * Based on the document from hardware guys the following bits
7984 * should be set unconditionally in order to enable FBC.
7985 * The bit 22 of 0x42000
7986 * The bit 22 of 0x42004
7987 * The bit 7,8,9 of 0x42020.
7988 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007989 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01007990 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007991 I915_WRITE(ILK_DISPLAY_CHICKEN1,
7992 I915_READ(ILK_DISPLAY_CHICKEN1) |
7993 ILK_FBCQ_DIS);
7994 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7995 I915_READ(ILK_DISPLAY_CHICKEN2) |
7996 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007997 }
7998
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007999 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
8000
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008001 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8002 I915_READ(ILK_DISPLAY_CHICKEN2) |
8003 ILK_ELPIN_409_SELECT);
8004 I915_WRITE(_3D_CHICKEN2,
8005 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
8006 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02008007
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008008 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02008009 I915_WRITE(CACHE_MODE_0,
8010 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01008011
Akash Goel4e046322014-04-04 17:14:38 +05308012 /* WaDisable_RenderCache_OperationalFlush:ilk */
8013 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8014
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008015 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03008016
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008017 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008018}
8019
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008020static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01008021{
Daniel Vetter3107bd42012-10-31 22:52:31 +01008022 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008023 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01008024
8025 /*
8026 * On Ibex Peak and Cougar Point, we need to disable clock
8027 * gating for the panel power sequencer or it will fail to
8028 * start up when no ports are active.
8029 */
Jesse Barnescd664072013-10-02 10:34:19 -07008030 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
8031 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
8032 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008033 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
8034 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01008035 /* The below fixes the weird display corruption, a few pixels shifted
8036 * downward, on (only) LVDS of some HP laptops with IVY.
8037 */
Damien Lespiau055e3932014-08-18 13:49:10 +01008038 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03008039 val = I915_READ(TRANS_CHICKEN2(pipe));
8040 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
8041 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008042 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008043 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03008044 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
8045 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
8046 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008047 I915_WRITE(TRANS_CHICKEN2(pipe), val);
8048 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01008049 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01008050 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01008051 I915_WRITE(TRANS_CHICKEN1(pipe),
8052 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
8053 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008054}
8055
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008056static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008057{
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008058 uint32_t tmp;
8059
8060 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02008061 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
8062 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
8063 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008064}
8065
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008066static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008067{
Damien Lespiau231e54f2012-10-19 17:55:41 +01008068 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008069
Damien Lespiau231e54f2012-10-19 17:55:41 +01008070 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008071
8072 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8073 I915_READ(ILK_DISPLAY_CHICKEN2) |
8074 ILK_ELPIN_409_SELECT);
8075
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008076 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01008077 I915_WRITE(_3D_CHICKEN,
8078 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
8079
Akash Goel4e046322014-04-04 17:14:38 +05308080 /* WaDisable_RenderCache_OperationalFlush:snb */
8081 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8082
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008083 /*
8084 * BSpec recoomends 8x4 when MSAA is used,
8085 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008086 *
8087 * Note that PS/WM thread counts depend on the WIZ hashing
8088 * disable bit, which we don't touch here, but it's good
8089 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008090 */
8091 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008092 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008093
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008094 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008095
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008096 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02008097 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008098
8099 I915_WRITE(GEN6_UCGCTL1,
8100 I915_READ(GEN6_UCGCTL1) |
8101 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
8102 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
8103
8104 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
8105 * gating disable must be set. Failure to set it results in
8106 * flickering pixels due to Z write ordering failures after
8107 * some amount of runtime in the Mesa "fire" demo, and Unigine
8108 * Sanctuary and Tropics, and apparently anything else with
8109 * alpha test or pixel discard.
8110 *
8111 * According to the spec, bit 11 (RCCUNIT) must also be set,
8112 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008113 *
Ville Syrjäläef593182014-01-22 21:32:47 +02008114 * WaDisableRCCUnitClockGating:snb
8115 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008116 */
8117 I915_WRITE(GEN6_UCGCTL2,
8118 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
8119 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
8120
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02008121 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02008122 I915_WRITE(_3D_CHICKEN3,
8123 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008124
8125 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02008126 * Bspec says:
8127 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
8128 * 3DSTATE_SF number of SF output attributes is more than 16."
8129 */
8130 I915_WRITE(_3D_CHICKEN3,
8131 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
8132
8133 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008134 * According to the spec the following bits should be
8135 * set in order to enable memory self-refresh and fbc:
8136 * The bit21 and bit22 of 0x42000
8137 * The bit21 and bit22 of 0x42004
8138 * The bit5 and bit7 of 0x42020
8139 * The bit14 of 0x70180
8140 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01008141 *
8142 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008143 */
8144 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8145 I915_READ(ILK_DISPLAY_CHICKEN1) |
8146 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
8147 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8148 I915_READ(ILK_DISPLAY_CHICKEN2) |
8149 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01008150 I915_WRITE(ILK_DSPCLK_GATE_D,
8151 I915_READ(ILK_DSPCLK_GATE_D) |
8152 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
8153 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008154
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008155 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07008156
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008157 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008158
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008159 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008160}
8161
8162static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
8163{
8164 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
8165
Ville Syrjälä3aad9052014-01-22 21:32:59 +02008166 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02008167 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02008168 *
8169 * This actually overrides the dispatch
8170 * mode for all thread types.
8171 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008172 reg &= ~GEN7_FF_SCHED_MASK;
8173 reg |= GEN7_FF_TS_SCHED_HW;
8174 reg |= GEN7_FF_VS_SCHED_HW;
8175 reg |= GEN7_FF_DS_SCHED_HW;
8176
8177 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
8178}
8179
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008180static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008181{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008182 /*
8183 * TODO: this bit should only be enabled when really needed, then
8184 * disabled when not needed anymore in order to save power.
8185 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008186 if (HAS_PCH_LPT_LP(dev_priv))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008187 I915_WRITE(SOUTH_DSPCLK_GATE_D,
8188 I915_READ(SOUTH_DSPCLK_GATE_D) |
8189 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03008190
8191 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03008192 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
8193 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03008194 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008195}
8196
Ville Syrjälä712bf362016-10-31 22:37:23 +02008197static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03008198{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008199 if (HAS_PCH_LPT_LP(dev_priv)) {
Imre Deak7d708ee2013-04-17 14:04:50 +03008200 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
8201
8202 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8203 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8204 }
8205}
8206
Imre Deak450174f2016-05-03 15:54:21 +03008207static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
8208 int general_prio_credits,
8209 int high_prio_credits)
8210{
8211 u32 misccpctl;
8212
8213 /* WaTempDisableDOPClkGating:bdw */
8214 misccpctl = I915_READ(GEN7_MISCCPCTL);
8215 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
8216
8217 I915_WRITE(GEN8_L3SQCREG1,
8218 L3_GENERAL_PRIO_CREDITS(general_prio_credits) |
8219 L3_HIGH_PRIO_CREDITS(high_prio_credits));
8220
8221 /*
8222 * Wait at least 100 clocks before re-enabling clock gating.
8223 * See the definition of L3SQCREG1 in BSpec.
8224 */
8225 POSTING_READ(GEN8_L3SQCREG1);
8226 udelay(1);
8227 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
8228}
8229
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008230static void kabylake_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008231{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008232 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008233
8234 /* WaDisableSDEUnitClockGating:kbl */
8235 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
8236 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8237 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03008238
8239 /* WaDisableGamClockGating:kbl */
8240 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
8241 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
8242 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008243
Rodrigo Vivi82525c12017-06-08 08:50:00 -07008244 /* WaFbcNukeOnHostModify:kbl,cfl */
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008245 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8246 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008247}
8248
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008249static void skylake_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008250{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008251 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03008252
8253 /* WAC6entrylatency:skl */
8254 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
8255 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008256
8257 /* WaFbcNukeOnHostModify:skl */
8258 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8259 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008260}
8261
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008262static void broadwell_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008263{
Damien Lespiau07d27e22014-03-03 17:31:46 +00008264 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008265
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008266 ilk_init_lp_watermarks(dev_priv);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07008267
Ben Widawskyab57fff2013-12-12 15:28:04 -08008268 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07008269 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008270
Ben Widawskyab57fff2013-12-12 15:28:04 -08008271 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008272 I915_WRITE(CHICKEN_PAR1_1,
8273 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
8274
Ben Widawskyab57fff2013-12-12 15:28:04 -08008275 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01008276 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00008277 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02008278 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02008279 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008280 }
Ben Widawsky63801f22013-12-12 17:26:03 -08008281
Ben Widawskyab57fff2013-12-12 15:28:04 -08008282 /* WaVSRefCountFullforceMissDisable:bdw */
8283 /* WaDSRefCountFullforceMissDisable:bdw */
8284 I915_WRITE(GEN7_FF_THREAD_MODE,
8285 I915_READ(GEN7_FF_THREAD_MODE) &
8286 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02008287
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02008288 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
8289 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02008290
8291 /* WaDisableSDEUnitClockGating:bdw */
8292 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8293 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00008294
Imre Deak450174f2016-05-03 15:54:21 +03008295 /* WaProgramL3SqcReg1Default:bdw */
8296 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03008297
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008298 /*
8299 * WaGttCachingOffByDefault:bdw
8300 * GTT cache may not work with big pages, so if those
8301 * are ever enabled GTT cache may need to be disabled.
8302 */
8303 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
8304
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03008305 /* WaKVMNotificationOnConfigChange:bdw */
8306 I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
8307 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
8308
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008309 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00008310
8311 /* WaDisableDopClockGating:bdw
8312 *
8313 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
8314 * clock gating.
8315 */
8316 I915_WRITE(GEN6_UCGCTL1,
8317 I915_READ(GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008318}
8319
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008320static void haswell_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008321{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008322 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008323
Francisco Jerezf3fc4882013-10-02 15:53:16 -07008324 /* L3 caching of data atomics doesn't work -- disable it. */
8325 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
8326 I915_WRITE(HSW_ROW_CHICKEN3,
8327 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
8328
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008329 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008330 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8331 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8332 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8333
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02008334 /* WaVSRefCountFullforceMissDisable:hsw */
8335 I915_WRITE(GEN7_FF_THREAD_MODE,
8336 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008337
Akash Goel4e046322014-04-04 17:14:38 +05308338 /* WaDisable_RenderCache_OperationalFlush:hsw */
8339 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8340
Chia-I Wufe27c602014-01-28 13:29:33 +08008341 /* enable HiZ Raw Stall Optimization */
8342 I915_WRITE(CACHE_MODE_0_GEN7,
8343 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
8344
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008345 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008346 I915_WRITE(CACHE_MODE_1,
8347 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03008348
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008349 /*
8350 * BSpec recommends 8x4 when MSAA is used,
8351 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008352 *
8353 * Note that PS/WM thread counts depend on the WIZ hashing
8354 * disable bit, which we don't touch here, but it's good
8355 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008356 */
8357 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008358 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008359
Kenneth Graunke94411592014-12-31 16:23:00 -08008360 /* WaSampleCChickenBitEnable:hsw */
8361 I915_WRITE(HALF_SLICE_CHICKEN3,
8362 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
8363
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008364 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07008365 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
8366
Paulo Zanoni90a88642013-05-03 17:23:45 -03008367 /* WaRsPkgCStateDisplayPMReq:hsw */
8368 I915_WRITE(CHICKEN_PAR1_1,
8369 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03008370
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008371 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008372}
8373
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008374static void ivybridge_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008375{
Ben Widawsky20848222012-05-04 18:58:59 -07008376 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008377
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008378 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008379
Damien Lespiau231e54f2012-10-19 17:55:41 +01008380 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008381
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008382 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05008383 I915_WRITE(_3D_CHICKEN3,
8384 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
8385
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008386 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008387 I915_WRITE(IVB_CHICKEN3,
8388 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8389 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8390
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008391 /* WaDisablePSDDualDispatchEnable:ivb */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008392 if (IS_IVB_GT1(dev_priv))
Jesse Barnes12f33822012-10-25 12:15:45 -07008393 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
8394 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07008395
Akash Goel4e046322014-04-04 17:14:38 +05308396 /* WaDisable_RenderCache_OperationalFlush:ivb */
8397 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8398
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008399 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008400 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
8401 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
8402
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008403 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008404 I915_WRITE(GEN7_L3CNTLREG1,
8405 GEN7_WA_FOR_GEN7_L3_CONTROL);
8406 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07008407 GEN7_WA_L3_CHICKEN_MODE);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008408 if (IS_IVB_GT1(dev_priv))
Jesse Barnes8ab43972012-10-25 12:15:42 -07008409 I915_WRITE(GEN7_ROW_CHICKEN2,
8410 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02008411 else {
8412 /* must write both registers */
8413 I915_WRITE(GEN7_ROW_CHICKEN2,
8414 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07008415 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
8416 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02008417 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008418
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008419 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05008420 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
8421 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
8422
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02008423 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07008424 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008425 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008426 */
8427 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02008428 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07008429
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008430 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008431 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8432 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8433 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8434
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008435 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008436
8437 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02008438
Chris Wilson22721342014-03-04 09:41:43 +00008439 if (0) { /* causes HiZ corruption on ivb:gt1 */
8440 /* enable HiZ Raw Stall Optimization */
8441 I915_WRITE(CACHE_MODE_0_GEN7,
8442 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
8443 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08008444
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008445 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02008446 I915_WRITE(CACHE_MODE_1,
8447 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07008448
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008449 /*
8450 * BSpec recommends 8x4 when MSAA is used,
8451 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008452 *
8453 * Note that PS/WM thread counts depend on the WIZ hashing
8454 * disable bit, which we don't touch here, but it's good
8455 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008456 */
8457 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008458 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008459
Ben Widawsky20848222012-05-04 18:58:59 -07008460 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
8461 snpcr &= ~GEN6_MBC_SNPCR_MASK;
8462 snpcr |= GEN6_MBC_SNPCR_MED;
8463 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008464
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008465 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008466 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008467
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008468 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008469}
8470
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008471static void valleyview_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008472{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008473 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05008474 I915_WRITE(_3D_CHICKEN3,
8475 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
8476
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008477 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008478 I915_WRITE(IVB_CHICKEN3,
8479 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8480 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8481
Ville Syrjäläfad7d362014-01-22 21:32:39 +02008482 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008483 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07008484 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08008485 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
8486 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07008487
Akash Goel4e046322014-04-04 17:14:38 +05308488 /* WaDisable_RenderCache_OperationalFlush:vlv */
8489 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8490
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008491 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05008492 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
8493 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
8494
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008495 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07008496 I915_WRITE(GEN7_ROW_CHICKEN2,
8497 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
8498
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008499 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008500 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8501 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8502 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8503
Ville Syrjälä46680e02014-01-22 21:33:01 +02008504 gen7_setup_fixed_func_scheduler(dev_priv);
8505
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02008506 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07008507 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008508 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008509 */
8510 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02008511 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07008512
Akash Goelc98f5062014-03-24 23:00:07 +05308513 /* WaDisableL3Bank2xClockGate:vlv
8514 * Disabling L3 clock gating- MMIO 940c[25] = 1
8515 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
8516 I915_WRITE(GEN7_UCGCTL4,
8517 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07008518
Ville Syrjäläafd58e72014-01-22 21:33:03 +02008519 /*
8520 * BSpec says this must be set, even though
8521 * WaDisable4x2SubspanOptimization isn't listed for VLV.
8522 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02008523 I915_WRITE(CACHE_MODE_1,
8524 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07008525
8526 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02008527 * BSpec recommends 8x4 when MSAA is used,
8528 * however in practice 16x4 seems fastest.
8529 *
8530 * Note that PS/WM thread counts depend on the WIZ hashing
8531 * disable bit, which we don't touch here, but it's good
8532 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
8533 */
8534 I915_WRITE(GEN7_GT_MODE,
8535 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
8536
8537 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02008538 * WaIncreaseL3CreditsForVLVB0:vlv
8539 * This is the hardware default actually.
8540 */
8541 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
8542
8543 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008544 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07008545 * Disable clock gating on th GCFG unit to prevent a delay
8546 * in the reporting of vblank events.
8547 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02008548 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008549}
8550
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008551static void cherryview_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03008552{
Ville Syrjälä232ce332014-04-09 13:28:35 +03008553 /* WaVSRefCountFullforceMissDisable:chv */
8554 /* WaDSRefCountFullforceMissDisable:chv */
8555 I915_WRITE(GEN7_FF_THREAD_MODE,
8556 I915_READ(GEN7_FF_THREAD_MODE) &
8557 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03008558
8559 /* WaDisableSemaphoreAndSyncFlipWait:chv */
8560 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
8561 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03008562
8563 /* WaDisableCSUnitClockGating:chv */
8564 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
8565 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03008566
8567 /* WaDisableSDEUnitClockGating:chv */
8568 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8569 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008570
8571 /*
Imre Deak450174f2016-05-03 15:54:21 +03008572 * WaProgramL3SqcReg1Default:chv
8573 * See gfxspecs/Related Documents/Performance Guide/
8574 * LSQC Setting Recommendations.
8575 */
8576 gen8_set_l3sqc_credits(dev_priv, 38, 2);
8577
8578 /*
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008579 * GTT cache may not work with big pages, so if those
8580 * are ever enabled GTT cache may need to be disabled.
8581 */
8582 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03008583}
8584
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008585static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008586{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008587 uint32_t dspclk_gate;
8588
8589 I915_WRITE(RENCLK_GATE_D1, 0);
8590 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
8591 GS_UNIT_CLOCK_GATE_DISABLE |
8592 CL_UNIT_CLOCK_GATE_DISABLE);
8593 I915_WRITE(RAMCLK_GATE_D, 0);
8594 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
8595 OVRUNIT_CLOCK_GATE_DISABLE |
8596 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008597 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008598 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
8599 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02008600
8601 /* WaDisableRenderCachePipelinedFlush */
8602 I915_WRITE(CACHE_MODE_0,
8603 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03008604
Akash Goel4e046322014-04-04 17:14:38 +05308605 /* WaDisable_RenderCache_OperationalFlush:g4x */
8606 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8607
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008608 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008609}
8610
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008611static void crestline_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008612{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008613 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
8614 I915_WRITE(RENCLK_GATE_D2, 0);
8615 I915_WRITE(DSPCLK_GATE_D, 0);
8616 I915_WRITE(RAMCLK_GATE_D, 0);
8617 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03008618 I915_WRITE(MI_ARB_STATE,
8619 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05308620
8621 /* WaDisable_RenderCache_OperationalFlush:gen4 */
8622 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008623}
8624
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008625static void broadwater_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008626{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008627 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
8628 I965_RCC_CLOCK_GATE_DISABLE |
8629 I965_RCPB_CLOCK_GATE_DISABLE |
8630 I965_ISC_CLOCK_GATE_DISABLE |
8631 I965_FBC_CLOCK_GATE_DISABLE);
8632 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03008633 I915_WRITE(MI_ARB_STATE,
8634 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05308635
8636 /* WaDisable_RenderCache_OperationalFlush:gen4 */
8637 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008638}
8639
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008640static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008641{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008642 u32 dstate = I915_READ(D_STATE);
8643
8644 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
8645 DSTATE_DOT_CLOCK_GATING;
8646 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01008647
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008648 if (IS_PINEVIEW(dev_priv))
Chris Wilson13a86b82012-04-24 14:51:43 +01008649 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02008650
8651 /* IIR "flip pending" means done if this bit is set */
8652 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02008653
8654 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02008655 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02008656
8657 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
8658 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03008659
8660 I915_WRITE(MI_ARB_STATE,
8661 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008662}
8663
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008664static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008665{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008666 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02008667
8668 /* interrupts should cause a wake up from C3 */
8669 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
8670 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03008671
8672 I915_WRITE(MEM_MODE,
8673 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008674}
8675
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008676static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008677{
Ville Syrjälä10383922014-08-15 01:21:54 +03008678 I915_WRITE(MEM_MODE,
8679 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
8680 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008681}
8682
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008683void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008684{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008685 dev_priv->display.init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008686}
8687
Ville Syrjälä712bf362016-10-31 22:37:23 +02008688void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03008689{
Ville Syrjälä712bf362016-10-31 22:37:23 +02008690 if (HAS_PCH_LPT(dev_priv))
8691 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03008692}
8693
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008694static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02008695{
8696 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
8697}
8698
8699/**
8700 * intel_init_clock_gating_hooks - setup the clock gating hooks
8701 * @dev_priv: device private
8702 *
8703 * Setup the hooks that configure which clocks of a given platform can be
8704 * gated and also apply various GT and display specific workarounds for these
8705 * platforms. Note that some GT specific workarounds are applied separately
8706 * when GPU contexts or batchbuffers start their execution.
8707 */
8708void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
8709{
8710 if (IS_SKYLAKE(dev_priv))
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008711 dev_priv->display.init_clock_gating = skylake_init_clock_gating;
Rodrigo Vivi82525c12017-06-08 08:50:00 -07008712 else if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv))
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008713 dev_priv->display.init_clock_gating = kabylake_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02008714 else if (IS_BROXTON(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02008715 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02008716 else if (IS_GEMINILAKE(dev_priv))
8717 dev_priv->display.init_clock_gating = glk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008718 else if (IS_BROADWELL(dev_priv))
8719 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
8720 else if (IS_CHERRYVIEW(dev_priv))
8721 dev_priv->display.init_clock_gating = cherryview_init_clock_gating;
8722 else if (IS_HASWELL(dev_priv))
8723 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
8724 else if (IS_IVYBRIDGE(dev_priv))
8725 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
8726 else if (IS_VALLEYVIEW(dev_priv))
8727 dev_priv->display.init_clock_gating = valleyview_init_clock_gating;
8728 else if (IS_GEN6(dev_priv))
8729 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
8730 else if (IS_GEN5(dev_priv))
8731 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
8732 else if (IS_G4X(dev_priv))
8733 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02008734 else if (IS_I965GM(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02008735 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02008736 else if (IS_I965G(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02008737 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
8738 else if (IS_GEN3(dev_priv))
8739 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
8740 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
8741 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
8742 else if (IS_GEN2(dev_priv))
8743 dev_priv->display.init_clock_gating = i830_init_clock_gating;
8744 else {
8745 MISSING_CASE(INTEL_DEVID(dev_priv));
8746 dev_priv->display.init_clock_gating = nop_init_clock_gating;
8747 }
8748}
8749
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008750/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008751void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008752{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02008753 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008754
Daniel Vetterc921aba2012-04-26 23:28:17 +02008755 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008756 if (IS_PINEVIEW(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02008757 i915_pineview_get_mem_freq(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008758 else if (IS_GEN5(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02008759 i915_ironlake_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02008760
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008761 /* For FIFO watermark updates */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008762 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008763 skl_setup_wm_latency(dev_priv);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01008764 dev_priv->display.initial_watermarks = skl_initial_wm;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01008765 dev_priv->display.atomic_update_watermarks = skl_atomic_update_crtc_wm;
Matt Roper98d39492016-05-12 07:06:03 -07008766 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008767 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008768 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03008769
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008770 if ((IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008771 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008772 (!IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008773 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07008774 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08008775 dev_priv->display.compute_intermediate_wm =
8776 ilk_compute_intermediate_wm;
8777 dev_priv->display.initial_watermarks =
8778 ilk_initial_watermarks;
8779 dev_priv->display.optimize_watermarks =
8780 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02008781 } else {
8782 DRM_DEBUG_KMS("Failed to read display plane latency. "
8783 "Disable CxSR\n");
8784 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02008785 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008786 vlv_setup_wm_latency(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02008787 dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
Ville Syrjälä4841da52017-03-02 19:14:59 +02008788 dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02008789 dev_priv->display.initial_watermarks = vlv_initial_watermarks;
Ville Syrjälä4841da52017-03-02 19:14:59 +02008790 dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
Ville Syrjäläff32c542017-03-02 19:14:57 +02008791 dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03008792 } else if (IS_G4X(dev_priv)) {
8793 g4x_setup_wm_latency(dev_priv);
8794 dev_priv->display.compute_pipe_wm = g4x_compute_pipe_wm;
8795 dev_priv->display.compute_intermediate_wm = g4x_compute_intermediate_wm;
8796 dev_priv->display.initial_watermarks = g4x_initial_watermarks;
8797 dev_priv->display.optimize_watermarks = g4x_optimize_watermarks;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008798 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008799 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008800 dev_priv->is_ddr3,
8801 dev_priv->fsb_freq,
8802 dev_priv->mem_freq)) {
8803 DRM_INFO("failed to find known CxSR latency "
8804 "(found ddr%s fsb freq %d, mem freq %d), "
8805 "disabling CxSR\n",
8806 (dev_priv->is_ddr3 == 1) ? "3" : "2",
8807 dev_priv->fsb_freq, dev_priv->mem_freq);
8808 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03008809 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008810 dev_priv->display.update_wm = NULL;
8811 } else
8812 dev_priv->display.update_wm = pineview_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008813 } else if (IS_GEN4(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008814 dev_priv->display.update_wm = i965_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008815 } else if (IS_GEN3(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008816 dev_priv->display.update_wm = i9xx_update_wm;
8817 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008818 } else if (IS_GEN2(dev_priv)) {
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008819 if (INTEL_INFO(dev_priv)->num_pipes == 1) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008820 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008821 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008822 } else {
8823 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008824 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008825 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008826 } else {
8827 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008828 }
8829}
8830
Lyude87660502016-08-17 15:55:53 -04008831static inline int gen6_check_mailbox_status(struct drm_i915_private *dev_priv)
8832{
8833 uint32_t flags =
8834 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
8835
8836 switch (flags) {
8837 case GEN6_PCODE_SUCCESS:
8838 return 0;
8839 case GEN6_PCODE_UNIMPLEMENTED_CMD:
Chris Wilson5a9cfff2017-07-28 09:50:22 +01008840 return -ENODEV;
Lyude87660502016-08-17 15:55:53 -04008841 case GEN6_PCODE_ILLEGAL_CMD:
8842 return -ENXIO;
8843 case GEN6_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Chris Wilson7850d1c2016-08-26 11:59:26 +01008844 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Lyude87660502016-08-17 15:55:53 -04008845 return -EOVERFLOW;
8846 case GEN6_PCODE_TIMEOUT:
8847 return -ETIMEDOUT;
8848 default:
Michal Wajdeczkof0d66152017-03-28 08:45:12 +00008849 MISSING_CASE(flags);
Lyude87660502016-08-17 15:55:53 -04008850 return 0;
8851 }
8852}
8853
8854static inline int gen7_check_mailbox_status(struct drm_i915_private *dev_priv)
8855{
8856 uint32_t flags =
8857 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
8858
8859 switch (flags) {
8860 case GEN6_PCODE_SUCCESS:
8861 return 0;
8862 case GEN6_PCODE_ILLEGAL_CMD:
8863 return -ENXIO;
8864 case GEN7_PCODE_TIMEOUT:
8865 return -ETIMEDOUT;
8866 case GEN7_PCODE_ILLEGAL_DATA:
8867 return -EINVAL;
8868 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
8869 return -EOVERFLOW;
8870 default:
8871 MISSING_CASE(flags);
8872 return 0;
8873 }
8874}
8875
Tom O'Rourke151a49d2014-11-13 18:50:10 -08008876int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07008877{
Lyude87660502016-08-17 15:55:53 -04008878 int status;
8879
Jesse Barnes4fc688c2012-11-02 11:14:01 -07008880 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07008881
Chris Wilson3f5582d2016-06-30 15:32:45 +01008882 /* GEN6_PCODE_* are outside of the forcewake domain, we can
8883 * use te fw I915_READ variants to reduce the amount of work
8884 * required when reading/writing.
8885 */
8886
8887 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01008888 DRM_DEBUG_DRIVER("warning: pcode (read from mbox %x) mailbox access failed for %ps\n",
8889 mbox, __builtin_return_address(0));
Ben Widawsky42c05262012-09-26 10:34:00 -07008890 return -EAGAIN;
8891 }
8892
Chris Wilson3f5582d2016-06-30 15:32:45 +01008893 I915_WRITE_FW(GEN6_PCODE_DATA, *val);
8894 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
8895 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07008896
Chris Wilsone09a3032017-04-11 11:13:39 +01008897 if (__intel_wait_for_register_fw(dev_priv,
8898 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
8899 500, 0, NULL)) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01008900 DRM_ERROR("timeout waiting for pcode read (from mbox %x) to finish for %ps\n",
8901 mbox, __builtin_return_address(0));
Ben Widawsky42c05262012-09-26 10:34:00 -07008902 return -ETIMEDOUT;
8903 }
8904
Chris Wilson3f5582d2016-06-30 15:32:45 +01008905 *val = I915_READ_FW(GEN6_PCODE_DATA);
8906 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07008907
Lyude87660502016-08-17 15:55:53 -04008908 if (INTEL_GEN(dev_priv) > 6)
8909 status = gen7_check_mailbox_status(dev_priv);
8910 else
8911 status = gen6_check_mailbox_status(dev_priv);
8912
8913 if (status) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01008914 DRM_DEBUG_DRIVER("warning: pcode (read from mbox %x) mailbox access failed for %ps: %d\n",
8915 mbox, __builtin_return_address(0), status);
Lyude87660502016-08-17 15:55:53 -04008916 return status;
8917 }
8918
Ben Widawsky42c05262012-09-26 10:34:00 -07008919 return 0;
8920}
8921
Chris Wilson3f5582d2016-06-30 15:32:45 +01008922int sandybridge_pcode_write(struct drm_i915_private *dev_priv,
Lyude87660502016-08-17 15:55:53 -04008923 u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07008924{
Lyude87660502016-08-17 15:55:53 -04008925 int status;
8926
Jesse Barnes4fc688c2012-11-02 11:14:01 -07008927 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07008928
Chris Wilson3f5582d2016-06-30 15:32:45 +01008929 /* GEN6_PCODE_* are outside of the forcewake domain, we can
8930 * use te fw I915_READ variants to reduce the amount of work
8931 * required when reading/writing.
8932 */
8933
8934 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01008935 DRM_DEBUG_DRIVER("warning: pcode (write of 0x%08x to mbox %x) mailbox access failed for %ps\n",
8936 val, mbox, __builtin_return_address(0));
Ben Widawsky42c05262012-09-26 10:34:00 -07008937 return -EAGAIN;
8938 }
8939
Chris Wilson3f5582d2016-06-30 15:32:45 +01008940 I915_WRITE_FW(GEN6_PCODE_DATA, val);
Imre Deak8bf41b72016-11-28 17:29:27 +02008941 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
Chris Wilson3f5582d2016-06-30 15:32:45 +01008942 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07008943
Chris Wilsone09a3032017-04-11 11:13:39 +01008944 if (__intel_wait_for_register_fw(dev_priv,
8945 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
8946 500, 0, NULL)) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01008947 DRM_ERROR("timeout waiting for pcode write of 0x%08x to mbox %x to finish for %ps\n",
8948 val, mbox, __builtin_return_address(0));
Ben Widawsky42c05262012-09-26 10:34:00 -07008949 return -ETIMEDOUT;
8950 }
8951
Chris Wilson3f5582d2016-06-30 15:32:45 +01008952 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07008953
Lyude87660502016-08-17 15:55:53 -04008954 if (INTEL_GEN(dev_priv) > 6)
8955 status = gen7_check_mailbox_status(dev_priv);
8956 else
8957 status = gen6_check_mailbox_status(dev_priv);
8958
8959 if (status) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01008960 DRM_DEBUG_DRIVER("warning: pcode (write of 0x%08x to mbox %x) mailbox access failed for %ps: %d\n",
8961 val, mbox, __builtin_return_address(0), status);
Lyude87660502016-08-17 15:55:53 -04008962 return status;
8963 }
8964
Ben Widawsky42c05262012-09-26 10:34:00 -07008965 return 0;
8966}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07008967
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008968static bool skl_pcode_try_request(struct drm_i915_private *dev_priv, u32 mbox,
8969 u32 request, u32 reply_mask, u32 reply,
8970 u32 *status)
8971{
8972 u32 val = request;
8973
8974 *status = sandybridge_pcode_read(dev_priv, mbox, &val);
8975
8976 return *status || ((val & reply_mask) == reply);
8977}
8978
8979/**
8980 * skl_pcode_request - send PCODE request until acknowledgment
8981 * @dev_priv: device private
8982 * @mbox: PCODE mailbox ID the request is targeted for
8983 * @request: request ID
8984 * @reply_mask: mask used to check for request acknowledgment
8985 * @reply: value used to check for request acknowledgment
8986 * @timeout_base_ms: timeout for polling with preemption enabled
8987 *
8988 * Keep resending the @request to @mbox until PCODE acknowledges it, PCODE
Imre Deak01299362017-02-24 16:32:10 +02008989 * reports an error or an overall timeout of @timeout_base_ms+50 ms expires.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008990 * The request is acknowledged once the PCODE reply dword equals @reply after
8991 * applying @reply_mask. Polling is first attempted with preemption enabled
Imre Deak01299362017-02-24 16:32:10 +02008992 * for @timeout_base_ms and if this times out for another 50 ms with
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008993 * preemption disabled.
8994 *
8995 * Returns 0 on success, %-ETIMEDOUT in case of a timeout, <0 in case of some
8996 * other error as reported by PCODE.
8997 */
8998int skl_pcode_request(struct drm_i915_private *dev_priv, u32 mbox, u32 request,
8999 u32 reply_mask, u32 reply, int timeout_base_ms)
9000{
9001 u32 status;
9002 int ret;
9003
9004 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
9005
9006#define COND skl_pcode_try_request(dev_priv, mbox, request, reply_mask, reply, \
9007 &status)
9008
9009 /*
9010 * Prime the PCODE by doing a request first. Normally it guarantees
9011 * that a subsequent request, at most @timeout_base_ms later, succeeds.
9012 * _wait_for() doesn't guarantee when its passed condition is evaluated
9013 * first, so send the first request explicitly.
9014 */
9015 if (COND) {
9016 ret = 0;
9017 goto out;
9018 }
9019 ret = _wait_for(COND, timeout_base_ms * 1000, 10);
9020 if (!ret)
9021 goto out;
9022
9023 /*
9024 * The above can time out if the number of requests was low (2 in the
9025 * worst case) _and_ PCODE was busy for some reason even after a
9026 * (queued) request and @timeout_base_ms delay. As a workaround retry
9027 * the poll with preemption disabled to maximize the number of
Imre Deak01299362017-02-24 16:32:10 +02009028 * requests. Increase the timeout from @timeout_base_ms to 50ms to
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009029 * account for interrupts that could reduce the number of these
Imre Deak01299362017-02-24 16:32:10 +02009030 * requests, and for any quirks of the PCODE firmware that delays
9031 * the request completion.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009032 */
9033 DRM_DEBUG_KMS("PCODE timeout, retrying with preemption disabled\n");
9034 WARN_ON_ONCE(timeout_base_ms > 3);
9035 preempt_disable();
Imre Deak01299362017-02-24 16:32:10 +02009036 ret = wait_for_atomic(COND, 50);
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009037 preempt_enable();
9038
9039out:
9040 return ret ? ret : status;
9041#undef COND
9042}
9043
Ville Syrjälädd06f882014-11-10 22:55:12 +02009044static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
9045{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009046 /*
9047 * N = val - 0xb7
9048 * Slow = Fast = GPLL ref * N
9049 */
9050 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * (val - 0xb7), 1000);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009051}
9052
Fengguang Wub55dd642014-07-12 11:21:39 +02009053static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009054{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009055 return DIV_ROUND_CLOSEST(1000 * val, dev_priv->rps.gpll_ref_freq) + 0xb7;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009056}
9057
Fengguang Wub55dd642014-07-12 11:21:39 +02009058static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05309059{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009060 /*
9061 * N = val / 2
9062 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
9063 */
9064 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * val, 2 * 2 * 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05309065}
9066
Fengguang Wub55dd642014-07-12 11:21:39 +02009067static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05309068{
Ville Syrjälä1c147622014-08-18 14:42:43 +03009069 /* CHV needs even values */
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009070 return DIV_ROUND_CLOSEST(2 * 1000 * val, dev_priv->rps.gpll_ref_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05309071}
9072
Ville Syrjälä616bc822015-01-23 21:04:25 +02009073int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
9074{
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07009075 if (INTEL_GEN(dev_priv) >= 9)
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009076 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
9077 GEN9_FREQ_SCALER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009078 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009079 return chv_gpu_freq(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009080 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009081 return byt_gpu_freq(dev_priv, val);
9082 else
9083 return val * GT_FREQUENCY_MULTIPLIER;
9084}
9085
Ville Syrjälä616bc822015-01-23 21:04:25 +02009086int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
9087{
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07009088 if (INTEL_GEN(dev_priv) >= 9)
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009089 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
9090 GT_FREQUENCY_MULTIPLIER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009091 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009092 return chv_freq_opcode(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009093 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009094 return byt_freq_opcode(dev_priv, val);
9095 else
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009096 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
Deepak S22b1b2f2014-07-12 14:54:33 +05309097}
9098
Chris Wilson6ad790c2015-04-07 16:20:31 +01009099struct request_boost {
9100 struct work_struct work;
Daniel Vettereed29a52015-05-21 14:21:25 +02009101 struct drm_i915_gem_request *req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01009102};
9103
9104static void __intel_rps_boost_work(struct work_struct *work)
9105{
9106 struct request_boost *boost = container_of(work, struct request_boost, work);
Chris Wilsone61b9952015-04-27 13:41:24 +01009107 struct drm_i915_gem_request *req = boost->req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01009108
Chris Wilsonf69a02c2016-07-01 17:23:16 +01009109 if (!i915_gem_request_completed(req))
Chris Wilson7b92c1b2017-06-28 13:35:48 +01009110 gen6_rps_boost(req, NULL);
Chris Wilson6ad790c2015-04-07 16:20:31 +01009111
Chris Wilsone8a261e2016-07-20 13:31:49 +01009112 i915_gem_request_put(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01009113 kfree(boost);
9114}
9115
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01009116void intel_queue_rps_boost_for_request(struct drm_i915_gem_request *req)
Chris Wilson6ad790c2015-04-07 16:20:31 +01009117{
9118 struct request_boost *boost;
9119
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01009120 if (req == NULL || INTEL_GEN(req->i915) < 6)
Chris Wilson6ad790c2015-04-07 16:20:31 +01009121 return;
9122
Chris Wilsonf69a02c2016-07-01 17:23:16 +01009123 if (i915_gem_request_completed(req))
Chris Wilsone61b9952015-04-27 13:41:24 +01009124 return;
9125
Chris Wilson6ad790c2015-04-07 16:20:31 +01009126 boost = kmalloc(sizeof(*boost), GFP_ATOMIC);
9127 if (boost == NULL)
9128 return;
9129
Chris Wilsone8a261e2016-07-20 13:31:49 +01009130 boost->req = i915_gem_request_get(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01009131
9132 INIT_WORK(&boost->work, __intel_rps_boost_work);
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01009133 queue_work(req->i915->wq, &boost->work);
Chris Wilson6ad790c2015-04-07 16:20:31 +01009134}
9135
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00009136void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01009137{
Daniel Vetterf742a552013-12-06 10:17:53 +01009138 mutex_init(&dev_priv->rps.hw_lock);
9139
Chris Wilson54b4f682016-07-21 21:16:19 +01009140 INIT_DELAYED_WORK(&dev_priv->rps.autoenable_work,
9141 __intel_autoenable_gt_powersave);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01009142 atomic_set(&dev_priv->rps.num_waiters, 0);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03009143
Paulo Zanoni33688d92014-03-07 20:08:19 -03009144 dev_priv->pm.suspended = false;
Imre Deak1f814da2015-12-16 02:52:19 +02009145 atomic_set(&dev_priv->pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01009146}
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009147
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009148static u64 vlv_residency_raw(struct drm_i915_private *dev_priv,
9149 const i915_reg_t reg)
9150{
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009151 u32 lower, upper, tmp;
Chris Wilson71cc2b12017-03-24 16:54:18 +00009152 int loop = 2;
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009153
9154 /* The register accessed do not need forcewake. We borrow
9155 * uncore lock to prevent concurrent access to range reg.
9156 */
9157 spin_lock_irq(&dev_priv->uncore.lock);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009158
9159 /* vlv and chv residency counters are 40 bits in width.
9160 * With a control bit, we can choose between upper or lower
9161 * 32bit window into this counter.
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009162 *
9163 * Although we always use the counter in high-range mode elsewhere,
9164 * userspace may attempt to read the value before rc6 is initialised,
9165 * before we have set the default VLV_COUNTER_CONTROL value. So always
9166 * set the high bit to be safe.
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009167 */
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009168 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9169 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009170 upper = I915_READ_FW(reg);
9171 do {
9172 tmp = upper;
9173
9174 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9175 _MASKED_BIT_DISABLE(VLV_COUNT_RANGE_HIGH));
9176 lower = I915_READ_FW(reg);
9177
9178 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9179 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
9180 upper = I915_READ_FW(reg);
Chris Wilson71cc2b12017-03-24 16:54:18 +00009181 } while (upper != tmp && --loop);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009182
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009183 /* Everywhere else we always use VLV_COUNTER_CONTROL with the
9184 * VLV_COUNT_RANGE_HIGH bit set - so it is safe to leave it set
9185 * now.
9186 */
9187
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009188 spin_unlock_irq(&dev_priv->uncore.lock);
9189
9190 return lower | (u64)upper << 8;
9191}
9192
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009193u64 intel_rc6_residency_us(struct drm_i915_private *dev_priv,
9194 const i915_reg_t reg)
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009195{
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009196 u64 time_hw, units, div;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009197
9198 if (!intel_enable_rc6())
9199 return 0;
9200
9201 intel_runtime_pm_get(dev_priv);
9202
9203 /* On VLV and CHV, residency time is in CZ units rather than 1.28us */
9204 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009205 units = 1000;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009206 div = dev_priv->czclk_freq;
9207
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009208 time_hw = vlv_residency_raw(dev_priv, reg);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009209 } else if (IS_GEN9_LP(dev_priv)) {
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009210 units = 1000;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009211 div = 1200; /* 833.33ns */
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009212
9213 time_hw = I915_READ(reg);
9214 } else {
9215 units = 128000; /* 1.28us */
9216 div = 100000;
9217
9218 time_hw = I915_READ(reg);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009219 }
9220
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009221 intel_runtime_pm_put(dev_priv);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009222 return DIV_ROUND_UP_ULL(time_hw * units, div);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009223}