blob: f904bf73dbd6936ee04cf1fa02000a25284e62e9 [file] [log] [blame]
Eugeni Dodonov85208be2012-04-16 22:20:34 -03001/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eugeni Dodonov <eugeni.dodonov@intel.com>
25 *
26 */
27
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -030028#include <linux/cpufreq.h>
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -070029#include <drm/drm_plane_helper.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030030#include "i915_drv.h"
31#include "intel_drv.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020032#include "../../../platform/x86/intel_ips.h"
33#include <linux/module.h>
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +020034#include <drm/drm_atomic_helper.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030035
Ben Widawskydc39fff2013-10-18 12:32:07 -070036/**
Jani Nikula18afd442016-01-18 09:19:48 +020037 * DOC: RC6
38 *
Ben Widawskydc39fff2013-10-18 12:32:07 -070039 * RC6 is a special power stage which allows the GPU to enter an very
40 * low-voltage mode when idle, using down to 0V while at this stage. This
41 * stage is entered automatically when the GPU is idle when RC6 support is
42 * enabled, and as soon as new workload arises GPU wakes up automatically as well.
43 *
44 * There are different RC6 modes available in Intel GPU, which differentiate
45 * among each other with the latency required to enter and leave RC6 and
46 * voltage consumed by the GPU in different states.
47 *
48 * The combination of the following flags define which states GPU is allowed
49 * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
50 * RC6pp is deepest RC6. Their support by hardware varies according to the
51 * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
52 * which brings the most power savings; deeper states save more power, but
53 * require higher latency to switch to and wake up.
54 */
55#define INTEL_RC6_ENABLE (1<<0)
56#define INTEL_RC6p_ENABLE (1<<1)
57#define INTEL_RC6pp_ENABLE (1<<2)
58
Ville Syrjälä46f16e62016-10-31 22:37:22 +020059static void gen9_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppalab033bb62016-06-07 17:19:04 +030060{
Ville Syrjälä93564042017-08-24 22:10:51 +030061 if (HAS_LLC(dev_priv)) {
62 /*
63 * WaCompressedResourceDisplayNewHashMode:skl,kbl
64 * Display WA#0390: skl,kbl
65 *
66 * Must match Sampler, Pixel Back End, and Media. See
67 * WaCompressedResourceSamplerPbeMediaNewHashMode.
68 */
69 I915_WRITE(CHICKEN_PAR1_1,
70 I915_READ(CHICKEN_PAR1_1) |
71 SKL_DE_COMPRESSED_HASH_MODE);
72 }
73
Rodrigo Vivi82525c12017-06-08 08:50:00 -070074 /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl,cfl */
Mika Kuoppalab033bb62016-06-07 17:19:04 +030075 I915_WRITE(CHICKEN_PAR1_1,
76 I915_READ(CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
77
Rodrigo Vivi82525c12017-06-08 08:50:00 -070078 /* WaEnableChickenDCPR:skl,bxt,kbl,glk,cfl */
Mika Kuoppala590e8ff2016-06-07 17:19:13 +030079 I915_WRITE(GEN8_CHICKEN_DCPR_1,
80 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
Mika Kuoppala0f78dee2016-06-07 17:19:16 +030081
Rodrigo Vivi82525c12017-06-08 08:50:00 -070082 /* WaFbcTurnOffFbcWatermark:skl,bxt,kbl,cfl */
83 /* WaFbcWakeMemOn:skl,bxt,kbl,glk,cfl */
Mika Kuoppala303d4ea2016-06-07 17:19:17 +030084 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
85 DISP_FBC_WM_DIS |
86 DISP_FBC_MEMORY_WAKE);
Mika Kuoppalad1b4eef2016-06-07 17:19:19 +030087
Rodrigo Vivi82525c12017-06-08 08:50:00 -070088 /* WaFbcHighMemBwCorruptionAvoidance:skl,bxt,kbl,cfl */
Mika Kuoppalad1b4eef2016-06-07 17:19:19 +030089 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
90 ILK_DPFC_DISABLE_DUMMY0);
Praveen Paneri32087d12017-08-03 23:02:10 +053091
92 if (IS_SKYLAKE(dev_priv)) {
93 /* WaDisableDopClockGating */
94 I915_WRITE(GEN7_MISCCPCTL, I915_READ(GEN7_MISCCPCTL)
95 & ~GEN7_DOP_CLOCK_GATE_ENABLE);
96 }
Mika Kuoppalab033bb62016-06-07 17:19:04 +030097}
98
Ville Syrjälä46f16e62016-10-31 22:37:22 +020099static void bxt_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deaka82abe42015-03-27 14:00:04 +0200100{
Ville Syrjälä46f16e62016-10-31 22:37:22 +0200101 gen9_init_clock_gating(dev_priv);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +0200102
Nick Hoatha7546152015-06-29 14:07:32 +0100103 /* WaDisableSDEUnitClockGating:bxt */
104 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
105 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
106
Imre Deak32608ca2015-03-11 11:10:27 +0200107 /*
108 * FIXME:
Ben Widawsky868434c2015-03-11 10:49:32 +0200109 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
Imre Deak32608ca2015-03-11 11:10:27 +0200110 */
Imre Deak32608ca2015-03-11 11:10:27 +0200111 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
Ben Widawsky868434c2015-03-11 10:49:32 +0200112 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
Imre Deakd965e7ac2015-12-01 10:23:52 +0200113
114 /*
115 * Wa: Backlight PWM may stop in the asserted state, causing backlight
116 * to stay fully on.
117 */
Jani Nikula8aeaf642017-02-15 17:21:37 +0200118 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
119 PWM1_GATING_DIS | PWM2_GATING_DIS);
Imre Deaka82abe42015-03-27 14:00:04 +0200120}
121
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200122static void glk_init_clock_gating(struct drm_i915_private *dev_priv)
123{
Rodrigo Vivi8f067832017-09-05 12:30:13 -0700124 u32 val;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200125 gen9_init_clock_gating(dev_priv);
126
127 /*
128 * WaDisablePWMClockGating:glk
129 * Backlight PWM may stop in the asserted state, causing backlight
130 * to stay fully on.
131 */
132 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
133 PWM1_GATING_DIS | PWM2_GATING_DIS);
Ander Conselvan de Oliveiraf4f4b592017-02-22 08:34:29 +0200134
135 /* WaDDIIOTimeout:glk */
136 if (IS_GLK_REVID(dev_priv, 0, GLK_REVID_A1)) {
137 u32 val = I915_READ(CHICKEN_MISC_2);
138 val &= ~(GLK_CL0_PWR_DOWN |
139 GLK_CL1_PWR_DOWN |
140 GLK_CL2_PWR_DOWN);
141 I915_WRITE(CHICKEN_MISC_2, val);
142 }
143
Rodrigo Vivi8f067832017-09-05 12:30:13 -0700144 /* Display WA #1133: WaFbcSkipSegments:glk */
145 val = I915_READ(ILK_DPFC_CHICKEN);
146 val &= ~GLK_SKIP_SEG_COUNT_MASK;
147 val |= GLK_SKIP_SEG_EN | GLK_SKIP_SEG_COUNT(1);
148 I915_WRITE(ILK_DPFC_CHICKEN, val);
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200149}
150
Ville Syrjälä148ac1f2016-10-31 22:37:16 +0200151static void i915_pineview_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200152{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200153 u32 tmp;
154
155 tmp = I915_READ(CLKCFG);
156
157 switch (tmp & CLKCFG_FSB_MASK) {
158 case CLKCFG_FSB_533:
159 dev_priv->fsb_freq = 533; /* 133*4 */
160 break;
161 case CLKCFG_FSB_800:
162 dev_priv->fsb_freq = 800; /* 200*4 */
163 break;
164 case CLKCFG_FSB_667:
165 dev_priv->fsb_freq = 667; /* 167*4 */
166 break;
167 case CLKCFG_FSB_400:
168 dev_priv->fsb_freq = 400; /* 100*4 */
169 break;
170 }
171
172 switch (tmp & CLKCFG_MEM_MASK) {
173 case CLKCFG_MEM_533:
174 dev_priv->mem_freq = 533;
175 break;
176 case CLKCFG_MEM_667:
177 dev_priv->mem_freq = 667;
178 break;
179 case CLKCFG_MEM_800:
180 dev_priv->mem_freq = 800;
181 break;
182 }
183
184 /* detect pineview DDR3 setting */
185 tmp = I915_READ(CSHRDDR3CTL);
186 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
187}
188
Ville Syrjälä148ac1f2016-10-31 22:37:16 +0200189static void i915_ironlake_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200190{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200191 u16 ddrpll, csipll;
192
193 ddrpll = I915_READ16(DDRMPLL1);
194 csipll = I915_READ16(CSIPLL0);
195
196 switch (ddrpll & 0xff) {
197 case 0xc:
198 dev_priv->mem_freq = 800;
199 break;
200 case 0x10:
201 dev_priv->mem_freq = 1066;
202 break;
203 case 0x14:
204 dev_priv->mem_freq = 1333;
205 break;
206 case 0x18:
207 dev_priv->mem_freq = 1600;
208 break;
209 default:
210 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
211 ddrpll & 0xff);
212 dev_priv->mem_freq = 0;
213 break;
214 }
215
Daniel Vetter20e4d402012-08-08 23:35:39 +0200216 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200217
218 switch (csipll & 0x3ff) {
219 case 0x00c:
220 dev_priv->fsb_freq = 3200;
221 break;
222 case 0x00e:
223 dev_priv->fsb_freq = 3733;
224 break;
225 case 0x010:
226 dev_priv->fsb_freq = 4266;
227 break;
228 case 0x012:
229 dev_priv->fsb_freq = 4800;
230 break;
231 case 0x014:
232 dev_priv->fsb_freq = 5333;
233 break;
234 case 0x016:
235 dev_priv->fsb_freq = 5866;
236 break;
237 case 0x018:
238 dev_priv->fsb_freq = 6400;
239 break;
240 default:
241 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
242 csipll & 0x3ff);
243 dev_priv->fsb_freq = 0;
244 break;
245 }
246
247 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200248 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200249 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200250 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200251 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200252 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200253 }
254}
255
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300256static const struct cxsr_latency cxsr_latency_table[] = {
257 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
258 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
259 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
260 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
261 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
262
263 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
264 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
265 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
266 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
267 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
268
269 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
270 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
271 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
272 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
273 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
274
275 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
276 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
277 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
278 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
279 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
280
281 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
282 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
283 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
284 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
285 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
286
287 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
288 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
289 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
290 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
291 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
292};
293
Tvrtko Ursulin44a655c2016-10-13 11:09:23 +0100294static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
295 bool is_ddr3,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300296 int fsb,
297 int mem)
298{
299 const struct cxsr_latency *latency;
300 int i;
301
302 if (fsb == 0 || mem == 0)
303 return NULL;
304
305 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
306 latency = &cxsr_latency_table[i];
307 if (is_desktop == latency->is_desktop &&
308 is_ddr3 == latency->is_ddr3 &&
309 fsb == latency->fsb_freq && mem == latency->mem_freq)
310 return latency;
311 }
312
313 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
314
315 return NULL;
316}
317
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200318static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
319{
320 u32 val;
321
Sagar Arun Kamble9f817502017-10-10 22:30:05 +0100322 mutex_lock(&dev_priv->pcu_lock);
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200323
324 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
325 if (enable)
326 val &= ~FORCE_DDR_HIGH_FREQ;
327 else
328 val |= FORCE_DDR_HIGH_FREQ;
329 val &= ~FORCE_DDR_LOW_FREQ;
330 val |= FORCE_DDR_FREQ_REQ_ACK;
331 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
332
333 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
334 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
335 DRM_ERROR("timed out waiting for Punit DDR DVFS request\n");
336
Sagar Arun Kamble9f817502017-10-10 22:30:05 +0100337 mutex_unlock(&dev_priv->pcu_lock);
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200338}
339
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200340static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
341{
342 u32 val;
343
Sagar Arun Kamble9f817502017-10-10 22:30:05 +0100344 mutex_lock(&dev_priv->pcu_lock);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200345
346 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
347 if (enable)
348 val |= DSP_MAXFIFO_PM5_ENABLE;
349 else
350 val &= ~DSP_MAXFIFO_PM5_ENABLE;
351 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
352
Sagar Arun Kamble9f817502017-10-10 22:30:05 +0100353 mutex_unlock(&dev_priv->pcu_lock);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200354}
355
Ville Syrjäläf4998962015-03-10 17:02:21 +0200356#define FW_WM(value, plane) \
357 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
358
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200359static bool _intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300360{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200361 bool was_enabled;
Imre Deak5209b1f2014-07-01 12:36:17 +0300362 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300363
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100364 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200365 was_enabled = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300366 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300367 POSTING_READ(FW_BLC_SELF_VLV);
Jani Nikulac0f86832016-12-07 12:13:04 +0200368 } else if (IS_G4X(dev_priv) || IS_I965GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200369 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300370 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300371 POSTING_READ(FW_BLC_SELF);
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +0200372 } else if (IS_PINEVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200373 val = I915_READ(DSPFW3);
374 was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
375 if (enable)
376 val |= PINEVIEW_SELF_REFRESH_EN;
377 else
378 val &= ~PINEVIEW_SELF_REFRESH_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300379 I915_WRITE(DSPFW3, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300380 POSTING_READ(DSPFW3);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100381 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200382 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300383 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
384 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
385 I915_WRITE(FW_BLC_SELF, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300386 POSTING_READ(FW_BLC_SELF);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100387 } else if (IS_I915GM(dev_priv)) {
Ville Syrjäläacb91352016-07-29 17:57:02 +0300388 /*
389 * FIXME can't find a bit like this for 915G, and
390 * and yet it does have the related watermark in
391 * FW_BLC_SELF. What's going on?
392 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200393 was_enabled = I915_READ(INSTPM) & INSTPM_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300394 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
395 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
396 I915_WRITE(INSTPM, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300397 POSTING_READ(INSTPM);
Imre Deak5209b1f2014-07-01 12:36:17 +0300398 } else {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200399 return false;
Imre Deak5209b1f2014-07-01 12:36:17 +0300400 }
401
Ville Syrjälä1489bba2017-03-02 19:15:07 +0200402 trace_intel_memory_cxsr(dev_priv, was_enabled, enable);
403
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200404 DRM_DEBUG_KMS("memory self-refresh is %s (was %s)\n",
405 enableddisabled(enable),
406 enableddisabled(was_enabled));
407
408 return was_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300409}
410
Ville Syrjälä62571fc2017-04-21 21:14:23 +0300411/**
412 * intel_set_memory_cxsr - Configure CxSR state
413 * @dev_priv: i915 device
414 * @enable: Allow vs. disallow CxSR
415 *
416 * Allow or disallow the system to enter a special CxSR
417 * (C-state self refresh) state. What typically happens in CxSR mode
418 * is that several display FIFOs may get combined into a single larger
419 * FIFO for a particular plane (so called max FIFO mode) to allow the
420 * system to defer memory fetches longer, and the memory will enter
421 * self refresh.
422 *
423 * Note that enabling CxSR does not guarantee that the system enter
424 * this special mode, nor does it guarantee that the system stays
425 * in that mode once entered. So this just allows/disallows the system
426 * to autonomously utilize the CxSR mode. Other factors such as core
427 * C-states will affect when/if the system actually enters/exits the
428 * CxSR mode.
429 *
430 * Note that on VLV/CHV this actually only controls the max FIFO mode,
431 * and the system is free to enter/exit memory self refresh at any time
432 * even when the use of CxSR has been disallowed.
433 *
434 * While the system is actually in the CxSR/max FIFO mode, some plane
435 * control registers will not get latched on vblank. Thus in order to
436 * guarantee the system will respond to changes in the plane registers
437 * we must always disallow CxSR prior to making changes to those registers.
438 * Unfortunately the system will re-evaluate the CxSR conditions at
439 * frame start which happens after vblank start (which is when the plane
440 * registers would get latched), so we can't proceed with the plane update
441 * during the same frame where we disallowed CxSR.
442 *
443 * Certain platforms also have a deeper HPLL SR mode. Fortunately the
444 * HPLL SR mode depends on CxSR itself, so we don't have to hand hold
445 * the hardware w.r.t. HPLL SR when writing to plane registers.
446 * Disallowing just CxSR is sufficient.
447 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200448bool intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200449{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200450 bool ret;
451
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200452 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200453 ret = _intel_set_memory_cxsr(dev_priv, enable);
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300454 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
455 dev_priv->wm.vlv.cxsr = enable;
456 else if (IS_G4X(dev_priv))
457 dev_priv->wm.g4x.cxsr = enable;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200458 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200459
460 return ret;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200461}
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200462
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300463/*
464 * Latency for FIFO fetches is dependent on several factors:
465 * - memory configuration (speed, channels)
466 * - chipset
467 * - current MCH state
468 * It can be fairly high in some situations, so here we assume a fairly
469 * pessimal value. It's a tradeoff between extra memory fetches (if we
470 * set this value too high, the FIFO will fetch frequently to stay full)
471 * and power consumption (set it too low to save power and we might see
472 * FIFO underruns and display "flicker").
473 *
474 * A value of 5us seems to be a good balance; safe for very low end
475 * platforms but not overly aggressive on lower latency configs.
476 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100477static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300478
Ville Syrjäläb5004722015-03-05 21:19:47 +0200479#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
480 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
481
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200482static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
Ville Syrjäläb5004722015-03-05 21:19:47 +0200483{
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200484 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200485 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200486 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200487 enum pipe pipe = crtc->pipe;
488 int sprite0_start, sprite1_start;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200489
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200490 switch (pipe) {
Ville Syrjäläb5004722015-03-05 21:19:47 +0200491 uint32_t dsparb, dsparb2, dsparb3;
492 case PIPE_A:
493 dsparb = I915_READ(DSPARB);
494 dsparb2 = I915_READ(DSPARB2);
495 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
496 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
497 break;
498 case PIPE_B:
499 dsparb = I915_READ(DSPARB);
500 dsparb2 = I915_READ(DSPARB2);
501 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
502 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
503 break;
504 case PIPE_C:
505 dsparb2 = I915_READ(DSPARB2);
506 dsparb3 = I915_READ(DSPARB3);
507 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
508 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
509 break;
510 default:
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200511 MISSING_CASE(pipe);
512 return;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200513 }
514
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200515 fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
516 fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
517 fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
518 fifo_state->plane[PLANE_CURSOR] = 63;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200519}
520
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200521static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300522{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300523 uint32_t dsparb = I915_READ(DSPARB);
524 int size;
525
526 size = dsparb & 0x7f;
527 if (plane)
528 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
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 i830_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 & 0x1ff;
542 if (plane)
543 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
544 size >>= 1; /* Convert to cachelines */
545
546 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
547 plane ? "B" : "A", size);
548
549 return size;
550}
551
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200552static int i845_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300553{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300554 uint32_t dsparb = I915_READ(DSPARB);
555 int size;
556
557 size = dsparb & 0x7f;
558 size >>= 2; /* Convert to cachelines */
559
560 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
561 plane ? "B" : "A",
562 size);
563
564 return size;
565}
566
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300567/* Pineview has different values for various configs */
568static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300569 .fifo_size = PINEVIEW_DISPLAY_FIFO,
570 .max_wm = PINEVIEW_MAX_WM,
571 .default_wm = PINEVIEW_DFT_WM,
572 .guard_size = PINEVIEW_GUARD_WM,
573 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300574};
575static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300576 .fifo_size = PINEVIEW_DISPLAY_FIFO,
577 .max_wm = PINEVIEW_MAX_WM,
578 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
579 .guard_size = PINEVIEW_GUARD_WM,
580 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300581};
582static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300583 .fifo_size = PINEVIEW_CURSOR_FIFO,
584 .max_wm = PINEVIEW_CURSOR_MAX_WM,
585 .default_wm = PINEVIEW_CURSOR_DFT_WM,
586 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
587 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300588};
589static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300590 .fifo_size = PINEVIEW_CURSOR_FIFO,
591 .max_wm = PINEVIEW_CURSOR_MAX_WM,
592 .default_wm = PINEVIEW_CURSOR_DFT_WM,
593 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
594 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300595};
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300596static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300597 .fifo_size = I965_CURSOR_FIFO,
598 .max_wm = I965_CURSOR_MAX_WM,
599 .default_wm = I965_CURSOR_DFT_WM,
600 .guard_size = 2,
601 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300602};
603static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300604 .fifo_size = I945_FIFO_SIZE,
605 .max_wm = I915_MAX_WM,
606 .default_wm = 1,
607 .guard_size = 2,
608 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300609};
610static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300611 .fifo_size = I915_FIFO_SIZE,
612 .max_wm = I915_MAX_WM,
613 .default_wm = 1,
614 .guard_size = 2,
615 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300616};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300617static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300618 .fifo_size = I855GM_FIFO_SIZE,
619 .max_wm = I915_MAX_WM,
620 .default_wm = 1,
621 .guard_size = 2,
622 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300623};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300624static const struct intel_watermark_params i830_bc_wm_info = {
625 .fifo_size = I855GM_FIFO_SIZE,
626 .max_wm = I915_MAX_WM/2,
627 .default_wm = 1,
628 .guard_size = 2,
629 .cacheline_size = I830_FIFO_LINE_SIZE,
630};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200631static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300632 .fifo_size = I830_FIFO_SIZE,
633 .max_wm = I915_MAX_WM,
634 .default_wm = 1,
635 .guard_size = 2,
636 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300637};
638
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300639/**
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300640 * intel_wm_method1 - Method 1 / "small buffer" watermark formula
641 * @pixel_rate: Pipe pixel rate in kHz
642 * @cpp: Plane bytes per pixel
643 * @latency: Memory wakeup latency in 0.1us units
644 *
645 * Compute the watermark using the method 1 or "small buffer"
646 * formula. The caller may additonally add extra cachelines
647 * to account for TLB misses and clock crossings.
648 *
649 * This method is concerned with the short term drain rate
650 * of the FIFO, ie. it does not account for blanking periods
651 * which would effectively reduce the average drain rate across
652 * a longer period. The name "small" refers to the fact the
653 * FIFO is relatively small compared to the amount of data
654 * fetched.
655 *
656 * The FIFO level vs. time graph might look something like:
657 *
658 * |\ |\
659 * | \ | \
660 * __---__---__ (- plane active, _ blanking)
661 * -> time
662 *
663 * or perhaps like this:
664 *
665 * |\|\ |\|\
666 * __----__----__ (- plane active, _ blanking)
667 * -> time
668 *
669 * Returns:
670 * The watermark in bytes
671 */
672static unsigned int intel_wm_method1(unsigned int pixel_rate,
673 unsigned int cpp,
674 unsigned int latency)
675{
676 uint64_t ret;
677
678 ret = (uint64_t) pixel_rate * cpp * latency;
679 ret = DIV_ROUND_UP_ULL(ret, 10000);
680
681 return ret;
682}
683
684/**
685 * intel_wm_method2 - Method 2 / "large buffer" watermark formula
686 * @pixel_rate: Pipe pixel rate in kHz
687 * @htotal: Pipe horizontal total
688 * @width: Plane width in pixels
689 * @cpp: Plane bytes per pixel
690 * @latency: Memory wakeup latency in 0.1us units
691 *
692 * Compute the watermark using the method 2 or "large buffer"
693 * formula. The caller may additonally add extra cachelines
694 * to account for TLB misses and clock crossings.
695 *
696 * This method is concerned with the long term drain rate
697 * of the FIFO, ie. it does account for blanking periods
698 * which effectively reduce the average drain rate across
699 * a longer period. The name "large" refers to the fact the
700 * FIFO is relatively large compared to the amount of data
701 * fetched.
702 *
703 * The FIFO level vs. time graph might look something like:
704 *
705 * |\___ |\___
706 * | \___ | \___
707 * | \ | \
708 * __ --__--__--__--__--__--__ (- plane active, _ blanking)
709 * -> time
710 *
711 * Returns:
712 * The watermark in bytes
713 */
714static unsigned int intel_wm_method2(unsigned int pixel_rate,
715 unsigned int htotal,
716 unsigned int width,
717 unsigned int cpp,
718 unsigned int latency)
719{
720 unsigned int ret;
721
722 /*
723 * FIXME remove once all users are computing
724 * watermarks in the correct place.
725 */
726 if (WARN_ON_ONCE(htotal == 0))
727 htotal = 1;
728
729 ret = (latency * pixel_rate) / (htotal * 10000);
730 ret = (ret + 1) * width * cpp;
731
732 return ret;
733}
734
735/**
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300736 * intel_calculate_wm - calculate watermark level
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300737 * @pixel_rate: pixel clock
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300738 * @wm: chip FIFO params
Ville Syrjäläac484962016-01-20 21:05:26 +0200739 * @cpp: bytes per pixel
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300740 * @latency_ns: memory latency for the platform
741 *
742 * Calculate the watermark level (the level at which the display plane will
743 * start fetching from memory again). Each chip has a different display
744 * FIFO size and allocation, so the caller needs to figure that out and pass
745 * in the correct intel_watermark_params structure.
746 *
747 * As the pixel clock runs, the FIFO will be drained at a rate that depends
748 * on the pixel size. When it reaches the watermark level, it'll start
749 * fetching FIFO line sized based chunks from memory until the FIFO fills
750 * past the watermark point. If the FIFO drains completely, a FIFO underrun
751 * will occur, and a display engine hang could result.
752 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300753static unsigned int intel_calculate_wm(int pixel_rate,
754 const struct intel_watermark_params *wm,
755 int fifo_size, int cpp,
756 unsigned int latency_ns)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300757{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300758 int entries, wm_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300759
760 /*
761 * Note: we need to make sure we don't overflow for various clock &
762 * latency values.
763 * clocks go from a few thousand to several hundred thousand.
764 * latency is usually a few thousand
765 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300766 entries = intel_wm_method1(pixel_rate, cpp,
767 latency_ns / 100);
768 entries = DIV_ROUND_UP(entries, wm->cacheline_size) +
769 wm->guard_size;
770 DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300771
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300772 wm_size = fifo_size - entries;
773 DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300774
775 /* Don't promote wm_size to unsigned... */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300776 if (wm_size > wm->max_wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300777 wm_size = wm->max_wm;
778 if (wm_size <= 0)
779 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300780
781 /*
782 * Bspec seems to indicate that the value shouldn't be lower than
783 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
784 * Lets go for 8 which is the burst size since certain platforms
785 * already use a hardcoded 8 (which is what the spec says should be
786 * done).
787 */
788 if (wm_size <= 8)
789 wm_size = 8;
790
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300791 return wm_size;
792}
793
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300794static bool is_disabling(int old, int new, int threshold)
795{
796 return old >= threshold && new < threshold;
797}
798
799static bool is_enabling(int old, int new, int threshold)
800{
801 return old < threshold && new >= threshold;
802}
803
Ville Syrjälä6d5019b2017-04-21 21:14:20 +0300804static int intel_wm_num_levels(struct drm_i915_private *dev_priv)
805{
806 return dev_priv->wm.max_level + 1;
807}
808
Ville Syrjälä24304d812017-03-14 17:10:49 +0200809static bool intel_wm_plane_visible(const struct intel_crtc_state *crtc_state,
810 const struct intel_plane_state *plane_state)
811{
812 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
813
814 /* FIXME check the 'enable' instead */
815 if (!crtc_state->base.active)
816 return false;
817
818 /*
819 * Treat cursor with fb as always visible since cursor updates
820 * can happen faster than the vrefresh rate, and the current
821 * watermark code doesn't handle that correctly. Cursor updates
822 * which set/clear the fb or change the cursor size are going
823 * to get throttled by intel_legacy_cursor_update() to work
824 * around this problem with the watermark code.
825 */
826 if (plane->id == PLANE_CURSOR)
827 return plane_state->base.fb != NULL;
828 else
829 return plane_state->base.visible;
830}
831
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200832static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300833{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200834 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300835
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200836 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200837 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300838 if (enabled)
839 return NULL;
840 enabled = crtc;
841 }
842 }
843
844 return enabled;
845}
846
Ville Syrjälä432081b2016-10-31 22:37:03 +0200847static void pineview_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300848{
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200849 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200850 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300851 const struct cxsr_latency *latency;
852 u32 reg;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300853 unsigned int wm;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300854
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100855 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
856 dev_priv->is_ddr3,
857 dev_priv->fsb_freq,
858 dev_priv->mem_freq);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300859 if (!latency) {
860 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300861 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300862 return;
863 }
864
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200865 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300866 if (crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200867 const struct drm_display_mode *adjusted_mode =
868 &crtc->config->base.adjusted_mode;
869 const struct drm_framebuffer *fb =
870 crtc->base.primary->state->fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200871 int cpp = fb->format->cpp[0];
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300872 int clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300873
874 /* Display SR */
875 wm = intel_calculate_wm(clock, &pineview_display_wm,
876 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200877 cpp, latency->display_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300878 reg = I915_READ(DSPFW1);
879 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200880 reg |= FW_WM(wm, SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300881 I915_WRITE(DSPFW1, reg);
882 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
883
884 /* cursor SR */
885 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
886 pineview_display_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300887 4, latency->cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300888 reg = I915_READ(DSPFW3);
889 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200890 reg |= FW_WM(wm, CURSOR_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300891 I915_WRITE(DSPFW3, reg);
892
893 /* Display HPLL off SR */
894 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
895 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200896 cpp, latency->display_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300897 reg = I915_READ(DSPFW3);
898 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200899 reg |= FW_WM(wm, HPLL_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300900 I915_WRITE(DSPFW3, reg);
901
902 /* cursor HPLL off SR */
903 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
904 pineview_display_hplloff_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300905 4, latency->cursor_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300906 reg = I915_READ(DSPFW3);
907 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200908 reg |= FW_WM(wm, HPLL_CURSOR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300909 I915_WRITE(DSPFW3, reg);
910 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
911
Imre Deak5209b1f2014-07-01 12:36:17 +0300912 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300913 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300914 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300915 }
916}
917
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300918/*
919 * Documentation says:
920 * "If the line size is small, the TLB fetches can get in the way of the
921 * data fetches, causing some lag in the pixel data return which is not
922 * accounted for in the above formulas. The following adjustment only
923 * needs to be applied if eight whole lines fit in the buffer at once.
924 * The WM is adjusted upwards by the difference between the FIFO size
925 * and the size of 8 whole lines. This adjustment is always performed
926 * in the actual pixel depth regardless of whether FBC is enabled or not."
927 */
Chris Wilson1a1f1282017-11-07 14:03:38 +0000928static unsigned int g4x_tlb_miss_wa(int fifo_size, int width, int cpp)
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300929{
930 int tlb_miss = fifo_size * 64 - width * cpp * 8;
931
932 return max(0, tlb_miss);
933}
934
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300935static void g4x_write_wm_values(struct drm_i915_private *dev_priv,
936 const struct g4x_wm_values *wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300937{
Ville Syrjäläe93329a2017-04-21 21:14:31 +0300938 enum pipe pipe;
939
940 for_each_pipe(dev_priv, pipe)
941 trace_g4x_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
942
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300943 I915_WRITE(DSPFW1,
944 FW_WM(wm->sr.plane, SR) |
945 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
946 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
947 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
948 I915_WRITE(DSPFW2,
949 (wm->fbc_en ? DSPFW_FBC_SR_EN : 0) |
950 FW_WM(wm->sr.fbc, FBC_SR) |
951 FW_WM(wm->hpll.fbc, FBC_HPLL_SR) |
952 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEB) |
953 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
954 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
955 I915_WRITE(DSPFW3,
956 (wm->hpll_en ? DSPFW_HPLL_SR_EN : 0) |
957 FW_WM(wm->sr.cursor, CURSOR_SR) |
958 FW_WM(wm->hpll.cursor, HPLL_CURSOR) |
959 FW_WM(wm->hpll.plane, HPLL_SR));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300960
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300961 POSTING_READ(DSPFW1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300962}
963
Ville Syrjälä15665972015-03-10 16:16:28 +0200964#define FW_WM_VLV(value, plane) \
965 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
966
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200967static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200968 const struct vlv_wm_values *wm)
969{
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200970 enum pipe pipe;
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200971
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200972 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläc137d662017-03-02 19:15:06 +0200973 trace_vlv_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
974
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200975 I915_WRITE(VLV_DDL(pipe),
976 (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
977 (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
978 (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
979 (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
980 }
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200981
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +0200982 /*
983 * Zero the (unused) WM1 watermarks, and also clear all the
984 * high order bits so that there are no out of bounds values
985 * present in the registers during the reprogramming.
986 */
987 I915_WRITE(DSPHOWM, 0);
988 I915_WRITE(DSPHOWM1, 0);
989 I915_WRITE(DSPFW4, 0);
990 I915_WRITE(DSPFW5, 0);
991 I915_WRITE(DSPFW6, 0);
992
Ville Syrjäläae801522015-03-05 21:19:49 +0200993 I915_WRITE(DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +0200994 FW_WM(wm->sr.plane, SR) |
Ville Syrjälä1b313892016-11-28 19:37:08 +0200995 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
996 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
997 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200998 I915_WRITE(DSPFW2,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200999 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
1000 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
1001 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Ville Syrjäläae801522015-03-05 21:19:49 +02001002 I915_WRITE(DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +02001003 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +02001004
1005 if (IS_CHERRYVIEW(dev_priv)) {
1006 I915_WRITE(DSPFW7_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001007 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1008 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001009 I915_WRITE(DSPFW8_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001010 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
1011 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
Ville Syrjäläae801522015-03-05 21:19:49 +02001012 I915_WRITE(DSPFW9_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001013 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
1014 FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001015 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001016 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001017 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
1018 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
1019 FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
1020 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1021 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1022 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1023 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1024 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1025 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001026 } else {
1027 I915_WRITE(DSPFW7,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001028 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1029 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001030 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001031 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001032 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1033 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1034 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1035 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1036 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1037 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001038 }
1039
1040 POSTING_READ(DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001041}
1042
Ville Syrjälä15665972015-03-10 16:16:28 +02001043#undef FW_WM_VLV
1044
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001045static void g4x_setup_wm_latency(struct drm_i915_private *dev_priv)
1046{
1047 /* all latencies in usec */
1048 dev_priv->wm.pri_latency[G4X_WM_LEVEL_NORMAL] = 5;
1049 dev_priv->wm.pri_latency[G4X_WM_LEVEL_SR] = 12;
Ville Syrjälä79d94302017-04-21 21:14:30 +03001050 dev_priv->wm.pri_latency[G4X_WM_LEVEL_HPLL] = 35;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001051
Ville Syrjälä79d94302017-04-21 21:14:30 +03001052 dev_priv->wm.max_level = G4X_WM_LEVEL_HPLL;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001053}
1054
1055static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
1056{
1057 /*
1058 * DSPCNTR[13] supposedly controls whether the
1059 * primary plane can use the FIFO space otherwise
1060 * reserved for the sprite plane. It's not 100% clear
1061 * what the actual FIFO size is, but it looks like we
1062 * can happily set both primary and sprite watermarks
1063 * up to 127 cachelines. So that would seem to mean
1064 * that either DSPCNTR[13] doesn't do anything, or that
1065 * the total FIFO is >= 256 cachelines in size. Either
1066 * way, we don't seem to have to worry about this
1067 * repartitioning as the maximum watermark value the
1068 * register can hold for each plane is lower than the
1069 * minimum FIFO size.
1070 */
1071 switch (plane_id) {
1072 case PLANE_CURSOR:
1073 return 63;
1074 case PLANE_PRIMARY:
1075 return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
1076 case PLANE_SPRITE0:
1077 return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
1078 default:
1079 MISSING_CASE(plane_id);
1080 return 0;
1081 }
1082}
1083
1084static int g4x_fbc_fifo_size(int level)
1085{
1086 switch (level) {
1087 case G4X_WM_LEVEL_SR:
1088 return 7;
1089 case G4X_WM_LEVEL_HPLL:
1090 return 15;
1091 default:
1092 MISSING_CASE(level);
1093 return 0;
1094 }
1095}
1096
1097static uint16_t g4x_compute_wm(const struct intel_crtc_state *crtc_state,
1098 const struct intel_plane_state *plane_state,
1099 int level)
1100{
1101 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1102 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1103 const struct drm_display_mode *adjusted_mode =
1104 &crtc_state->base.adjusted_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001105 unsigned int latency = dev_priv->wm.pri_latency[level] * 10;
1106 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001107
1108 if (latency == 0)
1109 return USHRT_MAX;
1110
1111 if (!intel_wm_plane_visible(crtc_state, plane_state))
1112 return 0;
1113
1114 /*
1115 * Not 100% sure which way ELK should go here as the
1116 * spec only says CL/CTG should assume 32bpp and BW
1117 * doesn't need to. But as these things followed the
1118 * mobile vs. desktop lines on gen3 as well, let's
1119 * assume ELK doesn't need this.
1120 *
1121 * The spec also fails to list such a restriction for
1122 * the HPLL watermark, which seems a little strange.
1123 * Let's use 32bpp for the HPLL watermark as well.
1124 */
1125 if (IS_GM45(dev_priv) && plane->id == PLANE_PRIMARY &&
1126 level != G4X_WM_LEVEL_NORMAL)
1127 cpp = 4;
1128 else
1129 cpp = plane_state->base.fb->format->cpp[0];
1130
1131 clock = adjusted_mode->crtc_clock;
1132 htotal = adjusted_mode->crtc_htotal;
1133
1134 if (plane->id == PLANE_CURSOR)
1135 width = plane_state->base.crtc_w;
1136 else
1137 width = drm_rect_width(&plane_state->base.dst);
1138
1139 if (plane->id == PLANE_CURSOR) {
1140 wm = intel_wm_method2(clock, htotal, width, cpp, latency);
1141 } else if (plane->id == PLANE_PRIMARY &&
1142 level == G4X_WM_LEVEL_NORMAL) {
1143 wm = intel_wm_method1(clock, cpp, latency);
1144 } else {
Chris Wilson1a1f1282017-11-07 14:03:38 +00001145 unsigned int small, large;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001146
1147 small = intel_wm_method1(clock, cpp, latency);
1148 large = intel_wm_method2(clock, htotal, width, cpp, latency);
1149
1150 wm = min(small, large);
1151 }
1152
1153 wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
1154 width, cpp);
1155
1156 wm = DIV_ROUND_UP(wm, 64) + 2;
1157
Chris Wilson1a1f1282017-11-07 14:03:38 +00001158 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001159}
1160
1161static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1162 int level, enum plane_id plane_id, u16 value)
1163{
1164 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1165 bool dirty = false;
1166
1167 for (; level < intel_wm_num_levels(dev_priv); level++) {
1168 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1169
1170 dirty |= raw->plane[plane_id] != value;
1171 raw->plane[plane_id] = value;
1172 }
1173
1174 return dirty;
1175}
1176
1177static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
1178 int level, u16 value)
1179{
1180 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1181 bool dirty = false;
1182
1183 /* NORMAL level doesn't have an FBC watermark */
1184 level = max(level, G4X_WM_LEVEL_SR);
1185
1186 for (; level < intel_wm_num_levels(dev_priv); level++) {
1187 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1188
1189 dirty |= raw->fbc != value;
1190 raw->fbc = value;
1191 }
1192
1193 return dirty;
1194}
1195
1196static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
1197 const struct intel_plane_state *pstate,
1198 uint32_t pri_val);
1199
1200static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1201 const struct intel_plane_state *plane_state)
1202{
1203 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1204 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
1205 enum plane_id plane_id = plane->id;
1206 bool dirty = false;
1207 int level;
1208
1209 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1210 dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1211 if (plane_id == PLANE_PRIMARY)
1212 dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
1213 goto out;
1214 }
1215
1216 for (level = 0; level < num_levels; level++) {
1217 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1218 int wm, max_wm;
1219
1220 wm = g4x_compute_wm(crtc_state, plane_state, level);
1221 max_wm = g4x_plane_fifo_size(plane_id, level);
1222
1223 if (wm > max_wm)
1224 break;
1225
1226 dirty |= raw->plane[plane_id] != wm;
1227 raw->plane[plane_id] = wm;
1228
1229 if (plane_id != PLANE_PRIMARY ||
1230 level == G4X_WM_LEVEL_NORMAL)
1231 continue;
1232
1233 wm = ilk_compute_fbc_wm(crtc_state, plane_state,
1234 raw->plane[plane_id]);
1235 max_wm = g4x_fbc_fifo_size(level);
1236
1237 /*
1238 * FBC wm is not mandatory as we
1239 * can always just disable its use.
1240 */
1241 if (wm > max_wm)
1242 wm = USHRT_MAX;
1243
1244 dirty |= raw->fbc != wm;
1245 raw->fbc = wm;
1246 }
1247
1248 /* mark watermarks as invalid */
1249 dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1250
1251 if (plane_id == PLANE_PRIMARY)
1252 dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
1253
1254 out:
1255 if (dirty) {
1256 DRM_DEBUG_KMS("%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
1257 plane->base.name,
1258 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
1259 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
1260 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
1261
1262 if (plane_id == PLANE_PRIMARY)
1263 DRM_DEBUG_KMS("FBC watermarks: SR=%d, HPLL=%d\n",
1264 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
1265 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
1266 }
1267
1268 return dirty;
1269}
1270
1271static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1272 enum plane_id plane_id, int level)
1273{
1274 const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1275
1276 return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
1277}
1278
1279static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
1280 int level)
1281{
1282 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1283
1284 if (level > dev_priv->wm.max_level)
1285 return false;
1286
1287 return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1288 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1289 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1290}
1291
1292/* mark all levels starting from 'level' as invalid */
1293static void g4x_invalidate_wms(struct intel_crtc *crtc,
1294 struct g4x_wm_state *wm_state, int level)
1295{
1296 if (level <= G4X_WM_LEVEL_NORMAL) {
1297 enum plane_id plane_id;
1298
1299 for_each_plane_id_on_crtc(crtc, plane_id)
1300 wm_state->wm.plane[plane_id] = USHRT_MAX;
1301 }
1302
1303 if (level <= G4X_WM_LEVEL_SR) {
1304 wm_state->cxsr = false;
1305 wm_state->sr.cursor = USHRT_MAX;
1306 wm_state->sr.plane = USHRT_MAX;
1307 wm_state->sr.fbc = USHRT_MAX;
1308 }
1309
1310 if (level <= G4X_WM_LEVEL_HPLL) {
1311 wm_state->hpll_en = false;
1312 wm_state->hpll.cursor = USHRT_MAX;
1313 wm_state->hpll.plane = USHRT_MAX;
1314 wm_state->hpll.fbc = USHRT_MAX;
1315 }
1316}
1317
1318static int g4x_compute_pipe_wm(struct intel_crtc_state *crtc_state)
1319{
1320 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1321 struct intel_atomic_state *state =
1322 to_intel_atomic_state(crtc_state->base.state);
1323 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
1324 int num_active_planes = hweight32(crtc_state->active_planes &
1325 ~BIT(PLANE_CURSOR));
1326 const struct g4x_pipe_wm *raw;
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001327 const struct intel_plane_state *old_plane_state;
1328 const struct intel_plane_state *new_plane_state;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001329 struct intel_plane *plane;
1330 enum plane_id plane_id;
1331 int i, level;
1332 unsigned int dirty = 0;
1333
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001334 for_each_oldnew_intel_plane_in_state(state, plane,
1335 old_plane_state,
1336 new_plane_state, i) {
1337 if (new_plane_state->base.crtc != &crtc->base &&
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001338 old_plane_state->base.crtc != &crtc->base)
1339 continue;
1340
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001341 if (g4x_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001342 dirty |= BIT(plane->id);
1343 }
1344
1345 if (!dirty)
1346 return 0;
1347
1348 level = G4X_WM_LEVEL_NORMAL;
1349 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1350 goto out;
1351
1352 raw = &crtc_state->wm.g4x.raw[level];
1353 for_each_plane_id_on_crtc(crtc, plane_id)
1354 wm_state->wm.plane[plane_id] = raw->plane[plane_id];
1355
1356 level = G4X_WM_LEVEL_SR;
1357
1358 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1359 goto out;
1360
1361 raw = &crtc_state->wm.g4x.raw[level];
1362 wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
1363 wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
1364 wm_state->sr.fbc = raw->fbc;
1365
1366 wm_state->cxsr = num_active_planes == BIT(PLANE_PRIMARY);
1367
1368 level = G4X_WM_LEVEL_HPLL;
1369
1370 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1371 goto out;
1372
1373 raw = &crtc_state->wm.g4x.raw[level];
1374 wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
1375 wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
1376 wm_state->hpll.fbc = raw->fbc;
1377
1378 wm_state->hpll_en = wm_state->cxsr;
1379
1380 level++;
1381
1382 out:
1383 if (level == G4X_WM_LEVEL_NORMAL)
1384 return -EINVAL;
1385
1386 /* invalidate the higher levels */
1387 g4x_invalidate_wms(crtc, wm_state, level);
1388
1389 /*
1390 * Determine if the FBC watermark(s) can be used. IF
1391 * this isn't the case we prefer to disable the FBC
1392 ( watermark(s) rather than disable the SR/HPLL
1393 * level(s) entirely.
1394 */
1395 wm_state->fbc_en = level > G4X_WM_LEVEL_NORMAL;
1396
1397 if (level >= G4X_WM_LEVEL_SR &&
1398 wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
1399 wm_state->fbc_en = false;
1400 else if (level >= G4X_WM_LEVEL_HPLL &&
1401 wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
1402 wm_state->fbc_en = false;
1403
1404 return 0;
1405}
1406
1407static int g4x_compute_intermediate_wm(struct drm_device *dev,
1408 struct intel_crtc *crtc,
1409 struct intel_crtc_state *crtc_state)
1410{
1411 struct g4x_wm_state *intermediate = &crtc_state->wm.g4x.intermediate;
1412 const struct g4x_wm_state *optimal = &crtc_state->wm.g4x.optimal;
1413 const struct g4x_wm_state *active = &crtc->wm.active.g4x;
1414 enum plane_id plane_id;
1415
1416 intermediate->cxsr = optimal->cxsr && active->cxsr &&
1417 !crtc_state->disable_cxsr;
1418 intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
1419 !crtc_state->disable_cxsr;
1420 intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
1421
1422 for_each_plane_id_on_crtc(crtc, plane_id) {
1423 intermediate->wm.plane[plane_id] =
1424 max(optimal->wm.plane[plane_id],
1425 active->wm.plane[plane_id]);
1426
1427 WARN_ON(intermediate->wm.plane[plane_id] >
1428 g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
1429 }
1430
1431 intermediate->sr.plane = max(optimal->sr.plane,
1432 active->sr.plane);
1433 intermediate->sr.cursor = max(optimal->sr.cursor,
1434 active->sr.cursor);
1435 intermediate->sr.fbc = max(optimal->sr.fbc,
1436 active->sr.fbc);
1437
1438 intermediate->hpll.plane = max(optimal->hpll.plane,
1439 active->hpll.plane);
1440 intermediate->hpll.cursor = max(optimal->hpll.cursor,
1441 active->hpll.cursor);
1442 intermediate->hpll.fbc = max(optimal->hpll.fbc,
1443 active->hpll.fbc);
1444
1445 WARN_ON((intermediate->sr.plane >
1446 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
1447 intermediate->sr.cursor >
1448 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
1449 intermediate->cxsr);
1450 WARN_ON((intermediate->sr.plane >
1451 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
1452 intermediate->sr.cursor >
1453 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
1454 intermediate->hpll_en);
1455
1456 WARN_ON(intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
1457 intermediate->fbc_en && intermediate->cxsr);
1458 WARN_ON(intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
1459 intermediate->fbc_en && intermediate->hpll_en);
1460
1461 /*
1462 * If our intermediate WM are identical to the final WM, then we can
1463 * omit the post-vblank programming; only update if it's different.
1464 */
1465 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
1466 crtc_state->wm.need_postvbl_update = true;
1467
1468 return 0;
1469}
1470
1471static void g4x_merge_wm(struct drm_i915_private *dev_priv,
1472 struct g4x_wm_values *wm)
1473{
1474 struct intel_crtc *crtc;
1475 int num_active_crtcs = 0;
1476
1477 wm->cxsr = true;
1478 wm->hpll_en = true;
1479 wm->fbc_en = true;
1480
1481 for_each_intel_crtc(&dev_priv->drm, crtc) {
1482 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1483
1484 if (!crtc->active)
1485 continue;
1486
1487 if (!wm_state->cxsr)
1488 wm->cxsr = false;
1489 if (!wm_state->hpll_en)
1490 wm->hpll_en = false;
1491 if (!wm_state->fbc_en)
1492 wm->fbc_en = false;
1493
1494 num_active_crtcs++;
1495 }
1496
1497 if (num_active_crtcs != 1) {
1498 wm->cxsr = false;
1499 wm->hpll_en = false;
1500 wm->fbc_en = false;
1501 }
1502
1503 for_each_intel_crtc(&dev_priv->drm, crtc) {
1504 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1505 enum pipe pipe = crtc->pipe;
1506
1507 wm->pipe[pipe] = wm_state->wm;
1508 if (crtc->active && wm->cxsr)
1509 wm->sr = wm_state->sr;
1510 if (crtc->active && wm->hpll_en)
1511 wm->hpll = wm_state->hpll;
1512 }
1513}
1514
1515static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
1516{
1517 struct g4x_wm_values *old_wm = &dev_priv->wm.g4x;
1518 struct g4x_wm_values new_wm = {};
1519
1520 g4x_merge_wm(dev_priv, &new_wm);
1521
1522 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
1523 return;
1524
1525 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
1526 _intel_set_memory_cxsr(dev_priv, false);
1527
1528 g4x_write_wm_values(dev_priv, &new_wm);
1529
1530 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
1531 _intel_set_memory_cxsr(dev_priv, true);
1532
1533 *old_wm = new_wm;
1534}
1535
1536static void g4x_initial_watermarks(struct intel_atomic_state *state,
1537 struct intel_crtc_state *crtc_state)
1538{
1539 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1540 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1541
1542 mutex_lock(&dev_priv->wm.wm_mutex);
1543 crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
1544 g4x_program_watermarks(dev_priv);
1545 mutex_unlock(&dev_priv->wm.wm_mutex);
1546}
1547
1548static void g4x_optimize_watermarks(struct intel_atomic_state *state,
1549 struct intel_crtc_state *crtc_state)
1550{
1551 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1552 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
1553
1554 if (!crtc_state->wm.need_postvbl_update)
1555 return;
1556
1557 mutex_lock(&dev_priv->wm.wm_mutex);
1558 intel_crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
1559 g4x_program_watermarks(dev_priv);
1560 mutex_unlock(&dev_priv->wm.wm_mutex);
1561}
1562
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001563/* latency must be in 0.1us units. */
1564static unsigned int vlv_wm_method2(unsigned int pixel_rate,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001565 unsigned int htotal,
1566 unsigned int width,
Ville Syrjäläac484962016-01-20 21:05:26 +02001567 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001568 unsigned int latency)
1569{
1570 unsigned int ret;
1571
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001572 ret = intel_wm_method2(pixel_rate, htotal,
1573 width, cpp, latency);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001574 ret = DIV_ROUND_UP(ret, 64);
1575
1576 return ret;
1577}
1578
Ville Syrjäläbb726512016-10-31 22:37:24 +02001579static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001580{
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001581 /* all latencies in usec */
1582 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1583
Ville Syrjälä58590c12015-09-08 21:05:12 +03001584 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
1585
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001586 if (IS_CHERRYVIEW(dev_priv)) {
1587 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1588 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001589
1590 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001591 }
1592}
1593
Ville Syrjäläe339d672016-11-28 19:37:17 +02001594static uint16_t vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1595 const struct intel_plane_state *plane_state,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001596 int level)
1597{
Ville Syrjäläe339d672016-11-28 19:37:17 +02001598 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001599 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläe339d672016-11-28 19:37:17 +02001600 const struct drm_display_mode *adjusted_mode =
1601 &crtc_state->base.adjusted_mode;
Chris Wilson1a1f1282017-11-07 14:03:38 +00001602 unsigned int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001603
1604 if (dev_priv->wm.pri_latency[level] == 0)
1605 return USHRT_MAX;
1606
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001607 if (!intel_wm_plane_visible(crtc_state, plane_state))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001608 return 0;
1609
Daniel Vetteref426c12017-01-04 11:41:10 +01001610 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjäläe339d672016-11-28 19:37:17 +02001611 clock = adjusted_mode->crtc_clock;
1612 htotal = adjusted_mode->crtc_htotal;
1613 width = crtc_state->pipe_src_w;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001614
Ville Syrjälä709f3fc2017-03-03 17:19:26 +02001615 if (plane->id == PLANE_CURSOR) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001616 /*
1617 * FIXME the formula gives values that are
1618 * too big for the cursor FIFO, and hence we
1619 * would never be able to use cursors. For
1620 * now just hardcode the watermark.
1621 */
1622 wm = 63;
1623 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +02001624 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001625 dev_priv->wm.pri_latency[level] * 10);
1626 }
1627
Chris Wilson1a1f1282017-11-07 14:03:38 +00001628 return min_t(unsigned int, wm, USHRT_MAX);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001629}
1630
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001631static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1632{
1633 return (active_planes & (BIT(PLANE_SPRITE0) |
1634 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1635}
1636
Ville Syrjälä5012e602017-03-02 19:14:56 +02001637static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001638{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001639 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001640 const struct g4x_pipe_wm *raw =
Ville Syrjälä5012e602017-03-02 19:14:56 +02001641 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001642 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001643 unsigned int active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1644 int num_active_planes = hweight32(active_planes);
1645 const int fifo_size = 511;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001646 int fifo_extra, fifo_left = fifo_size;
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001647 int sprite0_fifo_extra = 0;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001648 unsigned int total_rate;
1649 enum plane_id plane_id;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001650
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001651 /*
1652 * When enabling sprite0 after sprite1 has already been enabled
1653 * we tend to get an underrun unless sprite0 already has some
1654 * FIFO space allcoated. Hence we always allocate at least one
1655 * cacheline for sprite0 whenever sprite1 is enabled.
1656 *
1657 * All other plane enable sequences appear immune to this problem.
1658 */
1659 if (vlv_need_sprite0_fifo_workaround(active_planes))
1660 sprite0_fifo_extra = 1;
1661
Ville Syrjälä5012e602017-03-02 19:14:56 +02001662 total_rate = raw->plane[PLANE_PRIMARY] +
1663 raw->plane[PLANE_SPRITE0] +
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001664 raw->plane[PLANE_SPRITE1] +
1665 sprite0_fifo_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001666
Ville Syrjälä5012e602017-03-02 19:14:56 +02001667 if (total_rate > fifo_size)
1668 return -EINVAL;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001669
Ville Syrjälä5012e602017-03-02 19:14:56 +02001670 if (total_rate == 0)
1671 total_rate = 1;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001672
Ville Syrjälä5012e602017-03-02 19:14:56 +02001673 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001674 unsigned int rate;
1675
Ville Syrjälä5012e602017-03-02 19:14:56 +02001676 if ((active_planes & BIT(plane_id)) == 0) {
1677 fifo_state->plane[plane_id] = 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001678 continue;
1679 }
1680
Ville Syrjälä5012e602017-03-02 19:14:56 +02001681 rate = raw->plane[plane_id];
1682 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1683 fifo_left -= fifo_state->plane[plane_id];
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001684 }
1685
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001686 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1687 fifo_left -= sprite0_fifo_extra;
1688
Ville Syrjälä5012e602017-03-02 19:14:56 +02001689 fifo_state->plane[PLANE_CURSOR] = 63;
1690
1691 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001692
1693 /* spread the remainder evenly */
Ville Syrjälä5012e602017-03-02 19:14:56 +02001694 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001695 int plane_extra;
1696
1697 if (fifo_left == 0)
1698 break;
1699
Ville Syrjälä5012e602017-03-02 19:14:56 +02001700 if ((active_planes & BIT(plane_id)) == 0)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001701 continue;
1702
1703 plane_extra = min(fifo_extra, fifo_left);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001704 fifo_state->plane[plane_id] += plane_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001705 fifo_left -= plane_extra;
1706 }
1707
Ville Syrjälä5012e602017-03-02 19:14:56 +02001708 WARN_ON(active_planes != 0 && fifo_left != 0);
1709
1710 /* give it all to the first plane if none are active */
1711 if (active_planes == 0) {
1712 WARN_ON(fifo_left != fifo_size);
1713 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1714 }
1715
1716 return 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001717}
1718
Ville Syrjäläff32c542017-03-02 19:14:57 +02001719/* mark all levels starting from 'level' as invalid */
1720static void vlv_invalidate_wms(struct intel_crtc *crtc,
1721 struct vlv_wm_state *wm_state, int level)
1722{
1723 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1724
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001725 for (; level < intel_wm_num_levels(dev_priv); level++) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02001726 enum plane_id plane_id;
1727
1728 for_each_plane_id_on_crtc(crtc, plane_id)
1729 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1730
1731 wm_state->sr[level].cursor = USHRT_MAX;
1732 wm_state->sr[level].plane = USHRT_MAX;
1733 }
1734}
1735
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001736static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1737{
1738 if (wm > fifo_size)
1739 return USHRT_MAX;
1740 else
1741 return fifo_size - wm;
1742}
1743
Ville Syrjäläff32c542017-03-02 19:14:57 +02001744/*
1745 * Starting from 'level' set all higher
1746 * levels to 'value' in the "raw" watermarks.
1747 */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001748static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001749 int level, enum plane_id plane_id, u16 value)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001750{
Ville Syrjäläff32c542017-03-02 19:14:57 +02001751 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001752 int num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001753 bool dirty = false;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001754
Ville Syrjäläff32c542017-03-02 19:14:57 +02001755 for (; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001756 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001757
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001758 dirty |= raw->plane[plane_id] != value;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001759 raw->plane[plane_id] = value;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001760 }
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001761
1762 return dirty;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001763}
1764
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001765static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1766 const struct intel_plane_state *plane_state)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001767{
1768 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1769 enum plane_id plane_id = plane->id;
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001770 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
Ville Syrjäläff32c542017-03-02 19:14:57 +02001771 int level;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001772 bool dirty = false;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001773
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001774 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001775 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1776 goto out;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001777 }
1778
1779 for (level = 0; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001780 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001781 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1782 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1783
Ville Syrjäläff32c542017-03-02 19:14:57 +02001784 if (wm > max_wm)
1785 break;
1786
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001787 dirty |= raw->plane[plane_id] != wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001788 raw->plane[plane_id] = wm;
1789 }
1790
1791 /* mark all higher levels as invalid */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001792 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001793
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001794out:
1795 if (dirty)
Ville Syrjälä57a65282017-04-21 21:14:22 +03001796 DRM_DEBUG_KMS("%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001797 plane->base.name,
1798 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1799 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1800 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
1801
1802 return dirty;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001803}
1804
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001805static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1806 enum plane_id plane_id, int level)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001807{
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001808 const struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02001809 &crtc_state->wm.vlv.raw[level];
1810 const struct vlv_fifo_state *fifo_state =
1811 &crtc_state->wm.vlv.fifo_state;
1812
1813 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1814}
1815
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001816static 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 +02001817{
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001818 return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1819 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1820 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1821 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001822}
1823
1824static int vlv_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001825{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001826 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001827 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001828 struct intel_atomic_state *state =
1829 to_intel_atomic_state(crtc_state->base.state);
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001830 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001831 const struct vlv_fifo_state *fifo_state =
1832 &crtc_state->wm.vlv.fifo_state;
1833 int num_active_planes = hweight32(crtc_state->active_planes &
1834 ~BIT(PLANE_CURSOR));
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001835 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->base);
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001836 const struct intel_plane_state *old_plane_state;
1837 const struct intel_plane_state *new_plane_state;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001838 struct intel_plane *plane;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001839 enum plane_id plane_id;
1840 int level, ret, i;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001841 unsigned int dirty = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001842
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001843 for_each_oldnew_intel_plane_in_state(state, plane,
1844 old_plane_state,
1845 new_plane_state, i) {
1846 if (new_plane_state->base.crtc != &crtc->base &&
Ville Syrjäläff32c542017-03-02 19:14:57 +02001847 old_plane_state->base.crtc != &crtc->base)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001848 continue;
1849
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001850 if (vlv_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001851 dirty |= BIT(plane->id);
1852 }
1853
1854 /*
1855 * DSPARB registers may have been reset due to the
1856 * power well being turned off. Make sure we restore
1857 * them to a consistent state even if no primary/sprite
1858 * planes are initially active.
1859 */
1860 if (needs_modeset)
1861 crtc_state->fifo_changed = true;
1862
1863 if (!dirty)
1864 return 0;
1865
1866 /* cursor changes don't warrant a FIFO recompute */
1867 if (dirty & ~BIT(PLANE_CURSOR)) {
1868 const struct intel_crtc_state *old_crtc_state =
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001869 intel_atomic_get_old_crtc_state(state, crtc);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001870 const struct vlv_fifo_state *old_fifo_state =
1871 &old_crtc_state->wm.vlv.fifo_state;
1872
1873 ret = vlv_compute_fifo(crtc_state);
1874 if (ret)
1875 return ret;
1876
1877 if (needs_modeset ||
1878 memcmp(old_fifo_state, fifo_state,
1879 sizeof(*fifo_state)) != 0)
1880 crtc_state->fifo_changed = true;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001881 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001882
Ville Syrjäläff32c542017-03-02 19:14:57 +02001883 /* initially allow all levels */
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001884 wm_state->num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001885 /*
1886 * Note that enabling cxsr with no primary/sprite planes
1887 * enabled can wedge the pipe. Hence we only allow cxsr
1888 * with exactly one enabled primary/sprite plane.
1889 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001890 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001891
Ville Syrjälä5012e602017-03-02 19:14:56 +02001892 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001893 const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001894 const int sr_fifo_size = INTEL_INFO(dev_priv)->num_pipes * 512 - 1;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001895
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001896 if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
Ville Syrjäläff32c542017-03-02 19:14:57 +02001897 break;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001898
Ville Syrjäläff32c542017-03-02 19:14:57 +02001899 for_each_plane_id_on_crtc(crtc, plane_id) {
1900 wm_state->wm[level].plane[plane_id] =
1901 vlv_invert_wm_value(raw->plane[plane_id],
1902 fifo_state->plane[plane_id]);
1903 }
1904
1905 wm_state->sr[level].plane =
1906 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
Ville Syrjälä5012e602017-03-02 19:14:56 +02001907 raw->plane[PLANE_SPRITE0],
Ville Syrjäläff32c542017-03-02 19:14:57 +02001908 raw->plane[PLANE_SPRITE1]),
1909 sr_fifo_size);
1910
1911 wm_state->sr[level].cursor =
1912 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1913 63);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001914 }
1915
Ville Syrjäläff32c542017-03-02 19:14:57 +02001916 if (level == 0)
1917 return -EINVAL;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001918
Ville Syrjäläff32c542017-03-02 19:14:57 +02001919 /* limit to only levels we can actually handle */
1920 wm_state->num_levels = level;
1921
1922 /* invalidate the higher levels */
1923 vlv_invalidate_wms(crtc, wm_state, level);
1924
1925 return 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001926}
1927
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001928#define VLV_FIFO(plane, value) \
1929 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1930
Ville Syrjäläff32c542017-03-02 19:14:57 +02001931static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
1932 struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001933{
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001934 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001935 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001936 const struct vlv_fifo_state *fifo_state =
1937 &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001938 int sprite0_start, sprite1_start, fifo_size;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001939
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001940 if (!crtc_state->fifo_changed)
1941 return;
1942
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001943 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
1944 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
1945 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001946
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001947 WARN_ON(fifo_state->plane[PLANE_CURSOR] != 63);
1948 WARN_ON(fifo_size != 511);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001949
Ville Syrjäläc137d662017-03-02 19:15:06 +02001950 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
1951
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001952 /*
1953 * uncore.lock serves a double purpose here. It allows us to
1954 * use the less expensive I915_{READ,WRITE}_FW() functions, and
1955 * it protects the DSPARB registers from getting clobbered by
1956 * parallel updates from multiple pipes.
1957 *
1958 * intel_pipe_update_start() has already disabled interrupts
1959 * for us, so a plain spin_lock() is sufficient here.
1960 */
1961 spin_lock(&dev_priv->uncore.lock);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001962
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001963 switch (crtc->pipe) {
1964 uint32_t dsparb, dsparb2, dsparb3;
1965 case PIPE_A:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001966 dsparb = I915_READ_FW(DSPARB);
1967 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001968
1969 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1970 VLV_FIFO(SPRITEB, 0xff));
1971 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1972 VLV_FIFO(SPRITEB, sprite1_start));
1973
1974 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1975 VLV_FIFO(SPRITEB_HI, 0x1));
1976 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1977 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1978
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001979 I915_WRITE_FW(DSPARB, dsparb);
1980 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001981 break;
1982 case PIPE_B:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001983 dsparb = I915_READ_FW(DSPARB);
1984 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001985
1986 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1987 VLV_FIFO(SPRITED, 0xff));
1988 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1989 VLV_FIFO(SPRITED, sprite1_start));
1990
1991 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
1992 VLV_FIFO(SPRITED_HI, 0xff));
1993 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
1994 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
1995
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001996 I915_WRITE_FW(DSPARB, dsparb);
1997 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001998 break;
1999 case PIPE_C:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002000 dsparb3 = I915_READ_FW(DSPARB3);
2001 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002002
2003 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
2004 VLV_FIFO(SPRITEF, 0xff));
2005 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
2006 VLV_FIFO(SPRITEF, sprite1_start));
2007
2008 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
2009 VLV_FIFO(SPRITEF_HI, 0xff));
2010 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
2011 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
2012
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002013 I915_WRITE_FW(DSPARB3, dsparb3);
2014 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002015 break;
2016 default:
2017 break;
2018 }
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002019
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002020 POSTING_READ_FW(DSPARB);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002021
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002022 spin_unlock(&dev_priv->uncore.lock);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002023}
2024
2025#undef VLV_FIFO
2026
Ville Syrjälä4841da52017-03-02 19:14:59 +02002027static int vlv_compute_intermediate_wm(struct drm_device *dev,
2028 struct intel_crtc *crtc,
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002029 struct intel_crtc_state *new_crtc_state)
Ville Syrjälä4841da52017-03-02 19:14:59 +02002030{
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002031 struct vlv_wm_state *intermediate = &new_crtc_state->wm.vlv.intermediate;
2032 const struct vlv_wm_state *optimal = &new_crtc_state->wm.vlv.optimal;
2033 struct intel_atomic_state *intel_state =
2034 to_intel_atomic_state(new_crtc_state->base.state);
2035 const struct intel_crtc_state *old_crtc_state =
2036 intel_atomic_get_old_crtc_state(intel_state, crtc);
2037 const struct vlv_wm_state *active = &old_crtc_state->wm.vlv.optimal;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002038 int level;
2039
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002040 if (!new_crtc_state->base.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->base)) {
2041 *intermediate = *optimal;
2042
2043 intermediate->cxsr = false;
2044 goto out;
2045 }
2046
Ville Syrjälä4841da52017-03-02 19:14:59 +02002047 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002048 intermediate->cxsr = optimal->cxsr && active->cxsr &&
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002049 !new_crtc_state->disable_cxsr;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002050
2051 for (level = 0; level < intermediate->num_levels; level++) {
2052 enum plane_id plane_id;
2053
2054 for_each_plane_id_on_crtc(crtc, plane_id) {
2055 intermediate->wm[level].plane[plane_id] =
2056 min(optimal->wm[level].plane[plane_id],
2057 active->wm[level].plane[plane_id]);
2058 }
2059
2060 intermediate->sr[level].plane = min(optimal->sr[level].plane,
2061 active->sr[level].plane);
2062 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
2063 active->sr[level].cursor);
2064 }
2065
2066 vlv_invalidate_wms(crtc, intermediate, level);
2067
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002068out:
Ville Syrjälä4841da52017-03-02 19:14:59 +02002069 /*
2070 * If our intermediate WM are identical to the final WM, then we can
2071 * omit the post-vblank programming; only update if it's different.
2072 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002073 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
Maarten Lankhorst5b9489c2017-11-15 17:31:56 +01002074 new_crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002075
2076 return 0;
2077}
2078
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002079static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002080 struct vlv_wm_values *wm)
2081{
2082 struct intel_crtc *crtc;
2083 int num_active_crtcs = 0;
2084
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002085 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002086 wm->cxsr = true;
2087
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002088 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002089 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002090
2091 if (!crtc->active)
2092 continue;
2093
2094 if (!wm_state->cxsr)
2095 wm->cxsr = false;
2096
2097 num_active_crtcs++;
2098 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
2099 }
2100
2101 if (num_active_crtcs != 1)
2102 wm->cxsr = false;
2103
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03002104 if (num_active_crtcs > 1)
2105 wm->level = VLV_WM_LEVEL_PM2;
2106
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002107 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002108 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002109 enum pipe pipe = crtc->pipe;
2110
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002111 wm->pipe[pipe] = wm_state->wm[wm->level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02002112 if (crtc->active && wm->cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002113 wm->sr = wm_state->sr[wm->level];
2114
Ville Syrjälä1b313892016-11-28 19:37:08 +02002115 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
2116 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
2117 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
2118 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002119 }
2120}
2121
Ville Syrjäläff32c542017-03-02 19:14:57 +02002122static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002123{
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002124 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
2125 struct vlv_wm_values new_wm = {};
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002126
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002127 vlv_merge_wm(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002128
Ville Syrjäläff32c542017-03-02 19:14:57 +02002129 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002130 return;
2131
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002132 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002133 chv_set_memory_dvfs(dev_priv, false);
2134
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002135 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002136 chv_set_memory_pm5(dev_priv, false);
2137
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002138 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002139 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002140
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002141 vlv_write_wm_values(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002142
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002143 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002144 _intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002145
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002146 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002147 chv_set_memory_pm5(dev_priv, true);
2148
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002149 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002150 chv_set_memory_dvfs(dev_priv, true);
2151
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002152 *old_wm = new_wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03002153}
2154
Ville Syrjäläff32c542017-03-02 19:14:57 +02002155static void vlv_initial_watermarks(struct intel_atomic_state *state,
2156 struct intel_crtc_state *crtc_state)
2157{
2158 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2159 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
2160
2161 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002162 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
2163 vlv_program_watermarks(dev_priv);
2164 mutex_unlock(&dev_priv->wm.wm_mutex);
2165}
2166
2167static void vlv_optimize_watermarks(struct intel_atomic_state *state,
2168 struct intel_crtc_state *crtc_state)
2169{
2170 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2171 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2172
2173 if (!crtc_state->wm.need_postvbl_update)
2174 return;
2175
2176 mutex_lock(&dev_priv->wm.wm_mutex);
2177 intel_crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02002178 vlv_program_watermarks(dev_priv);
2179 mutex_unlock(&dev_priv->wm.wm_mutex);
2180}
2181
Ville Syrjälä432081b2016-10-31 22:37:03 +02002182static void i965_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002183{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002184 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002185 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002186 int srwm = 1;
2187 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03002188 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002189
2190 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002191 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002192 if (crtc) {
2193 /* self-refresh has much higher latency */
2194 static const int sr_latency_ns = 12000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002195 const struct drm_display_mode *adjusted_mode =
2196 &crtc->config->base.adjusted_mode;
2197 const struct drm_framebuffer *fb =
2198 crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002199 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002200 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002201 int hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002202 int cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002203 int entries;
2204
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002205 entries = intel_wm_method2(clock, htotal,
2206 hdisplay, cpp, sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002207 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
2208 srwm = I965_FIFO_SIZE - entries;
2209 if (srwm < 0)
2210 srwm = 1;
2211 srwm &= 0x1ff;
2212 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
2213 entries, srwm);
2214
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002215 entries = intel_wm_method2(clock, htotal,
2216 crtc->base.cursor->state->crtc_w, 4,
2217 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002218 entries = DIV_ROUND_UP(entries,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002219 i965_cursor_wm_info.cacheline_size) +
2220 i965_cursor_wm_info.guard_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002221
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002222 cursor_sr = i965_cursor_wm_info.fifo_size - entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002223 if (cursor_sr > i965_cursor_wm_info.max_wm)
2224 cursor_sr = i965_cursor_wm_info.max_wm;
2225
2226 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
2227 "cursor %d\n", srwm, cursor_sr);
2228
Imre Deak98584252014-06-13 14:54:20 +03002229 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002230 } else {
Imre Deak98584252014-06-13 14:54:20 +03002231 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002232 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03002233 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002234 }
2235
2236 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2237 srwm);
2238
2239 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002240 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
2241 FW_WM(8, CURSORB) |
2242 FW_WM(8, PLANEB) |
2243 FW_WM(8, PLANEA));
2244 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
2245 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002246 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002247 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03002248
2249 if (cxsr_enabled)
2250 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002251}
2252
Ville Syrjäläf4998962015-03-10 17:02:21 +02002253#undef FW_WM
2254
Ville Syrjälä432081b2016-10-31 22:37:03 +02002255static void i9xx_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002256{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002257 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002258 const struct intel_watermark_params *wm_info;
2259 uint32_t fwater_lo;
2260 uint32_t fwater_hi;
2261 int cwm, srwm = 1;
2262 int fifo_size;
2263 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002264 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002265
Ville Syrjäläa9097be2016-10-31 22:37:20 +02002266 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002267 wm_info = &i945_wm_info;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002268 else if (!IS_GEN2(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002269 wm_info = &i915_wm_info;
2270 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03002271 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002272
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02002273 fifo_size = dev_priv->display.get_fifo_size(dev_priv, 0);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02002274 crtc = intel_get_crtc_for_plane(dev_priv, 0);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002275 if (intel_crtc_active(crtc)) {
2276 const struct drm_display_mode *adjusted_mode =
2277 &crtc->config->base.adjusted_mode;
2278 const struct drm_framebuffer *fb =
2279 crtc->base.primary->state->fb;
2280 int cpp;
2281
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002282 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002283 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002284 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002285 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002286
Damien Lespiau241bfc32013-09-25 16:45:37 +01002287 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002288 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002289 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002290 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002291 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002292 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002293 if (planea_wm > (long)wm_info->max_wm)
2294 planea_wm = wm_info->max_wm;
2295 }
2296
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002297 if (IS_GEN2(dev_priv))
Ville Syrjälä9d539102014-08-15 01:21:53 +03002298 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002299
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02002300 fifo_size = dev_priv->display.get_fifo_size(dev_priv, 1);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02002301 crtc = intel_get_crtc_for_plane(dev_priv, 1);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002302 if (intel_crtc_active(crtc)) {
2303 const struct drm_display_mode *adjusted_mode =
2304 &crtc->config->base.adjusted_mode;
2305 const struct drm_framebuffer *fb =
2306 crtc->base.primary->state->fb;
2307 int cpp;
2308
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002309 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002310 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002311 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002312 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002313
Damien Lespiau241bfc32013-09-25 16:45:37 +01002314 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002315 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002316 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002317 if (enabled == NULL)
2318 enabled = crtc;
2319 else
2320 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002321 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002322 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002323 if (planeb_wm > (long)wm_info->max_wm)
2324 planeb_wm = wm_info->max_wm;
2325 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002326
2327 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
2328
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002329 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07002330 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002331
Ville Syrjäläefc26112016-10-31 22:37:04 +02002332 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002333
2334 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01002335 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002336 enabled = NULL;
2337 }
2338
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002339 /*
2340 * Overlay gets an aggressive default since video jitter is bad.
2341 */
2342 cwm = 2;
2343
2344 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03002345 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002346
2347 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02002348 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002349 /* self-refresh has much higher latency */
2350 static const int sr_latency_ns = 6000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002351 const struct drm_display_mode *adjusted_mode =
2352 &enabled->config->base.adjusted_mode;
2353 const struct drm_framebuffer *fb =
2354 enabled->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002355 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002356 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002357 int hdisplay = enabled->config->pipe_src_w;
2358 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002359 int entries;
2360
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002361 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002362 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002363 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002364 cpp = fb->format->cpp[0];
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002365
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002366 entries = intel_wm_method2(clock, htotal, hdisplay, cpp,
2367 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002368 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
2369 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
2370 srwm = wm_info->fifo_size - entries;
2371 if (srwm < 0)
2372 srwm = 1;
2373
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002374 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002375 I915_WRITE(FW_BLC_SELF,
2376 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03002377 else
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002378 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
2379 }
2380
2381 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
2382 planea_wm, planeb_wm, cwm, srwm);
2383
2384 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2385 fwater_hi = (cwm & 0x1f);
2386
2387 /* Set request length to 8 cachelines per fetch */
2388 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2389 fwater_hi = fwater_hi | (1 << 8);
2390
2391 I915_WRITE(FW_BLC, fwater_lo);
2392 I915_WRITE(FW_BLC2, fwater_hi);
2393
Imre Deak5209b1f2014-07-01 12:36:17 +03002394 if (enabled)
2395 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002396}
2397
Ville Syrjälä432081b2016-10-31 22:37:03 +02002398static void i845_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002399{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002400 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002401 struct intel_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002402 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002403 uint32_t fwater_lo;
2404 int planea_wm;
2405
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002406 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002407 if (crtc == NULL)
2408 return;
2409
Ville Syrjäläefc26112016-10-31 22:37:04 +02002410 adjusted_mode = &crtc->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002411 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02002412 &i845_wm_info,
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02002413 dev_priv->display.get_fifo_size(dev_priv, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01002414 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002415 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
2416 fwater_lo |= (3<<8) | planea_wm;
2417
2418 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
2419
2420 I915_WRITE(FW_BLC, fwater_lo);
2421}
2422
Ville Syrjälä37126462013-08-01 16:18:55 +03002423/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002424static unsigned int ilk_wm_method1(unsigned int pixel_rate,
2425 unsigned int cpp,
2426 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002427{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002428 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002429
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002430 ret = intel_wm_method1(pixel_rate, cpp, latency);
2431 ret = DIV_ROUND_UP(ret, 64) + 2;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002432
2433 return ret;
2434}
2435
Ville Syrjälä37126462013-08-01 16:18:55 +03002436/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002437static unsigned int ilk_wm_method2(unsigned int pixel_rate,
2438 unsigned int htotal,
2439 unsigned int width,
2440 unsigned int cpp,
2441 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002442{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002443 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002444
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002445 ret = intel_wm_method2(pixel_rate, htotal,
2446 width, cpp, latency);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002447 ret = DIV_ROUND_UP(ret, 64) + 2;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002448
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002449 return ret;
2450}
2451
Ville Syrjälä23297042013-07-05 11:57:17 +03002452static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Ville Syrjäläac484962016-01-20 21:05:26 +02002453 uint8_t cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002454{
Matt Roper15126882015-12-03 11:37:40 -08002455 /*
2456 * Neither of these should be possible since this function shouldn't be
2457 * called if the CRTC is off or the plane is invisible. But let's be
2458 * extra paranoid to avoid a potential divide-by-zero if we screw up
2459 * elsewhere in the driver.
2460 */
Ville Syrjäläac484962016-01-20 21:05:26 +02002461 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08002462 return 0;
2463 if (WARN_ON(!horiz_pixels))
2464 return 0;
2465
Ville Syrjäläac484962016-01-20 21:05:26 +02002466 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002467}
2468
Imre Deak820c1982013-12-17 14:46:36 +02002469struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002470 uint16_t pri;
2471 uint16_t spr;
2472 uint16_t cur;
2473 uint16_t fbc;
2474};
2475
Ville Syrjälä37126462013-08-01 16:18:55 +03002476/*
2477 * For both WM_PIPE and WM_LP.
2478 * mem_value must be in 0.1us units.
2479 */
Matt Roper7221fc32015-09-24 15:53:08 -07002480static uint32_t ilk_compute_pri_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002481 const struct intel_plane_state *pstate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03002482 uint32_t mem_value,
2483 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002484{
Paulo Zanonicca32e92013-05-31 11:45:06 -03002485 uint32_t method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002486 int cpp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002487
Ville Syrjälä24304d812017-03-14 17:10:49 +02002488 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002489 return 0;
2490
Ville Syrjälä353c8592016-12-14 23:30:57 +02002491 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002492
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002493 method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002494
2495 if (!is_lp)
2496 return method1;
2497
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002498 method2 = ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002499 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002500 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002501 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002502
2503 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002504}
2505
Ville Syrjälä37126462013-08-01 16:18:55 +03002506/*
2507 * For both WM_PIPE and WM_LP.
2508 * mem_value must be in 0.1us units.
2509 */
Matt Roper7221fc32015-09-24 15:53:08 -07002510static uint32_t ilk_compute_spr_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002511 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002512 uint32_t mem_value)
2513{
2514 uint32_t method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002515 int cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002516
Ville Syrjälä24304d812017-03-14 17:10:49 +02002517 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002518 return 0;
2519
Ville Syrjälä353c8592016-12-14 23:30:57 +02002520 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002521
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002522 method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
2523 method2 = ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002524 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002525 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002526 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002527 return min(method1, method2);
2528}
2529
Ville Syrjälä37126462013-08-01 16:18:55 +03002530/*
2531 * For both WM_PIPE and WM_LP.
2532 * mem_value must be in 0.1us units.
2533 */
Matt Roper7221fc32015-09-24 15:53:08 -07002534static uint32_t ilk_compute_cur_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002535 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002536 uint32_t mem_value)
2537{
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002538 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002539
Ville Syrjälä24304d812017-03-14 17:10:49 +02002540 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002541 return 0;
2542
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002543 cpp = pstate->base.fb->format->cpp[0];
2544
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002545 return ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002546 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002547 pstate->base.crtc_w, cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002548}
2549
Paulo Zanonicca32e92013-05-31 11:45:06 -03002550/* Only for WM_LP. */
Matt Roper7221fc32015-09-24 15:53:08 -07002551static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002552 const struct intel_plane_state *pstate,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03002553 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002554{
Ville Syrjälä83054942016-11-18 21:53:00 +02002555 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002556
Ville Syrjälä24304d812017-03-14 17:10:49 +02002557 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanonicca32e92013-05-31 11:45:06 -03002558 return 0;
2559
Ville Syrjälä353c8592016-12-14 23:30:57 +02002560 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002561
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002562 return ilk_wm_fbc(pri_val, drm_rect_width(&pstate->base.dst), cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002563}
2564
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002565static unsigned int
2566ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002567{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002568 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002569 return 3072;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002570 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002571 return 768;
2572 else
2573 return 512;
2574}
2575
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002576static unsigned int
2577ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2578 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002579{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002580 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002581 /* BDW primary/sprite plane watermarks */
2582 return level == 0 ? 255 : 2047;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002583 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002584 /* IVB/HSW primary/sprite plane watermarks */
2585 return level == 0 ? 127 : 1023;
2586 else if (!is_sprite)
2587 /* ILK/SNB primary plane watermarks */
2588 return level == 0 ? 127 : 511;
2589 else
2590 /* ILK/SNB sprite plane watermarks */
2591 return level == 0 ? 63 : 255;
2592}
2593
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002594static unsigned int
2595ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002596{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002597 if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002598 return level == 0 ? 63 : 255;
2599 else
2600 return level == 0 ? 31 : 63;
2601}
2602
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002603static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002604{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002605 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002606 return 31;
2607 else
2608 return 15;
2609}
2610
Ville Syrjälä158ae642013-08-07 13:28:19 +03002611/* Calculate the maximum primary/sprite plane watermark */
2612static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
2613 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002614 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002615 enum intel_ddb_partitioning ddb_partitioning,
2616 bool is_sprite)
2617{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002618 struct drm_i915_private *dev_priv = to_i915(dev);
2619 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002620
2621 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002622 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002623 return 0;
2624
2625 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002626 if (level == 0 || config->num_pipes_active > 1) {
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002627 fifo_size /= INTEL_INFO(dev_priv)->num_pipes;
Ville Syrjälä158ae642013-08-07 13:28:19 +03002628
2629 /*
2630 * For some reason the non self refresh
2631 * FIFO size is only half of the self
2632 * refresh FIFO size on ILK/SNB.
2633 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002634 if (INTEL_GEN(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002635 fifo_size /= 2;
2636 }
2637
Ville Syrjälä240264f2013-08-07 13:29:12 +03002638 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002639 /* level 0 is always calculated with 1:1 split */
2640 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2641 if (is_sprite)
2642 fifo_size *= 5;
2643 fifo_size /= 6;
2644 } else {
2645 fifo_size /= 2;
2646 }
2647 }
2648
2649 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002650 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002651}
2652
2653/* Calculate the maximum cursor plane watermark */
2654static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002655 int level,
2656 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002657{
2658 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002659 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002660 return 64;
2661
2662 /* otherwise just report max that registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002663 return ilk_cursor_wm_reg_max(to_i915(dev), level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002664}
2665
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002666static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002667 int level,
2668 const struct intel_wm_config *config,
2669 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002670 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002671{
Ville Syrjälä240264f2013-08-07 13:29:12 +03002672 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
2673 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
2674 max->cur = ilk_cursor_wm_max(dev, level, config);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002675 max->fbc = ilk_fbc_wm_reg_max(to_i915(dev));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002676}
2677
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002678static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002679 int level,
2680 struct ilk_wm_maximums *max)
2681{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002682 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2683 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2684 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2685 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002686}
2687
Ville Syrjäläd9395652013-10-09 19:18:10 +03002688static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002689 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002690 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002691{
2692 bool ret;
2693
2694 /* already determined to be invalid? */
2695 if (!result->enable)
2696 return false;
2697
2698 result->enable = result->pri_val <= max->pri &&
2699 result->spr_val <= max->spr &&
2700 result->cur_val <= max->cur;
2701
2702 ret = result->enable;
2703
2704 /*
2705 * HACK until we can pre-compute everything,
2706 * and thus fail gracefully if LP0 watermarks
2707 * are exceeded...
2708 */
2709 if (level == 0 && !result->enable) {
2710 if (result->pri_val > max->pri)
2711 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2712 level, result->pri_val, max->pri);
2713 if (result->spr_val > max->spr)
2714 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2715 level, result->spr_val, max->spr);
2716 if (result->cur_val > max->cur)
2717 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2718 level, result->cur_val, max->cur);
2719
2720 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
2721 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
2722 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
2723 result->enable = true;
2724 }
2725
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002726 return ret;
2727}
2728
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002729static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Matt Roper43d59ed2015-09-24 15:53:07 -07002730 const struct intel_crtc *intel_crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002731 int level,
Matt Roper7221fc32015-09-24 15:53:08 -07002732 struct intel_crtc_state *cstate,
Maarten Lankhorst28283f42017-10-19 17:13:40 +02002733 const struct intel_plane_state *pristate,
2734 const struct intel_plane_state *sprstate,
2735 const struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002736 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002737{
2738 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2739 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2740 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2741
2742 /* WM1+ latency values stored in 0.5us units */
2743 if (level > 0) {
2744 pri_latency *= 5;
2745 spr_latency *= 5;
2746 cur_latency *= 5;
2747 }
2748
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002749 if (pristate) {
2750 result->pri_val = ilk_compute_pri_wm(cstate, pristate,
2751 pri_latency, level);
2752 result->fbc_val = ilk_compute_fbc_wm(cstate, pristate, result->pri_val);
2753 }
2754
2755 if (sprstate)
2756 result->spr_val = ilk_compute_spr_wm(cstate, sprstate, spr_latency);
2757
2758 if (curstate)
2759 result->cur_val = ilk_compute_cur_wm(cstate, curstate, cur_latency);
2760
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002761 result->enable = true;
2762}
2763
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002764static uint32_t
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002765hsw_compute_linetime_wm(const struct intel_crtc_state *cstate)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002766{
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002767 const struct intel_atomic_state *intel_state =
2768 to_intel_atomic_state(cstate->base.state);
Matt Roperee91a152015-12-03 11:37:39 -08002769 const struct drm_display_mode *adjusted_mode =
2770 &cstate->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002771 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002772
Matt Roperee91a152015-12-03 11:37:39 -08002773 if (!cstate->base.active)
2774 return 0;
2775 if (WARN_ON(adjusted_mode->crtc_clock == 0))
2776 return 0;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002777 if (WARN_ON(intel_state->cdclk.logical.cdclk == 0))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002778 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002779
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002780 /* The WM are computed with base on how long it takes to fill a single
2781 * row at the given clock rate, multiplied by 8.
2782 * */
Ville Syrjälä124abe02015-09-08 13:40:45 +03002783 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2784 adjusted_mode->crtc_clock);
2785 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002786 intel_state->cdclk.logical.cdclk);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002787
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002788 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2789 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002790}
2791
Ville Syrjäläbb726512016-10-31 22:37:24 +02002792static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
2793 uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002794{
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002795 if (INTEL_GEN(dev_priv) >= 9) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002796 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002797 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002798 int level, max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002799
2800 /* read the first set of memory latencies[0:3] */
2801 val = 0; /* data0 to be programmed to 0 for first set */
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01002802 mutex_lock(&dev_priv->pcu_lock);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002803 ret = sandybridge_pcode_read(dev_priv,
2804 GEN9_PCODE_READ_MEM_LATENCY,
2805 &val);
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01002806 mutex_unlock(&dev_priv->pcu_lock);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002807
2808 if (ret) {
2809 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2810 return;
2811 }
2812
2813 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2814 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2815 GEN9_MEM_LATENCY_LEVEL_MASK;
2816 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2817 GEN9_MEM_LATENCY_LEVEL_MASK;
2818 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2819 GEN9_MEM_LATENCY_LEVEL_MASK;
2820
2821 /* read the second set of memory latencies[4:7] */
2822 val = 1; /* data0 to be programmed to 1 for second set */
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01002823 mutex_lock(&dev_priv->pcu_lock);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002824 ret = sandybridge_pcode_read(dev_priv,
2825 GEN9_PCODE_READ_MEM_LATENCY,
2826 &val);
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01002827 mutex_unlock(&dev_priv->pcu_lock);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002828 if (ret) {
2829 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2830 return;
2831 }
2832
2833 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2834 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2835 GEN9_MEM_LATENCY_LEVEL_MASK;
2836 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2837 GEN9_MEM_LATENCY_LEVEL_MASK;
2838 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2839 GEN9_MEM_LATENCY_LEVEL_MASK;
2840
Vandana Kannan367294b2014-11-04 17:06:46 +00002841 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002842 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2843 * need to be disabled. We make sure to sanitize the values out
2844 * of the punit to satisfy this requirement.
2845 */
2846 for (level = 1; level <= max_level; level++) {
2847 if (wm[level] == 0) {
2848 for (i = level + 1; i <= max_level; i++)
2849 wm[i] = 0;
2850 break;
2851 }
2852 }
2853
2854 /*
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002855 * WaWmMemoryReadLatency:skl+,glk
Damien Lespiau6f972352015-02-09 19:33:07 +00002856 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002857 * punit doesn't take into account the read latency so we need
Paulo Zanoni0727e402016-09-22 18:00:30 -03002858 * to add 2us to the various latency levels we retrieve from the
2859 * punit when level 0 response data us 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002860 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002861 if (wm[0] == 0) {
2862 wm[0] += 2;
2863 for (level = 1; level <= max_level; level++) {
2864 if (wm[level] == 0)
2865 break;
Vandana Kannan367294b2014-11-04 17:06:46 +00002866 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002867 }
Paulo Zanoni0727e402016-09-22 18:00:30 -03002868 }
2869
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002870 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002871 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2872
2873 wm[0] = (sskpd >> 56) & 0xFF;
2874 if (wm[0] == 0)
2875 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002876 wm[1] = (sskpd >> 4) & 0xFF;
2877 wm[2] = (sskpd >> 12) & 0xFF;
2878 wm[3] = (sskpd >> 20) & 0x1FF;
2879 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002880 } else if (INTEL_GEN(dev_priv) >= 6) {
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002881 uint32_t sskpd = I915_READ(MCH_SSKPD);
2882
2883 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2884 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2885 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2886 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002887 } else if (INTEL_GEN(dev_priv) >= 5) {
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002888 uint32_t mltr = I915_READ(MLTR_ILK);
2889
2890 /* ILK primary LP0 latency is 700 ns */
2891 wm[0] = 7;
2892 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2893 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002894 } else {
2895 MISSING_CASE(INTEL_DEVID(dev_priv));
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002896 }
2897}
2898
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002899static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
2900 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002901{
2902 /* ILK sprite LP0 latency is 1300 ns */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002903 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002904 wm[0] = 13;
2905}
2906
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002907static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
2908 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002909{
2910 /* ILK cursor LP0 latency is 1300 ns */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002911 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002912 wm[0] = 13;
2913
2914 /* WaDoubleCursorLP3Latency:ivb */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002915 if (IS_IVYBRIDGE(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002916 wm[3] *= 2;
2917}
2918
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002919int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002920{
2921 /* how many WM levels are we expecting */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002922 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002923 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002924 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002925 return 4;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002926 else if (INTEL_GEN(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002927 return 3;
2928 else
2929 return 2;
2930}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002931
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002932static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002933 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002934 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002935{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002936 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002937
2938 for (level = 0; level <= max_level; level++) {
2939 unsigned int latency = wm[level];
2940
2941 if (latency == 0) {
2942 DRM_ERROR("%s WM%d latency not provided\n",
2943 name, level);
2944 continue;
2945 }
2946
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002947 /*
2948 * - latencies are in us on gen9.
2949 * - before then, WM1+ latency values are in 0.5us units
2950 */
Paulo Zanonidfc267a2017-08-09 13:52:46 -07002951 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002952 latency *= 10;
2953 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002954 latency *= 5;
2955
2956 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2957 name, level, wm[level],
2958 latency / 10, latency % 10);
2959 }
2960}
2961
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002962static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2963 uint16_t wm[5], uint16_t min)
2964{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002965 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002966
2967 if (wm[0] >= min)
2968 return false;
2969
2970 wm[0] = max(wm[0], min);
2971 for (level = 1; level <= max_level; level++)
2972 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2973
2974 return true;
2975}
2976
Ville Syrjäläbb726512016-10-31 22:37:24 +02002977static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002978{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002979 bool changed;
2980
2981 /*
2982 * The BIOS provided WM memory latency values are often
2983 * inadequate for high resolution displays. Adjust them.
2984 */
2985 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2986 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2987 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2988
2989 if (!changed)
2990 return;
2991
2992 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002993 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
2994 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
2995 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002996}
2997
Ville Syrjäläbb726512016-10-31 22:37:24 +02002998static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002999{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003000 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03003001
3002 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
3003 sizeof(dev_priv->wm.pri_latency));
3004 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
3005 sizeof(dev_priv->wm.pri_latency));
3006
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003007 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003008 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003009
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003010 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3011 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3012 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003013
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003014 if (IS_GEN6(dev_priv))
Ville Syrjäläbb726512016-10-31 22:37:24 +02003015 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03003016}
3017
Ville Syrjäläbb726512016-10-31 22:37:24 +02003018static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003019{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003020 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003021 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003022}
3023
Matt Ropered4a6a72016-02-23 17:20:13 -08003024static bool ilk_validate_pipe_wm(struct drm_device *dev,
3025 struct intel_pipe_wm *pipe_wm)
3026{
3027 /* LP0 watermark maximums depend on this pipe alone */
3028 const struct intel_wm_config config = {
3029 .num_pipes_active = 1,
3030 .sprites_enabled = pipe_wm->sprites_enabled,
3031 .sprites_scaled = pipe_wm->sprites_scaled,
3032 };
3033 struct ilk_wm_maximums max;
3034
3035 /* LP0 watermarks always use 1/2 DDB partitioning */
3036 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
3037
3038 /* At least LP0 must be valid */
3039 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
3040 DRM_DEBUG_KMS("LP0 watermark invalid\n");
3041 return false;
3042 }
3043
3044 return true;
3045}
3046
Matt Roper261a27d2015-10-08 15:28:25 -07003047/* Compute new watermarks for the pipe */
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003048static int ilk_compute_pipe_wm(struct intel_crtc_state *cstate)
Matt Roper261a27d2015-10-08 15:28:25 -07003049{
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003050 struct drm_atomic_state *state = cstate->base.state;
3051 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07003052 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003053 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003054 const struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003055 struct drm_plane *plane;
3056 const struct drm_plane_state *plane_state;
3057 const struct intel_plane_state *pristate = NULL;
3058 const struct intel_plane_state *sprstate = NULL;
3059 const struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003060 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02003061 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003062
Matt Ropere8f1f022016-05-12 07:05:55 -07003063 pipe_wm = &cstate->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003064
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003065 drm_atomic_crtc_state_for_each_plane_state(plane, plane_state, &cstate->base) {
3066 const struct intel_plane_state *ps = to_intel_plane_state(plane_state);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003067
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003068 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003069 pristate = ps;
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003070 else if (plane->type == DRM_PLANE_TYPE_OVERLAY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003071 sprstate = ps;
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003072 else if (plane->type == DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003073 curstate = ps;
Matt Roper43d59ed2015-09-24 15:53:07 -07003074 }
3075
Matt Ropered4a6a72016-02-23 17:20:13 -08003076 pipe_wm->pipe_enabled = cstate->base.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003077 if (sprstate) {
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003078 pipe_wm->sprites_enabled = sprstate->base.visible;
3079 pipe_wm->sprites_scaled = sprstate->base.visible &&
3080 (drm_rect_width(&sprstate->base.dst) != drm_rect_width(&sprstate->base.src) >> 16 ||
3081 drm_rect_height(&sprstate->base.dst) != drm_rect_height(&sprstate->base.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003082 }
3083
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003084 usable_level = max_level;
3085
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003086 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003087 if (INTEL_GEN(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003088 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003089
3090 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08003091 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003092 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003093
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003094 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003095 ilk_compute_wm_level(dev_priv, intel_crtc, 0, cstate,
3096 pristate, sprstate, curstate, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003097
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003098 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjälä532f7a72016-04-29 17:31:17 +03003099 pipe_wm->linetime = hsw_compute_linetime_wm(cstate);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003100
Matt Ropered4a6a72016-02-23 17:20:13 -08003101 if (!ilk_validate_pipe_wm(dev, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01003102 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003103
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003104 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003105
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003106 for (level = 1; level <= usable_level; level++) {
3107 struct intel_wm_level *wm = &pipe_wm->wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003108
Matt Roper86c8bbb2015-09-24 15:53:16 -07003109 ilk_compute_wm_level(dev_priv, intel_crtc, level, cstate,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003110 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003111
3112 /*
3113 * Disable any watermark level that exceeds the
3114 * register maximums since such watermarks are
3115 * always invalid.
3116 */
Maarten Lankhorst28283f42017-10-19 17:13:40 +02003117 if (!ilk_validate_wm_level(level, &max, wm)) {
3118 memset(wm, 0, sizeof(*wm));
3119 break;
3120 }
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003121 }
3122
Matt Roper86c8bbb2015-09-24 15:53:16 -07003123 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003124}
3125
3126/*
Matt Ropered4a6a72016-02-23 17:20:13 -08003127 * Build a set of 'intermediate' watermark values that satisfy both the old
3128 * state and the new state. These can be programmed to the hardware
3129 * immediately.
3130 */
3131static int ilk_compute_intermediate_wm(struct drm_device *dev,
3132 struct intel_crtc *intel_crtc,
3133 struct intel_crtc_state *newstate)
3134{
Matt Ropere8f1f022016-05-12 07:05:55 -07003135 struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003136 struct intel_atomic_state *intel_state =
3137 to_intel_atomic_state(newstate->base.state);
3138 const struct intel_crtc_state *oldstate =
3139 intel_atomic_get_old_crtc_state(intel_state, intel_crtc);
3140 const struct intel_pipe_wm *b = &oldstate->wm.ilk.optimal;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003141 int level, max_level = ilk_wm_max_level(to_i915(dev));
Matt Ropered4a6a72016-02-23 17:20:13 -08003142
3143 /*
3144 * Start with the final, target watermarks, then combine with the
3145 * currently active watermarks to get values that are safe both before
3146 * and after the vblank.
3147 */
Matt Ropere8f1f022016-05-12 07:05:55 -07003148 *a = newstate->wm.ilk.optimal;
Maarten Lankhorstb6b178a2017-10-19 17:13:41 +02003149 if (!newstate->base.active || drm_atomic_crtc_needs_modeset(&newstate->base))
3150 return 0;
3151
Matt Ropered4a6a72016-02-23 17:20:13 -08003152 a->pipe_enabled |= b->pipe_enabled;
3153 a->sprites_enabled |= b->sprites_enabled;
3154 a->sprites_scaled |= b->sprites_scaled;
3155
3156 for (level = 0; level <= max_level; level++) {
3157 struct intel_wm_level *a_wm = &a->wm[level];
3158 const struct intel_wm_level *b_wm = &b->wm[level];
3159
3160 a_wm->enable &= b_wm->enable;
3161 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
3162 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
3163 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
3164 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
3165 }
3166
3167 /*
3168 * We need to make sure that these merged watermark values are
3169 * actually a valid configuration themselves. If they're not,
3170 * there's no safe way to transition from the old state to
3171 * the new state, so we need to fail the atomic transaction.
3172 */
3173 if (!ilk_validate_pipe_wm(dev, a))
3174 return -EINVAL;
3175
3176 /*
3177 * If our intermediate WM are identical to the final WM, then we can
3178 * omit the post-vblank programming; only update if it's different.
3179 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02003180 if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) != 0)
3181 newstate->wm.need_postvbl_update = true;
Matt Ropered4a6a72016-02-23 17:20:13 -08003182
3183 return 0;
3184}
3185
3186/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003187 * Merge the watermarks from all active pipes for a specific level.
3188 */
3189static void ilk_merge_wm_level(struct drm_device *dev,
3190 int level,
3191 struct intel_wm_level *ret_wm)
3192{
3193 const struct intel_crtc *intel_crtc;
3194
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003195 ret_wm->enable = true;
3196
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003197 for_each_intel_crtc(dev, intel_crtc) {
Matt Ropered4a6a72016-02-23 17:20:13 -08003198 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02003199 const struct intel_wm_level *wm = &active->wm[level];
3200
3201 if (!active->pipe_enabled)
3202 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003203
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003204 /*
3205 * The watermark values may have been used in the past,
3206 * so we must maintain them in the registers for some
3207 * time even if the level is now disabled.
3208 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003209 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003210 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003211
3212 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
3213 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
3214 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
3215 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
3216 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003217}
3218
3219/*
3220 * Merge all low power watermarks for all active pipes.
3221 */
3222static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003223 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02003224 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003225 struct intel_pipe_wm *merged)
3226{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003227 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003228 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003229 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003230
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003231 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003232 if ((INTEL_GEN(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003233 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03003234 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003235
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003236 /* ILK: FBC WM must be disabled always */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003237 merged->fbc_wm_enabled = INTEL_GEN(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003238
3239 /* merge each WM1+ level */
3240 for (level = 1; level <= max_level; level++) {
3241 struct intel_wm_level *wm = &merged->wm[level];
3242
3243 ilk_merge_wm_level(dev, level, wm);
3244
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003245 if (level > last_enabled_level)
3246 wm->enable = false;
3247 else if (!ilk_validate_wm_level(level, max, wm))
3248 /* make sure all following levels get disabled */
3249 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003250
3251 /*
3252 * The spec says it is preferred to disable
3253 * FBC WMs instead of disabling a WM level.
3254 */
3255 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003256 if (wm->enable)
3257 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003258 wm->fbc_val = 0;
3259 }
3260 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003261
3262 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
3263 /*
3264 * FIXME this is racy. FBC might get enabled later.
3265 * What we should check here is whether FBC can be
3266 * enabled sometime later.
3267 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003268 if (IS_GEN5(dev_priv) && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03003269 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003270 for (level = 2; level <= max_level; level++) {
3271 struct intel_wm_level *wm = &merged->wm[level];
3272
3273 wm->enable = false;
3274 }
3275 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003276}
3277
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003278static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
3279{
3280 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
3281 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
3282}
3283
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003284/* The value we need to program into the WM_LPx latency field */
3285static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
3286{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003287 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003288
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003289 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003290 return 2 * level;
3291 else
3292 return dev_priv->wm.pri_latency[level];
3293}
3294
Imre Deak820c1982013-12-17 14:46:36 +02003295static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03003296 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03003297 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02003298 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003299{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003300 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003301 struct intel_crtc *intel_crtc;
3302 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003303
Ville Syrjälä0362c782013-10-09 19:17:57 +03003304 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03003305 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003306
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003307 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03003308 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03003309 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003310
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003311 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003312
Ville Syrjälä0362c782013-10-09 19:17:57 +03003313 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03003314
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003315 /*
3316 * Maintain the watermark values even if the level is
3317 * disabled. Doing otherwise could cause underruns.
3318 */
3319 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003320 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07003321 (r->pri_val << WM1_LP_SR_SHIFT) |
3322 r->cur_val;
3323
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003324 if (r->enable)
3325 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
3326
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003327 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07003328 results->wm_lp[wm_lp - 1] |=
3329 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
3330 else
3331 results->wm_lp[wm_lp - 1] |=
3332 r->fbc_val << WM1_LP_FBC_SHIFT;
3333
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003334 /*
3335 * Always set WM1S_LP_EN when spr_val != 0, even if the
3336 * level is disabled. Doing otherwise could cause underruns.
3337 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003338 if (INTEL_GEN(dev_priv) <= 6 && r->spr_val) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003339 WARN_ON(wm_lp != 1);
3340 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
3341 } else
3342 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003343 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003344
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003345 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003346 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003347 enum pipe pipe = intel_crtc->pipe;
Matt Ropered4a6a72016-02-23 17:20:13 -08003348 const struct intel_wm_level *r =
3349 &intel_crtc->wm.active.ilk.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003350
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003351 if (WARN_ON(!r->enable))
3352 continue;
3353
Matt Ropered4a6a72016-02-23 17:20:13 -08003354 results->wm_linetime[pipe] = intel_crtc->wm.active.ilk.linetime;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003355
3356 results->wm_pipe[pipe] =
3357 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
3358 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
3359 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003360 }
3361}
3362
Paulo Zanoni861f3382013-05-31 10:19:21 -03003363/* Find the result with the highest level enabled. Check for enable_fbc_wm in
3364 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02003365static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003366 struct intel_pipe_wm *r1,
3367 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003368{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003369 int level, max_level = ilk_wm_max_level(to_i915(dev));
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003370 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003371
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003372 for (level = 1; level <= max_level; level++) {
3373 if (r1->wm[level].enable)
3374 level1 = level;
3375 if (r2->wm[level].enable)
3376 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003377 }
3378
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003379 if (level1 == level2) {
3380 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003381 return r2;
3382 else
3383 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003384 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03003385 return r1;
3386 } else {
3387 return r2;
3388 }
3389}
3390
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003391/* dirty bits used to track which watermarks need changes */
3392#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
3393#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
3394#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
3395#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
3396#define WM_DIRTY_FBC (1 << 24)
3397#define WM_DIRTY_DDB (1 << 25)
3398
Damien Lespiau055e3932014-08-18 13:49:10 +01003399static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02003400 const struct ilk_wm_values *old,
3401 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003402{
3403 unsigned int dirty = 0;
3404 enum pipe pipe;
3405 int wm_lp;
3406
Damien Lespiau055e3932014-08-18 13:49:10 +01003407 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003408 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
3409 dirty |= WM_DIRTY_LINETIME(pipe);
3410 /* Must disable LP1+ watermarks too */
3411 dirty |= WM_DIRTY_LP_ALL;
3412 }
3413
3414 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
3415 dirty |= WM_DIRTY_PIPE(pipe);
3416 /* Must disable LP1+ watermarks too */
3417 dirty |= WM_DIRTY_LP_ALL;
3418 }
3419 }
3420
3421 if (old->enable_fbc_wm != new->enable_fbc_wm) {
3422 dirty |= WM_DIRTY_FBC;
3423 /* Must disable LP1+ watermarks too */
3424 dirty |= WM_DIRTY_LP_ALL;
3425 }
3426
3427 if (old->partitioning != new->partitioning) {
3428 dirty |= WM_DIRTY_DDB;
3429 /* Must disable LP1+ watermarks too */
3430 dirty |= WM_DIRTY_LP_ALL;
3431 }
3432
3433 /* LP1+ watermarks already deemed dirty, no need to continue */
3434 if (dirty & WM_DIRTY_LP_ALL)
3435 return dirty;
3436
3437 /* Find the lowest numbered LP1+ watermark in need of an update... */
3438 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3439 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
3440 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
3441 break;
3442 }
3443
3444 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
3445 for (; wm_lp <= 3; wm_lp++)
3446 dirty |= WM_DIRTY_LP(wm_lp);
3447
3448 return dirty;
3449}
3450
Ville Syrjälä8553c182013-12-05 15:51:39 +02003451static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
3452 unsigned int dirty)
3453{
Imre Deak820c1982013-12-17 14:46:36 +02003454 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02003455 bool changed = false;
3456
3457 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
3458 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
3459 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
3460 changed = true;
3461 }
3462 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
3463 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
3464 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
3465 changed = true;
3466 }
3467 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
3468 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
3469 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
3470 changed = true;
3471 }
3472
3473 /*
3474 * Don't touch WM1S_LP_EN here.
3475 * Doing so could cause underruns.
3476 */
3477
3478 return changed;
3479}
3480
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003481/*
3482 * The spec says we shouldn't write when we don't need, because every write
3483 * causes WMs to be re-evaluated, expending some power.
3484 */
Imre Deak820c1982013-12-17 14:46:36 +02003485static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
3486 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003487{
Imre Deak820c1982013-12-17 14:46:36 +02003488 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003489 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003490 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003491
Damien Lespiau055e3932014-08-18 13:49:10 +01003492 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003493 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003494 return;
3495
Ville Syrjälä8553c182013-12-05 15:51:39 +02003496 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003497
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003498 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003499 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003500 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003501 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003502 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003503 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
3504
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003505 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003506 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003507 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003508 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003509 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003510 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
3511
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003512 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003513 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003514 val = I915_READ(WM_MISC);
3515 if (results->partitioning == INTEL_DDB_PART_1_2)
3516 val &= ~WM_MISC_DATA_PARTITION_5_6;
3517 else
3518 val |= WM_MISC_DATA_PARTITION_5_6;
3519 I915_WRITE(WM_MISC, val);
3520 } else {
3521 val = I915_READ(DISP_ARB_CTL2);
3522 if (results->partitioning == INTEL_DDB_PART_1_2)
3523 val &= ~DISP_DATA_PARTITION_5_6;
3524 else
3525 val |= DISP_DATA_PARTITION_5_6;
3526 I915_WRITE(DISP_ARB_CTL2, val);
3527 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003528 }
3529
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003530 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03003531 val = I915_READ(DISP_ARB_CTL);
3532 if (results->enable_fbc_wm)
3533 val &= ~DISP_FBC_WM_DIS;
3534 else
3535 val |= DISP_FBC_WM_DIS;
3536 I915_WRITE(DISP_ARB_CTL, val);
3537 }
3538
Imre Deak954911e2013-12-17 14:46:34 +02003539 if (dirty & WM_DIRTY_LP(1) &&
3540 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
3541 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
3542
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003543 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003544 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
3545 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
3546 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
3547 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
3548 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003549
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003550 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003551 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003552 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003553 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003554 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003555 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003556
3557 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003558}
3559
Matt Ropered4a6a72016-02-23 17:20:13 -08003560bool ilk_disable_lp_wm(struct drm_device *dev)
Ville Syrjälä8553c182013-12-05 15:51:39 +02003561{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003562 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003563
3564 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3565}
3566
Matt Roper024c9042015-09-24 15:53:11 -07003567/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003568 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3569 * so assume we'll always need it in order to avoid underruns.
3570 */
3571static bool skl_needs_memory_bw_wa(struct intel_atomic_state *state)
3572{
3573 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3574
Rodrigo Vivib976dc52017-01-23 10:32:37 -08003575 if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv))
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003576 return true;
3577
3578 return false;
3579}
3580
Paulo Zanoni56feca92016-09-22 18:00:28 -03003581static bool
3582intel_has_sagv(struct drm_i915_private *dev_priv)
3583{
Rodrigo Vivi01971812017-08-09 13:52:44 -07003584 if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv) ||
3585 IS_CANNONLAKE(dev_priv))
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003586 return true;
3587
3588 if (IS_SKYLAKE(dev_priv) &&
3589 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED)
3590 return true;
3591
3592 return false;
Paulo Zanoni56feca92016-09-22 18:00:28 -03003593}
3594
Lyude656d1b82016-08-17 15:55:54 -04003595/*
3596 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3597 * depending on power and performance requirements. The display engine access
3598 * to system memory is blocked during the adjustment time. Because of the
3599 * blocking time, having this enabled can cause full system hangs and/or pipe
3600 * underruns if we don't meet all of the following requirements:
3601 *
3602 * - <= 1 pipe enabled
3603 * - All planes can enable watermarks for latencies >= SAGV engine block time
3604 * - We're not using an interlaced display configuration
3605 */
3606int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003607intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003608{
3609 int ret;
3610
Paulo Zanoni56feca92016-09-22 18:00:28 -03003611 if (!intel_has_sagv(dev_priv))
3612 return 0;
3613
3614 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003615 return 0;
3616
3617 DRM_DEBUG_KMS("Enabling the SAGV\n");
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01003618 mutex_lock(&dev_priv->pcu_lock);
Lyude656d1b82016-08-17 15:55:54 -04003619
3620 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3621 GEN9_SAGV_ENABLE);
3622
3623 /* We don't need to wait for the SAGV when enabling */
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01003624 mutex_unlock(&dev_priv->pcu_lock);
Lyude656d1b82016-08-17 15:55:54 -04003625
3626 /*
3627 * Some skl systems, pre-release machines in particular,
3628 * don't actually have an SAGV.
3629 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003630 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003631 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003632 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003633 return 0;
3634 } else if (ret < 0) {
3635 DRM_ERROR("Failed to enable the SAGV\n");
3636 return ret;
3637 }
3638
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003639 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003640 return 0;
3641}
3642
Lyude656d1b82016-08-17 15:55:54 -04003643int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003644intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003645{
Imre Deakb3b8e992016-12-05 18:27:38 +02003646 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003647
Paulo Zanoni56feca92016-09-22 18:00:28 -03003648 if (!intel_has_sagv(dev_priv))
3649 return 0;
3650
3651 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003652 return 0;
3653
3654 DRM_DEBUG_KMS("Disabling the SAGV\n");
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01003655 mutex_lock(&dev_priv->pcu_lock);
Lyude656d1b82016-08-17 15:55:54 -04003656
3657 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003658 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3659 GEN9_SAGV_DISABLE,
3660 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3661 1);
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01003662 mutex_unlock(&dev_priv->pcu_lock);
Lyude656d1b82016-08-17 15:55:54 -04003663
Lyude656d1b82016-08-17 15:55:54 -04003664 /*
3665 * Some skl systems, pre-release machines in particular,
3666 * don't actually have an SAGV.
3667 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003668 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003669 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003670 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003671 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003672 } else if (ret < 0) {
3673 DRM_ERROR("Failed to disable the SAGV (%d)\n", ret);
3674 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003675 }
3676
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003677 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003678 return 0;
3679}
3680
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003681bool intel_can_enable_sagv(struct drm_atomic_state *state)
Lyude656d1b82016-08-17 15:55:54 -04003682{
3683 struct drm_device *dev = state->dev;
3684 struct drm_i915_private *dev_priv = to_i915(dev);
3685 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003686 struct intel_crtc *crtc;
3687 struct intel_plane *plane;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003688 struct intel_crtc_state *cstate;
Lyude656d1b82016-08-17 15:55:54 -04003689 enum pipe pipe;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003690 int level, latency;
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003691 int sagv_block_time_us = IS_GEN9(dev_priv) ? 30 : 20;
Lyude656d1b82016-08-17 15:55:54 -04003692
Paulo Zanoni56feca92016-09-22 18:00:28 -03003693 if (!intel_has_sagv(dev_priv))
3694 return false;
3695
Lyude656d1b82016-08-17 15:55:54 -04003696 /*
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003697 * SKL+ workaround: bspec recommends we disable the SAGV when we have
Lyude656d1b82016-08-17 15:55:54 -04003698 * more then one pipe enabled
3699 *
3700 * If there are no active CRTCs, no additional checks need be performed
3701 */
3702 if (hweight32(intel_state->active_crtcs) == 0)
3703 return true;
3704 else if (hweight32(intel_state->active_crtcs) > 1)
3705 return false;
3706
3707 /* Since we're now guaranteed to only have one active CRTC... */
3708 pipe = ffs(intel_state->active_crtcs) - 1;
Ville Syrjälä98187832016-10-31 22:37:10 +02003709 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003710 cstate = to_intel_crtc_state(crtc->base.state);
Lyude656d1b82016-08-17 15:55:54 -04003711
Paulo Zanonic89cadd2016-10-10 17:30:59 -03003712 if (crtc->base.state->adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003713 return false;
3714
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003715 for_each_intel_plane_on_crtc(dev, crtc, plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003716 struct skl_plane_wm *wm =
3717 &cstate->wm.skl.optimal.planes[plane->id];
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003718
Lyude656d1b82016-08-17 15:55:54 -04003719 /* Skip this plane if it's not enabled */
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003720 if (!wm->wm[0].plane_en)
Lyude656d1b82016-08-17 15:55:54 -04003721 continue;
3722
3723 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003724 for (level = ilk_wm_max_level(dev_priv);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003725 !wm->wm[level].plane_en; --level)
Lyude656d1b82016-08-17 15:55:54 -04003726 { }
3727
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003728 latency = dev_priv->wm.skl_latency[level];
3729
3730 if (skl_needs_memory_bw_wa(intel_state) &&
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003731 plane->base.state->fb->modifier ==
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003732 I915_FORMAT_MOD_X_TILED)
3733 latency += 15;
3734
Lyude656d1b82016-08-17 15:55:54 -04003735 /*
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003736 * If any of the planes on this pipe don't enable wm levels that
3737 * incur memory latencies higher than sagv_block_time_us we
3738 * can't enable the SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003739 */
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003740 if (latency < sagv_block_time_us)
Lyude656d1b82016-08-17 15:55:54 -04003741 return false;
3742 }
3743
3744 return true;
3745}
3746
Damien Lespiaub9cec072014-11-04 17:06:43 +00003747static void
3748skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
Matt Roper024c9042015-09-24 15:53:11 -07003749 const struct intel_crtc_state *cstate,
Matt Roperc107acf2016-05-12 07:06:01 -07003750 struct skl_ddb_entry *alloc, /* out */
3751 int *num_active /* out */)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003752{
Matt Roperc107acf2016-05-12 07:06:01 -07003753 struct drm_atomic_state *state = cstate->base.state;
3754 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
3755 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper024c9042015-09-24 15:53:11 -07003756 struct drm_crtc *for_crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003757 unsigned int pipe_size, ddb_size;
3758 int nth_active_pipe;
Matt Roperc107acf2016-05-12 07:06:01 -07003759
Matt Ropera6d3460e2016-05-12 07:06:04 -07003760 if (WARN_ON(!state) || !cstate->base.active) {
Damien Lespiaub9cec072014-11-04 17:06:43 +00003761 alloc->start = 0;
3762 alloc->end = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003763 *num_active = hweight32(dev_priv->active_crtcs);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003764 return;
3765 }
3766
Matt Ropera6d3460e2016-05-12 07:06:04 -07003767 if (intel_state->active_pipe_changes)
3768 *num_active = hweight32(intel_state->active_crtcs);
3769 else
3770 *num_active = hweight32(dev_priv->active_crtcs);
3771
Deepak M6f3fff62016-09-15 15:01:10 +05303772 ddb_size = INTEL_INFO(dev_priv)->ddb_size;
3773 WARN_ON(ddb_size == 0);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003774
3775 ddb_size -= 4; /* 4 blocks for bypass path allocation */
3776
Matt Roperc107acf2016-05-12 07:06:01 -07003777 /*
Matt Ropera6d3460e2016-05-12 07:06:04 -07003778 * If the state doesn't change the active CRTC's, then there's
3779 * no need to recalculate; the existing pipe allocation limits
3780 * should remain unchanged. Note that we're safe from racing
3781 * commits since any racing commit that changes the active CRTC
3782 * list would need to grab _all_ crtc locks, including the one
3783 * we currently hold.
Matt Roperc107acf2016-05-12 07:06:01 -07003784 */
Matt Ropera6d3460e2016-05-12 07:06:04 -07003785 if (!intel_state->active_pipe_changes) {
Maarten Lankhorst512b5522016-11-08 13:55:34 +01003786 /*
3787 * alloc may be cleared by clear_intel_crtc_state,
3788 * copy from old state to be sure
3789 */
3790 *alloc = to_intel_crtc_state(for_crtc->state)->wm.skl.ddb;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003791 return;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003792 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07003793
3794 nth_active_pipe = hweight32(intel_state->active_crtcs &
3795 (drm_crtc_mask(for_crtc) - 1));
3796 pipe_size = ddb_size / hweight32(intel_state->active_crtcs);
3797 alloc->start = nth_active_pipe * ddb_size / *num_active;
3798 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003799}
3800
Matt Roperc107acf2016-05-12 07:06:01 -07003801static unsigned int skl_cursor_allocation(int num_active)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003802{
Matt Roperc107acf2016-05-12 07:06:01 -07003803 if (num_active == 1)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003804 return 32;
3805
3806 return 8;
3807}
3808
Damien Lespiaua269c582014-11-04 17:06:49 +00003809static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
3810{
3811 entry->start = reg & 0x3ff;
3812 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00003813 if (entry->end)
3814 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00003815}
3816
Damien Lespiau08db6652014-11-04 17:06:52 +00003817void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
3818 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00003819{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003820 struct intel_crtc *crtc;
Damien Lespiaua269c582014-11-04 17:06:49 +00003821
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003822 memset(ddb, 0, sizeof(*ddb));
3823
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003824 for_each_intel_crtc(&dev_priv->drm, crtc) {
Imre Deak4d800032016-02-17 16:31:29 +02003825 enum intel_display_power_domain power_domain;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003826 enum plane_id plane_id;
3827 enum pipe pipe = crtc->pipe;
Imre Deak4d800032016-02-17 16:31:29 +02003828
3829 power_domain = POWER_DOMAIN_PIPE(pipe);
3830 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003831 continue;
3832
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003833 for_each_plane_id_on_crtc(crtc, plane_id) {
3834 u32 val;
Damien Lespiaua269c582014-11-04 17:06:49 +00003835
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003836 if (plane_id != PLANE_CURSOR)
3837 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
3838 else
3839 val = I915_READ(CUR_BUF_CFG(pipe));
3840
3841 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane_id], val);
3842 }
Imre Deak4d800032016-02-17 16:31:29 +02003843
3844 intel_display_power_put(dev_priv, power_domain);
Damien Lespiaua269c582014-11-04 17:06:49 +00003845 }
3846}
3847
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003848/*
3849 * Determines the downscale amount of a plane for the purposes of watermark calculations.
3850 * The bspec defines downscale amount as:
3851 *
3852 * """
3853 * Horizontal down scale amount = maximum[1, Horizontal source size /
3854 * Horizontal destination size]
3855 * Vertical down scale amount = maximum[1, Vertical source size /
3856 * Vertical destination size]
3857 * Total down scale amount = Horizontal down scale amount *
3858 * Vertical down scale amount
3859 * """
3860 *
3861 * Return value is provided in 16.16 fixed point form to retain fractional part.
3862 * Caller should take care of dividing & rounding off the value.
3863 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303864static uint_fixed_16_16_t
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003865skl_plane_downscale_amount(const struct intel_crtc_state *cstate,
3866 const struct intel_plane_state *pstate)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003867{
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003868 struct intel_plane *plane = to_intel_plane(pstate->base.plane);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003869 uint32_t src_w, src_h, dst_w, dst_h;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303870 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
3871 uint_fixed_16_16_t downscale_h, downscale_w;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003872
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003873 if (WARN_ON(!intel_wm_plane_visible(cstate, pstate)))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303874 return u32_to_fixed16(0);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003875
3876 /* n.b., src is 16.16 fixed point, dst is whole integer */
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003877 if (plane->id == PLANE_CURSOR) {
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03003878 /*
3879 * Cursors only support 0/180 degree rotation,
3880 * hence no need to account for rotation here.
3881 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303882 src_w = pstate->base.src_w >> 16;
3883 src_h = pstate->base.src_h >> 16;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003884 dst_w = pstate->base.crtc_w;
3885 dst_h = pstate->base.crtc_h;
3886 } else {
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03003887 /*
3888 * Src coordinates are already rotated by 270 degrees for
3889 * the 90/270 degree plane rotation cases (to match the
3890 * GTT mapping), hence no need to account for rotation here.
3891 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303892 src_w = drm_rect_width(&pstate->base.src) >> 16;
3893 src_h = drm_rect_height(&pstate->base.src) >> 16;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003894 dst_w = drm_rect_width(&pstate->base.dst);
3895 dst_h = drm_rect_height(&pstate->base.dst);
3896 }
3897
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303898 fp_w_ratio = div_fixed16(src_w, dst_w);
3899 fp_h_ratio = div_fixed16(src_h, dst_h);
3900 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
3901 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003902
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303903 return mul_fixed16(downscale_w, downscale_h);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003904}
3905
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303906static uint_fixed_16_16_t
3907skl_pipe_downscale_amount(const struct intel_crtc_state *crtc_state)
3908{
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303909 uint_fixed_16_16_t pipe_downscale = u32_to_fixed16(1);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303910
3911 if (!crtc_state->base.enable)
3912 return pipe_downscale;
3913
3914 if (crtc_state->pch_pfit.enabled) {
3915 uint32_t src_w, src_h, dst_w, dst_h;
3916 uint32_t pfit_size = crtc_state->pch_pfit.size;
3917 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
3918 uint_fixed_16_16_t downscale_h, downscale_w;
3919
3920 src_w = crtc_state->pipe_src_w;
3921 src_h = crtc_state->pipe_src_h;
3922 dst_w = pfit_size >> 16;
3923 dst_h = pfit_size & 0xffff;
3924
3925 if (!dst_w || !dst_h)
3926 return pipe_downscale;
3927
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303928 fp_w_ratio = div_fixed16(src_w, dst_w);
3929 fp_h_ratio = div_fixed16(src_h, dst_h);
3930 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
3931 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303932
3933 pipe_downscale = mul_fixed16(downscale_w, downscale_h);
3934 }
3935
3936 return pipe_downscale;
3937}
3938
3939int skl_check_pipe_max_pixel_rate(struct intel_crtc *intel_crtc,
3940 struct intel_crtc_state *cstate)
3941{
Rodrigo Vivi43037c82017-10-03 15:31:42 -07003942 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303943 struct drm_crtc_state *crtc_state = &cstate->base;
3944 struct drm_atomic_state *state = crtc_state->state;
3945 struct drm_plane *plane;
3946 const struct drm_plane_state *pstate;
3947 struct intel_plane_state *intel_pstate;
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02003948 int crtc_clock, dotclk;
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303949 uint32_t pipe_max_pixel_rate;
3950 uint_fixed_16_16_t pipe_downscale;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303951 uint_fixed_16_16_t max_downscale = u32_to_fixed16(1);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303952
3953 if (!cstate->base.enable)
3954 return 0;
3955
3956 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, crtc_state) {
3957 uint_fixed_16_16_t plane_downscale;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303958 uint_fixed_16_16_t fp_9_div_8 = div_fixed16(9, 8);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303959 int bpp;
3960
3961 if (!intel_wm_plane_visible(cstate,
3962 to_intel_plane_state(pstate)))
3963 continue;
3964
3965 if (WARN_ON(!pstate->fb))
3966 return -EINVAL;
3967
3968 intel_pstate = to_intel_plane_state(pstate);
3969 plane_downscale = skl_plane_downscale_amount(cstate,
3970 intel_pstate);
3971 bpp = pstate->fb->format->cpp[0] * 8;
3972 if (bpp == 64)
3973 plane_downscale = mul_fixed16(plane_downscale,
3974 fp_9_div_8);
3975
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303976 max_downscale = max_fixed16(plane_downscale, max_downscale);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303977 }
3978 pipe_downscale = skl_pipe_downscale_amount(cstate);
3979
3980 pipe_downscale = mul_fixed16(pipe_downscale, max_downscale);
3981
3982 crtc_clock = crtc_state->adjusted_mode.crtc_clock;
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02003983 dotclk = to_intel_atomic_state(state)->cdclk.logical.cdclk;
3984
Rodrigo Vivi43037c82017-10-03 15:31:42 -07003985 if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10)
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02003986 dotclk *= 2;
3987
3988 pipe_max_pixel_rate = div_round_up_u32_fixed16(dotclk, pipe_downscale);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303989
3990 if (pipe_max_pixel_rate < crtc_clock) {
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02003991 DRM_DEBUG_KMS("Max supported pixel clock with scaling exceeded\n");
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303992 return -EINVAL;
3993 }
3994
3995 return 0;
3996}
3997
Damien Lespiaub9cec072014-11-04 17:06:43 +00003998static unsigned int
Matt Roper024c9042015-09-24 15:53:11 -07003999skl_plane_relative_data_rate(const struct intel_crtc_state *cstate,
4000 const struct drm_plane_state *pstate,
4001 int y)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004002{
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004003 struct intel_plane *plane = to_intel_plane(pstate->plane);
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004004 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304005 uint32_t data_rate;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004006 uint32_t width = 0, height = 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02004007 struct drm_framebuffer *fb;
4008 u32 format;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304009 uint_fixed_16_16_t down_scale_amount;
Matt Ropera1de91e2016-05-12 07:05:57 -07004010
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004011 if (!intel_pstate->base.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07004012 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02004013
4014 fb = pstate->fb;
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004015 format = fb->format->format;
Ville Syrjälä83054942016-11-18 21:53:00 +02004016
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004017 if (plane->id == PLANE_CURSOR)
Matt Ropera1de91e2016-05-12 07:05:57 -07004018 return 0;
4019 if (y && format != DRM_FORMAT_NV12)
4020 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004021
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004022 /*
4023 * Src coordinates are already rotated by 270 degrees for
4024 * the 90/270 degree plane rotation cases (to match the
4025 * GTT mapping), hence no need to account for rotation here.
4026 */
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004027 width = drm_rect_width(&intel_pstate->base.src) >> 16;
4028 height = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004029
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004030 /* for planar format */
Matt Ropera1de91e2016-05-12 07:05:57 -07004031 if (format == DRM_FORMAT_NV12) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004032 if (y) /* y-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004033 data_rate = width * height *
Ville Syrjälä353c8592016-12-14 23:30:57 +02004034 fb->format->cpp[0];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004035 else /* uv-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004036 data_rate = (width / 2) * (height / 2) *
Ville Syrjälä353c8592016-12-14 23:30:57 +02004037 fb->format->cpp[1];
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004038 } else {
4039 /* for packed formats */
Ville Syrjälä353c8592016-12-14 23:30:57 +02004040 data_rate = width * height * fb->format->cpp[0];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004041 }
4042
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004043 down_scale_amount = skl_plane_downscale_amount(cstate, intel_pstate);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004044
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304045 return mul_round_up_u32_fixed16(data_rate, down_scale_amount);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004046}
4047
4048/*
4049 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
4050 * a 8192x4096@32bpp framebuffer:
4051 * 3 * 4096 * 8192 * 4 < 2^32
4052 */
4053static unsigned int
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004054skl_get_total_relative_data_rate(struct intel_crtc_state *intel_cstate,
4055 unsigned *plane_data_rate,
4056 unsigned *plane_y_data_rate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004057{
Matt Roper9c74d822016-05-12 07:05:58 -07004058 struct drm_crtc_state *cstate = &intel_cstate->base;
4059 struct drm_atomic_state *state = cstate->state;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02004060 struct drm_plane *plane;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02004061 const struct drm_plane_state *pstate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004062 unsigned int total_data_rate = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07004063
4064 if (WARN_ON(!state))
4065 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004066
Matt Ropera1de91e2016-05-12 07:05:57 -07004067 /* Calculate and cache data rate for each plane */
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02004068 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, cstate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004069 enum plane_id plane_id = to_intel_plane(plane)->id;
4070 unsigned int rate;
Matt Roper024c9042015-09-24 15:53:11 -07004071
Matt Ropera6d3460e2016-05-12 07:06:04 -07004072 /* packed/uv */
4073 rate = skl_plane_relative_data_rate(intel_cstate,
4074 pstate, 0);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004075 plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004076
4077 total_data_rate += rate;
Matt Roper9c74d822016-05-12 07:05:58 -07004078
Matt Ropera6d3460e2016-05-12 07:06:04 -07004079 /* y-plane */
4080 rate = skl_plane_relative_data_rate(intel_cstate,
4081 pstate, 1);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004082 plane_y_data_rate[plane_id] = rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07004083
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004084 total_data_rate += rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004085 }
4086
4087 return total_data_rate;
4088}
4089
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004090static uint16_t
4091skl_ddb_min_alloc(const struct drm_plane_state *pstate,
4092 const int y)
4093{
4094 struct drm_framebuffer *fb = pstate->fb;
4095 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
4096 uint32_t src_w, src_h;
4097 uint32_t min_scanlines = 8;
4098 uint8_t plane_bpp;
4099
4100 if (WARN_ON(!fb))
4101 return 0;
4102
4103 /* For packed formats, no y-plane, return 0 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004104 if (y && fb->format->format != DRM_FORMAT_NV12)
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004105 return 0;
4106
4107 /* For Non Y-tile return 8-blocks */
Ville Syrjäläbae781b2016-11-16 13:33:16 +02004108 if (fb->modifier != I915_FORMAT_MOD_Y_TILED &&
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07004109 fb->modifier != I915_FORMAT_MOD_Yf_TILED &&
4110 fb->modifier != I915_FORMAT_MOD_Y_TILED_CCS &&
4111 fb->modifier != I915_FORMAT_MOD_Yf_TILED_CCS)
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004112 return 8;
4113
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004114 /*
4115 * Src coordinates are already rotated by 270 degrees for
4116 * the 90/270 degree plane rotation cases (to match the
4117 * GTT mapping), hence no need to account for rotation here.
4118 */
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004119 src_w = drm_rect_width(&intel_pstate->base.src) >> 16;
4120 src_h = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004121
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004122 /* Halve UV plane width and height for NV12 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004123 if (fb->format->format == DRM_FORMAT_NV12 && !y) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004124 src_w /= 2;
4125 src_h /= 2;
4126 }
4127
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004128 if (fb->format->format == DRM_FORMAT_NV12 && !y)
Ville Syrjälä353c8592016-12-14 23:30:57 +02004129 plane_bpp = fb->format->cpp[1];
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004130 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02004131 plane_bpp = fb->format->cpp[0];
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004132
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03004133 if (drm_rotation_90_or_270(pstate->rotation)) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004134 switch (plane_bpp) {
4135 case 1:
4136 min_scanlines = 32;
4137 break;
4138 case 2:
4139 min_scanlines = 16;
4140 break;
4141 case 4:
4142 min_scanlines = 8;
4143 break;
4144 case 8:
4145 min_scanlines = 4;
4146 break;
4147 default:
4148 WARN(1, "Unsupported pixel depth %u for rotation",
4149 plane_bpp);
4150 min_scanlines = 32;
4151 }
4152 }
4153
4154 return DIV_ROUND_UP((4 * src_w * plane_bpp), 512) * min_scanlines/4 + 3;
4155}
4156
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004157static void
4158skl_ddb_calc_min(const struct intel_crtc_state *cstate, int num_active,
4159 uint16_t *minimum, uint16_t *y_minimum)
4160{
4161 const struct drm_plane_state *pstate;
4162 struct drm_plane *plane;
4163
4164 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, &cstate->base) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004165 enum plane_id plane_id = to_intel_plane(plane)->id;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004166
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004167 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004168 continue;
4169
4170 if (!pstate->visible)
4171 continue;
4172
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004173 minimum[plane_id] = skl_ddb_min_alloc(pstate, 0);
4174 y_minimum[plane_id] = skl_ddb_min_alloc(pstate, 1);
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004175 }
4176
4177 minimum[PLANE_CURSOR] = skl_cursor_allocation(num_active);
4178}
4179
Matt Roperc107acf2016-05-12 07:06:01 -07004180static int
Matt Roper024c9042015-09-24 15:53:11 -07004181skl_allocate_pipe_ddb(struct intel_crtc_state *cstate,
Damien Lespiaub9cec072014-11-04 17:06:43 +00004182 struct skl_ddb_allocation *ddb /* out */)
4183{
Matt Roperc107acf2016-05-12 07:06:01 -07004184 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07004185 struct drm_crtc *crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004186 struct drm_device *dev = crtc->dev;
4187 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4188 enum pipe pipe = intel_crtc->pipe;
Lyudece0ba282016-09-15 10:46:35 -04004189 struct skl_ddb_entry *alloc = &cstate->wm.skl.ddb;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004190 uint16_t alloc_size, start;
Maarten Lankhorstfefdd812016-10-26 15:41:33 +02004191 uint16_t minimum[I915_MAX_PLANES] = {};
4192 uint16_t y_minimum[I915_MAX_PLANES] = {};
Damien Lespiaub9cec072014-11-04 17:06:43 +00004193 unsigned int total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004194 enum plane_id plane_id;
Matt Roperc107acf2016-05-12 07:06:01 -07004195 int num_active;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004196 unsigned plane_data_rate[I915_MAX_PLANES] = {};
4197 unsigned plane_y_data_rate[I915_MAX_PLANES] = {};
Kumar, Mahesh5ba6faa2017-05-17 17:28:26 +05304198 uint16_t total_min_blocks = 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004199
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004200 /* Clear the partitioning for disabled planes. */
4201 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
4202 memset(ddb->y_plane[pipe], 0, sizeof(ddb->y_plane[pipe]));
4203
Matt Ropera6d3460e2016-05-12 07:06:04 -07004204 if (WARN_ON(!state))
4205 return 0;
4206
Matt Roperc107acf2016-05-12 07:06:01 -07004207 if (!cstate->base.active) {
Lyudece0ba282016-09-15 10:46:35 -04004208 alloc->start = alloc->end = 0;
Matt Roperc107acf2016-05-12 07:06:01 -07004209 return 0;
4210 }
4211
Matt Ropera6d3460e2016-05-12 07:06:04 -07004212 skl_ddb_get_pipe_allocation_limits(dev, cstate, alloc, &num_active);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00004213 alloc_size = skl_ddb_entry_size(alloc);
Kumar, Mahesh336031e2017-05-17 17:28:25 +05304214 if (alloc_size == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004215 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004216
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004217 skl_ddb_calc_min(cstate, num_active, minimum, y_minimum);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004218
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004219 /*
4220 * 1. Allocate the mininum required blocks for each active plane
4221 * and allocate the cursor, it doesn't require extra allocation
4222 * proportional to the data rate.
4223 */
Damien Lespiaub9cec072014-11-04 17:06:43 +00004224
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004225 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Kumar, Mahesh5ba6faa2017-05-17 17:28:26 +05304226 total_min_blocks += minimum[plane_id];
4227 total_min_blocks += y_minimum[plane_id];
Damien Lespiau80958152015-02-09 13:35:10 +00004228 }
4229
Kumar, Mahesh5ba6faa2017-05-17 17:28:26 +05304230 if (total_min_blocks > alloc_size) {
4231 DRM_DEBUG_KMS("Requested display configuration exceeds system DDB limitations");
4232 DRM_DEBUG_KMS("minimum required %d/%d\n", total_min_blocks,
4233 alloc_size);
4234 return -EINVAL;
4235 }
4236
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004237 alloc_size -= total_min_blocks;
4238 ddb->plane[pipe][PLANE_CURSOR].start = alloc->end - minimum[PLANE_CURSOR];
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004239 ddb->plane[pipe][PLANE_CURSOR].end = alloc->end;
4240
Damien Lespiaub9cec072014-11-04 17:06:43 +00004241 /*
Damien Lespiau80958152015-02-09 13:35:10 +00004242 * 2. Distribute the remaining space in proportion to the amount of
4243 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00004244 *
4245 * FIXME: we may not allocate every single block here.
4246 */
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004247 total_data_rate = skl_get_total_relative_data_rate(cstate,
4248 plane_data_rate,
4249 plane_y_data_rate);
Matt Ropera1de91e2016-05-12 07:05:57 -07004250 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004251 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004252
Damien Lespiau34bb56a2014-11-04 17:07:01 +00004253 start = alloc->start;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004254 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004255 unsigned int data_rate, y_data_rate;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004256 uint16_t plane_blocks, y_plane_blocks = 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004257
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004258 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004259 continue;
4260
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004261 data_rate = plane_data_rate[plane_id];
Damien Lespiaub9cec072014-11-04 17:06:43 +00004262
4263 /*
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004264 * allocation for (packed formats) or (uv-plane part of planar format):
Damien Lespiaub9cec072014-11-04 17:06:43 +00004265 * promote the expression to 64 bits to avoid overflowing, the
4266 * result is < available as data_rate / total_data_rate < 1
4267 */
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004268 plane_blocks = minimum[plane_id];
4269 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
4270 total_data_rate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004271
Matt Roperc107acf2016-05-12 07:06:01 -07004272 /* Leave disabled planes at (0,0) */
4273 if (data_rate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004274 ddb->plane[pipe][plane_id].start = start;
4275 ddb->plane[pipe][plane_id].end = start + plane_blocks;
Matt Roperc107acf2016-05-12 07:06:01 -07004276 }
Damien Lespiaub9cec072014-11-04 17:06:43 +00004277
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004278 start += plane_blocks;
4279
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004280 /*
4281 * allocation for y_plane part of planar format:
4282 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004283 y_data_rate = plane_y_data_rate[plane_id];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004284
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004285 y_plane_blocks = y_minimum[plane_id];
4286 y_plane_blocks += div_u64((uint64_t)alloc_size * y_data_rate,
4287 total_data_rate);
4288
Matt Roperc107acf2016-05-12 07:06:01 -07004289 if (y_data_rate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004290 ddb->y_plane[pipe][plane_id].start = start;
4291 ddb->y_plane[pipe][plane_id].end = start + y_plane_blocks;
Matt Roperc107acf2016-05-12 07:06:01 -07004292 }
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004293
4294 start += y_plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004295 }
4296
Matt Roperc107acf2016-05-12 07:06:01 -07004297 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004298}
4299
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004300/*
4301 * 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 +02004302 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004303 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
4304 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
4305*/
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07004306static uint_fixed_16_16_t
4307skl_wm_method1(const struct drm_i915_private *dev_priv, uint32_t pixel_rate,
4308 uint8_t cpp, uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004309{
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304310 uint32_t wm_intermediate_val;
4311 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004312
4313 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304314 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004315
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304316 wm_intermediate_val = latency * pixel_rate * cpp;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304317 ret = div_fixed16(wm_intermediate_val, 1000 * 512);
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07004318
4319 if (INTEL_GEN(dev_priv) >= 10)
4320 ret = add_fixed16_u32(ret, 1);
4321
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004322 return ret;
4323}
4324
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304325static uint_fixed_16_16_t skl_wm_method2(uint32_t pixel_rate,
4326 uint32_t pipe_htotal,
4327 uint32_t latency,
4328 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004329{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004330 uint32_t wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304331 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004332
4333 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304334 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004335
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004336 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304337 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
4338 pipe_htotal * 1000);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304339 ret = mul_u32_fixed16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004340 return ret;
4341}
4342
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304343static uint_fixed_16_16_t
4344intel_get_linetime_us(struct intel_crtc_state *cstate)
4345{
4346 uint32_t pixel_rate;
4347 uint32_t crtc_htotal;
4348 uint_fixed_16_16_t linetime_us;
4349
4350 if (!cstate->base.active)
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304351 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304352
4353 pixel_rate = cstate->pixel_rate;
4354
4355 if (WARN_ON(pixel_rate == 0))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304356 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304357
4358 crtc_htotal = cstate->base.adjusted_mode.crtc_htotal;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304359 linetime_us = div_fixed16(crtc_htotal * 1000, pixel_rate);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304360
4361 return linetime_us;
4362}
4363
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304364static uint32_t
4365skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *cstate,
4366 const struct intel_plane_state *pstate)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004367{
4368 uint64_t adjusted_pixel_rate;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304369 uint_fixed_16_16_t downscale_amount;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004370
4371 /* Shouldn't reach here on disabled planes... */
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004372 if (WARN_ON(!intel_wm_plane_visible(cstate, pstate)))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004373 return 0;
4374
4375 /*
4376 * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
4377 * with additional adjustments for plane-specific scaling.
4378 */
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02004379 adjusted_pixel_rate = cstate->pixel_rate;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004380 downscale_amount = skl_plane_downscale_amount(cstate, pstate);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004381
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304382 return mul_round_up_u32_fixed16(adjusted_pixel_rate,
4383 downscale_amount);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004384}
4385
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304386static int
4387skl_compute_plane_wm_params(const struct drm_i915_private *dev_priv,
4388 struct intel_crtc_state *cstate,
4389 const struct intel_plane_state *intel_pstate,
4390 struct skl_wm_params *wp)
4391{
4392 struct intel_plane *plane = to_intel_plane(intel_pstate->base.plane);
4393 const struct drm_plane_state *pstate = &intel_pstate->base;
4394 const struct drm_framebuffer *fb = pstate->fb;
4395 uint32_t interm_pbpl;
4396 struct intel_atomic_state *state =
4397 to_intel_atomic_state(cstate->base.state);
4398 bool apply_memory_bw_wa = skl_needs_memory_bw_wa(state);
4399
4400 if (!intel_wm_plane_visible(cstate, intel_pstate))
4401 return 0;
4402
4403 wp->y_tiled = fb->modifier == I915_FORMAT_MOD_Y_TILED ||
4404 fb->modifier == I915_FORMAT_MOD_Yf_TILED ||
4405 fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
4406 fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
4407 wp->x_tiled = fb->modifier == I915_FORMAT_MOD_X_TILED;
4408 wp->rc_surface = fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
4409 fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
4410
4411 if (plane->id == PLANE_CURSOR) {
4412 wp->width = intel_pstate->base.crtc_w;
4413 } else {
4414 /*
4415 * Src coordinates are already rotated by 270 degrees for
4416 * the 90/270 degree plane rotation cases (to match the
4417 * GTT mapping), hence no need to account for rotation here.
4418 */
4419 wp->width = drm_rect_width(&intel_pstate->base.src) >> 16;
4420 }
4421
4422 wp->cpp = (fb->format->format == DRM_FORMAT_NV12) ? fb->format->cpp[1] :
4423 fb->format->cpp[0];
4424 wp->plane_pixel_rate = skl_adjusted_plane_pixel_rate(cstate,
4425 intel_pstate);
4426
4427 if (drm_rotation_90_or_270(pstate->rotation)) {
4428
4429 switch (wp->cpp) {
4430 case 1:
4431 wp->y_min_scanlines = 16;
4432 break;
4433 case 2:
4434 wp->y_min_scanlines = 8;
4435 break;
4436 case 4:
4437 wp->y_min_scanlines = 4;
4438 break;
4439 default:
4440 MISSING_CASE(wp->cpp);
4441 return -EINVAL;
4442 }
4443 } else {
4444 wp->y_min_scanlines = 4;
4445 }
4446
4447 if (apply_memory_bw_wa)
4448 wp->y_min_scanlines *= 2;
4449
4450 wp->plane_bytes_per_line = wp->width * wp->cpp;
4451 if (wp->y_tiled) {
4452 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line *
4453 wp->y_min_scanlines, 512);
4454
4455 if (INTEL_GEN(dev_priv) >= 10)
4456 interm_pbpl++;
4457
4458 wp->plane_blocks_per_line = div_fixed16(interm_pbpl,
4459 wp->y_min_scanlines);
4460 } else if (wp->x_tiled && IS_GEN9(dev_priv)) {
4461 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line, 512);
4462 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
4463 } else {
4464 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line, 512) + 1;
4465 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
4466 }
4467
4468 wp->y_tile_minimum = mul_u32_fixed16(wp->y_min_scanlines,
4469 wp->plane_blocks_per_line);
4470 wp->linetime_us = fixed16_to_u32_round_up(
4471 intel_get_linetime_us(cstate));
4472
4473 return 0;
4474}
4475
Matt Roper55994c22016-05-12 07:06:08 -07004476static int skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
4477 struct intel_crtc_state *cstate,
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304478 const struct intel_plane_state *intel_pstate,
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004479 uint16_t ddb_allocation,
Matt Roper55994c22016-05-12 07:06:08 -07004480 int level,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304481 const struct skl_wm_params *wp,
Matt Roper55994c22016-05-12 07:06:08 -07004482 uint16_t *out_blocks, /* out */
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004483 uint8_t *out_lines, /* out */
4484 bool *enabled /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004485{
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304486 const struct drm_plane_state *pstate = &intel_pstate->base;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004487 uint32_t latency = dev_priv->wm.skl_latency[level];
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304488 uint_fixed_16_16_t method1, method2;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304489 uint_fixed_16_16_t selected_result;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004490 uint32_t res_blocks, res_lines;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03004491 struct intel_atomic_state *state =
4492 to_intel_atomic_state(cstate->base.state);
4493 bool apply_memory_bw_wa = skl_needs_memory_bw_wa(state);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004494
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004495 if (latency == 0 ||
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004496 !intel_wm_plane_visible(cstate, intel_pstate)) {
4497 *enabled = false;
Matt Roper55994c22016-05-12 07:06:08 -07004498 return 0;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004499 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004500
Rodrigo Vivi82525c12017-06-08 08:50:00 -07004501 /* Display WA #1141: kbl,cfl */
Kumar, Maheshd86ba622017-08-17 19:15:26 +05304502 if ((IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv) ||
4503 IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_B0)) &&
Rodrigo Vivi82525c12017-06-08 08:50:00 -07004504 dev_priv->ipc_enabled)
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05304505 latency += 4;
4506
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304507 if (apply_memory_bw_wa && wp->x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03004508 latency += 15;
4509
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304510 method1 = skl_wm_method1(dev_priv, wp->plane_pixel_rate,
4511 wp->cpp, latency);
4512 method2 = skl_wm_method2(wp->plane_pixel_rate,
Matt Roper024c9042015-09-24 15:53:11 -07004513 cstate->base.adjusted_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004514 latency,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304515 wp->plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004516
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304517 if (wp->y_tiled) {
4518 selected_result = max_fixed16(method2, wp->y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004519 } else {
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304520 if ((wp->cpp * cstate->base.adjusted_mode.crtc_htotal /
4521 512 < 1) && (wp->plane_bytes_per_line / 512 < 1))
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03004522 selected_result = method2;
Maarten Lankhorst54d20ed2017-07-17 14:02:30 +02004523 else if (ddb_allocation >=
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304524 fixed16_to_u32_round_up(wp->plane_blocks_per_line))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304525 selected_result = min_fixed16(method1, method2);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304526 else if (latency >= wp->linetime_us)
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304527 selected_result = min_fixed16(method1, method2);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004528 else
4529 selected_result = method1;
4530 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004531
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304532 res_blocks = fixed16_to_u32_round_up(selected_result) + 1;
Kumar, Maheshd273ecc2017-05-17 17:28:22 +05304533 res_lines = div_round_up_fixed16(selected_result,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304534 wp->plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00004535
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07004536 /* Display WA #1125: skl,bxt,kbl,glk */
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304537 if (level == 0 && wp->rc_surface)
4538 res_blocks += fixed16_to_u32_round_up(wp->y_tile_minimum);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07004539
4540 /* Display WA #1126: skl,bxt,kbl,glk */
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004541 if (level >= 1 && level <= 7) {
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304542 if (wp->y_tiled) {
4543 res_blocks += fixed16_to_u32_round_up(
4544 wp->y_tile_minimum);
4545 res_lines += wp->y_min_scanlines;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004546 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004547 res_blocks++;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004548 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004549 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004550
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004551 if (res_blocks >= ddb_allocation || res_lines > 31) {
4552 *enabled = false;
Matt Roper6b6bada2016-05-12 07:06:10 -07004553
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004554 /*
4555 * If there are no valid level 0 watermarks, then we can't
4556 * support this display configuration.
4557 */
4558 if (level) {
4559 return 0;
4560 } else {
4561 struct drm_plane *plane = pstate->plane;
4562
4563 DRM_DEBUG_KMS("Requested display configuration exceeds system watermark limitations\n");
4564 DRM_DEBUG_KMS("[PLANE:%d:%s] blocks required = %u/%u, lines required = %u/31\n",
4565 plane->base.id, plane->name,
4566 res_blocks, ddb_allocation, res_lines);
4567 return -EINVAL;
4568 }
Matt Roper55994c22016-05-12 07:06:08 -07004569 }
Damien Lespiaue6d66172014-11-04 17:06:55 +00004570
4571 *out_blocks = res_blocks;
4572 *out_lines = res_lines;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004573 *enabled = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004574
Matt Roper55994c22016-05-12 07:06:08 -07004575 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004576}
4577
Matt Roperf4a96752016-05-12 07:06:06 -07004578static int
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304579skl_compute_wm_levels(const struct drm_i915_private *dev_priv,
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004580 struct skl_ddb_allocation *ddb,
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304581 struct intel_crtc_state *cstate,
4582 const struct intel_plane_state *intel_pstate,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304583 const struct skl_wm_params *wm_params,
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304584 struct skl_plane_wm *wm)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004585{
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004586 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
4587 struct drm_plane *plane = intel_pstate->base.plane;
4588 struct intel_plane *intel_plane = to_intel_plane(plane);
4589 uint16_t ddb_blocks;
4590 enum pipe pipe = intel_crtc->pipe;
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304591 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Roper55994c22016-05-12 07:06:08 -07004592 int ret;
Lyudea62163e2016-10-04 14:28:20 -04004593
Kumar, Mahesh7b751192017-05-17 17:28:24 +05304594 if (WARN_ON(!intel_pstate->base.fb))
4595 return -EINVAL;
Matt Roper024c9042015-09-24 15:53:11 -07004596
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004597 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][intel_plane->id]);
4598
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304599 for (level = 0; level <= max_level; level++) {
4600 struct skl_wm_level *result = &wm->wm[level];
4601
4602 ret = skl_compute_plane_wm(dev_priv,
4603 cstate,
4604 intel_pstate,
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004605 ddb_blocks,
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304606 level,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304607 wm_params,
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304608 &result->plane_res_b,
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004609 &result->plane_res_l,
4610 &result->plane_en);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304611 if (ret)
4612 return ret;
4613 }
Matt Roperf4a96752016-05-12 07:06:06 -07004614
4615 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004616}
4617
Damien Lespiau407b50f2014-11-04 17:06:57 +00004618static uint32_t
Matt Roper024c9042015-09-24 15:53:11 -07004619skl_compute_linetime_wm(struct intel_crtc_state *cstate)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004620{
Mahesh Kumara3a89862016-12-01 21:19:34 +05304621 struct drm_atomic_state *state = cstate->base.state;
4622 struct drm_i915_private *dev_priv = to_i915(state->dev);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304623 uint_fixed_16_16_t linetime_us;
Mahesh Kumara3a89862016-12-01 21:19:34 +05304624 uint32_t linetime_wm;
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03004625
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304626 linetime_us = intel_get_linetime_us(cstate);
4627
4628 if (is_fixed16_zero(linetime_us))
Damien Lespiau407b50f2014-11-04 17:06:57 +00004629 return 0;
4630
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304631 linetime_wm = fixed16_to_u32_round_up(mul_u32_fixed16(8, linetime_us));
Mahesh Kumara3a89862016-12-01 21:19:34 +05304632
Kumar, Mahesh446e8502017-08-17 19:15:25 +05304633 /* Display WA #1135: bxt:ALL GLK:ALL */
4634 if ((IS_BROXTON(dev_priv) || IS_GEMINILAKE(dev_priv)) &&
4635 dev_priv->ipc_enabled)
4636 linetime_wm /= 2;
Mahesh Kumara3a89862016-12-01 21:19:34 +05304637
4638 return linetime_wm;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004639}
4640
Matt Roper024c9042015-09-24 15:53:11 -07004641static void skl_compute_transition_wm(struct intel_crtc_state *cstate,
Kumar, Maheshca476672017-08-17 19:15:24 +05304642 struct skl_wm_params *wp,
4643 struct skl_wm_level *wm_l0,
4644 uint16_t ddb_allocation,
Damien Lespiau9414f562014-11-04 17:06:58 +00004645 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004646{
Kumar, Maheshca476672017-08-17 19:15:24 +05304647 struct drm_device *dev = cstate->base.crtc->dev;
4648 const struct drm_i915_private *dev_priv = to_i915(dev);
4649 uint16_t trans_min, trans_y_tile_min;
4650 const uint16_t trans_amount = 10; /* This is configurable amount */
4651 uint16_t trans_offset_b, res_blocks;
Damien Lespiau9414f562014-11-04 17:06:58 +00004652
Kumar, Maheshca476672017-08-17 19:15:24 +05304653 if (!cstate->base.active)
4654 goto exit;
4655
4656 /* Transition WM are not recommended by HW team for GEN9 */
4657 if (INTEL_GEN(dev_priv) <= 9)
4658 goto exit;
4659
4660 /* Transition WM don't make any sense if ipc is disabled */
4661 if (!dev_priv->ipc_enabled)
4662 goto exit;
4663
4664 if (INTEL_GEN(dev_priv) >= 10)
4665 trans_min = 4;
4666
4667 trans_offset_b = trans_min + trans_amount;
4668
4669 if (wp->y_tiled) {
4670 trans_y_tile_min = (uint16_t) mul_round_up_u32_fixed16(2,
4671 wp->y_tile_minimum);
4672 res_blocks = max(wm_l0->plane_res_b, trans_y_tile_min) +
4673 trans_offset_b;
4674 } else {
4675 res_blocks = wm_l0->plane_res_b + trans_offset_b;
4676
4677 /* WA BUG:1938466 add one block for non y-tile planes */
4678 if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_A0))
4679 res_blocks += 1;
4680
4681 }
4682
4683 res_blocks += 1;
4684
4685 if (res_blocks < ddb_allocation) {
4686 trans_wm->plane_res_b = res_blocks;
4687 trans_wm->plane_en = true;
4688 return;
4689 }
4690
4691exit:
Lyudea62163e2016-10-04 14:28:20 -04004692 trans_wm->plane_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004693}
4694
Matt Roper55994c22016-05-12 07:06:08 -07004695static int skl_build_pipe_wm(struct intel_crtc_state *cstate,
4696 struct skl_ddb_allocation *ddb,
4697 struct skl_pipe_wm *pipe_wm)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004698{
Matt Roper024c9042015-09-24 15:53:11 -07004699 struct drm_device *dev = cstate->base.crtc->dev;
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304700 struct drm_crtc_state *crtc_state = &cstate->base;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004701 const struct drm_i915_private *dev_priv = to_i915(dev);
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304702 struct drm_plane *plane;
4703 const struct drm_plane_state *pstate;
Lyudea62163e2016-10-04 14:28:20 -04004704 struct skl_plane_wm *wm;
Matt Roper55994c22016-05-12 07:06:08 -07004705 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004706
Lyudea62163e2016-10-04 14:28:20 -04004707 /*
4708 * We'll only calculate watermarks for planes that are actually
4709 * enabled, so make sure all other planes are set as disabled.
4710 */
4711 memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
4712
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304713 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, crtc_state) {
4714 const struct intel_plane_state *intel_pstate =
4715 to_intel_plane_state(pstate);
4716 enum plane_id plane_id = to_intel_plane(plane)->id;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304717 struct skl_wm_params wm_params;
Kumar, Maheshca476672017-08-17 19:15:24 +05304718 enum pipe pipe = to_intel_crtc(cstate->base.crtc)->pipe;
4719 uint16_t ddb_blocks;
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304720
4721 wm = &pipe_wm->planes[plane_id];
Kumar, Maheshca476672017-08-17 19:15:24 +05304722 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][plane_id]);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304723 memset(&wm_params, 0, sizeof(struct skl_wm_params));
4724
4725 ret = skl_compute_plane_wm_params(dev_priv, cstate,
4726 intel_pstate, &wm_params);
4727 if (ret)
4728 return ret;
Lyudea62163e2016-10-04 14:28:20 -04004729
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004730 ret = skl_compute_wm_levels(dev_priv, ddb, cstate,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304731 intel_pstate, &wm_params, wm);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304732 if (ret)
4733 return ret;
Kumar, Maheshca476672017-08-17 19:15:24 +05304734 skl_compute_transition_wm(cstate, &wm_params, &wm->wm[0],
4735 ddb_blocks, &wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004736 }
Matt Roper024c9042015-09-24 15:53:11 -07004737 pipe_wm->linetime = skl_compute_linetime_wm(cstate);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004738
Matt Roper55994c22016-05-12 07:06:08 -07004739 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004740}
4741
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004742static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
4743 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00004744 const struct skl_ddb_entry *entry)
4745{
4746 if (entry->end)
4747 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
4748 else
4749 I915_WRITE(reg, 0);
4750}
4751
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004752static void skl_write_wm_level(struct drm_i915_private *dev_priv,
4753 i915_reg_t reg,
4754 const struct skl_wm_level *level)
4755{
4756 uint32_t val = 0;
4757
4758 if (level->plane_en) {
4759 val |= PLANE_WM_EN;
4760 val |= level->plane_res_b;
4761 val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
4762 }
4763
4764 I915_WRITE(reg, val);
4765}
4766
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004767static void skl_write_plane_wm(struct intel_crtc *intel_crtc,
4768 const struct skl_plane_wm *wm,
4769 const struct skl_ddb_allocation *ddb,
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004770 enum plane_id plane_id)
Lyude62e0fb82016-08-22 12:50:08 -04004771{
4772 struct drm_crtc *crtc = &intel_crtc->base;
4773 struct drm_device *dev = crtc->dev;
4774 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004775 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004776 enum pipe pipe = intel_crtc->pipe;
4777
4778 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004779 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004780 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004781 }
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004782 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004783 &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004784
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004785 skl_ddb_entry_write(dev_priv, PLANE_BUF_CFG(pipe, plane_id),
4786 &ddb->plane[pipe][plane_id]);
4787 skl_ddb_entry_write(dev_priv, PLANE_NV12_BUF_CFG(pipe, plane_id),
4788 &ddb->y_plane[pipe][plane_id]);
Lyude62e0fb82016-08-22 12:50:08 -04004789}
4790
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004791static void skl_write_cursor_wm(struct intel_crtc *intel_crtc,
4792 const struct skl_plane_wm *wm,
4793 const struct skl_ddb_allocation *ddb)
Lyude62e0fb82016-08-22 12:50:08 -04004794{
4795 struct drm_crtc *crtc = &intel_crtc->base;
4796 struct drm_device *dev = crtc->dev;
4797 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004798 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004799 enum pipe pipe = intel_crtc->pipe;
4800
4801 for (level = 0; level <= max_level; level++) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004802 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
4803 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004804 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004805 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004806
4807 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004808 &ddb->plane[pipe][PLANE_CURSOR]);
Lyude62e0fb82016-08-22 12:50:08 -04004809}
4810
cpaul@redhat.com45ece232016-10-14 17:31:56 -04004811bool skl_wm_level_equals(const struct skl_wm_level *l1,
4812 const struct skl_wm_level *l2)
4813{
4814 if (l1->plane_en != l2->plane_en)
4815 return false;
4816
4817 /* If both planes aren't enabled, the rest shouldn't matter */
4818 if (!l1->plane_en)
4819 return true;
4820
4821 return (l1->plane_res_l == l2->plane_res_l &&
4822 l1->plane_res_b == l2->plane_res_b);
4823}
4824
Lyude27082492016-08-24 07:48:10 +02004825static inline bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
4826 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004827{
Lyude27082492016-08-24 07:48:10 +02004828 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004829}
4830
Mika Kahola2b685042017-10-10 13:17:03 +03004831bool skl_ddb_allocation_overlaps(struct drm_i915_private *dev_priv,
4832 const struct skl_ddb_entry **entries,
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01004833 const struct skl_ddb_entry *ddb,
4834 int ignore)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004835{
Mika Kahola2b685042017-10-10 13:17:03 +03004836 enum pipe pipe;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004837
Mika Kahola2b685042017-10-10 13:17:03 +03004838 for_each_pipe(dev_priv, pipe) {
4839 if (pipe != ignore && entries[pipe] &&
4840 skl_ddb_entries_overlap(ddb, entries[pipe]))
Lyude27082492016-08-24 07:48:10 +02004841 return true;
Mika Kahola2b685042017-10-10 13:17:03 +03004842 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004843
Lyude27082492016-08-24 07:48:10 +02004844 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004845}
4846
Matt Roper55994c22016-05-12 07:06:08 -07004847static int skl_update_pipe_wm(struct drm_crtc_state *cstate,
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004848 const struct skl_pipe_wm *old_pipe_wm,
Matt Roper55994c22016-05-12 07:06:08 -07004849 struct skl_pipe_wm *pipe_wm, /* out */
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004850 struct skl_ddb_allocation *ddb, /* out */
Matt Roper55994c22016-05-12 07:06:08 -07004851 bool *changed /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004852{
Matt Roperf4a96752016-05-12 07:06:06 -07004853 struct intel_crtc_state *intel_cstate = to_intel_crtc_state(cstate);
Matt Roper55994c22016-05-12 07:06:08 -07004854 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004855
Matt Roper55994c22016-05-12 07:06:08 -07004856 ret = skl_build_pipe_wm(intel_cstate, ddb, pipe_wm);
4857 if (ret)
4858 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004859
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004860 if (!memcmp(old_pipe_wm, pipe_wm, sizeof(*pipe_wm)))
Matt Roper55994c22016-05-12 07:06:08 -07004861 *changed = false;
4862 else
4863 *changed = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004864
Matt Roper55994c22016-05-12 07:06:08 -07004865 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004866}
4867
Matt Roper9b613022016-06-27 16:42:44 -07004868static uint32_t
4869pipes_modified(struct drm_atomic_state *state)
4870{
4871 struct drm_crtc *crtc;
4872 struct drm_crtc_state *cstate;
4873 uint32_t i, ret = 0;
4874
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004875 for_each_new_crtc_in_state(state, crtc, cstate, i)
Matt Roper9b613022016-06-27 16:42:44 -07004876 ret |= drm_crtc_mask(crtc);
4877
4878 return ret;
4879}
4880
Jani Nikulabb7791b2016-10-04 12:29:17 +03004881static int
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004882skl_ddb_add_affected_planes(struct intel_crtc_state *cstate)
4883{
4884 struct drm_atomic_state *state = cstate->base.state;
4885 struct drm_device *dev = state->dev;
4886 struct drm_crtc *crtc = cstate->base.crtc;
4887 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4888 struct drm_i915_private *dev_priv = to_i915(dev);
4889 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4890 struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
4891 struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
4892 struct drm_plane_state *plane_state;
4893 struct drm_plane *plane;
4894 enum pipe pipe = intel_crtc->pipe;
4895
4896 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
4897
4898 drm_for_each_plane_mask(plane, dev, cstate->base.plane_mask) {
4899 enum plane_id plane_id = to_intel_plane(plane)->id;
4900
4901 if (skl_ddb_entry_equal(&cur_ddb->plane[pipe][plane_id],
4902 &new_ddb->plane[pipe][plane_id]) &&
4903 skl_ddb_entry_equal(&cur_ddb->y_plane[pipe][plane_id],
4904 &new_ddb->y_plane[pipe][plane_id]))
4905 continue;
4906
4907 plane_state = drm_atomic_get_plane_state(state, plane);
4908 if (IS_ERR(plane_state))
4909 return PTR_ERR(plane_state);
4910 }
4911
4912 return 0;
4913}
4914
4915static int
4916skl_compute_ddb(struct drm_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07004917{
4918 struct drm_device *dev = state->dev;
4919 struct drm_i915_private *dev_priv = to_i915(dev);
4920 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4921 struct intel_crtc *intel_crtc;
Matt Roper734fa012016-05-12 15:11:40 -07004922 struct skl_ddb_allocation *ddb = &intel_state->wm_results.ddb;
Matt Roper9b613022016-06-27 16:42:44 -07004923 uint32_t realloc_pipes = pipes_modified(state);
Matt Roper98d39492016-05-12 07:06:03 -07004924 int ret;
4925
4926 /*
4927 * If this is our first atomic update following hardware readout,
4928 * we can't trust the DDB that the BIOS programmed for us. Let's
4929 * pretend that all pipes switched active status so that we'll
4930 * ensure a full DDB recompute.
4931 */
Matt Roper1b54a882016-06-17 13:42:18 -07004932 if (dev_priv->wm.distrust_bios_wm) {
4933 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
4934 state->acquire_ctx);
4935 if (ret)
4936 return ret;
4937
Matt Roper98d39492016-05-12 07:06:03 -07004938 intel_state->active_pipe_changes = ~0;
4939
Matt Roper1b54a882016-06-17 13:42:18 -07004940 /*
4941 * We usually only initialize intel_state->active_crtcs if we
4942 * we're doing a modeset; make sure this field is always
4943 * initialized during the sanitization process that happens
4944 * on the first commit too.
4945 */
4946 if (!intel_state->modeset)
4947 intel_state->active_crtcs = dev_priv->active_crtcs;
4948 }
4949
Matt Roper98d39492016-05-12 07:06:03 -07004950 /*
4951 * If the modeset changes which CRTC's are active, we need to
4952 * recompute the DDB allocation for *all* active pipes, even
4953 * those that weren't otherwise being modified in any way by this
4954 * atomic commit. Due to the shrinking of the per-pipe allocations
4955 * when new active CRTC's are added, it's possible for a pipe that
4956 * we were already using and aren't changing at all here to suddenly
4957 * become invalid if its DDB needs exceeds its new allocation.
4958 *
4959 * Note that if we wind up doing a full DDB recompute, we can't let
4960 * any other display updates race with this transaction, so we need
4961 * to grab the lock on *all* CRTC's.
4962 */
Matt Roper734fa012016-05-12 15:11:40 -07004963 if (intel_state->active_pipe_changes) {
Matt Roper98d39492016-05-12 07:06:03 -07004964 realloc_pipes = ~0;
Matt Roper734fa012016-05-12 15:11:40 -07004965 intel_state->wm_results.dirty_pipes = ~0;
4966 }
Matt Roper98d39492016-05-12 07:06:03 -07004967
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004968 /*
4969 * We're not recomputing for the pipes not included in the commit, so
4970 * make sure we start with the current state.
4971 */
4972 memcpy(ddb, &dev_priv->wm.skl_hw.ddb, sizeof(*ddb));
4973
Matt Roper98d39492016-05-12 07:06:03 -07004974 for_each_intel_crtc_mask(dev, intel_crtc, realloc_pipes) {
4975 struct intel_crtc_state *cstate;
4976
4977 cstate = intel_atomic_get_crtc_state(state, intel_crtc);
4978 if (IS_ERR(cstate))
4979 return PTR_ERR(cstate);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004980
4981 ret = skl_allocate_pipe_ddb(cstate, ddb);
4982 if (ret)
4983 return ret;
4984
4985 ret = skl_ddb_add_affected_planes(cstate);
4986 if (ret)
4987 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07004988 }
4989
4990 return 0;
4991}
4992
Matt Roper2722efb2016-08-17 15:55:55 -04004993static void
4994skl_copy_wm_for_pipe(struct skl_wm_values *dst,
4995 struct skl_wm_values *src,
4996 enum pipe pipe)
4997{
Matt Roper2722efb2016-08-17 15:55:55 -04004998 memcpy(dst->ddb.y_plane[pipe], src->ddb.y_plane[pipe],
4999 sizeof(dst->ddb.y_plane[pipe]));
5000 memcpy(dst->ddb.plane[pipe], src->ddb.plane[pipe],
5001 sizeof(dst->ddb.plane[pipe]));
5002}
5003
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005004static void
5005skl_print_wm_changes(const struct drm_atomic_state *state)
5006{
5007 const struct drm_device *dev = state->dev;
5008 const struct drm_i915_private *dev_priv = to_i915(dev);
5009 const struct intel_atomic_state *intel_state =
5010 to_intel_atomic_state(state);
5011 const struct drm_crtc *crtc;
5012 const struct drm_crtc_state *cstate;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005013 const struct intel_plane *intel_plane;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005014 const struct skl_ddb_allocation *old_ddb = &dev_priv->wm.skl_hw.ddb;
5015 const struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
Maarten Lankhorst75704982016-11-01 12:04:10 +01005016 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005017
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01005018 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst75704982016-11-01 12:04:10 +01005019 const struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5020 enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005021
Maarten Lankhorst75704982016-11-01 12:04:10 +01005022 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005023 enum plane_id plane_id = intel_plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005024 const struct skl_ddb_entry *old, *new;
5025
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005026 old = &old_ddb->plane[pipe][plane_id];
5027 new = &new_ddb->plane[pipe][plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005028
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005029 if (skl_ddb_entry_equal(old, new))
5030 continue;
5031
Maarten Lankhorst75704982016-11-01 12:04:10 +01005032 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] ddb (%d - %d) -> (%d - %d)\n",
5033 intel_plane->base.base.id,
5034 intel_plane->base.name,
5035 old->start, old->end,
5036 new->start, new->end);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005037 }
5038 }
5039}
5040
Matt Roper98d39492016-05-12 07:06:03 -07005041static int
5042skl_compute_wm(struct drm_atomic_state *state)
5043{
5044 struct drm_crtc *crtc;
5045 struct drm_crtc_state *cstate;
Matt Roper734fa012016-05-12 15:11:40 -07005046 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
5047 struct skl_wm_values *results = &intel_state->wm_results;
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02005048 struct drm_device *dev = state->dev;
Matt Roper734fa012016-05-12 15:11:40 -07005049 struct skl_pipe_wm *pipe_wm;
Matt Roper98d39492016-05-12 07:06:03 -07005050 bool changed = false;
Matt Roper734fa012016-05-12 15:11:40 -07005051 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07005052
5053 /*
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02005054 * When we distrust bios wm we always need to recompute to set the
5055 * expected DDB allocations for each CRTC.
5056 */
5057 if (to_i915(dev)->wm.distrust_bios_wm)
5058 changed = true;
5059
5060 /*
Matt Roper98d39492016-05-12 07:06:03 -07005061 * If this transaction isn't actually touching any CRTC's, don't
5062 * bother with watermark calculation. Note that if we pass this
5063 * test, we're guaranteed to hold at least one CRTC state mutex,
5064 * which means we can safely use values like dev_priv->active_crtcs
5065 * since any racing commits that want to update them would need to
5066 * hold _all_ CRTC state mutexes.
5067 */
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01005068 for_each_new_crtc_in_state(state, crtc, cstate, i)
Matt Roper98d39492016-05-12 07:06:03 -07005069 changed = true;
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02005070
Matt Roper98d39492016-05-12 07:06:03 -07005071 if (!changed)
5072 return 0;
5073
Matt Roper734fa012016-05-12 15:11:40 -07005074 /* Clear all dirty flags */
5075 results->dirty_pipes = 0;
5076
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005077 ret = skl_compute_ddb(state);
Matt Roper98d39492016-05-12 07:06:03 -07005078 if (ret)
5079 return ret;
5080
Matt Roper734fa012016-05-12 15:11:40 -07005081 /*
5082 * Calculate WM's for all pipes that are part of this transaction.
5083 * Note that the DDB allocation above may have added more CRTC's that
5084 * weren't otherwise being modified (and set bits in dirty_pipes) if
5085 * pipe allocations had to change.
5086 *
5087 * FIXME: Now that we're doing this in the atomic check phase, we
5088 * should allow skl_update_pipe_wm() to return failure in cases where
5089 * no suitable watermark values can be found.
5090 */
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01005091 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Matt Roper734fa012016-05-12 15:11:40 -07005092 struct intel_crtc_state *intel_cstate =
5093 to_intel_crtc_state(cstate);
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02005094 const struct skl_pipe_wm *old_pipe_wm =
5095 &to_intel_crtc_state(crtc->state)->wm.skl.optimal;
Matt Roper734fa012016-05-12 15:11:40 -07005096
5097 pipe_wm = &intel_cstate->wm.skl.optimal;
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02005098 ret = skl_update_pipe_wm(cstate, old_pipe_wm, pipe_wm,
5099 &results->ddb, &changed);
Matt Roper734fa012016-05-12 15:11:40 -07005100 if (ret)
5101 return ret;
5102
5103 if (changed)
5104 results->dirty_pipes |= drm_crtc_mask(crtc);
5105
5106 if ((results->dirty_pipes & drm_crtc_mask(crtc)) == 0)
5107 /* This pipe's WM's did not change */
5108 continue;
5109
5110 intel_cstate->update_wm_pre = true;
Matt Roper734fa012016-05-12 15:11:40 -07005111 }
5112
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005113 skl_print_wm_changes(state);
5114
Matt Roper98d39492016-05-12 07:06:03 -07005115 return 0;
5116}
5117
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005118static void skl_atomic_update_crtc_wm(struct intel_atomic_state *state,
5119 struct intel_crtc_state *cstate)
5120{
5121 struct intel_crtc *crtc = to_intel_crtc(cstate->base.crtc);
5122 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5123 struct skl_pipe_wm *pipe_wm = &cstate->wm.skl.optimal;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005124 const struct skl_ddb_allocation *ddb = &state->wm_results.ddb;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005125 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005126 enum plane_id plane_id;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005127
5128 if (!(state->wm_results.dirty_pipes & drm_crtc_mask(&crtc->base)))
5129 return;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005130
5131 I915_WRITE(PIPE_WM_LINETIME(pipe), pipe_wm->linetime);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005132
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005133 for_each_plane_id_on_crtc(crtc, plane_id) {
5134 if (plane_id != PLANE_CURSOR)
5135 skl_write_plane_wm(crtc, &pipe_wm->planes[plane_id],
5136 ddb, plane_id);
5137 else
5138 skl_write_cursor_wm(crtc, &pipe_wm->planes[plane_id],
5139 ddb);
5140 }
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005141}
5142
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005143static void skl_initial_wm(struct intel_atomic_state *state,
5144 struct intel_crtc_state *cstate)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005145{
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005146 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02005147 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005148 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005149 struct skl_wm_values *results = &state->wm_results;
Matt Roper2722efb2016-08-17 15:55:55 -04005150 struct skl_wm_values *hw_vals = &dev_priv->wm.skl_hw;
Lyude27082492016-08-24 07:48:10 +02005151 enum pipe pipe = intel_crtc->pipe;
Bob Paauweadda50b2015-07-21 10:42:53 -07005152
Ville Syrjälä432081b2016-10-31 22:37:03 +02005153 if ((results->dirty_pipes & drm_crtc_mask(&intel_crtc->base)) == 0)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005154 return;
5155
Matt Roper734fa012016-05-12 15:11:40 -07005156 mutex_lock(&dev_priv->wm.wm_mutex);
5157
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005158 if (cstate->base.active_changed)
5159 skl_atomic_update_crtc_wm(state, cstate);
Lyude27082492016-08-24 07:48:10 +02005160
5161 skl_copy_wm_for_pipe(hw_vals, results, pipe);
Matt Roper734fa012016-05-12 15:11:40 -07005162
5163 mutex_unlock(&dev_priv->wm.wm_mutex);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005164}
5165
Ville Syrjäläd8905652016-01-14 14:53:35 +02005166static void ilk_compute_wm_config(struct drm_device *dev,
5167 struct intel_wm_config *config)
5168{
5169 struct intel_crtc *crtc;
5170
5171 /* Compute the currently _active_ config */
5172 for_each_intel_crtc(dev, crtc) {
5173 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
5174
5175 if (!wm->pipe_enabled)
5176 continue;
5177
5178 config->sprites_enabled |= wm->sprites_enabled;
5179 config->sprites_scaled |= wm->sprites_scaled;
5180 config->num_pipes_active++;
5181 }
5182}
5183
Matt Ropered4a6a72016-02-23 17:20:13 -08005184static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03005185{
Chris Wilson91c8a322016-07-05 10:40:23 +01005186 struct drm_device *dev = &dev_priv->drm;
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005187 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02005188 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02005189 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02005190 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005191 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07005192
Ville Syrjäläd8905652016-01-14 14:53:35 +02005193 ilk_compute_wm_config(dev, &config);
5194
5195 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
5196 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03005197
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005198 /* 5/6 split only in single pipe config on IVB+ */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00005199 if (INTEL_GEN(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02005200 config.num_pipes_active == 1 && config.sprites_enabled) {
5201 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
5202 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005203
Imre Deak820c1982013-12-17 14:46:36 +02005204 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03005205 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005206 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005207 }
5208
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005209 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005210 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005211
Imre Deak820c1982013-12-17 14:46:36 +02005212 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03005213
Imre Deak820c1982013-12-17 14:46:36 +02005214 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03005215}
5216
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005217static void ilk_initial_watermarks(struct intel_atomic_state *state,
5218 struct intel_crtc_state *cstate)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005219{
Matt Ropered4a6a72016-02-23 17:20:13 -08005220 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
5221 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005222
Matt Ropered4a6a72016-02-23 17:20:13 -08005223 mutex_lock(&dev_priv->wm.wm_mutex);
Matt Ropere8f1f022016-05-12 07:05:55 -07005224 intel_crtc->wm.active.ilk = cstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08005225 ilk_program_watermarks(dev_priv);
5226 mutex_unlock(&dev_priv->wm.wm_mutex);
5227}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005228
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005229static void ilk_optimize_watermarks(struct intel_atomic_state *state,
5230 struct intel_crtc_state *cstate)
Matt Ropered4a6a72016-02-23 17:20:13 -08005231{
5232 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
5233 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
5234
5235 mutex_lock(&dev_priv->wm.wm_mutex);
5236 if (cstate->wm.need_postvbl_update) {
Matt Ropere8f1f022016-05-12 07:05:55 -07005237 intel_crtc->wm.active.ilk = cstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08005238 ilk_program_watermarks(dev_priv);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005239 }
Matt Ropered4a6a72016-02-23 17:20:13 -08005240 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005241}
5242
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005243static inline void skl_wm_level_from_reg_val(uint32_t val,
5244 struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00005245{
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005246 level->plane_en = val & PLANE_WM_EN;
5247 level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
5248 level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
5249 PLANE_WM_LINES_MASK;
Pradeep Bhat30789992014-11-04 17:06:45 +00005250}
5251
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005252void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc,
5253 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00005254{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005255 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Pradeep Bhat30789992014-11-04 17:06:45 +00005256 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Pradeep Bhat30789992014-11-04 17:06:45 +00005257 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005258 int level, max_level;
5259 enum plane_id plane_id;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005260 uint32_t val;
Pradeep Bhat30789992014-11-04 17:06:45 +00005261
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005262 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00005263
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005264 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
5265 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00005266
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005267 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005268 if (plane_id != PLANE_CURSOR)
5269 val = I915_READ(PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005270 else
5271 val = I915_READ(CUR_WM(pipe, level));
5272
5273 skl_wm_level_from_reg_val(val, &wm->wm[level]);
5274 }
5275
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005276 if (plane_id != PLANE_CURSOR)
5277 val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005278 else
5279 val = I915_READ(CUR_WM_TRANS(pipe));
5280
5281 skl_wm_level_from_reg_val(val, &wm->trans_wm);
5282 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005283
Matt Roper3ef00282015-03-09 10:19:24 -07005284 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00005285 return;
5286
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005287 out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00005288}
5289
5290void skl_wm_get_hw_state(struct drm_device *dev)
5291{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005292 struct drm_i915_private *dev_priv = to_i915(dev);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005293 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
Damien Lespiaua269c582014-11-04 17:06:49 +00005294 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00005295 struct drm_crtc *crtc;
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005296 struct intel_crtc *intel_crtc;
5297 struct intel_crtc_state *cstate;
Pradeep Bhat30789992014-11-04 17:06:45 +00005298
Damien Lespiaua269c582014-11-04 17:06:49 +00005299 skl_ddb_get_hw_state(dev_priv, ddb);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005300 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5301 intel_crtc = to_intel_crtc(crtc);
5302 cstate = to_intel_crtc_state(crtc->state);
5303
5304 skl_pipe_wm_get_hw_state(crtc, &cstate->wm.skl.optimal);
5305
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02005306 if (intel_crtc->active)
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005307 hw->dirty_pipes |= drm_crtc_mask(crtc);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005308 }
Matt Ropera1de91e2016-05-12 07:05:57 -07005309
Matt Roper279e99d2016-05-12 07:06:02 -07005310 if (dev_priv->active_crtcs) {
5311 /* Fully recompute DDB on first atomic commit */
5312 dev_priv->wm.distrust_bios_wm = true;
5313 } else {
5314 /* Easy/common case; just sanitize DDB now if everything off */
5315 memset(ddb, 0, sizeof(*ddb));
5316 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005317}
5318
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005319static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
5320{
5321 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005322 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02005323 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005324 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper4e0963c2015-09-24 15:53:15 -07005325 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Matt Ropere8f1f022016-05-12 07:05:55 -07005326 struct intel_pipe_wm *active = &cstate->wm.ilk.optimal;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005327 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005328 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005329 [PIPE_A] = WM0_PIPEA_ILK,
5330 [PIPE_B] = WM0_PIPEB_ILK,
5331 [PIPE_C] = WM0_PIPEC_IVB,
5332 };
5333
5334 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005335 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02005336 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005337
Ville Syrjälä15606532016-05-13 17:55:17 +03005338 memset(active, 0, sizeof(*active));
5339
Matt Roper3ef00282015-03-09 10:19:24 -07005340 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02005341
5342 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005343 u32 tmp = hw->wm_pipe[pipe];
5344
5345 /*
5346 * For active pipes LP0 watermark is marked as
5347 * enabled, and LP1+ watermaks as disabled since
5348 * we can't really reverse compute them in case
5349 * multiple pipes are active.
5350 */
5351 active->wm[0].enable = true;
5352 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
5353 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
5354 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
5355 active->linetime = hw->wm_linetime[pipe];
5356 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005357 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005358
5359 /*
5360 * For inactive pipes, all watermark levels
5361 * should be marked as enabled but zeroed,
5362 * which is what we'd compute them to.
5363 */
5364 for (level = 0; level <= max_level; level++)
5365 active->wm[level].enable = true;
5366 }
Matt Roper4e0963c2015-09-24 15:53:15 -07005367
5368 intel_crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005369}
5370
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005371#define _FW_WM(value, plane) \
5372 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
5373#define _FW_WM_VLV(value, plane) \
5374 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
5375
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005376static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
5377 struct g4x_wm_values *wm)
5378{
5379 uint32_t tmp;
5380
5381 tmp = I915_READ(DSPFW1);
5382 wm->sr.plane = _FW_WM(tmp, SR);
5383 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5384 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
5385 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
5386
5387 tmp = I915_READ(DSPFW2);
5388 wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
5389 wm->sr.fbc = _FW_WM(tmp, FBC_SR);
5390 wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
5391 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
5392 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5393 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
5394
5395 tmp = I915_READ(DSPFW3);
5396 wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
5397 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5398 wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
5399 wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
5400}
5401
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005402static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
5403 struct vlv_wm_values *wm)
5404{
5405 enum pipe pipe;
5406 uint32_t tmp;
5407
5408 for_each_pipe(dev_priv, pipe) {
5409 tmp = I915_READ(VLV_DDL(pipe));
5410
Ville Syrjälä1b313892016-11-28 19:37:08 +02005411 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005412 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005413 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005414 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005415 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005416 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005417 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005418 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
5419 }
5420
5421 tmp = I915_READ(DSPFW1);
5422 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005423 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5424 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
5425 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005426
5427 tmp = I915_READ(DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005428 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
5429 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5430 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005431
5432 tmp = I915_READ(DSPFW3);
5433 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5434
5435 if (IS_CHERRYVIEW(dev_priv)) {
5436 tmp = I915_READ(DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005437 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5438 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005439
5440 tmp = I915_READ(DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005441 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
5442 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005443
5444 tmp = I915_READ(DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005445 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
5446 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005447
5448 tmp = I915_READ(DSPHOWM);
5449 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005450 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
5451 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
5452 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
5453 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5454 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5455 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5456 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5457 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5458 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005459 } else {
5460 tmp = I915_READ(DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005461 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5462 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005463
5464 tmp = I915_READ(DSPHOWM);
5465 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005466 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5467 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5468 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5469 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5470 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5471 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005472 }
5473}
5474
5475#undef _FW_WM
5476#undef _FW_WM_VLV
5477
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005478void g4x_wm_get_hw_state(struct drm_device *dev)
5479{
5480 struct drm_i915_private *dev_priv = to_i915(dev);
5481 struct g4x_wm_values *wm = &dev_priv->wm.g4x;
5482 struct intel_crtc *crtc;
5483
5484 g4x_read_wm_values(dev_priv, wm);
5485
5486 wm->cxsr = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
5487
5488 for_each_intel_crtc(dev, crtc) {
5489 struct intel_crtc_state *crtc_state =
5490 to_intel_crtc_state(crtc->base.state);
5491 struct g4x_wm_state *active = &crtc->wm.active.g4x;
5492 struct g4x_pipe_wm *raw;
5493 enum pipe pipe = crtc->pipe;
5494 enum plane_id plane_id;
5495 int level, max_level;
5496
5497 active->cxsr = wm->cxsr;
5498 active->hpll_en = wm->hpll_en;
5499 active->fbc_en = wm->fbc_en;
5500
5501 active->sr = wm->sr;
5502 active->hpll = wm->hpll;
5503
5504 for_each_plane_id_on_crtc(crtc, plane_id) {
5505 active->wm.plane[plane_id] =
5506 wm->pipe[pipe].plane[plane_id];
5507 }
5508
5509 if (wm->cxsr && wm->hpll_en)
5510 max_level = G4X_WM_LEVEL_HPLL;
5511 else if (wm->cxsr)
5512 max_level = G4X_WM_LEVEL_SR;
5513 else
5514 max_level = G4X_WM_LEVEL_NORMAL;
5515
5516 level = G4X_WM_LEVEL_NORMAL;
5517 raw = &crtc_state->wm.g4x.raw[level];
5518 for_each_plane_id_on_crtc(crtc, plane_id)
5519 raw->plane[plane_id] = active->wm.plane[plane_id];
5520
5521 if (++level > max_level)
5522 goto out;
5523
5524 raw = &crtc_state->wm.g4x.raw[level];
5525 raw->plane[PLANE_PRIMARY] = active->sr.plane;
5526 raw->plane[PLANE_CURSOR] = active->sr.cursor;
5527 raw->plane[PLANE_SPRITE0] = 0;
5528 raw->fbc = active->sr.fbc;
5529
5530 if (++level > max_level)
5531 goto out;
5532
5533 raw = &crtc_state->wm.g4x.raw[level];
5534 raw->plane[PLANE_PRIMARY] = active->hpll.plane;
5535 raw->plane[PLANE_CURSOR] = active->hpll.cursor;
5536 raw->plane[PLANE_SPRITE0] = 0;
5537 raw->fbc = active->hpll.fbc;
5538
5539 out:
5540 for_each_plane_id_on_crtc(crtc, plane_id)
5541 g4x_raw_plane_wm_set(crtc_state, level,
5542 plane_id, USHRT_MAX);
5543 g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
5544
5545 crtc_state->wm.g4x.optimal = *active;
5546 crtc_state->wm.g4x.intermediate = *active;
5547
5548 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
5549 pipe_name(pipe),
5550 wm->pipe[pipe].plane[PLANE_PRIMARY],
5551 wm->pipe[pipe].plane[PLANE_CURSOR],
5552 wm->pipe[pipe].plane[PLANE_SPRITE0]);
5553 }
5554
5555 DRM_DEBUG_KMS("Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
5556 wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
5557 DRM_DEBUG_KMS("Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
5558 wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
5559 DRM_DEBUG_KMS("Initial SR=%s HPLL=%s FBC=%s\n",
5560 yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
5561}
5562
5563void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
5564{
5565 struct intel_plane *plane;
5566 struct intel_crtc *crtc;
5567
5568 mutex_lock(&dev_priv->wm.wm_mutex);
5569
5570 for_each_intel_plane(&dev_priv->drm, plane) {
5571 struct intel_crtc *crtc =
5572 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
5573 struct intel_crtc_state *crtc_state =
5574 to_intel_crtc_state(crtc->base.state);
5575 struct intel_plane_state *plane_state =
5576 to_intel_plane_state(plane->base.state);
5577 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
5578 enum plane_id plane_id = plane->id;
5579 int level;
5580
5581 if (plane_state->base.visible)
5582 continue;
5583
5584 for (level = 0; level < 3; level++) {
5585 struct g4x_pipe_wm *raw =
5586 &crtc_state->wm.g4x.raw[level];
5587
5588 raw->plane[plane_id] = 0;
5589 wm_state->wm.plane[plane_id] = 0;
5590 }
5591
5592 if (plane_id == PLANE_PRIMARY) {
5593 for (level = 0; level < 3; level++) {
5594 struct g4x_pipe_wm *raw =
5595 &crtc_state->wm.g4x.raw[level];
5596 raw->fbc = 0;
5597 }
5598
5599 wm_state->sr.fbc = 0;
5600 wm_state->hpll.fbc = 0;
5601 wm_state->fbc_en = false;
5602 }
5603 }
5604
5605 for_each_intel_crtc(&dev_priv->drm, crtc) {
5606 struct intel_crtc_state *crtc_state =
5607 to_intel_crtc_state(crtc->base.state);
5608
5609 crtc_state->wm.g4x.intermediate =
5610 crtc_state->wm.g4x.optimal;
5611 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
5612 }
5613
5614 g4x_program_watermarks(dev_priv);
5615
5616 mutex_unlock(&dev_priv->wm.wm_mutex);
5617}
5618
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005619void vlv_wm_get_hw_state(struct drm_device *dev)
5620{
5621 struct drm_i915_private *dev_priv = to_i915(dev);
5622 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02005623 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005624 u32 val;
5625
5626 vlv_read_wm_values(dev_priv, wm);
5627
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005628 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
5629 wm->level = VLV_WM_LEVEL_PM2;
5630
5631 if (IS_CHERRYVIEW(dev_priv)) {
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01005632 mutex_lock(&dev_priv->pcu_lock);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005633
5634 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5635 if (val & DSP_MAXFIFO_PM5_ENABLE)
5636 wm->level = VLV_WM_LEVEL_PM5;
5637
Ville Syrjälä58590c12015-09-08 21:05:12 +03005638 /*
5639 * If DDR DVFS is disabled in the BIOS, Punit
5640 * will never ack the request. So if that happens
5641 * assume we don't have to enable/disable DDR DVFS
5642 * dynamically. To test that just set the REQ_ACK
5643 * bit to poke the Punit, but don't change the
5644 * HIGH/LOW bits so that we don't actually change
5645 * the current state.
5646 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005647 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03005648 val |= FORCE_DDR_FREQ_REQ_ACK;
5649 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
5650
5651 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
5652 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
5653 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
5654 "assuming DDR DVFS is disabled\n");
5655 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
5656 } else {
5657 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
5658 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
5659 wm->level = VLV_WM_LEVEL_DDR_DVFS;
5660 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005661
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01005662 mutex_unlock(&dev_priv->pcu_lock);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005663 }
5664
Ville Syrjäläff32c542017-03-02 19:14:57 +02005665 for_each_intel_crtc(dev, crtc) {
5666 struct intel_crtc_state *crtc_state =
5667 to_intel_crtc_state(crtc->base.state);
5668 struct vlv_wm_state *active = &crtc->wm.active.vlv;
5669 const struct vlv_fifo_state *fifo_state =
5670 &crtc_state->wm.vlv.fifo_state;
5671 enum pipe pipe = crtc->pipe;
5672 enum plane_id plane_id;
5673 int level;
5674
5675 vlv_get_fifo_size(crtc_state);
5676
5677 active->num_levels = wm->level + 1;
5678 active->cxsr = wm->cxsr;
5679
Ville Syrjäläff32c542017-03-02 19:14:57 +02005680 for (level = 0; level < active->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03005681 struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02005682 &crtc_state->wm.vlv.raw[level];
5683
5684 active->sr[level].plane = wm->sr.plane;
5685 active->sr[level].cursor = wm->sr.cursor;
5686
5687 for_each_plane_id_on_crtc(crtc, plane_id) {
5688 active->wm[level].plane[plane_id] =
5689 wm->pipe[pipe].plane[plane_id];
5690
5691 raw->plane[plane_id] =
5692 vlv_invert_wm_value(active->wm[level].plane[plane_id],
5693 fifo_state->plane[plane_id]);
5694 }
5695 }
5696
5697 for_each_plane_id_on_crtc(crtc, plane_id)
5698 vlv_raw_plane_wm_set(crtc_state, level,
5699 plane_id, USHRT_MAX);
5700 vlv_invalidate_wms(crtc, active, level);
5701
5702 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02005703 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02005704
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005705 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 +02005706 pipe_name(pipe),
5707 wm->pipe[pipe].plane[PLANE_PRIMARY],
5708 wm->pipe[pipe].plane[PLANE_CURSOR],
5709 wm->pipe[pipe].plane[PLANE_SPRITE0],
5710 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02005711 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005712
5713 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
5714 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
5715}
5716
Ville Syrjälä602ae832017-03-02 19:15:02 +02005717void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
5718{
5719 struct intel_plane *plane;
5720 struct intel_crtc *crtc;
5721
5722 mutex_lock(&dev_priv->wm.wm_mutex);
5723
5724 for_each_intel_plane(&dev_priv->drm, plane) {
5725 struct intel_crtc *crtc =
5726 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
5727 struct intel_crtc_state *crtc_state =
5728 to_intel_crtc_state(crtc->base.state);
5729 struct intel_plane_state *plane_state =
5730 to_intel_plane_state(plane->base.state);
5731 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
5732 const struct vlv_fifo_state *fifo_state =
5733 &crtc_state->wm.vlv.fifo_state;
5734 enum plane_id plane_id = plane->id;
5735 int level;
5736
5737 if (plane_state->base.visible)
5738 continue;
5739
5740 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03005741 struct g4x_pipe_wm *raw =
Ville Syrjälä602ae832017-03-02 19:15:02 +02005742 &crtc_state->wm.vlv.raw[level];
5743
5744 raw->plane[plane_id] = 0;
5745
5746 wm_state->wm[level].plane[plane_id] =
5747 vlv_invert_wm_value(raw->plane[plane_id],
5748 fifo_state->plane[plane_id]);
5749 }
5750 }
5751
5752 for_each_intel_crtc(&dev_priv->drm, crtc) {
5753 struct intel_crtc_state *crtc_state =
5754 to_intel_crtc_state(crtc->base.state);
5755
5756 crtc_state->wm.vlv.intermediate =
5757 crtc_state->wm.vlv.optimal;
5758 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
5759 }
5760
5761 vlv_program_watermarks(dev_priv);
5762
5763 mutex_unlock(&dev_priv->wm.wm_mutex);
5764}
5765
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02005766/*
5767 * FIXME should probably kill this and improve
5768 * the real watermark readout/sanitation instead
5769 */
5770static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
5771{
5772 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
5773 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
5774 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
5775
5776 /*
5777 * Don't touch WM1S_LP_EN here.
5778 * Doing so could cause underruns.
5779 */
5780}
5781
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005782void ilk_wm_get_hw_state(struct drm_device *dev)
5783{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005784 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02005785 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005786 struct drm_crtc *crtc;
5787
Ville Syrjäläf72b84c2017-11-08 15:35:55 +02005788 ilk_init_lp_watermarks(dev_priv);
5789
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01005790 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005791 ilk_pipe_wm_get_hw_state(crtc);
5792
5793 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
5794 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
5795 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
5796
5797 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00005798 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjäläcfa76982014-03-07 18:32:08 +02005799 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
5800 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
5801 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005802
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005803 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02005804 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
5805 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01005806 else if (IS_IVYBRIDGE(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02005807 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
5808 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005809
5810 hw->enable_fbc_wm =
5811 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
5812}
5813
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005814/**
5815 * intel_update_watermarks - update FIFO watermark values based on current modes
5816 *
5817 * Calculate watermark values for the various WM regs based on current mode
5818 * and plane configuration.
5819 *
5820 * There are several cases to deal with here:
5821 * - normal (i.e. non-self-refresh)
5822 * - self-refresh (SR) mode
5823 * - lines are large relative to FIFO size (buffer can hold up to 2)
5824 * - lines are small relative to FIFO size (buffer can hold more than 2
5825 * lines), so need to account for TLB latency
5826 *
5827 * The normal calculation is:
5828 * watermark = dotclock * bytes per pixel * latency
5829 * where latency is platform & configuration dependent (we assume pessimal
5830 * values here).
5831 *
5832 * The SR calculation is:
5833 * watermark = (trunc(latency/line time)+1) * surface width *
5834 * bytes per pixel
5835 * where
5836 * line time = htotal / dotclock
5837 * surface width = hdisplay for normal plane and 64 for cursor
5838 * and latency is assumed to be high, as above.
5839 *
5840 * The final value programmed to the register should always be rounded up,
5841 * and include an extra 2 entries to account for clock crossings.
5842 *
5843 * We don't use the sprite, so we can ignore that. And on Crestline we have
5844 * to set the non-SR watermarks to 8.
5845 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02005846void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005847{
Ville Syrjälä432081b2016-10-31 22:37:03 +02005848 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005849
5850 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005851 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005852}
5853
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05305854void intel_enable_ipc(struct drm_i915_private *dev_priv)
5855{
5856 u32 val;
5857
Rodrigo Vivi4d6ef0d2017-10-02 23:36:50 -07005858 /* Display WA #0477 WaDisableIPC: skl */
5859 if (IS_SKYLAKE(dev_priv)) {
5860 dev_priv->ipc_enabled = false;
5861 return;
5862 }
5863
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05305864 val = I915_READ(DISP_ARB_CTL2);
5865
5866 if (dev_priv->ipc_enabled)
5867 val |= DISP_IPC_ENABLE;
5868 else
5869 val &= ~DISP_IPC_ENABLE;
5870
5871 I915_WRITE(DISP_ARB_CTL2, val);
5872}
5873
5874void intel_init_ipc(struct drm_i915_private *dev_priv)
5875{
5876 dev_priv->ipc_enabled = false;
5877 if (!HAS_IPC(dev_priv))
5878 return;
5879
5880 dev_priv->ipc_enabled = true;
5881 intel_enable_ipc(dev_priv);
5882}
5883
Jani Nikulae2828912016-01-18 09:19:47 +02005884/*
Daniel Vetter92703882012-08-09 16:46:01 +02005885 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02005886 */
5887DEFINE_SPINLOCK(mchdev_lock);
5888
5889/* Global for IPS driver to get at the current i915 device. Protected by
5890 * mchdev_lock. */
5891static struct drm_i915_private *i915_mch_dev;
5892
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005893bool ironlake_set_drps(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005894{
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005895 u16 rgvswctl;
5896
Chris Wilson67520412017-03-02 13:28:01 +00005897 lockdep_assert_held(&mchdev_lock);
Daniel Vetter92703882012-08-09 16:46:01 +02005898
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005899 rgvswctl = I915_READ16(MEMSWCTL);
5900 if (rgvswctl & MEMCTL_CMD_STS) {
5901 DRM_DEBUG("gpu busy, RCS change rejected\n");
5902 return false; /* still busy with another command */
5903 }
5904
5905 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
5906 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
5907 I915_WRITE16(MEMSWCTL, rgvswctl);
5908 POSTING_READ16(MEMSWCTL);
5909
5910 rgvswctl |= MEMCTL_CMD_STS;
5911 I915_WRITE16(MEMSWCTL, rgvswctl);
5912
5913 return true;
5914}
5915
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005916static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005917{
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00005918 u32 rgvmodectl;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005919 u8 fmax, fmin, fstart, vstart;
5920
Daniel Vetter92703882012-08-09 16:46:01 +02005921 spin_lock_irq(&mchdev_lock);
5922
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00005923 rgvmodectl = I915_READ(MEMMODECTL);
5924
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005925 /* Enable temp reporting */
5926 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
5927 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
5928
5929 /* 100ms RC evaluation intervals */
5930 I915_WRITE(RCUPEI, 100000);
5931 I915_WRITE(RCDNEI, 100000);
5932
5933 /* Set max/min thresholds to 90ms and 80ms respectively */
5934 I915_WRITE(RCBMAXAVG, 90000);
5935 I915_WRITE(RCBMINAVG, 80000);
5936
5937 I915_WRITE(MEMIHYST, 1);
5938
5939 /* Set up min, max, and cur for interrupt handling */
5940 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
5941 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
5942 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
5943 MEMMODE_FSTART_SHIFT;
5944
Ville Syrjälä616847e2015-09-18 20:03:19 +03005945 vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005946 PXVFREQ_PX_SHIFT;
5947
Daniel Vetter20e4d402012-08-08 23:35:39 +02005948 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
5949 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005950
Daniel Vetter20e4d402012-08-08 23:35:39 +02005951 dev_priv->ips.max_delay = fstart;
5952 dev_priv->ips.min_delay = fmin;
5953 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005954
5955 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
5956 fmax, fmin, fstart);
5957
5958 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
5959
5960 /*
5961 * Interrupts will be enabled in ironlake_irq_postinstall
5962 */
5963
5964 I915_WRITE(VIDSTART, vstart);
5965 POSTING_READ(VIDSTART);
5966
5967 rgvmodectl |= MEMMODE_SWMODE_EN;
5968 I915_WRITE(MEMMODECTL, rgvmodectl);
5969
Daniel Vetter92703882012-08-09 16:46:01 +02005970 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005971 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005972 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005973
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005974 ironlake_set_drps(dev_priv, fstart);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005975
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03005976 dev_priv->ips.last_count1 = I915_READ(DMIEC) +
5977 I915_READ(DDREC) + I915_READ(CSIEC);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005978 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03005979 dev_priv->ips.last_count2 = I915_READ(GFXEC);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005980 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02005981
5982 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005983}
5984
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005985static void ironlake_disable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005986{
Daniel Vetter92703882012-08-09 16:46:01 +02005987 u16 rgvswctl;
5988
5989 spin_lock_irq(&mchdev_lock);
5990
5991 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005992
5993 /* Ack interrupts, disable EFC interrupt */
5994 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
5995 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
5996 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
5997 I915_WRITE(DEIIR, DE_PCU_EVENT);
5998 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
5999
6000 /* Go back to the starting frequency */
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006001 ironlake_set_drps(dev_priv, dev_priv->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02006002 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006003 rgvswctl |= MEMCTL_CMD_STS;
6004 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02006005 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006006
Daniel Vetter92703882012-08-09 16:46:01 +02006007 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006008}
6009
Daniel Vetteracbe9472012-07-26 11:50:05 +02006010/* There's a funny hw issue where the hw returns all 0 when reading from
6011 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
6012 * ourselves, instead of doing a rmw cycle (which might result in us clearing
6013 * all limits and the gpu stuck at whatever frequency it is at atm).
6014 */
Akash Goel74ef1172015-03-06 11:07:19 +05306015static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006016{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006017 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006018 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006019
Daniel Vetter20b46e52012-07-26 11:16:14 +02006020 /* Only set the down limit when we've reached the lowest level to avoid
6021 * getting more interrupts, otherwise leave this clear. This prevents a
6022 * race in the hw when coming out of rc6: There's a tiny window where
6023 * the hw runs at the minimal clock before selecting the desired
6024 * frequency, if the down threshold expires in that window we will not
6025 * receive a down interrupt. */
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006026 if (INTEL_GEN(dev_priv) >= 9) {
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006027 limits = (rps->max_freq_softlimit) << 23;
6028 if (val <= rps->min_freq_softlimit)
6029 limits |= (rps->min_freq_softlimit) << 14;
Akash Goel74ef1172015-03-06 11:07:19 +05306030 } else {
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006031 limits = rps->max_freq_softlimit << 24;
6032 if (val <= rps->min_freq_softlimit)
6033 limits |= rps->min_freq_softlimit << 16;
Akash Goel74ef1172015-03-06 11:07:19 +05306034 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02006035
6036 return limits;
6037}
6038
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006039static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
6040{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006041 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006042 int new_power;
Akash Goel8a586432015-03-06 11:07:18 +05306043 u32 threshold_up = 0, threshold_down = 0; /* in % */
6044 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006045
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006046 new_power = rps->power;
6047 switch (rps->power) {
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006048 case LOW_POWER:
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006049 if (val > rps->efficient_freq + 1 &&
6050 val > rps->cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006051 new_power = BETWEEN;
6052 break;
6053
6054 case BETWEEN:
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006055 if (val <= rps->efficient_freq &&
6056 val < rps->cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006057 new_power = LOW_POWER;
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006058 else if (val >= rps->rp0_freq &&
6059 val > rps->cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006060 new_power = HIGH_POWER;
6061 break;
6062
6063 case HIGH_POWER:
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006064 if (val < (rps->rp1_freq + rps->rp0_freq) >> 1 &&
6065 val < rps->cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006066 new_power = BETWEEN;
6067 break;
6068 }
6069 /* Max/min bins are special */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006070 if (val <= rps->min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006071 new_power = LOW_POWER;
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006072 if (val >= rps->max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006073 new_power = HIGH_POWER;
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006074 if (new_power == rps->power)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006075 return;
6076
6077 /* Note the units here are not exactly 1us, but 1280ns. */
6078 switch (new_power) {
6079 case LOW_POWER:
6080 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05306081 ei_up = 16000;
6082 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006083
6084 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306085 ei_down = 32000;
6086 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006087 break;
6088
6089 case BETWEEN:
6090 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05306091 ei_up = 13000;
6092 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006093
6094 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306095 ei_down = 32000;
6096 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006097 break;
6098
6099 case HIGH_POWER:
6100 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05306101 ei_up = 10000;
6102 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006103
6104 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306105 ei_down = 32000;
6106 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006107 break;
6108 }
6109
Mika Kuoppala6067a272017-02-15 15:52:59 +02006110 /* When byt can survive without system hang with dynamic
6111 * sw freq adjustments, this restriction can be lifted.
6112 */
6113 if (IS_VALLEYVIEW(dev_priv))
6114 goto skip_hw_write;
6115
Akash Goel8a586432015-03-06 11:07:18 +05306116 I915_WRITE(GEN6_RP_UP_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01006117 GT_INTERVAL_FROM_US(dev_priv, ei_up));
Akash Goel8a586432015-03-06 11:07:18 +05306118 I915_WRITE(GEN6_RP_UP_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01006119 GT_INTERVAL_FROM_US(dev_priv,
6120 ei_up * threshold_up / 100));
Akash Goel8a586432015-03-06 11:07:18 +05306121
6122 I915_WRITE(GEN6_RP_DOWN_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01006123 GT_INTERVAL_FROM_US(dev_priv, ei_down));
Akash Goel8a586432015-03-06 11:07:18 +05306124 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01006125 GT_INTERVAL_FROM_US(dev_priv,
6126 ei_down * threshold_down / 100));
Akash Goel8a586432015-03-06 11:07:18 +05306127
Chris Wilsona72b5622016-07-02 15:35:59 +01006128 I915_WRITE(GEN6_RP_CONTROL,
6129 GEN6_RP_MEDIA_TURBO |
6130 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6131 GEN6_RP_MEDIA_IS_GFX |
6132 GEN6_RP_ENABLE |
6133 GEN6_RP_UP_BUSY_AVG |
6134 GEN6_RP_DOWN_IDLE_AVG);
Akash Goel8a586432015-03-06 11:07:18 +05306135
Mika Kuoppala6067a272017-02-15 15:52:59 +02006136skip_hw_write:
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006137 rps->power = new_power;
6138 rps->up_threshold = threshold_up;
6139 rps->down_threshold = threshold_down;
6140 rps->last_adj = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006141}
6142
Chris Wilson2876ce72014-03-28 08:03:34 +00006143static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
6144{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006145 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Chris Wilson2876ce72014-03-28 08:03:34 +00006146 u32 mask = 0;
6147
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006148 /* We use UP_EI_EXPIRED interupts for both up/down in manual mode */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006149 if (val > rps->min_freq_softlimit)
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006150 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006151 if (val < rps->max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00006152 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00006153
Chris Wilson7b3c29f2014-07-10 20:31:19 +01006154 mask &= dev_priv->pm_rps_events;
6155
Imre Deak59d02a12014-12-19 19:33:26 +02006156 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00006157}
6158
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006159/* gen6_set_rps is called to update the frequency request, but should also be
6160 * called when the range (min_delay and max_delay) is modified so that we can
6161 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006162static int gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02006163{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006164 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6165
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006166 /* min/max delay may still have been modified so be sure to
6167 * write the limits value.
6168 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006169 if (val != rps->cur_freq) {
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006170 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006171
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006172 if (INTEL_GEN(dev_priv) >= 9)
Akash Goel57041952015-03-06 11:07:17 +05306173 I915_WRITE(GEN6_RPNSWREQ,
6174 GEN9_FREQUENCY(val));
Chris Wilsondc979972016-05-10 14:10:04 +01006175 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006176 I915_WRITE(GEN6_RPNSWREQ,
6177 HSW_FREQUENCY(val));
6178 else
6179 I915_WRITE(GEN6_RPNSWREQ,
6180 GEN6_FREQUENCY(val) |
6181 GEN6_OFFSET(0) |
6182 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006183 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006184
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006185 /* Make sure we continue to get interrupts
6186 * until we hit the minimum or maximum frequencies.
6187 */
Akash Goel74ef1172015-03-06 11:07:19 +05306188 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00006189 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006190
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006191 rps->cur_freq = val;
Mika Kuoppala0f945922015-11-17 18:14:26 +02006192 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006193
6194 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006195}
6196
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006197static int valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006198{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006199 int err;
6200
Chris Wilsondc979972016-05-10 14:10:04 +01006201 if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006202 "Odd GPU freq value\n"))
6203 val &= ~1;
6204
Deepak Scd25dd52015-07-10 18:31:40 +05306205 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
6206
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006207 if (val != dev_priv->gt_pm.rps.cur_freq) {
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006208 err = vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
6209 if (err)
6210 return err;
6211
Chris Wilsondb4c5e02017-02-10 15:03:46 +00006212 gen6_set_rps_thresholds(dev_priv, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01006213 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006214
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006215 dev_priv->gt_pm.rps.cur_freq = val;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006216 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006217
6218 return 0;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006219}
6220
Deepak Sa7f6e232015-05-09 18:04:44 +05306221/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05306222 *
6223 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05306224 * 1. Forcewake Media well.
6225 * 2. Request idle freq.
6226 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05306227*/
6228static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
6229{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006230 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6231 u32 val = rps->idle_freq;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006232 int err;
Deepak S5549d252014-06-28 11:26:11 +05306233
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006234 if (rps->cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05306235 return;
6236
Chris Wilsonc9efef72017-01-02 15:28:45 +00006237 /* The punit delays the write of the frequency and voltage until it
6238 * determines the GPU is awake. During normal usage we don't want to
6239 * waste power changing the frequency if the GPU is sleeping (rc6).
6240 * However, the GPU and driver is now idle and we do not want to delay
6241 * switching to minimum voltage (reducing power whilst idle) as we do
6242 * not expect to be woken in the near future and so must flush the
6243 * change by waking the device.
6244 *
6245 * We choose to take the media powerwell (either would do to trick the
6246 * punit into committing the voltage change) as that takes a lot less
6247 * power than the render powerwell.
6248 */
Deepak Sa7f6e232015-05-09 18:04:44 +05306249 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006250 err = valleyview_set_rps(dev_priv, val);
Deepak Sa7f6e232015-05-09 18:04:44 +05306251 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006252
6253 if (err)
6254 DRM_ERROR("Failed to set RPS for idle\n");
Deepak S76c3552f2014-01-30 23:08:16 +05306255}
6256
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006257void gen6_rps_busy(struct drm_i915_private *dev_priv)
6258{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006259 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6260
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01006261 mutex_lock(&dev_priv->pcu_lock);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006262 if (rps->enabled) {
Chris Wilsonbd648182017-02-10 15:03:48 +00006263 u8 freq;
6264
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006265 if (dev_priv->pm_rps_events & GEN6_PM_RP_UP_EI_EXPIRED)
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006266 gen6_rps_reset_ei(dev_priv);
6267 I915_WRITE(GEN6_PMINTRMSK,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006268 gen6_rps_pm_mask(dev_priv, rps->cur_freq));
Michał Winiarski2b83c4c2016-06-20 11:58:27 +02006269
Chris Wilsonc33d2472016-07-04 08:08:36 +01006270 gen6_enable_rps_interrupts(dev_priv);
6271
Chris Wilsonbd648182017-02-10 15:03:48 +00006272 /* Use the user's desired frequency as a guide, but for better
6273 * performance, jump directly to RPe as our starting frequency.
6274 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006275 freq = max(rps->cur_freq,
6276 rps->efficient_freq);
Chris Wilsonbd648182017-02-10 15:03:48 +00006277
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006278 if (intel_set_rps(dev_priv,
Chris Wilsonbd648182017-02-10 15:03:48 +00006279 clamp(freq,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006280 rps->min_freq_softlimit,
6281 rps->max_freq_softlimit)))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006282 DRM_DEBUG_DRIVER("Failed to set idle frequency\n");
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006283 }
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01006284 mutex_unlock(&dev_priv->pcu_lock);
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006285}
6286
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006287void gen6_rps_idle(struct drm_i915_private *dev_priv)
6288{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006289 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6290
Chris Wilsonc33d2472016-07-04 08:08:36 +01006291 /* Flush our bottom-half so that it does not race with us
6292 * setting the idle frequency and so that it is bounded by
6293 * our rpm wakeref. And then disable the interrupts to stop any
6294 * futher RPS reclocking whilst we are asleep.
6295 */
6296 gen6_disable_rps_interrupts(dev_priv);
6297
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01006298 mutex_lock(&dev_priv->pcu_lock);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006299 if (rps->enabled) {
Chris Wilsondc979972016-05-10 14:10:04 +01006300 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Deepak S76c3552f2014-01-30 23:08:16 +05306301 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02006302 else
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006303 gen6_set_rps(dev_priv, rps->idle_freq);
6304 rps->last_adj = 0;
Ville Syrjälä12c100b2016-05-23 17:42:48 +03006305 I915_WRITE(GEN6_PMINTRMSK,
6306 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Chris Wilsonc0951f02013-10-10 21:58:50 +01006307 }
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01006308 mutex_unlock(&dev_priv->pcu_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006309}
6310
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006311void gen6_rps_boost(struct drm_i915_gem_request *rq,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006312 struct intel_rps_client *rps_client)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006313{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006314 struct intel_rps *rps = &rq->i915->gt_pm.rps;
Chris Wilson74d290f2017-08-17 13:37:06 +01006315 unsigned long flags;
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006316 bool boost;
6317
Chris Wilson8d3afd72015-05-21 21:01:47 +01006318 /* This is intentionally racy! We peek at the state here, then
6319 * validate inside the RPS worker.
6320 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006321 if (!rps->enabled)
Chris Wilson8d3afd72015-05-21 21:01:47 +01006322 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006323
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006324 boost = false;
Chris Wilson74d290f2017-08-17 13:37:06 +01006325 spin_lock_irqsave(&rq->lock, flags);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006326 if (!rq->waitboost && !i915_gem_request_completed(rq)) {
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006327 atomic_inc(&rps->num_waiters);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006328 rq->waitboost = true;
6329 boost = true;
Chris Wilsonc0951f02013-10-10 21:58:50 +01006330 }
Chris Wilson74d290f2017-08-17 13:37:06 +01006331 spin_unlock_irqrestore(&rq->lock, flags);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006332 if (!boost)
6333 return;
6334
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006335 if (READ_ONCE(rps->cur_freq) < rps->boost_freq)
6336 schedule_work(&rps->work);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006337
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006338 atomic_inc(rps_client ? &rps_client->boosts : &rps->boosts);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006339}
6340
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006341int intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006342{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006343 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006344 int err;
6345
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01006346 lockdep_assert_held(&dev_priv->pcu_lock);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006347 GEM_BUG_ON(val > rps->max_freq);
6348 GEM_BUG_ON(val < rps->min_freq);
Chris Wilsoncfd1c482017-02-20 09:47:07 +00006349
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006350 if (!rps->enabled) {
6351 rps->cur_freq = val;
Chris Wilson76e4e4b2017-02-20 09:47:08 +00006352 return 0;
6353 }
6354
Chris Wilsondc979972016-05-10 14:10:04 +01006355 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006356 err = valleyview_set_rps(dev_priv, val);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006357 else
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006358 err = gen6_set_rps(dev_priv, val);
6359
6360 return err;
Jesse Barnes0a073b82013-04-17 15:54:58 -07006361}
6362
Chris Wilsondc979972016-05-10 14:10:04 +01006363static void gen9_disable_rc6(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00006364{
Zhe Wang20e49362014-11-04 17:07:05 +00006365 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00006366 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00006367}
6368
Chris Wilsondc979972016-05-10 14:10:04 +01006369static void gen9_disable_rps(struct drm_i915_private *dev_priv)
Akash Goel2030d682016-04-23 00:05:45 +05306370{
Akash Goel2030d682016-04-23 00:05:45 +05306371 I915_WRITE(GEN6_RP_CONTROL, 0);
6372}
6373
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01006374static void gen6_disable_rc6(struct drm_i915_private *dev_priv)
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006375{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006376 I915_WRITE(GEN6_RC_CONTROL, 0);
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01006377}
6378
6379static void gen6_disable_rps(struct drm_i915_private *dev_priv)
6380{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006381 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Akash Goel2030d682016-04-23 00:05:45 +05306382 I915_WRITE(GEN6_RP_CONTROL, 0);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006383}
6384
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01006385static void cherryview_disable_rc6(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306386{
Deepak S38807742014-05-23 21:00:15 +05306387 I915_WRITE(GEN6_RC_CONTROL, 0);
6388}
6389
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01006390static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
6391{
6392 I915_WRITE(GEN6_RP_CONTROL, 0);
6393}
6394
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01006395static void valleyview_disable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006396{
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01006397 /* We're doing forcewake before Disabling RC6,
Deepak S98a2e5f2014-08-18 10:35:27 -07006398 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02006399 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07006400
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006401 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006402
Mika Kuoppala59bad942015-01-16 11:34:40 +02006403 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006404}
6405
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01006406static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
6407{
6408 I915_WRITE(GEN6_RP_CONTROL, 0);
6409}
6410
Chris Wilsondc979972016-05-10 14:10:04 +01006411static void intel_print_rc6_info(struct drm_i915_private *dev_priv, u32 mode)
Ben Widawskydc39fff2013-10-18 12:32:07 -07006412{
Chris Wilsondc979972016-05-10 14:10:04 +01006413 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Imre Deak91ca6892014-04-14 20:24:25 +03006414 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
6415 mode = GEN6_RC_CTL_RC6_ENABLE;
6416 else
6417 mode = 0;
6418 }
Chris Wilsondc979972016-05-10 14:10:04 +01006419 if (HAS_RC6p(dev_priv))
Imre Deakb99d49c2016-06-29 19:13:54 +03006420 DRM_DEBUG_DRIVER("Enabling RC6 states: "
6421 "RC6 %s RC6p %s RC6pp %s\n",
6422 onoff(mode & GEN6_RC_CTL_RC6_ENABLE),
6423 onoff(mode & GEN6_RC_CTL_RC6p_ENABLE),
6424 onoff(mode & GEN6_RC_CTL_RC6pp_ENABLE));
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07006425
6426 else
Imre Deakb99d49c2016-06-29 19:13:54 +03006427 DRM_DEBUG_DRIVER("Enabling RC6 states: RC6 %s\n",
6428 onoff(mode & GEN6_RC_CTL_RC6_ENABLE));
Ben Widawskydc39fff2013-10-18 12:32:07 -07006429}
6430
Chris Wilsondc979972016-05-10 14:10:04 +01006431static bool bxt_check_bios_rc6_setup(struct drm_i915_private *dev_priv)
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306432{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006433 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306434 bool enable_rc6 = true;
6435 unsigned long rc6_ctx_base;
Imre Deakfc619842016-06-29 19:13:55 +03006436 u32 rc_ctl;
6437 int rc_sw_target;
6438
6439 rc_ctl = I915_READ(GEN6_RC_CONTROL);
6440 rc_sw_target = (I915_READ(GEN6_RC_STATE) & RC_SW_TARGET_STATE_MASK) >>
6441 RC_SW_TARGET_STATE_SHIFT;
6442 DRM_DEBUG_DRIVER("BIOS enabled RC states: "
6443 "HW_CTRL %s HW_RC6 %s SW_TARGET_STATE %x\n",
6444 onoff(rc_ctl & GEN6_RC_CTL_HW_ENABLE),
6445 onoff(rc_ctl & GEN6_RC_CTL_RC6_ENABLE),
6446 rc_sw_target);
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306447
6448 if (!(I915_READ(RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006449 DRM_DEBUG_DRIVER("RC6 Base location not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306450 enable_rc6 = false;
6451 }
6452
6453 /*
6454 * The exact context size is not known for BXT, so assume a page size
6455 * for this check.
6456 */
6457 rc6_ctx_base = I915_READ(RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006458 if (!((rc6_ctx_base >= ggtt->stolen_reserved_base) &&
6459 (rc6_ctx_base + PAGE_SIZE <= ggtt->stolen_reserved_base +
6460 ggtt->stolen_reserved_size))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006461 DRM_DEBUG_DRIVER("RC6 Base address not as expected.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306462 enable_rc6 = false;
6463 }
6464
6465 if (!(((I915_READ(PWRCTX_MAXCNT_RCSUNIT) & IDLE_TIME_MASK) > 1) &&
6466 ((I915_READ(PWRCTX_MAXCNT_VCSUNIT0) & IDLE_TIME_MASK) > 1) &&
6467 ((I915_READ(PWRCTX_MAXCNT_BCSUNIT) & IDLE_TIME_MASK) > 1) &&
6468 ((I915_READ(PWRCTX_MAXCNT_VECSUNIT) & IDLE_TIME_MASK) > 1))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006469 DRM_DEBUG_DRIVER("Engine Idle wait time not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306470 enable_rc6 = false;
6471 }
6472
Imre Deakfc619842016-06-29 19:13:55 +03006473 if (!I915_READ(GEN8_PUSHBUS_CONTROL) ||
6474 !I915_READ(GEN8_PUSHBUS_ENABLE) ||
6475 !I915_READ(GEN8_PUSHBUS_SHIFT)) {
6476 DRM_DEBUG_DRIVER("Pushbus not setup properly.\n");
6477 enable_rc6 = false;
6478 }
6479
6480 if (!I915_READ(GEN6_GFXPAUSE)) {
6481 DRM_DEBUG_DRIVER("GFX pause not setup properly.\n");
6482 enable_rc6 = false;
6483 }
6484
6485 if (!I915_READ(GEN8_MISC_CTRL0)) {
6486 DRM_DEBUG_DRIVER("GPM control not setup properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306487 enable_rc6 = false;
6488 }
6489
6490 return enable_rc6;
6491}
6492
Chris Wilsondc979972016-05-10 14:10:04 +01006493int sanitize_rc6_option(struct drm_i915_private *dev_priv, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006494{
Daniel Vettere7d66d82015-06-15 23:23:54 +02006495 /* No RC6 before Ironlake and code is gone for ilk. */
Chris Wilsondc979972016-05-10 14:10:04 +01006496 if (INTEL_INFO(dev_priv)->gen < 6)
Imre Deake6069ca2014-04-18 16:01:02 +03006497 return 0;
6498
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306499 if (!enable_rc6)
6500 return 0;
6501
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02006502 if (IS_GEN9_LP(dev_priv) && !bxt_check_bios_rc6_setup(dev_priv)) {
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306503 DRM_INFO("RC6 disabled by BIOS\n");
6504 return 0;
6505 }
6506
Daniel Vetter456470e2012-08-08 23:35:40 +02006507 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03006508 if (enable_rc6 >= 0) {
6509 int mask;
6510
Chris Wilsondc979972016-05-10 14:10:04 +01006511 if (HAS_RC6p(dev_priv))
Imre Deake6069ca2014-04-18 16:01:02 +03006512 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
6513 INTEL_RC6pp_ENABLE;
6514 else
6515 mask = INTEL_RC6_ENABLE;
6516
6517 if ((enable_rc6 & mask) != enable_rc6)
Imre Deakb99d49c2016-06-29 19:13:54 +03006518 DRM_DEBUG_DRIVER("Adjusting RC6 mask to %d "
6519 "(requested %d, valid %d)\n",
6520 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03006521
6522 return enable_rc6 & mask;
6523 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006524
Chris Wilsondc979972016-05-10 14:10:04 +01006525 if (IS_IVYBRIDGE(dev_priv))
Ben Widawskycca84a12014-01-28 20:25:38 -08006526 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08006527
6528 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006529}
6530
Chris Wilsondc979972016-05-10 14:10:04 +01006531static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
Imre Deake6069ca2014-04-18 16:01:02 +03006532{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006533 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6534
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006535 /* All of these values are in units of 50MHz */
Chris Wilson773ea9a2016-07-13 09:10:33 +01006536
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006537 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02006538 if (IS_GEN9_LP(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006539 u32 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006540 rps->rp0_freq = (rp_state_cap >> 16) & 0xff;
6541 rps->rp1_freq = (rp_state_cap >> 8) & 0xff;
6542 rps->min_freq = (rp_state_cap >> 0) & 0xff;
Bob Paauwe35040562015-06-25 14:54:07 -07006543 } else {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006544 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006545 rps->rp0_freq = (rp_state_cap >> 0) & 0xff;
6546 rps->rp1_freq = (rp_state_cap >> 8) & 0xff;
6547 rps->min_freq = (rp_state_cap >> 16) & 0xff;
Bob Paauwe35040562015-06-25 14:54:07 -07006548 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006549 /* hw_max = RP0 until we check for overclocking */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006550 rps->max_freq = rps->rp0_freq;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006551
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006552 rps->efficient_freq = rps->rp1_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01006553 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006554 IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006555 u32 ddcc_status = 0;
6556
6557 if (sandybridge_pcode_read(dev_priv,
6558 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
6559 &ddcc_status) == 0)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006560 rps->efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08006561 clamp_t(u8,
6562 ((ddcc_status >> 8) & 0xff),
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006563 rps->min_freq,
6564 rps->max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006565 }
6566
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006567 if (IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Akash Goelc5e06882015-06-29 14:50:19 +05306568 /* Store the frequency values in 16.66 MHZ units, which is
Chris Wilson773ea9a2016-07-13 09:10:33 +01006569 * the natural hardware unit for SKL
6570 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006571 rps->rp0_freq *= GEN9_FREQ_SCALER;
6572 rps->rp1_freq *= GEN9_FREQ_SCALER;
6573 rps->min_freq *= GEN9_FREQ_SCALER;
6574 rps->max_freq *= GEN9_FREQ_SCALER;
6575 rps->efficient_freq *= GEN9_FREQ_SCALER;
Akash Goelc5e06882015-06-29 14:50:19 +05306576 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006577}
6578
Chris Wilson3a45b052016-07-13 09:10:32 +01006579static void reset_rps(struct drm_i915_private *dev_priv,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006580 int (*set)(struct drm_i915_private *, u8))
Chris Wilson3a45b052016-07-13 09:10:32 +01006581{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006582 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6583 u8 freq = rps->cur_freq;
Chris Wilson3a45b052016-07-13 09:10:32 +01006584
6585 /* force a reset */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006586 rps->power = -1;
6587 rps->cur_freq = -1;
Chris Wilson3a45b052016-07-13 09:10:32 +01006588
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006589 if (set(dev_priv, freq))
6590 DRM_ERROR("Failed to reset RPS to initial values\n");
Chris Wilson3a45b052016-07-13 09:10:32 +01006591}
6592
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006593/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Chris Wilsondc979972016-05-10 14:10:04 +01006594static void gen9_enable_rps(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00006595{
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006596 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
6597
Akash Goel0beb0592015-03-06 11:07:20 +05306598 /* Program defaults and thresholds for RPS*/
6599 I915_WRITE(GEN6_RC_VIDEO_FREQ,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006600 GEN9_FREQUENCY(dev_priv->gt_pm.rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006601
Akash Goel0beb0592015-03-06 11:07:20 +05306602 /* 1 second timeout*/
6603 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
6604 GT_INTERVAL_FROM_US(dev_priv, 1000000));
6605
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006606 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006607
Akash Goel0beb0592015-03-06 11:07:20 +05306608 /* Leaning on the below call to gen6_set_rps to program/setup the
6609 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
6610 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
Chris Wilson3a45b052016-07-13 09:10:32 +01006611 reset_rps(dev_priv, gen6_set_rps);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006612
6613 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
6614}
6615
Chris Wilsondc979972016-05-10 14:10:04 +01006616static void gen9_enable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006617{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006618 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306619 enum intel_engine_id id;
Rodrigo Vivie4ffc832017-08-22 16:58:28 -07006620 u32 rc6_mode, rc6_mask = 0;
Zhe Wang20e49362014-11-04 17:07:05 +00006621
6622 /* 1a: Software RC state - RC0 */
6623 I915_WRITE(GEN6_RC_STATE, 0);
6624
6625 /* 1b: Get forcewake during program sequence. Although the driver
6626 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006627 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00006628
6629 /* 2a: Disable RC states. */
6630 I915_WRITE(GEN6_RC_CONTROL, 0);
6631
6632 /* 2b: Program RC6 thresholds.*/
Rodrigo Vivi0aab2012017-10-23 15:46:12 -07006633 if (INTEL_GEN(dev_priv) >= 10) {
6634 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16 | 85);
6635 I915_WRITE(GEN10_MEDIA_WAKE_RATE_LIMIT, 150);
6636 } else if (IS_SKYLAKE(dev_priv)) {
6637 /*
6638 * WaRsDoubleRc6WrlWithCoarsePowerGating:skl Doubling WRL only
6639 * when CPG is enabled
6640 */
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05306641 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
Rodrigo Vivi0aab2012017-10-23 15:46:12 -07006642 } else {
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05306643 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
Rodrigo Vivi0aab2012017-10-23 15:46:12 -07006644 }
6645
Zhe Wang20e49362014-11-04 17:07:05 +00006646 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6647 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05306648 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006649 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05306650
Dave Gordon1a3d1892016-05-13 15:36:30 +01006651 if (HAS_GUC(dev_priv))
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05306652 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
6653
Zhe Wang20e49362014-11-04 17:07:05 +00006654 I915_WRITE(GEN6_RC_SLEEP, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00006655
Zhe Wang38c23522015-01-20 12:23:04 +00006656 /* 2c: Program Coarse Power Gating Policies. */
6657 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
6658 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
6659
Zhe Wang20e49362014-11-04 17:07:05 +00006660 /* 3a: Enable RC6 */
Sagar Arun Kamble771decb2017-10-10 22:30:07 +01006661 if (intel_rc6_enabled() & INTEL_RC6_ENABLE)
Zhe Wang20e49362014-11-04 17:07:05 +00006662 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Jani Nikula87ad3212016-01-14 12:53:34 +02006663 DRM_INFO("RC6 %s\n", onoff(rc6_mask & GEN6_RC_CTL_RC6_ENABLE));
Chris Wilson1c044f92017-01-25 17:26:01 +00006664 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
Rodrigo Vivie4ffc832017-08-22 16:58:28 -07006665
6666 /* WaRsUseTimeoutMode:cnl (pre-prod) */
6667 if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_C0))
6668 rc6_mode = GEN7_RC_CTL_TO_MODE;
6669 else
6670 rc6_mode = GEN6_RC_CTL_EI_MODE(1);
6671
Chris Wilson1c044f92017-01-25 17:26:01 +00006672 I915_WRITE(GEN6_RC_CONTROL,
Rodrigo Vivie4ffc832017-08-22 16:58:28 -07006673 GEN6_RC_CTL_HW_ENABLE | rc6_mode | rc6_mask);
Zhe Wang20e49362014-11-04 17:07:05 +00006674
Sagar Kamblecb07bae2015-04-12 11:28:14 +05306675 /*
6676 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05306677 * WaRsDisableCoarsePowerGating:skl,bxt - Render/Media PG need to be disabled with RC6.
Sagar Kamblecb07bae2015-04-12 11:28:14 +05306678 */
Chris Wilsondc979972016-05-10 14:10:04 +01006679 if (NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05306680 I915_WRITE(GEN9_PG_ENABLE, 0);
6681 else
6682 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
6683 (GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE) : 0);
Zhe Wang38c23522015-01-20 12:23:04 +00006684
Mika Kuoppala59bad942015-01-16 11:34:40 +02006685 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00006686}
6687
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01006688static void gen8_enable_rc6(struct drm_i915_private *dev_priv)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006689{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006690 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306691 enum intel_engine_id id;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006692 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006693
6694 /* 1a: Software RC state - RC0 */
6695 I915_WRITE(GEN6_RC_STATE, 0);
6696
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01006697 /* 1b: Get forcewake during program sequence. Although the driver
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006698 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006699 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006700
6701 /* 2a: Disable RC states. */
6702 I915_WRITE(GEN6_RC_CONTROL, 0);
6703
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006704 /* 2b: Program RC6 thresholds.*/
6705 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
6706 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6707 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05306708 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006709 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006710 I915_WRITE(GEN6_RC_SLEEP, 0);
Sagar Arun Kamble415544d2017-10-10 22:30:00 +01006711 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006712
6713 /* 3: Enable RC6 */
Sagar Arun Kamble771decb2017-10-10 22:30:07 +01006714 if (intel_rc6_enabled() & INTEL_RC6_ENABLE)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006715 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Chris Wilsondc979972016-05-10 14:10:04 +01006716 intel_print_rc6_info(dev_priv, rc6_mask);
Sagar Arun Kamble415544d2017-10-10 22:30:00 +01006717
6718 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
6719 GEN7_RC_CTL_TO_MODE |
6720 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006721
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01006722 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
6723}
6724
6725static void gen8_enable_rps(struct drm_i915_private *dev_priv)
6726{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006727 struct intel_rps *rps = &dev_priv->gt_pm.rps;
6728
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01006729 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
6730
6731 /* 1 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07006732 I915_WRITE(GEN6_RPNSWREQ,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006733 HSW_FREQUENCY(rps->rp1_freq));
Ben Widawskyf9bdc582014-03-31 17:16:41 -07006734 I915_WRITE(GEN6_RC_VIDEO_FREQ,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006735 HSW_FREQUENCY(rps->rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02006736 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
6737 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006738
Daniel Vetter7526ed72014-09-29 15:07:19 +02006739 /* Docs recommend 900MHz, and 300 MHz respectively */
6740 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006741 rps->max_freq_softlimit << 24 |
6742 rps->min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006743
Daniel Vetter7526ed72014-09-29 15:07:19 +02006744 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
6745 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
6746 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
6747 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006748
Daniel Vetter7526ed72014-09-29 15:07:19 +02006749 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006750
Sagar Arun Kamble3a853922017-10-10 22:30:01 +01006751 /* 2: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02006752 I915_WRITE(GEN6_RP_CONTROL,
6753 GEN6_RP_MEDIA_TURBO |
6754 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6755 GEN6_RP_MEDIA_IS_GFX |
6756 GEN6_RP_ENABLE |
6757 GEN6_RP_UP_BUSY_AVG |
6758 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006759
Chris Wilson3a45b052016-07-13 09:10:32 +01006760 reset_rps(dev_priv, gen6_set_rps);
Daniel Vetter7526ed72014-09-29 15:07:19 +02006761
Mika Kuoppala59bad942015-01-16 11:34:40 +02006762 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006763}
6764
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01006765static void gen6_enable_rc6(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006766{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006767 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306768 enum intel_engine_id id;
Chris Wilson99ac9612016-07-13 09:10:34 +01006769 u32 rc6vids, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006770 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006771 int rc6_mode;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00006772 int ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006773
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006774 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006775
6776 /* Clear the DBG now so we don't confuse earlier errors */
Ville Syrjälä297b32e2016-04-13 21:09:30 +03006777 gtfifodbg = I915_READ(GTFIFODBG);
6778 if (gtfifodbg) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006779 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
6780 I915_WRITE(GTFIFODBG, gtfifodbg);
6781 }
6782
Mika Kuoppala59bad942015-01-16 11:34:40 +02006783 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006784
6785 /* disable the counters and set deterministic thresholds */
6786 I915_WRITE(GEN6_RC_CONTROL, 0);
6787
6788 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
6789 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
6790 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
6791 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
6792 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
6793
Akash Goel3b3f1652016-10-13 22:44:48 +05306794 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006795 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006796
6797 I915_WRITE(GEN6_RC_SLEEP, 0);
6798 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Chris Wilsondc979972016-05-10 14:10:04 +01006799 if (IS_IVYBRIDGE(dev_priv))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07006800 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
6801 else
6802 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08006803 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006804 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
6805
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006806 /* Check if we are enabling RC6 */
Sagar Arun Kamble771decb2017-10-10 22:30:07 +01006807 rc6_mode = intel_rc6_enabled();
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006808 if (rc6_mode & INTEL_RC6_ENABLE)
6809 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
6810
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006811 /* We don't use those on Haswell */
Chris Wilsondc979972016-05-10 14:10:04 +01006812 if (!IS_HASWELL(dev_priv)) {
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006813 if (rc6_mode & INTEL_RC6p_ENABLE)
6814 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006815
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006816 if (rc6_mode & INTEL_RC6pp_ENABLE)
6817 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
6818 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006819
Chris Wilsondc979972016-05-10 14:10:04 +01006820 intel_print_rc6_info(dev_priv, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006821
6822 I915_WRITE(GEN6_RC_CONTROL,
6823 rc6_mask |
6824 GEN6_RC_CTL_EI_MODE(1) |
6825 GEN6_RC_CTL_HW_ENABLE);
6826
Ben Widawsky31643d52012-09-26 10:34:01 -07006827 rc6vids = 0;
6828 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
Chris Wilsondc979972016-05-10 14:10:04 +01006829 if (IS_GEN6(dev_priv) && ret) {
Ben Widawsky31643d52012-09-26 10:34:01 -07006830 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
Chris Wilsondc979972016-05-10 14:10:04 +01006831 } else if (IS_GEN6(dev_priv) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
Ben Widawsky31643d52012-09-26 10:34:01 -07006832 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
6833 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
6834 rc6vids &= 0xffff00;
6835 rc6vids |= GEN6_ENCODE_RC6_VID(450);
6836 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
6837 if (ret)
6838 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
6839 }
6840
Mika Kuoppala59bad942015-01-16 11:34:40 +02006841 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006842}
6843
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01006844static void gen6_enable_rps(struct drm_i915_private *dev_priv)
6845{
Sagar Arun Kamble960e5462017-10-10 22:29:59 +01006846 /* Here begins a magic sequence of register writes to enable
6847 * auto-downclocking.
6848 *
6849 * Perhaps there might be some value in exposing these to
6850 * userspace...
6851 */
6852 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
6853
6854 /* Power down if completely idle for over 50ms */
6855 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
6856 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
6857
6858 reset_rps(dev_priv, gen6_set_rps);
6859
6860 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
6861}
6862
Chris Wilsonfb7404e2016-07-13 09:10:38 +01006863static void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006864{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006865 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006866 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01006867 unsigned int gpu_freq;
6868 unsigned int max_ia_freq, min_ring_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05306869 unsigned int max_gpu_freq, min_gpu_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006870 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03006871 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006872
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01006873 WARN_ON(!mutex_is_locked(&dev_priv->pcu_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02006874
Ben Widawskyeda79642013-10-07 17:15:48 -03006875 policy = cpufreq_cpu_get(0);
6876 if (policy) {
6877 max_ia_freq = policy->cpuinfo.max_freq;
6878 cpufreq_cpu_put(policy);
6879 } else {
6880 /*
6881 * Default to measured freq if none found, PCU will ensure we
6882 * don't go over
6883 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006884 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03006885 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006886
6887 /* Convert from kHz to MHz */
6888 max_ia_freq /= 1000;
6889
Ben Widawsky153b4b952013-10-22 22:05:09 -07006890 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07006891 /* convert DDR frequency from units of 266.6MHz to bandwidth */
6892 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01006893
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006894 if (IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05306895 /* Convert GT frequency to 50 HZ units */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006896 min_gpu_freq = rps->min_freq / GEN9_FREQ_SCALER;
6897 max_gpu_freq = rps->max_freq / GEN9_FREQ_SCALER;
Akash Goel4c8c7742015-06-29 14:50:20 +05306898 } else {
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01006899 min_gpu_freq = rps->min_freq;
6900 max_gpu_freq = rps->max_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05306901 }
6902
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006903 /*
6904 * For each potential GPU frequency, load a ring frequency we'd like
6905 * to use for memory access. We do this by specifying the IA frequency
6906 * the PCU should use as a reference to determine the ring frequency.
6907 */
Akash Goel4c8c7742015-06-29 14:50:20 +05306908 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
6909 int diff = max_gpu_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01006910 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006911
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006912 if (IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05306913 /*
6914 * ring_freq = 2 * GT. ring_freq is in 100MHz units
6915 * No floor required for ring frequency on SKL.
6916 */
6917 ring_freq = gpu_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01006918 } else if (INTEL_INFO(dev_priv)->gen >= 8) {
Ben Widawsky46c764d2013-11-02 21:07:49 -07006919 /* max(2 * GT, DDR). NB: GT is 50MHz units */
6920 ring_freq = max(min_ring_freq, gpu_freq);
Chris Wilsondc979972016-05-10 14:10:04 +01006921 } else if (IS_HASWELL(dev_priv)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07006922 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01006923 ring_freq = max(min_ring_freq, ring_freq);
6924 /* leave ia_freq as the default, chosen by cpufreq */
6925 } else {
6926 /* On older processors, there is no separate ring
6927 * clock domain, so in order to boost the bandwidth
6928 * of the ring, we need to upclock the CPU (ia_freq).
6929 *
6930 * For GPU frequencies less than 750MHz,
6931 * just use the lowest ring freq.
6932 */
6933 if (gpu_freq < min_freq)
6934 ia_freq = 800;
6935 else
6936 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
6937 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
6938 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006939
Ben Widawsky42c05262012-09-26 10:34:00 -07006940 sandybridge_pcode_write(dev_priv,
6941 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01006942 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
6943 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
6944 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006945 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006946}
6947
Ville Syrjälä03af2042014-06-28 02:03:53 +03006948static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05306949{
6950 u32 val, rp0;
6951
Jani Nikula5b5929c2015-10-07 11:17:46 +03006952 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05306953
Imre Deak43b67992016-08-31 19:13:02 +03006954 switch (INTEL_INFO(dev_priv)->sseu.eu_total) {
Jani Nikula5b5929c2015-10-07 11:17:46 +03006955 case 8:
6956 /* (2 * 4) config */
6957 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
6958 break;
6959 case 12:
6960 /* (2 * 6) config */
6961 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
6962 break;
6963 case 16:
6964 /* (2 * 8) config */
6965 default:
6966 /* Setting (2 * 8) Min RP0 for any other combination */
6967 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
6968 break;
Deepak S095acd52015-01-17 11:05:59 +05306969 }
Jani Nikula5b5929c2015-10-07 11:17:46 +03006970
6971 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
6972
Deepak S2b6b3a02014-05-27 15:59:30 +05306973 return rp0;
6974}
6975
6976static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
6977{
6978 u32 val, rpe;
6979
6980 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
6981 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
6982
6983 return rpe;
6984}
6985
Deepak S7707df42014-07-12 18:46:14 +05306986static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
6987{
6988 u32 val, rp1;
6989
Jani Nikula5b5929c2015-10-07 11:17:46 +03006990 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
6991 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
6992
Deepak S7707df42014-07-12 18:46:14 +05306993 return rp1;
6994}
6995
Deepak S96676fe2016-08-12 18:46:41 +05306996static u32 cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
6997{
6998 u32 val, rpn;
6999
7000 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
7001 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
7002 FB_GFX_FREQ_FUSE_MASK);
7003
7004 return rpn;
7005}
7006
Deepak Sf8f2b002014-07-10 13:16:21 +05307007static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
7008{
7009 u32 val, rp1;
7010
7011 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
7012
7013 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
7014
7015 return rp1;
7016}
7017
Ville Syrjälä03af2042014-06-28 02:03:53 +03007018static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07007019{
7020 u32 val, rp0;
7021
Jani Nikula64936252013-05-22 15:36:20 +03007022 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007023
7024 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
7025 /* Clamp to max */
7026 rp0 = min_t(u32, rp0, 0xea);
7027
7028 return rp0;
7029}
7030
7031static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
7032{
7033 u32 val, rpe;
7034
Jani Nikula64936252013-05-22 15:36:20 +03007035 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007036 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03007037 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007038 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
7039
7040 return rpe;
7041}
7042
Ville Syrjälä03af2042014-06-28 02:03:53 +03007043static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07007044{
Imre Deak36146032014-12-04 18:39:35 +02007045 u32 val;
7046
7047 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
7048 /*
7049 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
7050 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
7051 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
7052 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
7053 * to make sure it matches what Punit accepts.
7054 */
7055 return max_t(u32, val, 0xc0);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007056}
7057
Imre Deakae484342014-03-31 15:10:44 +03007058/* Check that the pctx buffer wasn't move under us. */
7059static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
7060{
7061 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
7062
7063 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
7064 dev_priv->vlv_pctx->stolen->start);
7065}
7066
Deepak S38807742014-05-23 21:00:15 +05307067
7068/* Check that the pcbr address is not empty. */
7069static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
7070{
7071 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
7072
7073 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
7074}
7075
Chris Wilsondc979972016-05-10 14:10:04 +01007076static void cherryview_setup_pctx(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05307077{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02007078 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03007079 unsigned long pctx_paddr, paddr;
Deepak S38807742014-05-23 21:00:15 +05307080 u32 pcbr;
7081 int pctx_size = 32*1024;
7082
Deepak S38807742014-05-23 21:00:15 +05307083 pcbr = I915_READ(VLV_PCBR);
7084 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02007085 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05307086 paddr = (dev_priv->mm.stolen_base +
Joonas Lahtinen62106b42016-03-18 10:42:57 +02007087 (ggtt->stolen_size - pctx_size));
Deepak S38807742014-05-23 21:00:15 +05307088
7089 pctx_paddr = (paddr & (~4095));
7090 I915_WRITE(VLV_PCBR, pctx_paddr);
7091 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02007092
7093 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05307094}
7095
Chris Wilsondc979972016-05-10 14:10:04 +01007096static void valleyview_setup_pctx(struct drm_i915_private *dev_priv)
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007097{
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007098 struct drm_i915_gem_object *pctx;
7099 unsigned long pctx_paddr;
7100 u32 pcbr;
7101 int pctx_size = 24*1024;
7102
7103 pcbr = I915_READ(VLV_PCBR);
7104 if (pcbr) {
7105 /* BIOS set it up already, grab the pre-alloc'd space */
7106 int pcbr_offset;
7107
7108 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00007109 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007110 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02007111 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007112 pctx_size);
7113 goto out;
7114 }
7115
Ville Syrjäläce611ef2014-11-07 21:33:46 +02007116 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
7117
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007118 /*
7119 * From the Gunit register HAS:
7120 * The Gfx driver is expected to program this register and ensure
7121 * proper allocation within Gfx stolen memory. For example, this
7122 * register should be programmed such than the PCBR range does not
7123 * overlap with other ranges, such as the frame buffer, protected
7124 * memory, or any other relevant ranges.
7125 */
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00007126 pctx = i915_gem_object_create_stolen(dev_priv, pctx_size);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007127 if (!pctx) {
7128 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
Tvrtko Ursulinee504892016-02-11 10:27:30 +00007129 goto out;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007130 }
7131
7132 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
7133 I915_WRITE(VLV_PCBR, pctx_paddr);
7134
7135out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02007136 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007137 dev_priv->vlv_pctx = pctx;
7138}
7139
Chris Wilsondc979972016-05-10 14:10:04 +01007140static void valleyview_cleanup_pctx(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007141{
Imre Deakae484342014-03-31 15:10:44 +03007142 if (WARN_ON(!dev_priv->vlv_pctx))
7143 return;
7144
Chris Wilsonf0cd5182016-10-28 13:58:43 +01007145 i915_gem_object_put(dev_priv->vlv_pctx);
Imre Deakae484342014-03-31 15:10:44 +03007146 dev_priv->vlv_pctx = NULL;
7147}
7148
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007149static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
7150{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007151 dev_priv->gt_pm.rps.gpll_ref_freq =
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007152 vlv_get_cck_clock(dev_priv, "GPLL ref",
7153 CCK_GPLL_CLOCK_CONTROL,
7154 dev_priv->czclk_freq);
7155
7156 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007157 dev_priv->gt_pm.rps.gpll_ref_freq);
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007158}
7159
Chris Wilsondc979972016-05-10 14:10:04 +01007160static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03007161{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007162 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007163 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03007164
Chris Wilsondc979972016-05-10 14:10:04 +01007165 valleyview_setup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007166
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007167 vlv_init_gpll_ref_freq(dev_priv);
7168
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007169 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
7170 switch ((val >> 6) & 3) {
7171 case 0:
7172 case 1:
7173 dev_priv->mem_freq = 800;
7174 break;
7175 case 2:
7176 dev_priv->mem_freq = 1066;
7177 break;
7178 case 3:
7179 dev_priv->mem_freq = 1333;
7180 break;
7181 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02007182 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007183
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007184 rps->max_freq = valleyview_rps_max_freq(dev_priv);
7185 rps->rp0_freq = rps->max_freq;
Imre Deak4e805192014-04-14 20:24:41 +03007186 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007187 intel_gpu_freq(dev_priv, rps->max_freq),
7188 rps->max_freq);
Imre Deak4e805192014-04-14 20:24:41 +03007189
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007190 rps->efficient_freq = valleyview_rps_rpe_freq(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007191 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007192 intel_gpu_freq(dev_priv, rps->efficient_freq),
7193 rps->efficient_freq);
Imre Deak4e805192014-04-14 20:24:41 +03007194
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007195 rps->rp1_freq = valleyview_rps_guar_freq(dev_priv);
Deepak Sf8f2b002014-07-10 13:16:21 +05307196 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007197 intel_gpu_freq(dev_priv, rps->rp1_freq),
7198 rps->rp1_freq);
Deepak Sf8f2b002014-07-10 13:16:21 +05307199
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007200 rps->min_freq = valleyview_rps_min_freq(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007201 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007202 intel_gpu_freq(dev_priv, rps->min_freq),
7203 rps->min_freq);
Imre Deak4e805192014-04-14 20:24:41 +03007204}
7205
Chris Wilsondc979972016-05-10 14:10:04 +01007206static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05307207{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007208 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007209 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05307210
Chris Wilsondc979972016-05-10 14:10:04 +01007211 cherryview_setup_pctx(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307212
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007213 vlv_init_gpll_ref_freq(dev_priv);
7214
Ville Syrjäläa5805162015-05-26 20:42:30 +03007215 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02007216 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007217 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02007218
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007219 switch ((val >> 2) & 0x7) {
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007220 case 3:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007221 dev_priv->mem_freq = 2000;
7222 break;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03007223 default:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007224 dev_priv->mem_freq = 1600;
7225 break;
7226 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02007227 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007228
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007229 rps->max_freq = cherryview_rps_max_freq(dev_priv);
7230 rps->rp0_freq = rps->max_freq;
Deepak S2b6b3a02014-05-27 15:59:30 +05307231 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007232 intel_gpu_freq(dev_priv, rps->max_freq),
7233 rps->max_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05307234
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007235 rps->efficient_freq = cherryview_rps_rpe_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307236 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007237 intel_gpu_freq(dev_priv, rps->efficient_freq),
7238 rps->efficient_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05307239
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007240 rps->rp1_freq = cherryview_rps_guar_freq(dev_priv);
Deepak S7707df42014-07-12 18:46:14 +05307241 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007242 intel_gpu_freq(dev_priv, rps->rp1_freq),
7243 rps->rp1_freq);
Deepak S7707df42014-07-12 18:46:14 +05307244
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007245 rps->min_freq = cherryview_rps_min_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307246 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007247 intel_gpu_freq(dev_priv, rps->min_freq),
7248 rps->min_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05307249
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007250 WARN_ONCE((rps->max_freq | rps->efficient_freq | rps->rp1_freq |
7251 rps->min_freq) & 1,
Ville Syrjälä1c147622014-08-18 14:42:43 +03007252 "Odd GPU freq values\n");
Deepak S38807742014-05-23 21:00:15 +05307253}
7254
Chris Wilsondc979972016-05-10 14:10:04 +01007255static void valleyview_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03007256{
Chris Wilsondc979972016-05-10 14:10:04 +01007257 valleyview_cleanup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007258}
7259
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007260static void cherryview_enable_rc6(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05307261{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007262 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05307263 enum intel_engine_id id;
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007264 u32 gtfifodbg, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05307265
Ville Syrjälä297b32e2016-04-13 21:09:30 +03007266 gtfifodbg = I915_READ(GTFIFODBG) & ~(GT_FIFO_SBDEDICATE_FREE_ENTRY_CHV |
7267 GT_FIFO_FREE_ENTRIES_CHV);
Deepak S38807742014-05-23 21:00:15 +05307268 if (gtfifodbg) {
7269 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
7270 gtfifodbg);
7271 I915_WRITE(GTFIFODBG, gtfifodbg);
7272 }
7273
7274 cherryview_check_pctx(dev_priv);
7275
7276 /* 1a & 1b: Get forcewake during program sequence. Although the driver
7277 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02007278 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05307279
Ville Syrjälä160614a2015-01-19 13:50:47 +02007280 /* Disable RC states. */
7281 I915_WRITE(GEN6_RC_CONTROL, 0);
7282
Deepak S38807742014-05-23 21:00:15 +05307283 /* 2a: Program RC6 thresholds.*/
7284 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
7285 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
7286 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
7287
Akash Goel3b3f1652016-10-13 22:44:48 +05307288 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007289 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Deepak S38807742014-05-23 21:00:15 +05307290 I915_WRITE(GEN6_RC_SLEEP, 0);
7291
Deepak Sf4f71c72015-03-28 15:23:35 +05307292 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
7293 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
Deepak S38807742014-05-23 21:00:15 +05307294
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007295 /* Allows RC6 residency counter to work */
Deepak S38807742014-05-23 21:00:15 +05307296 I915_WRITE(VLV_COUNTER_CONTROL,
7297 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
7298 VLV_MEDIA_RC6_COUNT_EN |
7299 VLV_RENDER_RC6_COUNT_EN));
7300
7301 /* For now we assume BIOS is allocating and populating the PCBR */
7302 pcbr = I915_READ(VLV_PCBR);
7303
Deepak S38807742014-05-23 21:00:15 +05307304 /* 3: Enable RC6 */
Sagar Arun Kamble771decb2017-10-10 22:30:07 +01007305 if ((intel_rc6_enabled() & INTEL_RC6_ENABLE) &&
Chris Wilsondc979972016-05-10 14:10:04 +01007306 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02007307 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05307308
7309 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
7310
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007311 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
7312}
7313
7314static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
7315{
7316 u32 val;
7317
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007318 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
7319
7320 /* 1: Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02007321 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05307322 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
7323 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
7324 I915_WRITE(GEN6_RP_UP_EI, 66000);
7325 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
7326
7327 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7328
Sagar Arun Kambled46b00d2017-10-10 22:30:03 +01007329 /* 2: Enable RPS */
Deepak S2b6b3a02014-05-27 15:59:30 +05307330 I915_WRITE(GEN6_RP_CONTROL,
7331 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02007332 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05307333 GEN6_RP_ENABLE |
7334 GEN6_RP_UP_BUSY_AVG |
7335 GEN6_RP_DOWN_IDLE_AVG);
7336
Deepak S3ef62342015-04-29 08:36:24 +05307337 /* Setting Fixed Bias */
7338 val = VLV_OVERRIDE_EN |
7339 VLV_SOC_TDP_EN |
7340 CHV_BIAS_CPU_50_SOC_50;
7341 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
7342
Deepak S2b6b3a02014-05-27 15:59:30 +05307343 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
7344
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02007345 /* RPS code assumes GPLL is used */
7346 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
7347
Jani Nikula742f4912015-09-03 11:16:09 +03007348 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05307349 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7350
Chris Wilson3a45b052016-07-13 09:10:32 +01007351 reset_rps(dev_priv, valleyview_set_rps);
Deepak S2b6b3a02014-05-27 15:59:30 +05307352
Mika Kuoppala59bad942015-01-16 11:34:40 +02007353 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05307354}
7355
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01007356static void valleyview_enable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07007357{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007358 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05307359 enum intel_engine_id id;
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01007360 u32 gtfifodbg, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07007361
Imre Deakae484342014-03-31 15:10:44 +03007362 valleyview_check_pctx(dev_priv);
7363
Ville Syrjälä297b32e2016-04-13 21:09:30 +03007364 gtfifodbg = I915_READ(GTFIFODBG);
7365 if (gtfifodbg) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07007366 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
7367 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007368 I915_WRITE(GTFIFODBG, gtfifodbg);
7369 }
7370
Mika Kuoppala59bad942015-01-16 11:34:40 +02007371 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007372
Ville Syrjälä160614a2015-01-19 13:50:47 +02007373 /* Disable RC states. */
7374 I915_WRITE(GEN6_RC_CONTROL, 0);
7375
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01007376 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
7377 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
7378 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
7379
7380 for_each_engine(engine, dev_priv, id)
7381 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
7382
7383 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
7384
7385 /* Allows RC6 residency counter to work */
7386 I915_WRITE(VLV_COUNTER_CONTROL,
7387 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
7388 VLV_MEDIA_RC0_COUNT_EN |
7389 VLV_RENDER_RC0_COUNT_EN |
7390 VLV_MEDIA_RC6_COUNT_EN |
7391 VLV_RENDER_RC6_COUNT_EN));
7392
Sagar Arun Kamble771decb2017-10-10 22:30:07 +01007393 if (intel_rc6_enabled() & INTEL_RC6_ENABLE)
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01007394 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
7395
7396 intel_print_rc6_info(dev_priv, rc6_mode);
7397
7398 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
7399
7400 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
7401}
7402
7403static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
7404{
7405 u32 val;
7406
Sagar Arun Kamble0d6fc922017-10-10 22:30:02 +01007407 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
7408
Ville Syrjäläcad725f2015-01-19 13:50:48 +02007409 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007410 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
7411 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
7412 I915_WRITE(GEN6_RP_UP_EI, 66000);
7413 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
7414
7415 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7416
7417 I915_WRITE(GEN6_RP_CONTROL,
7418 GEN6_RP_MEDIA_TURBO |
7419 GEN6_RP_MEDIA_HW_NORMAL_MODE |
7420 GEN6_RP_MEDIA_IS_GFX |
7421 GEN6_RP_ENABLE |
7422 GEN6_RP_UP_BUSY_AVG |
7423 GEN6_RP_DOWN_IDLE_CONT);
7424
Deepak S3ef62342015-04-29 08:36:24 +05307425 /* Setting Fixed Bias */
7426 val = VLV_OVERRIDE_EN |
7427 VLV_SOC_TDP_EN |
7428 VLV_BIAS_CPU_125_SOC_875;
7429 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
7430
Jani Nikula64936252013-05-22 15:36:20 +03007431 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007432
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02007433 /* RPS code assumes GPLL is used */
7434 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
7435
Jani Nikula742f4912015-09-03 11:16:09 +03007436 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07007437 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7438
Chris Wilson3a45b052016-07-13 09:10:32 +01007439 reset_rps(dev_priv, valleyview_set_rps);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007440
Mika Kuoppala59bad942015-01-16 11:34:40 +02007441 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007442}
7443
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007444static unsigned long intel_pxfreq(u32 vidfreq)
7445{
7446 unsigned long freq;
7447 int div = (vidfreq & 0x3f0000) >> 16;
7448 int post = (vidfreq & 0x3000) >> 12;
7449 int pre = (vidfreq & 0x7);
7450
7451 if (!pre)
7452 return 0;
7453
7454 freq = ((div * 133333) / ((1<<post) * pre));
7455
7456 return freq;
7457}
7458
Daniel Vettereb48eb02012-04-26 23:28:12 +02007459static const struct cparams {
7460 u16 i;
7461 u16 t;
7462 u16 m;
7463 u16 c;
7464} cparams[] = {
7465 { 1, 1333, 301, 28664 },
7466 { 1, 1066, 294, 24460 },
7467 { 1, 800, 294, 25192 },
7468 { 0, 1333, 276, 27605 },
7469 { 0, 1066, 276, 27605 },
7470 { 0, 800, 231, 23784 },
7471};
7472
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007473static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007474{
7475 u64 total_count, diff, ret;
7476 u32 count1, count2, count3, m = 0, c = 0;
7477 unsigned long now = jiffies_to_msecs(jiffies), diff1;
7478 int i;
7479
Chris Wilson67520412017-03-02 13:28:01 +00007480 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007481
Daniel Vetter20e4d402012-08-08 23:35:39 +02007482 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007483
7484 /* Prevent division-by-zero if we are asking too fast.
7485 * Also, we don't get interesting results if we are polling
7486 * faster than once in 10ms, so just return the saved value
7487 * in such cases.
7488 */
7489 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02007490 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007491
7492 count1 = I915_READ(DMIEC);
7493 count2 = I915_READ(DDREC);
7494 count3 = I915_READ(CSIEC);
7495
7496 total_count = count1 + count2 + count3;
7497
7498 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02007499 if (total_count < dev_priv->ips.last_count1) {
7500 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007501 diff += total_count;
7502 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007503 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007504 }
7505
7506 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007507 if (cparams[i].i == dev_priv->ips.c_m &&
7508 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02007509 m = cparams[i].m;
7510 c = cparams[i].c;
7511 break;
7512 }
7513 }
7514
7515 diff = div_u64(diff, diff1);
7516 ret = ((m * diff) + c);
7517 ret = div_u64(ret, 10);
7518
Daniel Vetter20e4d402012-08-08 23:35:39 +02007519 dev_priv->ips.last_count1 = total_count;
7520 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007521
Daniel Vetter20e4d402012-08-08 23:35:39 +02007522 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007523
7524 return ret;
7525}
7526
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007527unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
7528{
7529 unsigned long val;
7530
Chris Wilsondc979972016-05-10 14:10:04 +01007531 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007532 return 0;
7533
7534 spin_lock_irq(&mchdev_lock);
7535
7536 val = __i915_chipset_val(dev_priv);
7537
7538 spin_unlock_irq(&mchdev_lock);
7539
7540 return val;
7541}
7542
Daniel Vettereb48eb02012-04-26 23:28:12 +02007543unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
7544{
7545 unsigned long m, x, b;
7546 u32 tsfs;
7547
7548 tsfs = I915_READ(TSFS);
7549
7550 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
7551 x = I915_READ8(TR1);
7552
7553 b = tsfs & TSFS_INTR_MASK;
7554
7555 return ((m * x) / 127) - b;
7556}
7557
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007558static int _pxvid_to_vd(u8 pxvid)
7559{
7560 if (pxvid == 0)
7561 return 0;
7562
7563 if (pxvid >= 8 && pxvid < 31)
7564 pxvid = 31;
7565
7566 return (pxvid + 2) * 125;
7567}
7568
7569static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007570{
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007571 const int vd = _pxvid_to_vd(pxvid);
7572 const int vm = vd - 1125;
7573
Chris Wilsondc979972016-05-10 14:10:04 +01007574 if (INTEL_INFO(dev_priv)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007575 return vm > 0 ? vm : 0;
7576
7577 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007578}
7579
Daniel Vetter02d71952012-08-09 16:44:54 +02007580static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007581{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00007582 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007583 u32 count;
7584
Chris Wilson67520412017-03-02 13:28:01 +00007585 lockdep_assert_held(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007586
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00007587 now = ktime_get_raw_ns();
7588 diffms = now - dev_priv->ips.last_time2;
7589 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007590
7591 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02007592 if (!diffms)
7593 return;
7594
7595 count = I915_READ(GFXEC);
7596
Daniel Vetter20e4d402012-08-08 23:35:39 +02007597 if (count < dev_priv->ips.last_count2) {
7598 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007599 diff += count;
7600 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007601 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007602 }
7603
Daniel Vetter20e4d402012-08-08 23:35:39 +02007604 dev_priv->ips.last_count2 = count;
7605 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007606
7607 /* More magic constants... */
7608 diff = diff * 1181;
7609 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02007610 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007611}
7612
Daniel Vetter02d71952012-08-09 16:44:54 +02007613void i915_update_gfx_val(struct drm_i915_private *dev_priv)
7614{
Chris Wilsondc979972016-05-10 14:10:04 +01007615 if (INTEL_INFO(dev_priv)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02007616 return;
7617
Daniel Vetter92703882012-08-09 16:46:01 +02007618 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007619
7620 __i915_update_gfx_val(dev_priv);
7621
Daniel Vetter92703882012-08-09 16:46:01 +02007622 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007623}
7624
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007625static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007626{
7627 unsigned long t, corr, state1, corr2, state2;
7628 u32 pxvid, ext_v;
7629
Chris Wilson67520412017-03-02 13:28:01 +00007630 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007631
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007632 pxvid = I915_READ(PXVFREQ(dev_priv->gt_pm.rps.cur_freq));
Daniel Vettereb48eb02012-04-26 23:28:12 +02007633 pxvid = (pxvid >> 24) & 0x7f;
7634 ext_v = pvid_to_extvid(dev_priv, pxvid);
7635
7636 state1 = ext_v;
7637
7638 t = i915_mch_val(dev_priv);
7639
7640 /* Revel in the empirically derived constants */
7641
7642 /* Correction factor in 1/100000 units */
7643 if (t > 80)
7644 corr = ((t * 2349) + 135940);
7645 else if (t >= 50)
7646 corr = ((t * 964) + 29317);
7647 else /* < 50 */
7648 corr = ((t * 301) + 1004);
7649
7650 corr = corr * ((150142 * state1) / 10000 - 78642);
7651 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02007652 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007653
7654 state2 = (corr2 * state1) / 10000;
7655 state2 /= 100; /* convert to mW */
7656
Daniel Vetter02d71952012-08-09 16:44:54 +02007657 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007658
Daniel Vetter20e4d402012-08-08 23:35:39 +02007659 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007660}
7661
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007662unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
7663{
7664 unsigned long val;
7665
Chris Wilsondc979972016-05-10 14:10:04 +01007666 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007667 return 0;
7668
7669 spin_lock_irq(&mchdev_lock);
7670
7671 val = __i915_gfx_val(dev_priv);
7672
7673 spin_unlock_irq(&mchdev_lock);
7674
7675 return val;
7676}
7677
Daniel Vettereb48eb02012-04-26 23:28:12 +02007678/**
7679 * i915_read_mch_val - return value for IPS use
7680 *
7681 * Calculate and return a value for the IPS driver to use when deciding whether
7682 * we have thermal and power headroom to increase CPU or GPU power budget.
7683 */
7684unsigned long i915_read_mch_val(void)
7685{
7686 struct drm_i915_private *dev_priv;
7687 unsigned long chipset_val, graphics_val, ret = 0;
7688
Daniel Vetter92703882012-08-09 16:46:01 +02007689 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007690 if (!i915_mch_dev)
7691 goto out_unlock;
7692 dev_priv = i915_mch_dev;
7693
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007694 chipset_val = __i915_chipset_val(dev_priv);
7695 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007696
7697 ret = chipset_val + graphics_val;
7698
7699out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007700 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007701
7702 return ret;
7703}
7704EXPORT_SYMBOL_GPL(i915_read_mch_val);
7705
7706/**
7707 * i915_gpu_raise - raise GPU frequency limit
7708 *
7709 * Raise the limit; IPS indicates we have thermal headroom.
7710 */
7711bool i915_gpu_raise(void)
7712{
7713 struct drm_i915_private *dev_priv;
7714 bool ret = true;
7715
Daniel Vetter92703882012-08-09 16:46:01 +02007716 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007717 if (!i915_mch_dev) {
7718 ret = false;
7719 goto out_unlock;
7720 }
7721 dev_priv = i915_mch_dev;
7722
Daniel Vetter20e4d402012-08-08 23:35:39 +02007723 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
7724 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007725
7726out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007727 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007728
7729 return ret;
7730}
7731EXPORT_SYMBOL_GPL(i915_gpu_raise);
7732
7733/**
7734 * i915_gpu_lower - lower GPU frequency limit
7735 *
7736 * IPS indicates we're close to a thermal limit, so throttle back the GPU
7737 * frequency maximum.
7738 */
7739bool i915_gpu_lower(void)
7740{
7741 struct drm_i915_private *dev_priv;
7742 bool ret = true;
7743
Daniel Vetter92703882012-08-09 16:46:01 +02007744 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007745 if (!i915_mch_dev) {
7746 ret = false;
7747 goto out_unlock;
7748 }
7749 dev_priv = i915_mch_dev;
7750
Daniel Vetter20e4d402012-08-08 23:35:39 +02007751 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
7752 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007753
7754out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007755 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007756
7757 return ret;
7758}
7759EXPORT_SYMBOL_GPL(i915_gpu_lower);
7760
7761/**
7762 * i915_gpu_busy - indicate GPU business to IPS
7763 *
7764 * Tell the IPS driver whether or not the GPU is busy.
7765 */
7766bool i915_gpu_busy(void)
7767{
Daniel Vettereb48eb02012-04-26 23:28:12 +02007768 bool ret = false;
7769
Daniel Vetter92703882012-08-09 16:46:01 +02007770 spin_lock_irq(&mchdev_lock);
Chris Wilsondcff85c2016-08-05 10:14:11 +01007771 if (i915_mch_dev)
7772 ret = i915_mch_dev->gt.awake;
Daniel Vetter92703882012-08-09 16:46:01 +02007773 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007774
7775 return ret;
7776}
7777EXPORT_SYMBOL_GPL(i915_gpu_busy);
7778
7779/**
7780 * i915_gpu_turbo_disable - disable graphics turbo
7781 *
7782 * Disable graphics turbo by resetting the max frequency and setting the
7783 * current frequency to the default.
7784 */
7785bool i915_gpu_turbo_disable(void)
7786{
7787 struct drm_i915_private *dev_priv;
7788 bool ret = true;
7789
Daniel Vetter92703882012-08-09 16:46:01 +02007790 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007791 if (!i915_mch_dev) {
7792 ret = false;
7793 goto out_unlock;
7794 }
7795 dev_priv = i915_mch_dev;
7796
Daniel Vetter20e4d402012-08-08 23:35:39 +02007797 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007798
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01007799 if (!ironlake_set_drps(dev_priv, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02007800 ret = false;
7801
7802out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007803 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007804
7805 return ret;
7806}
7807EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
7808
7809/**
7810 * Tells the intel_ips driver that the i915 driver is now loaded, if
7811 * IPS got loaded first.
7812 *
7813 * This awkward dance is so that neither module has to depend on the
7814 * other in order for IPS to do the appropriate communication of
7815 * GPU turbo limits to i915.
7816 */
7817static void
7818ips_ping_for_i915_load(void)
7819{
7820 void (*link)(void);
7821
7822 link = symbol_get(ips_link_to_i915_driver);
7823 if (link) {
7824 link();
7825 symbol_put(ips_link_to_i915_driver);
7826 }
7827}
7828
7829void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
7830{
Daniel Vetter02d71952012-08-09 16:44:54 +02007831 /* We only register the i915 ips part with intel-ips once everything is
7832 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02007833 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007834 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02007835 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007836
7837 ips_ping_for_i915_load();
7838}
7839
7840void intel_gpu_ips_teardown(void)
7841{
Daniel Vetter92703882012-08-09 16:46:01 +02007842 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007843 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02007844 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007845}
Deepak S76c3552f2014-01-30 23:08:16 +05307846
Chris Wilsondc979972016-05-10 14:10:04 +01007847static void intel_init_emon(struct drm_i915_private *dev_priv)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007848{
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007849 u32 lcfuse;
7850 u8 pxw[16];
7851 int i;
7852
7853 /* Disable to program */
7854 I915_WRITE(ECR, 0);
7855 POSTING_READ(ECR);
7856
7857 /* Program energy weights for various events */
7858 I915_WRITE(SDEW, 0x15040d00);
7859 I915_WRITE(CSIEW0, 0x007f0000);
7860 I915_WRITE(CSIEW1, 0x1e220004);
7861 I915_WRITE(CSIEW2, 0x04000004);
7862
7863 for (i = 0; i < 5; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007864 I915_WRITE(PEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007865 for (i = 0; i < 3; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007866 I915_WRITE(DEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007867
7868 /* Program P-state weights to account for frequency power adjustment */
7869 for (i = 0; i < 16; i++) {
Ville Syrjälä616847e2015-09-18 20:03:19 +03007870 u32 pxvidfreq = I915_READ(PXVFREQ(i));
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007871 unsigned long freq = intel_pxfreq(pxvidfreq);
7872 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
7873 PXVFREQ_PX_SHIFT;
7874 unsigned long val;
7875
7876 val = vid * vid;
7877 val *= (freq / 1000);
7878 val *= 255;
7879 val /= (127*127*900);
7880 if (val > 0xff)
7881 DRM_ERROR("bad pxval: %ld\n", val);
7882 pxw[i] = val;
7883 }
7884 /* Render standby states get 0 weight */
7885 pxw[14] = 0;
7886 pxw[15] = 0;
7887
7888 for (i = 0; i < 4; i++) {
7889 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
7890 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
Ville Syrjälä616847e2015-09-18 20:03:19 +03007891 I915_WRITE(PXW(i), val);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007892 }
7893
7894 /* Adjust magic regs to magic values (more experimental results) */
7895 I915_WRITE(OGW0, 0);
7896 I915_WRITE(OGW1, 0);
7897 I915_WRITE(EG0, 0x00007f00);
7898 I915_WRITE(EG1, 0x0000000e);
7899 I915_WRITE(EG2, 0x000e0000);
7900 I915_WRITE(EG3, 0x68000300);
7901 I915_WRITE(EG4, 0x42000000);
7902 I915_WRITE(EG5, 0x00140031);
7903 I915_WRITE(EG6, 0);
7904 I915_WRITE(EG7, 0);
7905
7906 for (i = 0; i < 8; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007907 I915_WRITE(PXWL(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007908
7909 /* Enable PMON + select events */
7910 I915_WRITE(ECR, 0x80000019);
7911
7912 lcfuse = I915_READ(LCFUSE02);
7913
Daniel Vetter20e4d402012-08-08 23:35:39 +02007914 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007915}
7916
Chris Wilsondc979972016-05-10 14:10:04 +01007917void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007918{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007919 struct intel_rps *rps = &dev_priv->gt_pm.rps;
7920
Imre Deakb268c692015-12-15 20:10:31 +02007921 /*
7922 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
7923 * requirement.
7924 */
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00007925 if (!i915_modparams.enable_rc6) {
Imre Deakb268c692015-12-15 20:10:31 +02007926 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
7927 intel_runtime_pm_get(dev_priv);
7928 }
Imre Deake6069ca2014-04-18 16:01:02 +03007929
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01007930 mutex_lock(&dev_priv->pcu_lock);
Chris Wilson773ea9a2016-07-13 09:10:33 +01007931
7932 /* Initialize RPS limits (for userspace) */
Chris Wilsondc979972016-05-10 14:10:04 +01007933 if (IS_CHERRYVIEW(dev_priv))
7934 cherryview_init_gt_powersave(dev_priv);
7935 else if (IS_VALLEYVIEW(dev_priv))
7936 valleyview_init_gt_powersave(dev_priv);
Chris Wilson2a13ae72016-08-02 11:15:27 +01007937 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson773ea9a2016-07-13 09:10:33 +01007938 gen6_init_rps_frequencies(dev_priv);
7939
7940 /* Derive initial user preferences/limits from the hardware limits */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007941 rps->idle_freq = rps->min_freq;
7942 rps->cur_freq = rps->idle_freq;
Chris Wilson773ea9a2016-07-13 09:10:33 +01007943
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007944 rps->max_freq_softlimit = rps->max_freq;
7945 rps->min_freq_softlimit = rps->min_freq;
Chris Wilson773ea9a2016-07-13 09:10:33 +01007946
7947 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007948 rps->min_freq_softlimit =
Chris Wilson773ea9a2016-07-13 09:10:33 +01007949 max_t(int,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007950 rps->efficient_freq,
Chris Wilson773ea9a2016-07-13 09:10:33 +01007951 intel_freq_opcode(dev_priv, 450));
7952
Chris Wilson99ac9612016-07-13 09:10:34 +01007953 /* After setting max-softlimit, find the overclock max freq */
7954 if (IS_GEN6(dev_priv) ||
7955 IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
7956 u32 params = 0;
7957
7958 sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &params);
7959 if (params & BIT(31)) { /* OC supported */
7960 DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007961 (rps->max_freq & 0xff) * 50,
Chris Wilson99ac9612016-07-13 09:10:34 +01007962 (params & 0xff) * 50);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007963 rps->max_freq = params & 0xff;
Chris Wilson99ac9612016-07-13 09:10:34 +01007964 }
7965 }
7966
Chris Wilson29ecd78d2016-07-13 09:10:35 +01007967 /* Finally allow us to boost to max by default */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01007968 rps->boost_freq = rps->max_freq;
Chris Wilson29ecd78d2016-07-13 09:10:35 +01007969
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01007970 mutex_unlock(&dev_priv->pcu_lock);
Imre Deakae484342014-03-31 15:10:44 +03007971}
7972
Chris Wilsondc979972016-05-10 14:10:04 +01007973void intel_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007974{
Ville Syrjälä8dac1e12016-08-02 14:07:33 +03007975 if (IS_VALLEYVIEW(dev_priv))
Chris Wilsondc979972016-05-10 14:10:04 +01007976 valleyview_cleanup_gt_powersave(dev_priv);
Imre Deakb268c692015-12-15 20:10:31 +02007977
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00007978 if (!i915_modparams.enable_rc6)
Imre Deakb268c692015-12-15 20:10:31 +02007979 intel_runtime_pm_put(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03007980}
7981
Chris Wilson54b4f682016-07-21 21:16:19 +01007982/**
7983 * intel_suspend_gt_powersave - suspend PM work and helper threads
7984 * @dev_priv: i915 device
7985 *
7986 * We don't want to disable RC6 or other features here, we just want
7987 * to make sure any work we've queued has finished and won't bother
7988 * us while we're suspended.
7989 */
7990void intel_suspend_gt_powersave(struct drm_i915_private *dev_priv)
7991{
7992 if (INTEL_GEN(dev_priv) < 6)
7993 return;
7994
Chris Wilson54b4f682016-07-21 21:16:19 +01007995 /* gen6_rps_idle() will be called later to disable interrupts */
7996}
7997
Chris Wilsonb7137e02016-07-13 09:10:37 +01007998void intel_sanitize_gt_powersave(struct drm_i915_private *dev_priv)
7999{
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008000 dev_priv->gt_pm.rps.enabled = true; /* force RPS disabling */
8001 dev_priv->gt_pm.rc6.enabled = true; /* force RC6 disabling */
Chris Wilsonb7137e02016-07-13 09:10:37 +01008002 intel_disable_gt_powersave(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01008003
8004 gen6_reset_rps_interrupts(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07008005}
8006
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008007static inline void intel_disable_llc_pstate(struct drm_i915_private *i915)
8008{
8009 lockdep_assert_held(&i915->pcu_lock);
8010
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008011 if (!i915->gt_pm.llc_pstate.enabled)
8012 return;
8013
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008014 /* Currently there is no HW configuration to be done to disable. */
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008015
8016 i915->gt_pm.llc_pstate.enabled = false;
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008017}
8018
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008019static void intel_disable_rc6(struct drm_i915_private *dev_priv)
8020{
8021 lockdep_assert_held(&dev_priv->pcu_lock);
8022
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008023 if (!dev_priv->gt_pm.rc6.enabled)
8024 return;
8025
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008026 if (INTEL_GEN(dev_priv) >= 9)
8027 gen9_disable_rc6(dev_priv);
8028 else if (IS_CHERRYVIEW(dev_priv))
8029 cherryview_disable_rc6(dev_priv);
8030 else if (IS_VALLEYVIEW(dev_priv))
8031 valleyview_disable_rc6(dev_priv);
8032 else if (INTEL_GEN(dev_priv) >= 6)
8033 gen6_disable_rc6(dev_priv);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008034
8035 dev_priv->gt_pm.rc6.enabled = false;
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008036}
8037
8038static void intel_disable_rps(struct drm_i915_private *dev_priv)
8039{
8040 lockdep_assert_held(&dev_priv->pcu_lock);
8041
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008042 if (!dev_priv->gt_pm.rps.enabled)
8043 return;
8044
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008045 if (INTEL_GEN(dev_priv) >= 9)
8046 gen9_disable_rps(dev_priv);
8047 else if (IS_CHERRYVIEW(dev_priv))
8048 cherryview_disable_rps(dev_priv);
8049 else if (IS_VALLEYVIEW(dev_priv))
8050 valleyview_disable_rps(dev_priv);
8051 else if (INTEL_GEN(dev_priv) >= 6)
8052 gen6_disable_rps(dev_priv);
8053 else if (IS_IRONLAKE_M(dev_priv))
8054 ironlake_disable_drps(dev_priv);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008055
8056 dev_priv->gt_pm.rps.enabled = false;
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008057}
8058
Chris Wilsondc979972016-05-10 14:10:04 +01008059void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
Daniel Vetter8090c6b2012-06-24 16:42:32 +02008060{
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01008061 mutex_lock(&dev_priv->pcu_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07008062
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008063 intel_disable_rc6(dev_priv);
8064 intel_disable_rps(dev_priv);
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008065 if (HAS_LLC(dev_priv))
8066 intel_disable_llc_pstate(dev_priv);
8067
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01008068 mutex_unlock(&dev_priv->pcu_lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01008069}
8070
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008071static inline void intel_enable_llc_pstate(struct drm_i915_private *i915)
8072{
8073 lockdep_assert_held(&i915->pcu_lock);
8074
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008075 if (i915->gt_pm.llc_pstate.enabled)
8076 return;
8077
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008078 gen6_update_ring_freq(i915);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008079
8080 i915->gt_pm.llc_pstate.enabled = true;
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008081}
8082
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008083static void intel_enable_rc6(struct drm_i915_private *dev_priv)
8084{
8085 lockdep_assert_held(&dev_priv->pcu_lock);
8086
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008087 if (dev_priv->gt_pm.rc6.enabled)
8088 return;
8089
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008090 if (IS_CHERRYVIEW(dev_priv))
8091 cherryview_enable_rc6(dev_priv);
8092 else if (IS_VALLEYVIEW(dev_priv))
8093 valleyview_enable_rc6(dev_priv);
8094 else if (INTEL_GEN(dev_priv) >= 9)
8095 gen9_enable_rc6(dev_priv);
8096 else if (IS_BROADWELL(dev_priv))
8097 gen8_enable_rc6(dev_priv);
8098 else if (INTEL_GEN(dev_priv) >= 6)
8099 gen6_enable_rc6(dev_priv);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008100
8101 dev_priv->gt_pm.rc6.enabled = true;
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008102}
8103
8104static void intel_enable_rps(struct drm_i915_private *dev_priv)
8105{
8106 struct intel_rps *rps = &dev_priv->gt_pm.rps;
8107
8108 lockdep_assert_held(&dev_priv->pcu_lock);
8109
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008110 if (rps->enabled)
8111 return;
8112
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008113 if (IS_CHERRYVIEW(dev_priv)) {
8114 cherryview_enable_rps(dev_priv);
8115 } else if (IS_VALLEYVIEW(dev_priv)) {
8116 valleyview_enable_rps(dev_priv);
8117 } else if (INTEL_GEN(dev_priv) >= 9) {
8118 gen9_enable_rps(dev_priv);
8119 } else if (IS_BROADWELL(dev_priv)) {
8120 gen8_enable_rps(dev_priv);
8121 } else if (INTEL_GEN(dev_priv) >= 6) {
8122 gen6_enable_rps(dev_priv);
8123 } else if (IS_IRONLAKE_M(dev_priv)) {
8124 ironlake_enable_drps(dev_priv);
8125 intel_init_emon(dev_priv);
8126 }
8127
8128 WARN_ON(rps->max_freq < rps->min_freq);
8129 WARN_ON(rps->idle_freq > rps->max_freq);
8130
8131 WARN_ON(rps->efficient_freq < rps->min_freq);
8132 WARN_ON(rps->efficient_freq > rps->max_freq);
Sagar Arun Kamble37d933f2017-10-10 22:30:10 +01008133
8134 rps->enabled = true;
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008135}
8136
Chris Wilsonb7137e02016-07-13 09:10:37 +01008137void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
8138{
Chris Wilsonb7137e02016-07-13 09:10:37 +01008139 /* Powersaving is controlled by the host when inside a VM */
8140 if (intel_vgpu_active(dev_priv))
8141 return;
8142
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01008143 mutex_lock(&dev_priv->pcu_lock);
Imre Deak3cc134e2014-11-19 15:30:03 +02008144
Sagar Arun Kamblefc774262017-10-10 22:30:09 +01008145 intel_enable_rc6(dev_priv);
8146 intel_enable_rps(dev_priv);
Sagar Arun Kamble0870a2a2017-10-10 22:30:08 +01008147 if (HAS_LLC(dev_priv))
8148 intel_enable_llc_pstate(dev_priv);
8149
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01008150 mutex_unlock(&dev_priv->pcu_lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01008151}
Imre Deakc6df39b2014-04-14 20:24:29 +03008152
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008153static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01008154{
Daniel Vetter3107bd42012-10-31 22:52:31 +01008155 /*
8156 * On Ibex Peak and Cougar Point, we need to disable clock
8157 * gating for the panel power sequencer or it will fail to
8158 * start up when no ports are active.
8159 */
8160 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
8161}
8162
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008163static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008164{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03008165 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008166
Damien Lespiau055e3932014-08-18 13:49:10 +01008167 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008168 I915_WRITE(DSPCNTR(pipe),
8169 I915_READ(DSPCNTR(pipe)) |
8170 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03008171
8172 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
8173 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008174 }
8175}
8176
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008177static void ilk_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008178{
Damien Lespiau231e54f2012-10-19 17:55:41 +01008179 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008180
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01008181 /*
8182 * Required for FBC
8183 * WaFbcDisableDpfcClockGating:ilk
8184 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008185 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
8186 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
8187 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008188
8189 I915_WRITE(PCH_3DCGDIS0,
8190 MARIUNIT_CLOCK_GATE_DISABLE |
8191 SVSMUNIT_CLOCK_GATE_DISABLE);
8192 I915_WRITE(PCH_3DCGDIS1,
8193 VFMUNIT_CLOCK_GATE_DISABLE);
8194
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008195 /*
8196 * According to the spec the following bits should be set in
8197 * order to enable memory self-refresh
8198 * The bit 22/21 of 0x42004
8199 * The bit 5 of 0x42020
8200 * The bit 15 of 0x45000
8201 */
8202 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8203 (I915_READ(ILK_DISPLAY_CHICKEN2) |
8204 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008205 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008206 I915_WRITE(DISP_ARB_CTL,
8207 (I915_READ(DISP_ARB_CTL) |
8208 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02008209
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008210 /*
8211 * Based on the document from hardware guys the following bits
8212 * should be set unconditionally in order to enable FBC.
8213 * The bit 22 of 0x42000
8214 * The bit 22 of 0x42004
8215 * The bit 7,8,9 of 0x42020.
8216 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008217 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01008218 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008219 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8220 I915_READ(ILK_DISPLAY_CHICKEN1) |
8221 ILK_FBCQ_DIS);
8222 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8223 I915_READ(ILK_DISPLAY_CHICKEN2) |
8224 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008225 }
8226
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008227 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
8228
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008229 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8230 I915_READ(ILK_DISPLAY_CHICKEN2) |
8231 ILK_ELPIN_409_SELECT);
8232 I915_WRITE(_3D_CHICKEN2,
8233 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
8234 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02008235
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008236 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02008237 I915_WRITE(CACHE_MODE_0,
8238 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01008239
Akash Goel4e046322014-04-04 17:14:38 +05308240 /* WaDisable_RenderCache_OperationalFlush:ilk */
8241 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8242
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008243 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03008244
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008245 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008246}
8247
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008248static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01008249{
Daniel Vetter3107bd42012-10-31 22:52:31 +01008250 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008251 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01008252
8253 /*
8254 * On Ibex Peak and Cougar Point, we need to disable clock
8255 * gating for the panel power sequencer or it will fail to
8256 * start up when no ports are active.
8257 */
Jesse Barnescd664072013-10-02 10:34:19 -07008258 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
8259 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
8260 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008261 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
8262 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01008263 /* The below fixes the weird display corruption, a few pixels shifted
8264 * downward, on (only) LVDS of some HP laptops with IVY.
8265 */
Damien Lespiau055e3932014-08-18 13:49:10 +01008266 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03008267 val = I915_READ(TRANS_CHICKEN2(pipe));
8268 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
8269 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008270 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008271 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03008272 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
8273 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
8274 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008275 I915_WRITE(TRANS_CHICKEN2(pipe), val);
8276 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01008277 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01008278 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01008279 I915_WRITE(TRANS_CHICKEN1(pipe),
8280 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
8281 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008282}
8283
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008284static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008285{
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008286 uint32_t tmp;
8287
8288 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02008289 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
8290 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
8291 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008292}
8293
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008294static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008295{
Damien Lespiau231e54f2012-10-19 17:55:41 +01008296 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008297
Damien Lespiau231e54f2012-10-19 17:55:41 +01008298 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008299
8300 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8301 I915_READ(ILK_DISPLAY_CHICKEN2) |
8302 ILK_ELPIN_409_SELECT);
8303
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008304 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01008305 I915_WRITE(_3D_CHICKEN,
8306 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
8307
Akash Goel4e046322014-04-04 17:14:38 +05308308 /* WaDisable_RenderCache_OperationalFlush:snb */
8309 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8310
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008311 /*
8312 * BSpec recoomends 8x4 when MSAA is used,
8313 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008314 *
8315 * Note that PS/WM thread counts depend on the WIZ hashing
8316 * disable bit, which we don't touch here, but it's good
8317 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008318 */
8319 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008320 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008321
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008322 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02008323 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008324
8325 I915_WRITE(GEN6_UCGCTL1,
8326 I915_READ(GEN6_UCGCTL1) |
8327 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
8328 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
8329
8330 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
8331 * gating disable must be set. Failure to set it results in
8332 * flickering pixels due to Z write ordering failures after
8333 * some amount of runtime in the Mesa "fire" demo, and Unigine
8334 * Sanctuary and Tropics, and apparently anything else with
8335 * alpha test or pixel discard.
8336 *
8337 * According to the spec, bit 11 (RCCUNIT) must also be set,
8338 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008339 *
Ville Syrjäläef593182014-01-22 21:32:47 +02008340 * WaDisableRCCUnitClockGating:snb
8341 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008342 */
8343 I915_WRITE(GEN6_UCGCTL2,
8344 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
8345 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
8346
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02008347 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02008348 I915_WRITE(_3D_CHICKEN3,
8349 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008350
8351 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02008352 * Bspec says:
8353 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
8354 * 3DSTATE_SF number of SF output attributes is more than 16."
8355 */
8356 I915_WRITE(_3D_CHICKEN3,
8357 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
8358
8359 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008360 * According to the spec the following bits should be
8361 * set in order to enable memory self-refresh and fbc:
8362 * The bit21 and bit22 of 0x42000
8363 * The bit21 and bit22 of 0x42004
8364 * The bit5 and bit7 of 0x42020
8365 * The bit14 of 0x70180
8366 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01008367 *
8368 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008369 */
8370 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8371 I915_READ(ILK_DISPLAY_CHICKEN1) |
8372 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
8373 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8374 I915_READ(ILK_DISPLAY_CHICKEN2) |
8375 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01008376 I915_WRITE(ILK_DSPCLK_GATE_D,
8377 I915_READ(ILK_DSPCLK_GATE_D) |
8378 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
8379 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008380
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008381 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07008382
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008383 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008384
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008385 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008386}
8387
8388static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
8389{
8390 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
8391
Ville Syrjälä3aad9052014-01-22 21:32:59 +02008392 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02008393 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02008394 *
8395 * This actually overrides the dispatch
8396 * mode for all thread types.
8397 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008398 reg &= ~GEN7_FF_SCHED_MASK;
8399 reg |= GEN7_FF_TS_SCHED_HW;
8400 reg |= GEN7_FF_VS_SCHED_HW;
8401 reg |= GEN7_FF_DS_SCHED_HW;
8402
8403 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
8404}
8405
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008406static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008407{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008408 /*
8409 * TODO: this bit should only be enabled when really needed, then
8410 * disabled when not needed anymore in order to save power.
8411 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008412 if (HAS_PCH_LPT_LP(dev_priv))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008413 I915_WRITE(SOUTH_DSPCLK_GATE_D,
8414 I915_READ(SOUTH_DSPCLK_GATE_D) |
8415 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03008416
8417 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03008418 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
8419 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03008420 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008421}
8422
Ville Syrjälä712bf362016-10-31 22:37:23 +02008423static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03008424{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008425 if (HAS_PCH_LPT_LP(dev_priv)) {
Imre Deak7d708ee2013-04-17 14:04:50 +03008426 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
8427
8428 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8429 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8430 }
8431}
8432
Imre Deak450174f2016-05-03 15:54:21 +03008433static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
8434 int general_prio_credits,
8435 int high_prio_credits)
8436{
8437 u32 misccpctl;
Oscar Mateo930a7842017-10-17 13:25:45 -07008438 u32 val;
Imre Deak450174f2016-05-03 15:54:21 +03008439
8440 /* WaTempDisableDOPClkGating:bdw */
8441 misccpctl = I915_READ(GEN7_MISCCPCTL);
8442 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
8443
Oscar Mateo930a7842017-10-17 13:25:45 -07008444 val = I915_READ(GEN8_L3SQCREG1);
8445 val &= ~L3_PRIO_CREDITS_MASK;
8446 val |= L3_GENERAL_PRIO_CREDITS(general_prio_credits);
8447 val |= L3_HIGH_PRIO_CREDITS(high_prio_credits);
8448 I915_WRITE(GEN8_L3SQCREG1, val);
Imre Deak450174f2016-05-03 15:54:21 +03008449
8450 /*
8451 * Wait at least 100 clocks before re-enabling clock gating.
8452 * See the definition of L3SQCREG1 in BSpec.
8453 */
8454 POSTING_READ(GEN8_L3SQCREG1);
8455 udelay(1);
8456 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
8457}
8458
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008459static void cnp_init_clock_gating(struct drm_i915_private *dev_priv)
8460{
8461 if (!HAS_PCH_CNP(dev_priv))
8462 return;
8463
8464 /* Wa #1181 */
Rodrigo Vivi4cc6feb2017-09-08 16:45:33 -07008465 I915_WRITE(SOUTH_DSPCLK_GATE_D, I915_READ(SOUTH_DSPCLK_GATE_D) |
8466 CNP_PWM_CGE_GATING_DISABLE);
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008467}
8468
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008469static void cnl_init_clock_gating(struct drm_i915_private *dev_priv)
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008470{
Rodrigo Vivi8f067832017-09-05 12:30:13 -07008471 u32 val;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008472 cnp_init_clock_gating(dev_priv);
8473
Rodrigo Vivi1a25db62017-08-15 16:16:51 -07008474 /* This is not an Wa. Enable for better image quality */
8475 I915_WRITE(_3D_CHICKEN3,
8476 _MASKED_BIT_ENABLE(_3D_CHICKEN3_AA_LINE_QUALITY_FIX_ENABLE));
8477
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008478 /* WaEnableChickenDCPR:cnl */
8479 I915_WRITE(GEN8_CHICKEN_DCPR_1,
8480 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
8481
8482 /* WaFbcWakeMemOn:cnl */
8483 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
8484 DISP_FBC_MEMORY_WAKE);
8485
Chris Wilson34991bd2017-11-11 10:03:36 +00008486 val = I915_READ(SLICE_UNIT_LEVEL_CLKGATE);
8487 /* ReadHitWriteOnlyDisable:cnl */
8488 val |= RCCUNIT_CLKGATE_DIS;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008489 /* WaSarbUnitClockGatingDisable:cnl (pre-prod) */
8490 if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_B0))
Chris Wilson34991bd2017-11-11 10:03:36 +00008491 val |= SARBUNIT_CLKGATE_DIS;
8492 I915_WRITE(SLICE_UNIT_LEVEL_CLKGATE, val);
Rodrigo Vivi8f067832017-09-05 12:30:13 -07008493
8494 /* Display WA #1133: WaFbcSkipSegments:cnl */
8495 val = I915_READ(ILK_DPFC_CHICKEN);
8496 val &= ~GLK_SKIP_SEG_COUNT_MASK;
8497 val |= GLK_SKIP_SEG_EN | GLK_SKIP_SEG_COUNT(1);
8498 I915_WRITE(ILK_DPFC_CHICKEN, val);
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008499}
8500
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008501static void cfl_init_clock_gating(struct drm_i915_private *dev_priv)
8502{
8503 cnp_init_clock_gating(dev_priv);
8504 gen9_init_clock_gating(dev_priv);
8505
8506 /* WaFbcNukeOnHostModify:cfl */
8507 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8508 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
8509}
8510
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008511static void kbl_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008512{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008513 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008514
8515 /* WaDisableSDEUnitClockGating:kbl */
8516 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
8517 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8518 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03008519
8520 /* WaDisableGamClockGating:kbl */
8521 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
8522 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
8523 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008524
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008525 /* WaFbcNukeOnHostModify:kbl */
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008526 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8527 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008528}
8529
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008530static void skl_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008531{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008532 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03008533
8534 /* WAC6entrylatency:skl */
8535 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
8536 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008537
8538 /* WaFbcNukeOnHostModify:skl */
8539 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8540 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008541}
8542
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008543static void bdw_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008544{
Matthew Auld8cb09832017-10-06 23:18:23 +01008545 /* The GTT cache must be disabled if the system is using 2M pages. */
8546 bool can_use_gtt_cache = !HAS_PAGE_SIZES(dev_priv,
8547 I915_GTT_PAGE_SIZE_2M);
Damien Lespiau07d27e22014-03-03 17:31:46 +00008548 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008549
Ben Widawskyab57fff2013-12-12 15:28:04 -08008550 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07008551 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008552
Ben Widawskyab57fff2013-12-12 15:28:04 -08008553 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008554 I915_WRITE(CHICKEN_PAR1_1,
8555 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
8556
Ben Widawskyab57fff2013-12-12 15:28:04 -08008557 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01008558 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00008559 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02008560 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02008561 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008562 }
Ben Widawsky63801f22013-12-12 17:26:03 -08008563
Ben Widawskyab57fff2013-12-12 15:28:04 -08008564 /* WaVSRefCountFullforceMissDisable:bdw */
8565 /* WaDSRefCountFullforceMissDisable:bdw */
8566 I915_WRITE(GEN7_FF_THREAD_MODE,
8567 I915_READ(GEN7_FF_THREAD_MODE) &
8568 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02008569
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02008570 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
8571 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02008572
8573 /* WaDisableSDEUnitClockGating:bdw */
8574 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8575 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00008576
Imre Deak450174f2016-05-03 15:54:21 +03008577 /* WaProgramL3SqcReg1Default:bdw */
8578 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03008579
Matthew Auld8cb09832017-10-06 23:18:23 +01008580 /* WaGttCachingOffByDefault:bdw */
8581 I915_WRITE(HSW_GTT_CACHE_EN, can_use_gtt_cache ? GTT_CACHE_EN_ALL : 0);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008582
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03008583 /* WaKVMNotificationOnConfigChange:bdw */
8584 I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
8585 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
8586
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008587 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00008588
8589 /* WaDisableDopClockGating:bdw
8590 *
8591 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
8592 * clock gating.
8593 */
8594 I915_WRITE(GEN6_UCGCTL1,
8595 I915_READ(GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008596}
8597
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008598static void hsw_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008599{
Francisco Jerezf3fc4882013-10-02 15:53:16 -07008600 /* L3 caching of data atomics doesn't work -- disable it. */
8601 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
8602 I915_WRITE(HSW_ROW_CHICKEN3,
8603 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
8604
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008605 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008606 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8607 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8608 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8609
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02008610 /* WaVSRefCountFullforceMissDisable:hsw */
8611 I915_WRITE(GEN7_FF_THREAD_MODE,
8612 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008613
Akash Goel4e046322014-04-04 17:14:38 +05308614 /* WaDisable_RenderCache_OperationalFlush:hsw */
8615 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8616
Chia-I Wufe27c602014-01-28 13:29:33 +08008617 /* enable HiZ Raw Stall Optimization */
8618 I915_WRITE(CACHE_MODE_0_GEN7,
8619 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
8620
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008621 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008622 I915_WRITE(CACHE_MODE_1,
8623 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03008624
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008625 /*
8626 * BSpec recommends 8x4 when MSAA is used,
8627 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008628 *
8629 * Note that PS/WM thread counts depend on the WIZ hashing
8630 * disable bit, which we don't touch here, but it's good
8631 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008632 */
8633 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008634 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008635
Kenneth Graunke94411592014-12-31 16:23:00 -08008636 /* WaSampleCChickenBitEnable:hsw */
8637 I915_WRITE(HALF_SLICE_CHICKEN3,
8638 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
8639
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008640 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07008641 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
8642
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008643 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008644}
8645
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008646static void ivb_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008647{
Ben Widawsky20848222012-05-04 18:58:59 -07008648 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008649
Damien Lespiau231e54f2012-10-19 17:55:41 +01008650 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008651
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008652 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05008653 I915_WRITE(_3D_CHICKEN3,
8654 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
8655
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008656 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008657 I915_WRITE(IVB_CHICKEN3,
8658 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8659 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8660
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008661 /* WaDisablePSDDualDispatchEnable:ivb */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008662 if (IS_IVB_GT1(dev_priv))
Jesse Barnes12f33822012-10-25 12:15:45 -07008663 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
8664 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07008665
Akash Goel4e046322014-04-04 17:14:38 +05308666 /* WaDisable_RenderCache_OperationalFlush:ivb */
8667 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8668
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008669 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008670 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
8671 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
8672
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008673 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008674 I915_WRITE(GEN7_L3CNTLREG1,
8675 GEN7_WA_FOR_GEN7_L3_CONTROL);
8676 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07008677 GEN7_WA_L3_CHICKEN_MODE);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008678 if (IS_IVB_GT1(dev_priv))
Jesse Barnes8ab43972012-10-25 12:15:42 -07008679 I915_WRITE(GEN7_ROW_CHICKEN2,
8680 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02008681 else {
8682 /* must write both registers */
8683 I915_WRITE(GEN7_ROW_CHICKEN2,
8684 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07008685 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
8686 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02008687 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008688
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008689 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05008690 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
8691 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
8692
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02008693 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07008694 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008695 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008696 */
8697 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02008698 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07008699
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008700 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008701 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8702 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8703 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8704
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008705 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008706
8707 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02008708
Chris Wilson22721342014-03-04 09:41:43 +00008709 if (0) { /* causes HiZ corruption on ivb:gt1 */
8710 /* enable HiZ Raw Stall Optimization */
8711 I915_WRITE(CACHE_MODE_0_GEN7,
8712 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
8713 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08008714
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008715 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02008716 I915_WRITE(CACHE_MODE_1,
8717 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07008718
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008719 /*
8720 * BSpec recommends 8x4 when MSAA is used,
8721 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008722 *
8723 * Note that PS/WM thread counts depend on the WIZ hashing
8724 * disable bit, which we don't touch here, but it's good
8725 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008726 */
8727 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008728 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008729
Ben Widawsky20848222012-05-04 18:58:59 -07008730 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
8731 snpcr &= ~GEN6_MBC_SNPCR_MASK;
8732 snpcr |= GEN6_MBC_SNPCR_MED;
8733 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008734
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008735 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008736 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008737
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008738 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008739}
8740
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008741static void vlv_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008742{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008743 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05008744 I915_WRITE(_3D_CHICKEN3,
8745 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
8746
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008747 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008748 I915_WRITE(IVB_CHICKEN3,
8749 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8750 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8751
Ville Syrjäläfad7d362014-01-22 21:32:39 +02008752 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008753 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07008754 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08008755 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
8756 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07008757
Akash Goel4e046322014-04-04 17:14:38 +05308758 /* WaDisable_RenderCache_OperationalFlush:vlv */
8759 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8760
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008761 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05008762 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
8763 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
8764
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008765 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07008766 I915_WRITE(GEN7_ROW_CHICKEN2,
8767 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
8768
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008769 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008770 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8771 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8772 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8773
Ville Syrjälä46680e02014-01-22 21:33:01 +02008774 gen7_setup_fixed_func_scheduler(dev_priv);
8775
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02008776 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07008777 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008778 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008779 */
8780 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02008781 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07008782
Akash Goelc98f5062014-03-24 23:00:07 +05308783 /* WaDisableL3Bank2xClockGate:vlv
8784 * Disabling L3 clock gating- MMIO 940c[25] = 1
8785 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
8786 I915_WRITE(GEN7_UCGCTL4,
8787 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07008788
Ville Syrjäläafd58e72014-01-22 21:33:03 +02008789 /*
8790 * BSpec says this must be set, even though
8791 * WaDisable4x2SubspanOptimization isn't listed for VLV.
8792 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02008793 I915_WRITE(CACHE_MODE_1,
8794 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07008795
8796 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02008797 * BSpec recommends 8x4 when MSAA is used,
8798 * however in practice 16x4 seems fastest.
8799 *
8800 * Note that PS/WM thread counts depend on the WIZ hashing
8801 * disable bit, which we don't touch here, but it's good
8802 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
8803 */
8804 I915_WRITE(GEN7_GT_MODE,
8805 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
8806
8807 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02008808 * WaIncreaseL3CreditsForVLVB0:vlv
8809 * This is the hardware default actually.
8810 */
8811 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
8812
8813 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008814 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07008815 * Disable clock gating on th GCFG unit to prevent a delay
8816 * in the reporting of vblank events.
8817 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02008818 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008819}
8820
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008821static void chv_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03008822{
Ville Syrjälä232ce332014-04-09 13:28:35 +03008823 /* WaVSRefCountFullforceMissDisable:chv */
8824 /* WaDSRefCountFullforceMissDisable:chv */
8825 I915_WRITE(GEN7_FF_THREAD_MODE,
8826 I915_READ(GEN7_FF_THREAD_MODE) &
8827 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03008828
8829 /* WaDisableSemaphoreAndSyncFlipWait:chv */
8830 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
8831 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03008832
8833 /* WaDisableCSUnitClockGating:chv */
8834 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
8835 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03008836
8837 /* WaDisableSDEUnitClockGating:chv */
8838 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8839 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008840
8841 /*
Imre Deak450174f2016-05-03 15:54:21 +03008842 * WaProgramL3SqcReg1Default:chv
8843 * See gfxspecs/Related Documents/Performance Guide/
8844 * LSQC Setting Recommendations.
8845 */
8846 gen8_set_l3sqc_credits(dev_priv, 38, 2);
8847
8848 /*
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008849 * GTT cache may not work with big pages, so if those
8850 * are ever enabled GTT cache may need to be disabled.
8851 */
8852 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03008853}
8854
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008855static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008856{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008857 uint32_t dspclk_gate;
8858
8859 I915_WRITE(RENCLK_GATE_D1, 0);
8860 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
8861 GS_UNIT_CLOCK_GATE_DISABLE |
8862 CL_UNIT_CLOCK_GATE_DISABLE);
8863 I915_WRITE(RAMCLK_GATE_D, 0);
8864 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
8865 OVRUNIT_CLOCK_GATE_DISABLE |
8866 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008867 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008868 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
8869 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02008870
8871 /* WaDisableRenderCachePipelinedFlush */
8872 I915_WRITE(CACHE_MODE_0,
8873 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03008874
Akash Goel4e046322014-04-04 17:14:38 +05308875 /* WaDisable_RenderCache_OperationalFlush:g4x */
8876 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8877
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008878 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008879}
8880
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008881static void i965gm_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008882{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008883 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
8884 I915_WRITE(RENCLK_GATE_D2, 0);
8885 I915_WRITE(DSPCLK_GATE_D, 0);
8886 I915_WRITE(RAMCLK_GATE_D, 0);
8887 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03008888 I915_WRITE(MI_ARB_STATE,
8889 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05308890
8891 /* WaDisable_RenderCache_OperationalFlush:gen4 */
8892 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008893}
8894
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008895static void i965g_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008896{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008897 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
8898 I965_RCC_CLOCK_GATE_DISABLE |
8899 I965_RCPB_CLOCK_GATE_DISABLE |
8900 I965_ISC_CLOCK_GATE_DISABLE |
8901 I965_FBC_CLOCK_GATE_DISABLE);
8902 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03008903 I915_WRITE(MI_ARB_STATE,
8904 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05308905
8906 /* WaDisable_RenderCache_OperationalFlush:gen4 */
8907 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008908}
8909
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008910static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008911{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008912 u32 dstate = I915_READ(D_STATE);
8913
8914 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
8915 DSTATE_DOT_CLOCK_GATING;
8916 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01008917
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008918 if (IS_PINEVIEW(dev_priv))
Chris Wilson13a86b82012-04-24 14:51:43 +01008919 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02008920
8921 /* IIR "flip pending" means done if this bit is set */
8922 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02008923
8924 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02008925 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02008926
8927 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
8928 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03008929
8930 I915_WRITE(MI_ARB_STATE,
8931 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008932}
8933
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008934static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008935{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008936 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02008937
8938 /* interrupts should cause a wake up from C3 */
8939 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
8940 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03008941
8942 I915_WRITE(MEM_MODE,
8943 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008944}
8945
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008946static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008947{
Ville Syrjälä10383922014-08-15 01:21:54 +03008948 I915_WRITE(MEM_MODE,
8949 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
8950 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008951}
8952
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008953void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008954{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008955 dev_priv->display.init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008956}
8957
Ville Syrjälä712bf362016-10-31 22:37:23 +02008958void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03008959{
Ville Syrjälä712bf362016-10-31 22:37:23 +02008960 if (HAS_PCH_LPT(dev_priv))
8961 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03008962}
8963
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008964static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02008965{
8966 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
8967}
8968
8969/**
8970 * intel_init_clock_gating_hooks - setup the clock gating hooks
8971 * @dev_priv: device private
8972 *
8973 * Setup the hooks that configure which clocks of a given platform can be
8974 * gated and also apply various GT and display specific workarounds for these
8975 * platforms. Note that some GT specific workarounds are applied separately
8976 * when GPU contexts or batchbuffers start their execution.
8977 */
8978void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
8979{
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008980 if (IS_CANNONLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008981 dev_priv->display.init_clock_gating = cnl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008982 else if (IS_COFFEELAKE(dev_priv))
8983 dev_priv->display.init_clock_gating = cfl_init_clock_gating;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008984 else if (IS_SKYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008985 dev_priv->display.init_clock_gating = skl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008986 else if (IS_KABYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008987 dev_priv->display.init_clock_gating = kbl_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02008988 else if (IS_BROXTON(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02008989 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02008990 else if (IS_GEMINILAKE(dev_priv))
8991 dev_priv->display.init_clock_gating = glk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008992 else if (IS_BROADWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008993 dev_priv->display.init_clock_gating = bdw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008994 else if (IS_CHERRYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008995 dev_priv->display.init_clock_gating = chv_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008996 else if (IS_HASWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008997 dev_priv->display.init_clock_gating = hsw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008998 else if (IS_IVYBRIDGE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008999 dev_priv->display.init_clock_gating = ivb_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02009000 else if (IS_VALLEYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009001 dev_priv->display.init_clock_gating = vlv_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02009002 else if (IS_GEN6(dev_priv))
9003 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
9004 else if (IS_GEN5(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009005 dev_priv->display.init_clock_gating = ilk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02009006 else if (IS_G4X(dev_priv))
9007 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02009008 else if (IS_I965GM(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009009 dev_priv->display.init_clock_gating = i965gm_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02009010 else if (IS_I965G(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07009011 dev_priv->display.init_clock_gating = i965g_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02009012 else if (IS_GEN3(dev_priv))
9013 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
9014 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
9015 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
9016 else if (IS_GEN2(dev_priv))
9017 dev_priv->display.init_clock_gating = i830_init_clock_gating;
9018 else {
9019 MISSING_CASE(INTEL_DEVID(dev_priv));
9020 dev_priv->display.init_clock_gating = nop_init_clock_gating;
9021 }
9022}
9023
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009024/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02009025void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009026{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02009027 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009028
Daniel Vetterc921aba2012-04-26 23:28:17 +02009029 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02009030 if (IS_PINEVIEW(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02009031 i915_pineview_get_mem_freq(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01009032 else if (IS_GEN5(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02009033 i915_ironlake_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02009034
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009035 /* For FIFO watermark updates */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02009036 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02009037 skl_setup_wm_latency(dev_priv);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01009038 dev_priv->display.initial_watermarks = skl_initial_wm;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01009039 dev_priv->display.atomic_update_watermarks = skl_atomic_update_crtc_wm;
Matt Roper98d39492016-05-12 07:06:03 -07009040 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01009041 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02009042 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03009043
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01009044 if ((IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02009045 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01009046 (!IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02009047 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07009048 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08009049 dev_priv->display.compute_intermediate_wm =
9050 ilk_compute_intermediate_wm;
9051 dev_priv->display.initial_watermarks =
9052 ilk_initial_watermarks;
9053 dev_priv->display.optimize_watermarks =
9054 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02009055 } else {
9056 DRM_DEBUG_KMS("Failed to read display plane latency. "
9057 "Disable CxSR\n");
9058 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02009059 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02009060 vlv_setup_wm_latency(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02009061 dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
Ville Syrjälä4841da52017-03-02 19:14:59 +02009062 dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02009063 dev_priv->display.initial_watermarks = vlv_initial_watermarks;
Ville Syrjälä4841da52017-03-02 19:14:59 +02009064 dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
Ville Syrjäläff32c542017-03-02 19:14:57 +02009065 dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03009066 } else if (IS_G4X(dev_priv)) {
9067 g4x_setup_wm_latency(dev_priv);
9068 dev_priv->display.compute_pipe_wm = g4x_compute_pipe_wm;
9069 dev_priv->display.compute_intermediate_wm = g4x_compute_intermediate_wm;
9070 dev_priv->display.initial_watermarks = g4x_initial_watermarks;
9071 dev_priv->display.optimize_watermarks = g4x_optimize_watermarks;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02009072 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01009073 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009074 dev_priv->is_ddr3,
9075 dev_priv->fsb_freq,
9076 dev_priv->mem_freq)) {
9077 DRM_INFO("failed to find known CxSR latency "
9078 "(found ddr%s fsb freq %d, mem freq %d), "
9079 "disabling CxSR\n",
9080 (dev_priv->is_ddr3 == 1) ? "3" : "2",
9081 dev_priv->fsb_freq, dev_priv->mem_freq);
9082 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03009083 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009084 dev_priv->display.update_wm = NULL;
9085 } else
9086 dev_priv->display.update_wm = pineview_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01009087 } else if (IS_GEN4(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009088 dev_priv->display.update_wm = i965_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01009089 } else if (IS_GEN3(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009090 dev_priv->display.update_wm = i9xx_update_wm;
9091 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01009092 } else if (IS_GEN2(dev_priv)) {
Ville Syrjälä62d75df2016-10-31 22:37:25 +02009093 if (INTEL_INFO(dev_priv)->num_pipes == 1) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009094 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009095 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009096 } else {
9097 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009098 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009099 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009100 } else {
9101 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009102 }
9103}
9104
Lyude87660502016-08-17 15:55:53 -04009105static inline int gen6_check_mailbox_status(struct drm_i915_private *dev_priv)
9106{
9107 uint32_t flags =
9108 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
9109
9110 switch (flags) {
9111 case GEN6_PCODE_SUCCESS:
9112 return 0;
9113 case GEN6_PCODE_UNIMPLEMENTED_CMD:
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009114 return -ENODEV;
Lyude87660502016-08-17 15:55:53 -04009115 case GEN6_PCODE_ILLEGAL_CMD:
9116 return -ENXIO;
9117 case GEN6_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Chris Wilson7850d1c2016-08-26 11:59:26 +01009118 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Lyude87660502016-08-17 15:55:53 -04009119 return -EOVERFLOW;
9120 case GEN6_PCODE_TIMEOUT:
9121 return -ETIMEDOUT;
9122 default:
Michal Wajdeczkof0d66152017-03-28 08:45:12 +00009123 MISSING_CASE(flags);
Lyude87660502016-08-17 15:55:53 -04009124 return 0;
9125 }
9126}
9127
9128static inline int gen7_check_mailbox_status(struct drm_i915_private *dev_priv)
9129{
9130 uint32_t flags =
9131 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
9132
9133 switch (flags) {
9134 case GEN6_PCODE_SUCCESS:
9135 return 0;
9136 case GEN6_PCODE_ILLEGAL_CMD:
9137 return -ENXIO;
9138 case GEN7_PCODE_TIMEOUT:
9139 return -ETIMEDOUT;
9140 case GEN7_PCODE_ILLEGAL_DATA:
9141 return -EINVAL;
9142 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
9143 return -EOVERFLOW;
9144 default:
9145 MISSING_CASE(flags);
9146 return 0;
9147 }
9148}
9149
Tom O'Rourke151a49d2014-11-13 18:50:10 -08009150int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07009151{
Lyude87660502016-08-17 15:55:53 -04009152 int status;
9153
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01009154 WARN_ON(!mutex_is_locked(&dev_priv->pcu_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07009155
Chris Wilson3f5582d2016-06-30 15:32:45 +01009156 /* GEN6_PCODE_* are outside of the forcewake domain, we can
9157 * use te fw I915_READ variants to reduce the amount of work
9158 * required when reading/writing.
9159 */
9160
9161 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009162 DRM_DEBUG_DRIVER("warning: pcode (read from mbox %x) mailbox access failed for %ps\n",
9163 mbox, __builtin_return_address(0));
Ben Widawsky42c05262012-09-26 10:34:00 -07009164 return -EAGAIN;
9165 }
9166
Chris Wilson3f5582d2016-06-30 15:32:45 +01009167 I915_WRITE_FW(GEN6_PCODE_DATA, *val);
9168 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
9169 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07009170
Chris Wilsone09a3032017-04-11 11:13:39 +01009171 if (__intel_wait_for_register_fw(dev_priv,
9172 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
9173 500, 0, NULL)) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009174 DRM_ERROR("timeout waiting for pcode read (from mbox %x) to finish for %ps\n",
9175 mbox, __builtin_return_address(0));
Ben Widawsky42c05262012-09-26 10:34:00 -07009176 return -ETIMEDOUT;
9177 }
9178
Chris Wilson3f5582d2016-06-30 15:32:45 +01009179 *val = I915_READ_FW(GEN6_PCODE_DATA);
9180 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07009181
Lyude87660502016-08-17 15:55:53 -04009182 if (INTEL_GEN(dev_priv) > 6)
9183 status = gen7_check_mailbox_status(dev_priv);
9184 else
9185 status = gen6_check_mailbox_status(dev_priv);
9186
9187 if (status) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009188 DRM_DEBUG_DRIVER("warning: pcode (read from mbox %x) mailbox access failed for %ps: %d\n",
9189 mbox, __builtin_return_address(0), status);
Lyude87660502016-08-17 15:55:53 -04009190 return status;
9191 }
9192
Ben Widawsky42c05262012-09-26 10:34:00 -07009193 return 0;
9194}
9195
Chris Wilson3f5582d2016-06-30 15:32:45 +01009196int sandybridge_pcode_write(struct drm_i915_private *dev_priv,
Lyude87660502016-08-17 15:55:53 -04009197 u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07009198{
Lyude87660502016-08-17 15:55:53 -04009199 int status;
9200
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01009201 WARN_ON(!mutex_is_locked(&dev_priv->pcu_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07009202
Chris Wilson3f5582d2016-06-30 15:32:45 +01009203 /* GEN6_PCODE_* are outside of the forcewake domain, we can
9204 * use te fw I915_READ variants to reduce the amount of work
9205 * required when reading/writing.
9206 */
9207
9208 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009209 DRM_DEBUG_DRIVER("warning: pcode (write of 0x%08x to mbox %x) mailbox access failed for %ps\n",
9210 val, mbox, __builtin_return_address(0));
Ben Widawsky42c05262012-09-26 10:34:00 -07009211 return -EAGAIN;
9212 }
9213
Chris Wilson3f5582d2016-06-30 15:32:45 +01009214 I915_WRITE_FW(GEN6_PCODE_DATA, val);
Imre Deak8bf41b72016-11-28 17:29:27 +02009215 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
Chris Wilson3f5582d2016-06-30 15:32:45 +01009216 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07009217
Chris Wilsone09a3032017-04-11 11:13:39 +01009218 if (__intel_wait_for_register_fw(dev_priv,
9219 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
9220 500, 0, NULL)) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009221 DRM_ERROR("timeout waiting for pcode write of 0x%08x to mbox %x to finish for %ps\n",
9222 val, mbox, __builtin_return_address(0));
Ben Widawsky42c05262012-09-26 10:34:00 -07009223 return -ETIMEDOUT;
9224 }
9225
Chris Wilson3f5582d2016-06-30 15:32:45 +01009226 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07009227
Lyude87660502016-08-17 15:55:53 -04009228 if (INTEL_GEN(dev_priv) > 6)
9229 status = gen7_check_mailbox_status(dev_priv);
9230 else
9231 status = gen6_check_mailbox_status(dev_priv);
9232
9233 if (status) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009234 DRM_DEBUG_DRIVER("warning: pcode (write of 0x%08x to mbox %x) mailbox access failed for %ps: %d\n",
9235 val, mbox, __builtin_return_address(0), status);
Lyude87660502016-08-17 15:55:53 -04009236 return status;
9237 }
9238
Ben Widawsky42c05262012-09-26 10:34:00 -07009239 return 0;
9240}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07009241
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009242static bool skl_pcode_try_request(struct drm_i915_private *dev_priv, u32 mbox,
9243 u32 request, u32 reply_mask, u32 reply,
9244 u32 *status)
9245{
9246 u32 val = request;
9247
9248 *status = sandybridge_pcode_read(dev_priv, mbox, &val);
9249
9250 return *status || ((val & reply_mask) == reply);
9251}
9252
9253/**
9254 * skl_pcode_request - send PCODE request until acknowledgment
9255 * @dev_priv: device private
9256 * @mbox: PCODE mailbox ID the request is targeted for
9257 * @request: request ID
9258 * @reply_mask: mask used to check for request acknowledgment
9259 * @reply: value used to check for request acknowledgment
9260 * @timeout_base_ms: timeout for polling with preemption enabled
9261 *
9262 * Keep resending the @request to @mbox until PCODE acknowledges it, PCODE
Imre Deak01299362017-02-24 16:32:10 +02009263 * reports an error or an overall timeout of @timeout_base_ms+50 ms expires.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009264 * The request is acknowledged once the PCODE reply dword equals @reply after
9265 * applying @reply_mask. Polling is first attempted with preemption enabled
Imre Deak01299362017-02-24 16:32:10 +02009266 * for @timeout_base_ms and if this times out for another 50 ms with
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009267 * preemption disabled.
9268 *
9269 * Returns 0 on success, %-ETIMEDOUT in case of a timeout, <0 in case of some
9270 * other error as reported by PCODE.
9271 */
9272int skl_pcode_request(struct drm_i915_private *dev_priv, u32 mbox, u32 request,
9273 u32 reply_mask, u32 reply, int timeout_base_ms)
9274{
9275 u32 status;
9276 int ret;
9277
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01009278 WARN_ON(!mutex_is_locked(&dev_priv->pcu_lock));
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009279
9280#define COND skl_pcode_try_request(dev_priv, mbox, request, reply_mask, reply, \
9281 &status)
9282
9283 /*
9284 * Prime the PCODE by doing a request first. Normally it guarantees
9285 * that a subsequent request, at most @timeout_base_ms later, succeeds.
9286 * _wait_for() doesn't guarantee when its passed condition is evaluated
9287 * first, so send the first request explicitly.
9288 */
9289 if (COND) {
9290 ret = 0;
9291 goto out;
9292 }
9293 ret = _wait_for(COND, timeout_base_ms * 1000, 10);
9294 if (!ret)
9295 goto out;
9296
9297 /*
9298 * The above can time out if the number of requests was low (2 in the
9299 * worst case) _and_ PCODE was busy for some reason even after a
9300 * (queued) request and @timeout_base_ms delay. As a workaround retry
9301 * the poll with preemption disabled to maximize the number of
Imre Deak01299362017-02-24 16:32:10 +02009302 * requests. Increase the timeout from @timeout_base_ms to 50ms to
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009303 * account for interrupts that could reduce the number of these
Imre Deak01299362017-02-24 16:32:10 +02009304 * requests, and for any quirks of the PCODE firmware that delays
9305 * the request completion.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009306 */
9307 DRM_DEBUG_KMS("PCODE timeout, retrying with preemption disabled\n");
9308 WARN_ON_ONCE(timeout_base_ms > 3);
9309 preempt_disable();
Imre Deak01299362017-02-24 16:32:10 +02009310 ret = wait_for_atomic(COND, 50);
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009311 preempt_enable();
9312
9313out:
9314 return ret ? ret : status;
9315#undef COND
9316}
9317
Ville Syrjälädd06f882014-11-10 22:55:12 +02009318static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
9319{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009320 struct intel_rps *rps = &dev_priv->gt_pm.rps;
9321
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009322 /*
9323 * N = val - 0xb7
9324 * Slow = Fast = GPLL ref * N
9325 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009326 return DIV_ROUND_CLOSEST(rps->gpll_ref_freq * (val - 0xb7), 1000);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009327}
9328
Fengguang Wub55dd642014-07-12 11:21:39 +02009329static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009330{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009331 struct intel_rps *rps = &dev_priv->gt_pm.rps;
9332
9333 return DIV_ROUND_CLOSEST(1000 * val, rps->gpll_ref_freq) + 0xb7;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009334}
9335
Fengguang Wub55dd642014-07-12 11:21:39 +02009336static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05309337{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009338 struct intel_rps *rps = &dev_priv->gt_pm.rps;
9339
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009340 /*
9341 * N = val / 2
9342 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
9343 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009344 return DIV_ROUND_CLOSEST(rps->gpll_ref_freq * val, 2 * 2 * 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05309345}
9346
Fengguang Wub55dd642014-07-12 11:21:39 +02009347static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05309348{
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009349 struct intel_rps *rps = &dev_priv->gt_pm.rps;
9350
Ville Syrjälä1c147622014-08-18 14:42:43 +03009351 /* CHV needs even values */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009352 return DIV_ROUND_CLOSEST(2 * 1000 * val, rps->gpll_ref_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05309353}
9354
Ville Syrjälä616bc822015-01-23 21:04:25 +02009355int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
9356{
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07009357 if (INTEL_GEN(dev_priv) >= 9)
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009358 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
9359 GEN9_FREQ_SCALER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009360 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009361 return chv_gpu_freq(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009362 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009363 return byt_gpu_freq(dev_priv, val);
9364 else
9365 return val * GT_FREQUENCY_MULTIPLIER;
9366}
9367
Ville Syrjälä616bc822015-01-23 21:04:25 +02009368int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
9369{
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07009370 if (INTEL_GEN(dev_priv) >= 9)
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009371 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
9372 GT_FREQUENCY_MULTIPLIER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009373 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009374 return chv_freq_opcode(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009375 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009376 return byt_freq_opcode(dev_priv, val);
9377 else
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009378 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
Deepak S22b1b2f2014-07-12 14:54:33 +05309379}
9380
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00009381void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01009382{
Sagar Arun Kamble9f817502017-10-10 22:30:05 +01009383 mutex_init(&dev_priv->pcu_lock);
Daniel Vetterf742a552013-12-06 10:17:53 +01009384
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +01009385 atomic_set(&dev_priv->gt_pm.rps.num_waiters, 0);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03009386
Sagar Arun Kamblead1443f2017-10-10 22:30:04 +01009387 dev_priv->runtime_pm.suspended = false;
9388 atomic_set(&dev_priv->runtime_pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01009389}
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009390
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009391static u64 vlv_residency_raw(struct drm_i915_private *dev_priv,
9392 const i915_reg_t reg)
9393{
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009394 u32 lower, upper, tmp;
Chris Wilson71cc2b12017-03-24 16:54:18 +00009395 int loop = 2;
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009396
9397 /* The register accessed do not need forcewake. We borrow
9398 * uncore lock to prevent concurrent access to range reg.
9399 */
9400 spin_lock_irq(&dev_priv->uncore.lock);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009401
9402 /* vlv and chv residency counters are 40 bits in width.
9403 * With a control bit, we can choose between upper or lower
9404 * 32bit window into this counter.
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009405 *
9406 * Although we always use the counter in high-range mode elsewhere,
9407 * userspace may attempt to read the value before rc6 is initialised,
9408 * before we have set the default VLV_COUNTER_CONTROL value. So always
9409 * set the high bit to be safe.
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009410 */
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009411 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9412 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009413 upper = I915_READ_FW(reg);
9414 do {
9415 tmp = upper;
9416
9417 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9418 _MASKED_BIT_DISABLE(VLV_COUNT_RANGE_HIGH));
9419 lower = I915_READ_FW(reg);
9420
9421 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9422 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
9423 upper = I915_READ_FW(reg);
Chris Wilson71cc2b12017-03-24 16:54:18 +00009424 } while (upper != tmp && --loop);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009425
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009426 /* Everywhere else we always use VLV_COUNTER_CONTROL with the
9427 * VLV_COUNT_RANGE_HIGH bit set - so it is safe to leave it set
9428 * now.
9429 */
9430
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009431 spin_unlock_irq(&dev_priv->uncore.lock);
9432
9433 return lower | (u64)upper << 8;
9434}
9435
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009436u64 intel_rc6_residency_us(struct drm_i915_private *dev_priv,
9437 const i915_reg_t reg)
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009438{
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009439 u64 time_hw, units, div;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009440
Sagar Arun Kamble771decb2017-10-10 22:30:07 +01009441 if (!intel_rc6_enabled())
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009442 return 0;
9443
9444 intel_runtime_pm_get(dev_priv);
9445
9446 /* On VLV and CHV, residency time is in CZ units rather than 1.28us */
9447 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009448 units = 1000;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009449 div = dev_priv->czclk_freq;
9450
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009451 time_hw = vlv_residency_raw(dev_priv, reg);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009452 } else if (IS_GEN9_LP(dev_priv)) {
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009453 units = 1000;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009454 div = 1200; /* 833.33ns */
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009455
9456 time_hw = I915_READ(reg);
9457 } else {
9458 units = 128000; /* 1.28us */
9459 div = 100000;
9460
9461 time_hw = I915_READ(reg);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009462 }
9463
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009464 intel_runtime_pm_put(dev_priv);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009465 return DIV_ROUND_UP_ULL(time_hw * units, div);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009466}