blob: fa9055a4f79024df90af0d58a4e2e025de773818 [file] [log] [blame]
Eugeni Dodonov85208be2012-04-16 22:20:34 -03001/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eugeni Dodonov <eugeni.dodonov@intel.com>
25 *
26 */
27
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -030028#include <linux/cpufreq.h>
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -070029#include <drm/drm_plane_helper.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030030#include "i915_drv.h"
31#include "intel_drv.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020032#include "../../../platform/x86/intel_ips.h"
33#include <linux/module.h>
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +020034#include <drm/drm_atomic_helper.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030035
Ben Widawskydc39fff2013-10-18 12:32:07 -070036/**
Jani Nikula18afd442016-01-18 09:19:48 +020037 * DOC: RC6
38 *
Ben Widawskydc39fff2013-10-18 12:32:07 -070039 * RC6 is a special power stage which allows the GPU to enter an very
40 * low-voltage mode when idle, using down to 0V while at this stage. This
41 * stage is entered automatically when the GPU is idle when RC6 support is
42 * enabled, and as soon as new workload arises GPU wakes up automatically as well.
43 *
44 * There are different RC6 modes available in Intel GPU, which differentiate
45 * among each other with the latency required to enter and leave RC6 and
46 * voltage consumed by the GPU in different states.
47 *
48 * The combination of the following flags define which states GPU is allowed
49 * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
50 * RC6pp is deepest RC6. Their support by hardware varies according to the
51 * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
52 * which brings the most power savings; deeper states save more power, but
53 * require higher latency to switch to and wake up.
54 */
55#define INTEL_RC6_ENABLE (1<<0)
56#define INTEL_RC6p_ENABLE (1<<1)
57#define INTEL_RC6pp_ENABLE (1<<2)
58
Ville Syrjälä46f16e62016-10-31 22:37:22 +020059static void gen9_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppalab033bb62016-06-07 17:19:04 +030060{
Rodrigo Vivi82525c12017-06-08 08:50:00 -070061 /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl,cfl */
Mika Kuoppalab033bb62016-06-07 17:19:04 +030062 I915_WRITE(CHICKEN_PAR1_1,
63 I915_READ(CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
64
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070065 /*
66 * Display WA#0390: skl,bxt,kbl,glk
67 *
68 * Must match Sampler, Pixel Back End, and Media
69 * (0xE194 bit 8, 0x7014 bit 13, 0x4DDC bits 27 and 31).
70 *
71 * Including bits outside the page in the hash would
72 * require 2 (or 4?) MiB alignment of resources. Just
73 * assume the defaul hashing mode which only uses bits
74 * within the page.
75 */
76 I915_WRITE(CHICKEN_PAR1_1,
77 I915_READ(CHICKEN_PAR1_1) & ~SKL_RC_HASH_OUTSIDE);
78
Mika Kuoppalab033bb62016-06-07 17:19:04 +030079 I915_WRITE(GEN8_CONFIG0,
80 I915_READ(GEN8_CONFIG0) | GEN9_DEFAULT_FIXES);
Mika Kuoppala590e8ff2016-06-07 17:19:13 +030081
Rodrigo Vivi82525c12017-06-08 08:50:00 -070082 /* WaEnableChickenDCPR:skl,bxt,kbl,glk,cfl */
Mika Kuoppala590e8ff2016-06-07 17:19:13 +030083 I915_WRITE(GEN8_CHICKEN_DCPR_1,
84 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
Mika Kuoppala0f78dee2016-06-07 17:19:16 +030085
Rodrigo Vivi82525c12017-06-08 08:50:00 -070086 /* WaFbcTurnOffFbcWatermark:skl,bxt,kbl,cfl */
87 /* WaFbcWakeMemOn:skl,bxt,kbl,glk,cfl */
Mika Kuoppala303d4ea2016-06-07 17:19:17 +030088 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
89 DISP_FBC_WM_DIS |
90 DISP_FBC_MEMORY_WAKE);
Mika Kuoppalad1b4eef2016-06-07 17:19:19 +030091
Rodrigo Vivi82525c12017-06-08 08:50:00 -070092 /* WaFbcHighMemBwCorruptionAvoidance:skl,bxt,kbl,cfl */
Mika Kuoppalad1b4eef2016-06-07 17:19:19 +030093 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
94 ILK_DPFC_DISABLE_DUMMY0);
Praveen Paneri32087d12017-08-03 23:02:10 +053095
96 if (IS_SKYLAKE(dev_priv)) {
97 /* WaDisableDopClockGating */
98 I915_WRITE(GEN7_MISCCPCTL, I915_READ(GEN7_MISCCPCTL)
99 & ~GEN7_DOP_CLOCK_GATE_ENABLE);
100 }
Mika Kuoppalab033bb62016-06-07 17:19:04 +0300101}
102
Ville Syrjälä46f16e62016-10-31 22:37:22 +0200103static void bxt_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deaka82abe42015-03-27 14:00:04 +0200104{
Ville Syrjälä46f16e62016-10-31 22:37:22 +0200105 gen9_init_clock_gating(dev_priv);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +0200106
Nick Hoatha7546152015-06-29 14:07:32 +0100107 /* WaDisableSDEUnitClockGating:bxt */
108 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
109 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
110
Imre Deak32608ca2015-03-11 11:10:27 +0200111 /*
112 * FIXME:
Ben Widawsky868434c2015-03-11 10:49:32 +0200113 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
Imre Deak32608ca2015-03-11 11:10:27 +0200114 */
Imre Deak32608ca2015-03-11 11:10:27 +0200115 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
Ben Widawsky868434c2015-03-11 10:49:32 +0200116 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
Imre Deakd965e7ac2015-12-01 10:23:52 +0200117
118 /*
119 * Wa: Backlight PWM may stop in the asserted state, causing backlight
120 * to stay fully on.
121 */
Jani Nikula8aeaf642017-02-15 17:21:37 +0200122 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
123 PWM1_GATING_DIS | PWM2_GATING_DIS);
Imre Deaka82abe42015-03-27 14:00:04 +0200124}
125
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200126static void glk_init_clock_gating(struct drm_i915_private *dev_priv)
127{
Rodrigo Vivi8f067832017-09-05 12:30:13 -0700128 u32 val;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200129 gen9_init_clock_gating(dev_priv);
130
131 /*
132 * WaDisablePWMClockGating:glk
133 * Backlight PWM may stop in the asserted state, causing backlight
134 * to stay fully on.
135 */
136 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
137 PWM1_GATING_DIS | PWM2_GATING_DIS);
Ander Conselvan de Oliveiraf4f4b592017-02-22 08:34:29 +0200138
139 /* WaDDIIOTimeout:glk */
140 if (IS_GLK_REVID(dev_priv, 0, GLK_REVID_A1)) {
141 u32 val = I915_READ(CHICKEN_MISC_2);
142 val &= ~(GLK_CL0_PWR_DOWN |
143 GLK_CL1_PWR_DOWN |
144 GLK_CL2_PWR_DOWN);
145 I915_WRITE(CHICKEN_MISC_2, val);
146 }
147
Rodrigo Vivi8f067832017-09-05 12:30:13 -0700148 /* Display WA #1133: WaFbcSkipSegments:glk */
149 val = I915_READ(ILK_DPFC_CHICKEN);
150 val &= ~GLK_SKIP_SEG_COUNT_MASK;
151 val |= GLK_SKIP_SEG_EN | GLK_SKIP_SEG_COUNT(1);
152 I915_WRITE(ILK_DPFC_CHICKEN, val);
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200153}
154
Ville Syrjälä148ac1f2016-10-31 22:37:16 +0200155static void i915_pineview_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200156{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200157 u32 tmp;
158
159 tmp = I915_READ(CLKCFG);
160
161 switch (tmp & CLKCFG_FSB_MASK) {
162 case CLKCFG_FSB_533:
163 dev_priv->fsb_freq = 533; /* 133*4 */
164 break;
165 case CLKCFG_FSB_800:
166 dev_priv->fsb_freq = 800; /* 200*4 */
167 break;
168 case CLKCFG_FSB_667:
169 dev_priv->fsb_freq = 667; /* 167*4 */
170 break;
171 case CLKCFG_FSB_400:
172 dev_priv->fsb_freq = 400; /* 100*4 */
173 break;
174 }
175
176 switch (tmp & CLKCFG_MEM_MASK) {
177 case CLKCFG_MEM_533:
178 dev_priv->mem_freq = 533;
179 break;
180 case CLKCFG_MEM_667:
181 dev_priv->mem_freq = 667;
182 break;
183 case CLKCFG_MEM_800:
184 dev_priv->mem_freq = 800;
185 break;
186 }
187
188 /* detect pineview DDR3 setting */
189 tmp = I915_READ(CSHRDDR3CTL);
190 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
191}
192
Ville Syrjälä148ac1f2016-10-31 22:37:16 +0200193static void i915_ironlake_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200194{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200195 u16 ddrpll, csipll;
196
197 ddrpll = I915_READ16(DDRMPLL1);
198 csipll = I915_READ16(CSIPLL0);
199
200 switch (ddrpll & 0xff) {
201 case 0xc:
202 dev_priv->mem_freq = 800;
203 break;
204 case 0x10:
205 dev_priv->mem_freq = 1066;
206 break;
207 case 0x14:
208 dev_priv->mem_freq = 1333;
209 break;
210 case 0x18:
211 dev_priv->mem_freq = 1600;
212 break;
213 default:
214 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
215 ddrpll & 0xff);
216 dev_priv->mem_freq = 0;
217 break;
218 }
219
Daniel Vetter20e4d402012-08-08 23:35:39 +0200220 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200221
222 switch (csipll & 0x3ff) {
223 case 0x00c:
224 dev_priv->fsb_freq = 3200;
225 break;
226 case 0x00e:
227 dev_priv->fsb_freq = 3733;
228 break;
229 case 0x010:
230 dev_priv->fsb_freq = 4266;
231 break;
232 case 0x012:
233 dev_priv->fsb_freq = 4800;
234 break;
235 case 0x014:
236 dev_priv->fsb_freq = 5333;
237 break;
238 case 0x016:
239 dev_priv->fsb_freq = 5866;
240 break;
241 case 0x018:
242 dev_priv->fsb_freq = 6400;
243 break;
244 default:
245 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
246 csipll & 0x3ff);
247 dev_priv->fsb_freq = 0;
248 break;
249 }
250
251 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200252 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200253 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200254 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200255 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200256 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200257 }
258}
259
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300260static const struct cxsr_latency cxsr_latency_table[] = {
261 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
262 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
263 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
264 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
265 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
266
267 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
268 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
269 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
270 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
271 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
272
273 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
274 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
275 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
276 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
277 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
278
279 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
280 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
281 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
282 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
283 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
284
285 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
286 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
287 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
288 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
289 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
290
291 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
292 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
293 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
294 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
295 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
296};
297
Tvrtko Ursulin44a655c2016-10-13 11:09:23 +0100298static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
299 bool is_ddr3,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300300 int fsb,
301 int mem)
302{
303 const struct cxsr_latency *latency;
304 int i;
305
306 if (fsb == 0 || mem == 0)
307 return NULL;
308
309 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
310 latency = &cxsr_latency_table[i];
311 if (is_desktop == latency->is_desktop &&
312 is_ddr3 == latency->is_ddr3 &&
313 fsb == latency->fsb_freq && mem == latency->mem_freq)
314 return latency;
315 }
316
317 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
318
319 return NULL;
320}
321
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200322static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
323{
324 u32 val;
325
326 mutex_lock(&dev_priv->rps.hw_lock);
327
328 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
329 if (enable)
330 val &= ~FORCE_DDR_HIGH_FREQ;
331 else
332 val |= FORCE_DDR_HIGH_FREQ;
333 val &= ~FORCE_DDR_LOW_FREQ;
334 val |= FORCE_DDR_FREQ_REQ_ACK;
335 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
336
337 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
338 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
339 DRM_ERROR("timed out waiting for Punit DDR DVFS request\n");
340
341 mutex_unlock(&dev_priv->rps.hw_lock);
342}
343
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200344static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
345{
346 u32 val;
347
348 mutex_lock(&dev_priv->rps.hw_lock);
349
350 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
351 if (enable)
352 val |= DSP_MAXFIFO_PM5_ENABLE;
353 else
354 val &= ~DSP_MAXFIFO_PM5_ENABLE;
355 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
356
357 mutex_unlock(&dev_priv->rps.hw_lock);
358}
359
Ville Syrjäläf4998962015-03-10 17:02:21 +0200360#define FW_WM(value, plane) \
361 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
362
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200363static bool _intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300364{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200365 bool was_enabled;
Imre Deak5209b1f2014-07-01 12:36:17 +0300366 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300367
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100368 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200369 was_enabled = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300370 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300371 POSTING_READ(FW_BLC_SELF_VLV);
Jani Nikulac0f86832016-12-07 12:13:04 +0200372 } else if (IS_G4X(dev_priv) || IS_I965GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200373 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300374 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300375 POSTING_READ(FW_BLC_SELF);
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +0200376 } else if (IS_PINEVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200377 val = I915_READ(DSPFW3);
378 was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
379 if (enable)
380 val |= PINEVIEW_SELF_REFRESH_EN;
381 else
382 val &= ~PINEVIEW_SELF_REFRESH_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300383 I915_WRITE(DSPFW3, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300384 POSTING_READ(DSPFW3);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100385 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200386 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300387 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
388 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
389 I915_WRITE(FW_BLC_SELF, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300390 POSTING_READ(FW_BLC_SELF);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100391 } else if (IS_I915GM(dev_priv)) {
Ville Syrjäläacb91352016-07-29 17:57:02 +0300392 /*
393 * FIXME can't find a bit like this for 915G, and
394 * and yet it does have the related watermark in
395 * FW_BLC_SELF. What's going on?
396 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200397 was_enabled = I915_READ(INSTPM) & INSTPM_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300398 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
399 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
400 I915_WRITE(INSTPM, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300401 POSTING_READ(INSTPM);
Imre Deak5209b1f2014-07-01 12:36:17 +0300402 } else {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200403 return false;
Imre Deak5209b1f2014-07-01 12:36:17 +0300404 }
405
Ville Syrjälä1489bba2017-03-02 19:15:07 +0200406 trace_intel_memory_cxsr(dev_priv, was_enabled, enable);
407
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200408 DRM_DEBUG_KMS("memory self-refresh is %s (was %s)\n",
409 enableddisabled(enable),
410 enableddisabled(was_enabled));
411
412 return was_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300413}
414
Ville Syrjälä62571fc2017-04-21 21:14:23 +0300415/**
416 * intel_set_memory_cxsr - Configure CxSR state
417 * @dev_priv: i915 device
418 * @enable: Allow vs. disallow CxSR
419 *
420 * Allow or disallow the system to enter a special CxSR
421 * (C-state self refresh) state. What typically happens in CxSR mode
422 * is that several display FIFOs may get combined into a single larger
423 * FIFO for a particular plane (so called max FIFO mode) to allow the
424 * system to defer memory fetches longer, and the memory will enter
425 * self refresh.
426 *
427 * Note that enabling CxSR does not guarantee that the system enter
428 * this special mode, nor does it guarantee that the system stays
429 * in that mode once entered. So this just allows/disallows the system
430 * to autonomously utilize the CxSR mode. Other factors such as core
431 * C-states will affect when/if the system actually enters/exits the
432 * CxSR mode.
433 *
434 * Note that on VLV/CHV this actually only controls the max FIFO mode,
435 * and the system is free to enter/exit memory self refresh at any time
436 * even when the use of CxSR has been disallowed.
437 *
438 * While the system is actually in the CxSR/max FIFO mode, some plane
439 * control registers will not get latched on vblank. Thus in order to
440 * guarantee the system will respond to changes in the plane registers
441 * we must always disallow CxSR prior to making changes to those registers.
442 * Unfortunately the system will re-evaluate the CxSR conditions at
443 * frame start which happens after vblank start (which is when the plane
444 * registers would get latched), so we can't proceed with the plane update
445 * during the same frame where we disallowed CxSR.
446 *
447 * Certain platforms also have a deeper HPLL SR mode. Fortunately the
448 * HPLL SR mode depends on CxSR itself, so we don't have to hand hold
449 * the hardware w.r.t. HPLL SR when writing to plane registers.
450 * Disallowing just CxSR is sufficient.
451 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200452bool intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200453{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200454 bool ret;
455
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200456 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200457 ret = _intel_set_memory_cxsr(dev_priv, enable);
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300458 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
459 dev_priv->wm.vlv.cxsr = enable;
460 else if (IS_G4X(dev_priv))
461 dev_priv->wm.g4x.cxsr = enable;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200462 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200463
464 return ret;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200465}
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200466
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300467/*
468 * Latency for FIFO fetches is dependent on several factors:
469 * - memory configuration (speed, channels)
470 * - chipset
471 * - current MCH state
472 * It can be fairly high in some situations, so here we assume a fairly
473 * pessimal value. It's a tradeoff between extra memory fetches (if we
474 * set this value too high, the FIFO will fetch frequently to stay full)
475 * and power consumption (set it too low to save power and we might see
476 * FIFO underruns and display "flicker").
477 *
478 * A value of 5us seems to be a good balance; safe for very low end
479 * platforms but not overly aggressive on lower latency configs.
480 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100481static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300482
Ville Syrjäläb5004722015-03-05 21:19:47 +0200483#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
484 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
485
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200486static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
Ville Syrjäläb5004722015-03-05 21:19:47 +0200487{
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200488 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200489 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200490 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200491 enum pipe pipe = crtc->pipe;
492 int sprite0_start, sprite1_start;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200493
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200494 switch (pipe) {
Ville Syrjäläb5004722015-03-05 21:19:47 +0200495 uint32_t dsparb, dsparb2, dsparb3;
496 case PIPE_A:
497 dsparb = I915_READ(DSPARB);
498 dsparb2 = I915_READ(DSPARB2);
499 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
500 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
501 break;
502 case PIPE_B:
503 dsparb = I915_READ(DSPARB);
504 dsparb2 = I915_READ(DSPARB2);
505 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
506 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
507 break;
508 case PIPE_C:
509 dsparb2 = I915_READ(DSPARB2);
510 dsparb3 = I915_READ(DSPARB3);
511 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
512 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
513 break;
514 default:
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200515 MISSING_CASE(pipe);
516 return;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200517 }
518
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200519 fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
520 fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
521 fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
522 fifo_state->plane[PLANE_CURSOR] = 63;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200523}
524
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200525static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300526{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300527 uint32_t dsparb = I915_READ(DSPARB);
528 int size;
529
530 size = dsparb & 0x7f;
531 if (plane)
532 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
533
534 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
535 plane ? "B" : "A", size);
536
537 return size;
538}
539
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200540static int i830_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300541{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300542 uint32_t dsparb = I915_READ(DSPARB);
543 int size;
544
545 size = dsparb & 0x1ff;
546 if (plane)
547 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
548 size >>= 1; /* Convert to cachelines */
549
550 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
551 plane ? "B" : "A", size);
552
553 return size;
554}
555
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200556static int i845_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300557{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300558 uint32_t dsparb = I915_READ(DSPARB);
559 int size;
560
561 size = dsparb & 0x7f;
562 size >>= 2; /* Convert to cachelines */
563
564 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
565 plane ? "B" : "A",
566 size);
567
568 return size;
569}
570
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300571/* Pineview has different values for various configs */
572static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300573 .fifo_size = PINEVIEW_DISPLAY_FIFO,
574 .max_wm = PINEVIEW_MAX_WM,
575 .default_wm = PINEVIEW_DFT_WM,
576 .guard_size = PINEVIEW_GUARD_WM,
577 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300578};
579static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300580 .fifo_size = PINEVIEW_DISPLAY_FIFO,
581 .max_wm = PINEVIEW_MAX_WM,
582 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
583 .guard_size = PINEVIEW_GUARD_WM,
584 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300585};
586static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300587 .fifo_size = PINEVIEW_CURSOR_FIFO,
588 .max_wm = PINEVIEW_CURSOR_MAX_WM,
589 .default_wm = PINEVIEW_CURSOR_DFT_WM,
590 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
591 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300592};
593static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300594 .fifo_size = PINEVIEW_CURSOR_FIFO,
595 .max_wm = PINEVIEW_CURSOR_MAX_WM,
596 .default_wm = PINEVIEW_CURSOR_DFT_WM,
597 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
598 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300599};
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300600static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300601 .fifo_size = I965_CURSOR_FIFO,
602 .max_wm = I965_CURSOR_MAX_WM,
603 .default_wm = I965_CURSOR_DFT_WM,
604 .guard_size = 2,
605 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300606};
607static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300608 .fifo_size = I945_FIFO_SIZE,
609 .max_wm = I915_MAX_WM,
610 .default_wm = 1,
611 .guard_size = 2,
612 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300613};
614static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300615 .fifo_size = I915_FIFO_SIZE,
616 .max_wm = I915_MAX_WM,
617 .default_wm = 1,
618 .guard_size = 2,
619 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300620};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300621static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300622 .fifo_size = I855GM_FIFO_SIZE,
623 .max_wm = I915_MAX_WM,
624 .default_wm = 1,
625 .guard_size = 2,
626 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300627};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300628static const struct intel_watermark_params i830_bc_wm_info = {
629 .fifo_size = I855GM_FIFO_SIZE,
630 .max_wm = I915_MAX_WM/2,
631 .default_wm = 1,
632 .guard_size = 2,
633 .cacheline_size = I830_FIFO_LINE_SIZE,
634};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200635static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300636 .fifo_size = I830_FIFO_SIZE,
637 .max_wm = I915_MAX_WM,
638 .default_wm = 1,
639 .guard_size = 2,
640 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300641};
642
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300643/**
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300644 * intel_wm_method1 - Method 1 / "small buffer" watermark formula
645 * @pixel_rate: Pipe pixel rate in kHz
646 * @cpp: Plane bytes per pixel
647 * @latency: Memory wakeup latency in 0.1us units
648 *
649 * Compute the watermark using the method 1 or "small buffer"
650 * formula. The caller may additonally add extra cachelines
651 * to account for TLB misses and clock crossings.
652 *
653 * This method is concerned with the short term drain rate
654 * of the FIFO, ie. it does not account for blanking periods
655 * which would effectively reduce the average drain rate across
656 * a longer period. The name "small" refers to the fact the
657 * FIFO is relatively small compared to the amount of data
658 * fetched.
659 *
660 * The FIFO level vs. time graph might look something like:
661 *
662 * |\ |\
663 * | \ | \
664 * __---__---__ (- plane active, _ blanking)
665 * -> time
666 *
667 * or perhaps like this:
668 *
669 * |\|\ |\|\
670 * __----__----__ (- plane active, _ blanking)
671 * -> time
672 *
673 * Returns:
674 * The watermark in bytes
675 */
676static unsigned int intel_wm_method1(unsigned int pixel_rate,
677 unsigned int cpp,
678 unsigned int latency)
679{
680 uint64_t ret;
681
682 ret = (uint64_t) pixel_rate * cpp * latency;
683 ret = DIV_ROUND_UP_ULL(ret, 10000);
684
685 return ret;
686}
687
688/**
689 * intel_wm_method2 - Method 2 / "large buffer" watermark formula
690 * @pixel_rate: Pipe pixel rate in kHz
691 * @htotal: Pipe horizontal total
692 * @width: Plane width in pixels
693 * @cpp: Plane bytes per pixel
694 * @latency: Memory wakeup latency in 0.1us units
695 *
696 * Compute the watermark using the method 2 or "large buffer"
697 * formula. The caller may additonally add extra cachelines
698 * to account for TLB misses and clock crossings.
699 *
700 * This method is concerned with the long term drain rate
701 * of the FIFO, ie. it does account for blanking periods
702 * which effectively reduce the average drain rate across
703 * a longer period. The name "large" refers to the fact the
704 * FIFO is relatively large compared to the amount of data
705 * fetched.
706 *
707 * The FIFO level vs. time graph might look something like:
708 *
709 * |\___ |\___
710 * | \___ | \___
711 * | \ | \
712 * __ --__--__--__--__--__--__ (- plane active, _ blanking)
713 * -> time
714 *
715 * Returns:
716 * The watermark in bytes
717 */
718static unsigned int intel_wm_method2(unsigned int pixel_rate,
719 unsigned int htotal,
720 unsigned int width,
721 unsigned int cpp,
722 unsigned int latency)
723{
724 unsigned int ret;
725
726 /*
727 * FIXME remove once all users are computing
728 * watermarks in the correct place.
729 */
730 if (WARN_ON_ONCE(htotal == 0))
731 htotal = 1;
732
733 ret = (latency * pixel_rate) / (htotal * 10000);
734 ret = (ret + 1) * width * cpp;
735
736 return ret;
737}
738
739/**
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300740 * intel_calculate_wm - calculate watermark level
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300741 * @pixel_rate: pixel clock
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300742 * @wm: chip FIFO params
Ville Syrjäläac484962016-01-20 21:05:26 +0200743 * @cpp: bytes per pixel
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300744 * @latency_ns: memory latency for the platform
745 *
746 * Calculate the watermark level (the level at which the display plane will
747 * start fetching from memory again). Each chip has a different display
748 * FIFO size and allocation, so the caller needs to figure that out and pass
749 * in the correct intel_watermark_params structure.
750 *
751 * As the pixel clock runs, the FIFO will be drained at a rate that depends
752 * on the pixel size. When it reaches the watermark level, it'll start
753 * fetching FIFO line sized based chunks from memory until the FIFO fills
754 * past the watermark point. If the FIFO drains completely, a FIFO underrun
755 * will occur, and a display engine hang could result.
756 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300757static unsigned int intel_calculate_wm(int pixel_rate,
758 const struct intel_watermark_params *wm,
759 int fifo_size, int cpp,
760 unsigned int latency_ns)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300761{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300762 int entries, wm_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300763
764 /*
765 * Note: we need to make sure we don't overflow for various clock &
766 * latency values.
767 * clocks go from a few thousand to several hundred thousand.
768 * latency is usually a few thousand
769 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300770 entries = intel_wm_method1(pixel_rate, cpp,
771 latency_ns / 100);
772 entries = DIV_ROUND_UP(entries, wm->cacheline_size) +
773 wm->guard_size;
774 DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300775
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300776 wm_size = fifo_size - entries;
777 DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300778
779 /* Don't promote wm_size to unsigned... */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300780 if (wm_size > wm->max_wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300781 wm_size = wm->max_wm;
782 if (wm_size <= 0)
783 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300784
785 /*
786 * Bspec seems to indicate that the value shouldn't be lower than
787 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
788 * Lets go for 8 which is the burst size since certain platforms
789 * already use a hardcoded 8 (which is what the spec says should be
790 * done).
791 */
792 if (wm_size <= 8)
793 wm_size = 8;
794
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300795 return wm_size;
796}
797
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300798static bool is_disabling(int old, int new, int threshold)
799{
800 return old >= threshold && new < threshold;
801}
802
803static bool is_enabling(int old, int new, int threshold)
804{
805 return old < threshold && new >= threshold;
806}
807
Ville Syrjälä6d5019b2017-04-21 21:14:20 +0300808static int intel_wm_num_levels(struct drm_i915_private *dev_priv)
809{
810 return dev_priv->wm.max_level + 1;
811}
812
Ville Syrjälä24304d812017-03-14 17:10:49 +0200813static bool intel_wm_plane_visible(const struct intel_crtc_state *crtc_state,
814 const struct intel_plane_state *plane_state)
815{
816 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
817
818 /* FIXME check the 'enable' instead */
819 if (!crtc_state->base.active)
820 return false;
821
822 /*
823 * Treat cursor with fb as always visible since cursor updates
824 * can happen faster than the vrefresh rate, and the current
825 * watermark code doesn't handle that correctly. Cursor updates
826 * which set/clear the fb or change the cursor size are going
827 * to get throttled by intel_legacy_cursor_update() to work
828 * around this problem with the watermark code.
829 */
830 if (plane->id == PLANE_CURSOR)
831 return plane_state->base.fb != NULL;
832 else
833 return plane_state->base.visible;
834}
835
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200836static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300837{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200838 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300839
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200840 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200841 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300842 if (enabled)
843 return NULL;
844 enabled = crtc;
845 }
846 }
847
848 return enabled;
849}
850
Ville Syrjälä432081b2016-10-31 22:37:03 +0200851static void pineview_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300852{
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200853 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200854 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300855 const struct cxsr_latency *latency;
856 u32 reg;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300857 unsigned int wm;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300858
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100859 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
860 dev_priv->is_ddr3,
861 dev_priv->fsb_freq,
862 dev_priv->mem_freq);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300863 if (!latency) {
864 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300865 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300866 return;
867 }
868
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200869 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300870 if (crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200871 const struct drm_display_mode *adjusted_mode =
872 &crtc->config->base.adjusted_mode;
873 const struct drm_framebuffer *fb =
874 crtc->base.primary->state->fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200875 int cpp = fb->format->cpp[0];
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300876 int clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300877
878 /* Display SR */
879 wm = intel_calculate_wm(clock, &pineview_display_wm,
880 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200881 cpp, latency->display_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300882 reg = I915_READ(DSPFW1);
883 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200884 reg |= FW_WM(wm, SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300885 I915_WRITE(DSPFW1, reg);
886 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
887
888 /* cursor SR */
889 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
890 pineview_display_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300891 4, latency->cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300892 reg = I915_READ(DSPFW3);
893 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200894 reg |= FW_WM(wm, CURSOR_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300895 I915_WRITE(DSPFW3, reg);
896
897 /* Display HPLL off SR */
898 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
899 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200900 cpp, latency->display_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300901 reg = I915_READ(DSPFW3);
902 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200903 reg |= FW_WM(wm, HPLL_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300904 I915_WRITE(DSPFW3, reg);
905
906 /* cursor HPLL off SR */
907 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
908 pineview_display_hplloff_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300909 4, latency->cursor_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300910 reg = I915_READ(DSPFW3);
911 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200912 reg |= FW_WM(wm, HPLL_CURSOR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300913 I915_WRITE(DSPFW3, reg);
914 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
915
Imre Deak5209b1f2014-07-01 12:36:17 +0300916 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300917 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300918 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300919 }
920}
921
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300922/*
923 * Documentation says:
924 * "If the line size is small, the TLB fetches can get in the way of the
925 * data fetches, causing some lag in the pixel data return which is not
926 * accounted for in the above formulas. The following adjustment only
927 * needs to be applied if eight whole lines fit in the buffer at once.
928 * The WM is adjusted upwards by the difference between the FIFO size
929 * and the size of 8 whole lines. This adjustment is always performed
930 * in the actual pixel depth regardless of whether FBC is enabled or not."
931 */
932static int g4x_tlb_miss_wa(int fifo_size, int width, int cpp)
933{
934 int tlb_miss = fifo_size * 64 - width * cpp * 8;
935
936 return max(0, tlb_miss);
937}
938
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300939static void g4x_write_wm_values(struct drm_i915_private *dev_priv,
940 const struct g4x_wm_values *wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300941{
Ville Syrjäläe93329a2017-04-21 21:14:31 +0300942 enum pipe pipe;
943
944 for_each_pipe(dev_priv, pipe)
945 trace_g4x_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
946
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300947 I915_WRITE(DSPFW1,
948 FW_WM(wm->sr.plane, SR) |
949 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
950 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
951 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
952 I915_WRITE(DSPFW2,
953 (wm->fbc_en ? DSPFW_FBC_SR_EN : 0) |
954 FW_WM(wm->sr.fbc, FBC_SR) |
955 FW_WM(wm->hpll.fbc, FBC_HPLL_SR) |
956 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEB) |
957 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
958 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
959 I915_WRITE(DSPFW3,
960 (wm->hpll_en ? DSPFW_HPLL_SR_EN : 0) |
961 FW_WM(wm->sr.cursor, CURSOR_SR) |
962 FW_WM(wm->hpll.cursor, HPLL_CURSOR) |
963 FW_WM(wm->hpll.plane, HPLL_SR));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300964
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300965 POSTING_READ(DSPFW1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300966}
967
Ville Syrjälä15665972015-03-10 16:16:28 +0200968#define FW_WM_VLV(value, plane) \
969 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
970
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200971static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200972 const struct vlv_wm_values *wm)
973{
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200974 enum pipe pipe;
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200975
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200976 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläc137d662017-03-02 19:15:06 +0200977 trace_vlv_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
978
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200979 I915_WRITE(VLV_DDL(pipe),
980 (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
981 (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
982 (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
983 (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
984 }
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200985
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +0200986 /*
987 * Zero the (unused) WM1 watermarks, and also clear all the
988 * high order bits so that there are no out of bounds values
989 * present in the registers during the reprogramming.
990 */
991 I915_WRITE(DSPHOWM, 0);
992 I915_WRITE(DSPHOWM1, 0);
993 I915_WRITE(DSPFW4, 0);
994 I915_WRITE(DSPFW5, 0);
995 I915_WRITE(DSPFW6, 0);
996
Ville Syrjäläae801522015-03-05 21:19:49 +0200997 I915_WRITE(DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +0200998 FW_WM(wm->sr.plane, SR) |
Ville Syrjälä1b313892016-11-28 19:37:08 +0200999 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
1000 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
1001 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Ville Syrjäläae801522015-03-05 21:19:49 +02001002 I915_WRITE(DSPFW2,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001003 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
1004 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
1005 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Ville Syrjäläae801522015-03-05 21:19:49 +02001006 I915_WRITE(DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +02001007 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +02001008
1009 if (IS_CHERRYVIEW(dev_priv)) {
1010 I915_WRITE(DSPFW7_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001011 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1012 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001013 I915_WRITE(DSPFW8_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001014 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
1015 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
Ville Syrjäläae801522015-03-05 21:19:49 +02001016 I915_WRITE(DSPFW9_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001017 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
1018 FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001019 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001020 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001021 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
1022 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
1023 FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
1024 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1025 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1026 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1027 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1028 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1029 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001030 } else {
1031 I915_WRITE(DSPFW7,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001032 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1033 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001034 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001035 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001036 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1037 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1038 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1039 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1040 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1041 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001042 }
1043
1044 POSTING_READ(DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001045}
1046
Ville Syrjälä15665972015-03-10 16:16:28 +02001047#undef FW_WM_VLV
1048
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001049static void g4x_setup_wm_latency(struct drm_i915_private *dev_priv)
1050{
1051 /* all latencies in usec */
1052 dev_priv->wm.pri_latency[G4X_WM_LEVEL_NORMAL] = 5;
1053 dev_priv->wm.pri_latency[G4X_WM_LEVEL_SR] = 12;
Ville Syrjälä79d94302017-04-21 21:14:30 +03001054 dev_priv->wm.pri_latency[G4X_WM_LEVEL_HPLL] = 35;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001055
Ville Syrjälä79d94302017-04-21 21:14:30 +03001056 dev_priv->wm.max_level = G4X_WM_LEVEL_HPLL;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001057}
1058
1059static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
1060{
1061 /*
1062 * DSPCNTR[13] supposedly controls whether the
1063 * primary plane can use the FIFO space otherwise
1064 * reserved for the sprite plane. It's not 100% clear
1065 * what the actual FIFO size is, but it looks like we
1066 * can happily set both primary and sprite watermarks
1067 * up to 127 cachelines. So that would seem to mean
1068 * that either DSPCNTR[13] doesn't do anything, or that
1069 * the total FIFO is >= 256 cachelines in size. Either
1070 * way, we don't seem to have to worry about this
1071 * repartitioning as the maximum watermark value the
1072 * register can hold for each plane is lower than the
1073 * minimum FIFO size.
1074 */
1075 switch (plane_id) {
1076 case PLANE_CURSOR:
1077 return 63;
1078 case PLANE_PRIMARY:
1079 return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
1080 case PLANE_SPRITE0:
1081 return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
1082 default:
1083 MISSING_CASE(plane_id);
1084 return 0;
1085 }
1086}
1087
1088static int g4x_fbc_fifo_size(int level)
1089{
1090 switch (level) {
1091 case G4X_WM_LEVEL_SR:
1092 return 7;
1093 case G4X_WM_LEVEL_HPLL:
1094 return 15;
1095 default:
1096 MISSING_CASE(level);
1097 return 0;
1098 }
1099}
1100
1101static uint16_t g4x_compute_wm(const struct intel_crtc_state *crtc_state,
1102 const struct intel_plane_state *plane_state,
1103 int level)
1104{
1105 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1106 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1107 const struct drm_display_mode *adjusted_mode =
1108 &crtc_state->base.adjusted_mode;
1109 int clock, htotal, cpp, width, wm;
1110 int latency = dev_priv->wm.pri_latency[level] * 10;
1111
1112 if (latency == 0)
1113 return USHRT_MAX;
1114
1115 if (!intel_wm_plane_visible(crtc_state, plane_state))
1116 return 0;
1117
1118 /*
1119 * Not 100% sure which way ELK should go here as the
1120 * spec only says CL/CTG should assume 32bpp and BW
1121 * doesn't need to. But as these things followed the
1122 * mobile vs. desktop lines on gen3 as well, let's
1123 * assume ELK doesn't need this.
1124 *
1125 * The spec also fails to list such a restriction for
1126 * the HPLL watermark, which seems a little strange.
1127 * Let's use 32bpp for the HPLL watermark as well.
1128 */
1129 if (IS_GM45(dev_priv) && plane->id == PLANE_PRIMARY &&
1130 level != G4X_WM_LEVEL_NORMAL)
1131 cpp = 4;
1132 else
1133 cpp = plane_state->base.fb->format->cpp[0];
1134
1135 clock = adjusted_mode->crtc_clock;
1136 htotal = adjusted_mode->crtc_htotal;
1137
1138 if (plane->id == PLANE_CURSOR)
1139 width = plane_state->base.crtc_w;
1140 else
1141 width = drm_rect_width(&plane_state->base.dst);
1142
1143 if (plane->id == PLANE_CURSOR) {
1144 wm = intel_wm_method2(clock, htotal, width, cpp, latency);
1145 } else if (plane->id == PLANE_PRIMARY &&
1146 level == G4X_WM_LEVEL_NORMAL) {
1147 wm = intel_wm_method1(clock, cpp, latency);
1148 } else {
1149 int small, large;
1150
1151 small = intel_wm_method1(clock, cpp, latency);
1152 large = intel_wm_method2(clock, htotal, width, cpp, latency);
1153
1154 wm = min(small, large);
1155 }
1156
1157 wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
1158 width, cpp);
1159
1160 wm = DIV_ROUND_UP(wm, 64) + 2;
1161
1162 return min_t(int, wm, USHRT_MAX);
1163}
1164
1165static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1166 int level, enum plane_id plane_id, u16 value)
1167{
1168 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1169 bool dirty = false;
1170
1171 for (; level < intel_wm_num_levels(dev_priv); level++) {
1172 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1173
1174 dirty |= raw->plane[plane_id] != value;
1175 raw->plane[plane_id] = value;
1176 }
1177
1178 return dirty;
1179}
1180
1181static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
1182 int level, u16 value)
1183{
1184 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1185 bool dirty = false;
1186
1187 /* NORMAL level doesn't have an FBC watermark */
1188 level = max(level, G4X_WM_LEVEL_SR);
1189
1190 for (; level < intel_wm_num_levels(dev_priv); level++) {
1191 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1192
1193 dirty |= raw->fbc != value;
1194 raw->fbc = value;
1195 }
1196
1197 return dirty;
1198}
1199
1200static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
1201 const struct intel_plane_state *pstate,
1202 uint32_t pri_val);
1203
1204static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1205 const struct intel_plane_state *plane_state)
1206{
1207 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1208 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
1209 enum plane_id plane_id = plane->id;
1210 bool dirty = false;
1211 int level;
1212
1213 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1214 dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1215 if (plane_id == PLANE_PRIMARY)
1216 dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
1217 goto out;
1218 }
1219
1220 for (level = 0; level < num_levels; level++) {
1221 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1222 int wm, max_wm;
1223
1224 wm = g4x_compute_wm(crtc_state, plane_state, level);
1225 max_wm = g4x_plane_fifo_size(plane_id, level);
1226
1227 if (wm > max_wm)
1228 break;
1229
1230 dirty |= raw->plane[plane_id] != wm;
1231 raw->plane[plane_id] = wm;
1232
1233 if (plane_id != PLANE_PRIMARY ||
1234 level == G4X_WM_LEVEL_NORMAL)
1235 continue;
1236
1237 wm = ilk_compute_fbc_wm(crtc_state, plane_state,
1238 raw->plane[plane_id]);
1239 max_wm = g4x_fbc_fifo_size(level);
1240
1241 /*
1242 * FBC wm is not mandatory as we
1243 * can always just disable its use.
1244 */
1245 if (wm > max_wm)
1246 wm = USHRT_MAX;
1247
1248 dirty |= raw->fbc != wm;
1249 raw->fbc = wm;
1250 }
1251
1252 /* mark watermarks as invalid */
1253 dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1254
1255 if (plane_id == PLANE_PRIMARY)
1256 dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
1257
1258 out:
1259 if (dirty) {
1260 DRM_DEBUG_KMS("%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
1261 plane->base.name,
1262 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
1263 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
1264 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
1265
1266 if (plane_id == PLANE_PRIMARY)
1267 DRM_DEBUG_KMS("FBC watermarks: SR=%d, HPLL=%d\n",
1268 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
1269 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
1270 }
1271
1272 return dirty;
1273}
1274
1275static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1276 enum plane_id plane_id, int level)
1277{
1278 const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1279
1280 return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
1281}
1282
1283static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
1284 int level)
1285{
1286 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1287
1288 if (level > dev_priv->wm.max_level)
1289 return false;
1290
1291 return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1292 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1293 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1294}
1295
1296/* mark all levels starting from 'level' as invalid */
1297static void g4x_invalidate_wms(struct intel_crtc *crtc,
1298 struct g4x_wm_state *wm_state, int level)
1299{
1300 if (level <= G4X_WM_LEVEL_NORMAL) {
1301 enum plane_id plane_id;
1302
1303 for_each_plane_id_on_crtc(crtc, plane_id)
1304 wm_state->wm.plane[plane_id] = USHRT_MAX;
1305 }
1306
1307 if (level <= G4X_WM_LEVEL_SR) {
1308 wm_state->cxsr = false;
1309 wm_state->sr.cursor = USHRT_MAX;
1310 wm_state->sr.plane = USHRT_MAX;
1311 wm_state->sr.fbc = USHRT_MAX;
1312 }
1313
1314 if (level <= G4X_WM_LEVEL_HPLL) {
1315 wm_state->hpll_en = false;
1316 wm_state->hpll.cursor = USHRT_MAX;
1317 wm_state->hpll.plane = USHRT_MAX;
1318 wm_state->hpll.fbc = USHRT_MAX;
1319 }
1320}
1321
1322static int g4x_compute_pipe_wm(struct intel_crtc_state *crtc_state)
1323{
1324 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1325 struct intel_atomic_state *state =
1326 to_intel_atomic_state(crtc_state->base.state);
1327 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
1328 int num_active_planes = hweight32(crtc_state->active_planes &
1329 ~BIT(PLANE_CURSOR));
1330 const struct g4x_pipe_wm *raw;
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001331 const struct intel_plane_state *old_plane_state;
1332 const struct intel_plane_state *new_plane_state;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001333 struct intel_plane *plane;
1334 enum plane_id plane_id;
1335 int i, level;
1336 unsigned int dirty = 0;
1337
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001338 for_each_oldnew_intel_plane_in_state(state, plane,
1339 old_plane_state,
1340 new_plane_state, i) {
1341 if (new_plane_state->base.crtc != &crtc->base &&
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001342 old_plane_state->base.crtc != &crtc->base)
1343 continue;
1344
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001345 if (g4x_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001346 dirty |= BIT(plane->id);
1347 }
1348
1349 if (!dirty)
1350 return 0;
1351
1352 level = G4X_WM_LEVEL_NORMAL;
1353 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1354 goto out;
1355
1356 raw = &crtc_state->wm.g4x.raw[level];
1357 for_each_plane_id_on_crtc(crtc, plane_id)
1358 wm_state->wm.plane[plane_id] = raw->plane[plane_id];
1359
1360 level = G4X_WM_LEVEL_SR;
1361
1362 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1363 goto out;
1364
1365 raw = &crtc_state->wm.g4x.raw[level];
1366 wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
1367 wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
1368 wm_state->sr.fbc = raw->fbc;
1369
1370 wm_state->cxsr = num_active_planes == BIT(PLANE_PRIMARY);
1371
1372 level = G4X_WM_LEVEL_HPLL;
1373
1374 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1375 goto out;
1376
1377 raw = &crtc_state->wm.g4x.raw[level];
1378 wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
1379 wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
1380 wm_state->hpll.fbc = raw->fbc;
1381
1382 wm_state->hpll_en = wm_state->cxsr;
1383
1384 level++;
1385
1386 out:
1387 if (level == G4X_WM_LEVEL_NORMAL)
1388 return -EINVAL;
1389
1390 /* invalidate the higher levels */
1391 g4x_invalidate_wms(crtc, wm_state, level);
1392
1393 /*
1394 * Determine if the FBC watermark(s) can be used. IF
1395 * this isn't the case we prefer to disable the FBC
1396 ( watermark(s) rather than disable the SR/HPLL
1397 * level(s) entirely.
1398 */
1399 wm_state->fbc_en = level > G4X_WM_LEVEL_NORMAL;
1400
1401 if (level >= G4X_WM_LEVEL_SR &&
1402 wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
1403 wm_state->fbc_en = false;
1404 else if (level >= G4X_WM_LEVEL_HPLL &&
1405 wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
1406 wm_state->fbc_en = false;
1407
1408 return 0;
1409}
1410
1411static int g4x_compute_intermediate_wm(struct drm_device *dev,
1412 struct intel_crtc *crtc,
1413 struct intel_crtc_state *crtc_state)
1414{
1415 struct g4x_wm_state *intermediate = &crtc_state->wm.g4x.intermediate;
1416 const struct g4x_wm_state *optimal = &crtc_state->wm.g4x.optimal;
1417 const struct g4x_wm_state *active = &crtc->wm.active.g4x;
1418 enum plane_id plane_id;
1419
1420 intermediate->cxsr = optimal->cxsr && active->cxsr &&
1421 !crtc_state->disable_cxsr;
1422 intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
1423 !crtc_state->disable_cxsr;
1424 intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
1425
1426 for_each_plane_id_on_crtc(crtc, plane_id) {
1427 intermediate->wm.plane[plane_id] =
1428 max(optimal->wm.plane[plane_id],
1429 active->wm.plane[plane_id]);
1430
1431 WARN_ON(intermediate->wm.plane[plane_id] >
1432 g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
1433 }
1434
1435 intermediate->sr.plane = max(optimal->sr.plane,
1436 active->sr.plane);
1437 intermediate->sr.cursor = max(optimal->sr.cursor,
1438 active->sr.cursor);
1439 intermediate->sr.fbc = max(optimal->sr.fbc,
1440 active->sr.fbc);
1441
1442 intermediate->hpll.plane = max(optimal->hpll.plane,
1443 active->hpll.plane);
1444 intermediate->hpll.cursor = max(optimal->hpll.cursor,
1445 active->hpll.cursor);
1446 intermediate->hpll.fbc = max(optimal->hpll.fbc,
1447 active->hpll.fbc);
1448
1449 WARN_ON((intermediate->sr.plane >
1450 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
1451 intermediate->sr.cursor >
1452 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
1453 intermediate->cxsr);
1454 WARN_ON((intermediate->sr.plane >
1455 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
1456 intermediate->sr.cursor >
1457 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
1458 intermediate->hpll_en);
1459
1460 WARN_ON(intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
1461 intermediate->fbc_en && intermediate->cxsr);
1462 WARN_ON(intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
1463 intermediate->fbc_en && intermediate->hpll_en);
1464
1465 /*
1466 * If our intermediate WM are identical to the final WM, then we can
1467 * omit the post-vblank programming; only update if it's different.
1468 */
1469 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
1470 crtc_state->wm.need_postvbl_update = true;
1471
1472 return 0;
1473}
1474
1475static void g4x_merge_wm(struct drm_i915_private *dev_priv,
1476 struct g4x_wm_values *wm)
1477{
1478 struct intel_crtc *crtc;
1479 int num_active_crtcs = 0;
1480
1481 wm->cxsr = true;
1482 wm->hpll_en = true;
1483 wm->fbc_en = true;
1484
1485 for_each_intel_crtc(&dev_priv->drm, crtc) {
1486 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1487
1488 if (!crtc->active)
1489 continue;
1490
1491 if (!wm_state->cxsr)
1492 wm->cxsr = false;
1493 if (!wm_state->hpll_en)
1494 wm->hpll_en = false;
1495 if (!wm_state->fbc_en)
1496 wm->fbc_en = false;
1497
1498 num_active_crtcs++;
1499 }
1500
1501 if (num_active_crtcs != 1) {
1502 wm->cxsr = false;
1503 wm->hpll_en = false;
1504 wm->fbc_en = false;
1505 }
1506
1507 for_each_intel_crtc(&dev_priv->drm, crtc) {
1508 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1509 enum pipe pipe = crtc->pipe;
1510
1511 wm->pipe[pipe] = wm_state->wm;
1512 if (crtc->active && wm->cxsr)
1513 wm->sr = wm_state->sr;
1514 if (crtc->active && wm->hpll_en)
1515 wm->hpll = wm_state->hpll;
1516 }
1517}
1518
1519static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
1520{
1521 struct g4x_wm_values *old_wm = &dev_priv->wm.g4x;
1522 struct g4x_wm_values new_wm = {};
1523
1524 g4x_merge_wm(dev_priv, &new_wm);
1525
1526 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
1527 return;
1528
1529 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
1530 _intel_set_memory_cxsr(dev_priv, false);
1531
1532 g4x_write_wm_values(dev_priv, &new_wm);
1533
1534 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
1535 _intel_set_memory_cxsr(dev_priv, true);
1536
1537 *old_wm = new_wm;
1538}
1539
1540static void g4x_initial_watermarks(struct intel_atomic_state *state,
1541 struct intel_crtc_state *crtc_state)
1542{
1543 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1544 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1545
1546 mutex_lock(&dev_priv->wm.wm_mutex);
1547 crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
1548 g4x_program_watermarks(dev_priv);
1549 mutex_unlock(&dev_priv->wm.wm_mutex);
1550}
1551
1552static void g4x_optimize_watermarks(struct intel_atomic_state *state,
1553 struct intel_crtc_state *crtc_state)
1554{
1555 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1556 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
1557
1558 if (!crtc_state->wm.need_postvbl_update)
1559 return;
1560
1561 mutex_lock(&dev_priv->wm.wm_mutex);
1562 intel_crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
1563 g4x_program_watermarks(dev_priv);
1564 mutex_unlock(&dev_priv->wm.wm_mutex);
1565}
1566
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001567/* latency must be in 0.1us units. */
1568static unsigned int vlv_wm_method2(unsigned int pixel_rate,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001569 unsigned int htotal,
1570 unsigned int width,
Ville Syrjäläac484962016-01-20 21:05:26 +02001571 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001572 unsigned int latency)
1573{
1574 unsigned int ret;
1575
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001576 ret = intel_wm_method2(pixel_rate, htotal,
1577 width, cpp, latency);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001578 ret = DIV_ROUND_UP(ret, 64);
1579
1580 return ret;
1581}
1582
Ville Syrjäläbb726512016-10-31 22:37:24 +02001583static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001584{
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001585 /* all latencies in usec */
1586 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1587
Ville Syrjälä58590c12015-09-08 21:05:12 +03001588 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
1589
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001590 if (IS_CHERRYVIEW(dev_priv)) {
1591 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1592 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001593
1594 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001595 }
1596}
1597
Ville Syrjäläe339d672016-11-28 19:37:17 +02001598static uint16_t vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1599 const struct intel_plane_state *plane_state,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001600 int level)
1601{
Ville Syrjäläe339d672016-11-28 19:37:17 +02001602 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001603 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläe339d672016-11-28 19:37:17 +02001604 const struct drm_display_mode *adjusted_mode =
1605 &crtc_state->base.adjusted_mode;
Ville Syrjäläac484962016-01-20 21:05:26 +02001606 int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001607
1608 if (dev_priv->wm.pri_latency[level] == 0)
1609 return USHRT_MAX;
1610
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001611 if (!intel_wm_plane_visible(crtc_state, plane_state))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001612 return 0;
1613
Daniel Vetteref426c12017-01-04 11:41:10 +01001614 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjäläe339d672016-11-28 19:37:17 +02001615 clock = adjusted_mode->crtc_clock;
1616 htotal = adjusted_mode->crtc_htotal;
1617 width = crtc_state->pipe_src_w;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001618
Ville Syrjälä709f3fc2017-03-03 17:19:26 +02001619 if (plane->id == PLANE_CURSOR) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001620 /*
1621 * FIXME the formula gives values that are
1622 * too big for the cursor FIFO, and hence we
1623 * would never be able to use cursors. For
1624 * now just hardcode the watermark.
1625 */
1626 wm = 63;
1627 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +02001628 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001629 dev_priv->wm.pri_latency[level] * 10);
1630 }
1631
1632 return min_t(int, wm, USHRT_MAX);
1633}
1634
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001635static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1636{
1637 return (active_planes & (BIT(PLANE_SPRITE0) |
1638 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1639}
1640
Ville Syrjälä5012e602017-03-02 19:14:56 +02001641static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001642{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001643 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001644 const struct g4x_pipe_wm *raw =
Ville Syrjälä5012e602017-03-02 19:14:56 +02001645 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001646 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001647 unsigned int active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1648 int num_active_planes = hweight32(active_planes);
1649 const int fifo_size = 511;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001650 int fifo_extra, fifo_left = fifo_size;
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001651 int sprite0_fifo_extra = 0;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001652 unsigned int total_rate;
1653 enum plane_id plane_id;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001654
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001655 /*
1656 * When enabling sprite0 after sprite1 has already been enabled
1657 * we tend to get an underrun unless sprite0 already has some
1658 * FIFO space allcoated. Hence we always allocate at least one
1659 * cacheline for sprite0 whenever sprite1 is enabled.
1660 *
1661 * All other plane enable sequences appear immune to this problem.
1662 */
1663 if (vlv_need_sprite0_fifo_workaround(active_planes))
1664 sprite0_fifo_extra = 1;
1665
Ville Syrjälä5012e602017-03-02 19:14:56 +02001666 total_rate = raw->plane[PLANE_PRIMARY] +
1667 raw->plane[PLANE_SPRITE0] +
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001668 raw->plane[PLANE_SPRITE1] +
1669 sprite0_fifo_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001670
Ville Syrjälä5012e602017-03-02 19:14:56 +02001671 if (total_rate > fifo_size)
1672 return -EINVAL;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001673
Ville Syrjälä5012e602017-03-02 19:14:56 +02001674 if (total_rate == 0)
1675 total_rate = 1;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001676
Ville Syrjälä5012e602017-03-02 19:14:56 +02001677 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001678 unsigned int rate;
1679
Ville Syrjälä5012e602017-03-02 19:14:56 +02001680 if ((active_planes & BIT(plane_id)) == 0) {
1681 fifo_state->plane[plane_id] = 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001682 continue;
1683 }
1684
Ville Syrjälä5012e602017-03-02 19:14:56 +02001685 rate = raw->plane[plane_id];
1686 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1687 fifo_left -= fifo_state->plane[plane_id];
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001688 }
1689
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001690 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1691 fifo_left -= sprite0_fifo_extra;
1692
Ville Syrjälä5012e602017-03-02 19:14:56 +02001693 fifo_state->plane[PLANE_CURSOR] = 63;
1694
1695 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001696
1697 /* spread the remainder evenly */
Ville Syrjälä5012e602017-03-02 19:14:56 +02001698 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001699 int plane_extra;
1700
1701 if (fifo_left == 0)
1702 break;
1703
Ville Syrjälä5012e602017-03-02 19:14:56 +02001704 if ((active_planes & BIT(plane_id)) == 0)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001705 continue;
1706
1707 plane_extra = min(fifo_extra, fifo_left);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001708 fifo_state->plane[plane_id] += plane_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001709 fifo_left -= plane_extra;
1710 }
1711
Ville Syrjälä5012e602017-03-02 19:14:56 +02001712 WARN_ON(active_planes != 0 && fifo_left != 0);
1713
1714 /* give it all to the first plane if none are active */
1715 if (active_planes == 0) {
1716 WARN_ON(fifo_left != fifo_size);
1717 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1718 }
1719
1720 return 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001721}
1722
Ville Syrjäläff32c542017-03-02 19:14:57 +02001723/* mark all levels starting from 'level' as invalid */
1724static void vlv_invalidate_wms(struct intel_crtc *crtc,
1725 struct vlv_wm_state *wm_state, int level)
1726{
1727 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1728
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001729 for (; level < intel_wm_num_levels(dev_priv); level++) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02001730 enum plane_id plane_id;
1731
1732 for_each_plane_id_on_crtc(crtc, plane_id)
1733 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1734
1735 wm_state->sr[level].cursor = USHRT_MAX;
1736 wm_state->sr[level].plane = USHRT_MAX;
1737 }
1738}
1739
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001740static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1741{
1742 if (wm > fifo_size)
1743 return USHRT_MAX;
1744 else
1745 return fifo_size - wm;
1746}
1747
Ville Syrjäläff32c542017-03-02 19:14:57 +02001748/*
1749 * Starting from 'level' set all higher
1750 * levels to 'value' in the "raw" watermarks.
1751 */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001752static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001753 int level, enum plane_id plane_id, u16 value)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001754{
Ville Syrjäläff32c542017-03-02 19:14:57 +02001755 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001756 int num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001757 bool dirty = false;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001758
Ville Syrjäläff32c542017-03-02 19:14:57 +02001759 for (; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001760 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001761
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001762 dirty |= raw->plane[plane_id] != value;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001763 raw->plane[plane_id] = value;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001764 }
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001765
1766 return dirty;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001767}
1768
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001769static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1770 const struct intel_plane_state *plane_state)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001771{
1772 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1773 enum plane_id plane_id = plane->id;
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001774 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
Ville Syrjäläff32c542017-03-02 19:14:57 +02001775 int level;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001776 bool dirty = false;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001777
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001778 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001779 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1780 goto out;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001781 }
1782
1783 for (level = 0; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001784 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001785 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1786 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1787
Ville Syrjäläff32c542017-03-02 19:14:57 +02001788 if (wm > max_wm)
1789 break;
1790
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001791 dirty |= raw->plane[plane_id] != wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001792 raw->plane[plane_id] = wm;
1793 }
1794
1795 /* mark all higher levels as invalid */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001796 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001797
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001798out:
1799 if (dirty)
Ville Syrjälä57a65282017-04-21 21:14:22 +03001800 DRM_DEBUG_KMS("%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001801 plane->base.name,
1802 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1803 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1804 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
1805
1806 return dirty;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001807}
1808
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001809static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1810 enum plane_id plane_id, int level)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001811{
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001812 const struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02001813 &crtc_state->wm.vlv.raw[level];
1814 const struct vlv_fifo_state *fifo_state =
1815 &crtc_state->wm.vlv.fifo_state;
1816
1817 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1818}
1819
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001820static 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 +02001821{
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001822 return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1823 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1824 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1825 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001826}
1827
1828static int vlv_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001829{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001830 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001831 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001832 struct intel_atomic_state *state =
1833 to_intel_atomic_state(crtc_state->base.state);
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001834 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001835 const struct vlv_fifo_state *fifo_state =
1836 &crtc_state->wm.vlv.fifo_state;
1837 int num_active_planes = hweight32(crtc_state->active_planes &
1838 ~BIT(PLANE_CURSOR));
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001839 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->base);
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001840 const struct intel_plane_state *old_plane_state;
1841 const struct intel_plane_state *new_plane_state;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001842 struct intel_plane *plane;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001843 enum plane_id plane_id;
1844 int level, ret, i;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001845 unsigned int dirty = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001846
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001847 for_each_oldnew_intel_plane_in_state(state, plane,
1848 old_plane_state,
1849 new_plane_state, i) {
1850 if (new_plane_state->base.crtc != &crtc->base &&
Ville Syrjäläff32c542017-03-02 19:14:57 +02001851 old_plane_state->base.crtc != &crtc->base)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001852 continue;
1853
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001854 if (vlv_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001855 dirty |= BIT(plane->id);
1856 }
1857
1858 /*
1859 * DSPARB registers may have been reset due to the
1860 * power well being turned off. Make sure we restore
1861 * them to a consistent state even if no primary/sprite
1862 * planes are initially active.
1863 */
1864 if (needs_modeset)
1865 crtc_state->fifo_changed = true;
1866
1867 if (!dirty)
1868 return 0;
1869
1870 /* cursor changes don't warrant a FIFO recompute */
1871 if (dirty & ~BIT(PLANE_CURSOR)) {
1872 const struct intel_crtc_state *old_crtc_state =
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001873 intel_atomic_get_old_crtc_state(state, crtc);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001874 const struct vlv_fifo_state *old_fifo_state =
1875 &old_crtc_state->wm.vlv.fifo_state;
1876
1877 ret = vlv_compute_fifo(crtc_state);
1878 if (ret)
1879 return ret;
1880
1881 if (needs_modeset ||
1882 memcmp(old_fifo_state, fifo_state,
1883 sizeof(*fifo_state)) != 0)
1884 crtc_state->fifo_changed = true;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001885 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001886
Ville Syrjäläff32c542017-03-02 19:14:57 +02001887 /* initially allow all levels */
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001888 wm_state->num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001889 /*
1890 * Note that enabling cxsr with no primary/sprite planes
1891 * enabled can wedge the pipe. Hence we only allow cxsr
1892 * with exactly one enabled primary/sprite plane.
1893 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001894 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001895
Ville Syrjälä5012e602017-03-02 19:14:56 +02001896 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001897 const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001898 const int sr_fifo_size = INTEL_INFO(dev_priv)->num_pipes * 512 - 1;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001899
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001900 if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
Ville Syrjäläff32c542017-03-02 19:14:57 +02001901 break;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001902
Ville Syrjäläff32c542017-03-02 19:14:57 +02001903 for_each_plane_id_on_crtc(crtc, plane_id) {
1904 wm_state->wm[level].plane[plane_id] =
1905 vlv_invert_wm_value(raw->plane[plane_id],
1906 fifo_state->plane[plane_id]);
1907 }
1908
1909 wm_state->sr[level].plane =
1910 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
Ville Syrjälä5012e602017-03-02 19:14:56 +02001911 raw->plane[PLANE_SPRITE0],
Ville Syrjäläff32c542017-03-02 19:14:57 +02001912 raw->plane[PLANE_SPRITE1]),
1913 sr_fifo_size);
1914
1915 wm_state->sr[level].cursor =
1916 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1917 63);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001918 }
1919
Ville Syrjäläff32c542017-03-02 19:14:57 +02001920 if (level == 0)
1921 return -EINVAL;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001922
Ville Syrjäläff32c542017-03-02 19:14:57 +02001923 /* limit to only levels we can actually handle */
1924 wm_state->num_levels = level;
1925
1926 /* invalidate the higher levels */
1927 vlv_invalidate_wms(crtc, wm_state, level);
1928
1929 return 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001930}
1931
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001932#define VLV_FIFO(plane, value) \
1933 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1934
Ville Syrjäläff32c542017-03-02 19:14:57 +02001935static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
1936 struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001937{
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001938 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001939 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001940 const struct vlv_fifo_state *fifo_state =
1941 &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001942 int sprite0_start, sprite1_start, fifo_size;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001943
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001944 if (!crtc_state->fifo_changed)
1945 return;
1946
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001947 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
1948 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
1949 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001950
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001951 WARN_ON(fifo_state->plane[PLANE_CURSOR] != 63);
1952 WARN_ON(fifo_size != 511);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001953
Ville Syrjäläc137d662017-03-02 19:15:06 +02001954 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
1955
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001956 /*
1957 * uncore.lock serves a double purpose here. It allows us to
1958 * use the less expensive I915_{READ,WRITE}_FW() functions, and
1959 * it protects the DSPARB registers from getting clobbered by
1960 * parallel updates from multiple pipes.
1961 *
1962 * intel_pipe_update_start() has already disabled interrupts
1963 * for us, so a plain spin_lock() is sufficient here.
1964 */
1965 spin_lock(&dev_priv->uncore.lock);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001966
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001967 switch (crtc->pipe) {
1968 uint32_t dsparb, dsparb2, dsparb3;
1969 case PIPE_A:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001970 dsparb = I915_READ_FW(DSPARB);
1971 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001972
1973 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1974 VLV_FIFO(SPRITEB, 0xff));
1975 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1976 VLV_FIFO(SPRITEB, sprite1_start));
1977
1978 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1979 VLV_FIFO(SPRITEB_HI, 0x1));
1980 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1981 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1982
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001983 I915_WRITE_FW(DSPARB, dsparb);
1984 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001985 break;
1986 case PIPE_B:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001987 dsparb = I915_READ_FW(DSPARB);
1988 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001989
1990 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1991 VLV_FIFO(SPRITED, 0xff));
1992 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1993 VLV_FIFO(SPRITED, sprite1_start));
1994
1995 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
1996 VLV_FIFO(SPRITED_HI, 0xff));
1997 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
1998 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
1999
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002000 I915_WRITE_FW(DSPARB, dsparb);
2001 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002002 break;
2003 case PIPE_C:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002004 dsparb3 = I915_READ_FW(DSPARB3);
2005 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002006
2007 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
2008 VLV_FIFO(SPRITEF, 0xff));
2009 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
2010 VLV_FIFO(SPRITEF, sprite1_start));
2011
2012 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
2013 VLV_FIFO(SPRITEF_HI, 0xff));
2014 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
2015 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
2016
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002017 I915_WRITE_FW(DSPARB3, dsparb3);
2018 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002019 break;
2020 default:
2021 break;
2022 }
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002023
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002024 POSTING_READ_FW(DSPARB);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002025
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002026 spin_unlock(&dev_priv->uncore.lock);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002027}
2028
2029#undef VLV_FIFO
2030
Ville Syrjälä4841da52017-03-02 19:14:59 +02002031static int vlv_compute_intermediate_wm(struct drm_device *dev,
2032 struct intel_crtc *crtc,
2033 struct intel_crtc_state *crtc_state)
2034{
2035 struct vlv_wm_state *intermediate = &crtc_state->wm.vlv.intermediate;
2036 const struct vlv_wm_state *optimal = &crtc_state->wm.vlv.optimal;
2037 const struct vlv_wm_state *active = &crtc->wm.active.vlv;
2038 int level;
2039
2040 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002041 intermediate->cxsr = optimal->cxsr && active->cxsr &&
2042 !crtc_state->disable_cxsr;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002043
2044 for (level = 0; level < intermediate->num_levels; level++) {
2045 enum plane_id plane_id;
2046
2047 for_each_plane_id_on_crtc(crtc, plane_id) {
2048 intermediate->wm[level].plane[plane_id] =
2049 min(optimal->wm[level].plane[plane_id],
2050 active->wm[level].plane[plane_id]);
2051 }
2052
2053 intermediate->sr[level].plane = min(optimal->sr[level].plane,
2054 active->sr[level].plane);
2055 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
2056 active->sr[level].cursor);
2057 }
2058
2059 vlv_invalidate_wms(crtc, intermediate, level);
2060
2061 /*
2062 * If our intermediate WM are identical to the final WM, then we can
2063 * omit the post-vblank programming; only update if it's different.
2064 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002065 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
2066 crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002067
2068 return 0;
2069}
2070
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002071static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002072 struct vlv_wm_values *wm)
2073{
2074 struct intel_crtc *crtc;
2075 int num_active_crtcs = 0;
2076
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002077 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002078 wm->cxsr = true;
2079
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002080 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002081 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002082
2083 if (!crtc->active)
2084 continue;
2085
2086 if (!wm_state->cxsr)
2087 wm->cxsr = false;
2088
2089 num_active_crtcs++;
2090 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
2091 }
2092
2093 if (num_active_crtcs != 1)
2094 wm->cxsr = false;
2095
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03002096 if (num_active_crtcs > 1)
2097 wm->level = VLV_WM_LEVEL_PM2;
2098
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002099 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002100 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002101 enum pipe pipe = crtc->pipe;
2102
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002103 wm->pipe[pipe] = wm_state->wm[wm->level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02002104 if (crtc->active && wm->cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002105 wm->sr = wm_state->sr[wm->level];
2106
Ville Syrjälä1b313892016-11-28 19:37:08 +02002107 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
2108 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
2109 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
2110 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002111 }
2112}
2113
Ville Syrjäläff32c542017-03-02 19:14:57 +02002114static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002115{
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002116 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
2117 struct vlv_wm_values new_wm = {};
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002118
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002119 vlv_merge_wm(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002120
Ville Syrjäläff32c542017-03-02 19:14:57 +02002121 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002122 return;
2123
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002124 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002125 chv_set_memory_dvfs(dev_priv, false);
2126
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002127 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002128 chv_set_memory_pm5(dev_priv, false);
2129
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002130 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002131 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002132
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002133 vlv_write_wm_values(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002134
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002135 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002136 _intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002137
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002138 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002139 chv_set_memory_pm5(dev_priv, true);
2140
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002141 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002142 chv_set_memory_dvfs(dev_priv, true);
2143
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002144 *old_wm = new_wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03002145}
2146
Ville Syrjäläff32c542017-03-02 19:14:57 +02002147static void vlv_initial_watermarks(struct intel_atomic_state *state,
2148 struct intel_crtc_state *crtc_state)
2149{
2150 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2151 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
2152
2153 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002154 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
2155 vlv_program_watermarks(dev_priv);
2156 mutex_unlock(&dev_priv->wm.wm_mutex);
2157}
2158
2159static void vlv_optimize_watermarks(struct intel_atomic_state *state,
2160 struct intel_crtc_state *crtc_state)
2161{
2162 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2163 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2164
2165 if (!crtc_state->wm.need_postvbl_update)
2166 return;
2167
2168 mutex_lock(&dev_priv->wm.wm_mutex);
2169 intel_crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02002170 vlv_program_watermarks(dev_priv);
2171 mutex_unlock(&dev_priv->wm.wm_mutex);
2172}
2173
Ville Syrjälä432081b2016-10-31 22:37:03 +02002174static void i965_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002175{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002176 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002177 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002178 int srwm = 1;
2179 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03002180 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002181
2182 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002183 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002184 if (crtc) {
2185 /* self-refresh has much higher latency */
2186 static const int sr_latency_ns = 12000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002187 const struct drm_display_mode *adjusted_mode =
2188 &crtc->config->base.adjusted_mode;
2189 const struct drm_framebuffer *fb =
2190 crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002191 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002192 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002193 int hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002194 int cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002195 int entries;
2196
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002197 entries = intel_wm_method2(clock, htotal,
2198 hdisplay, cpp, sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002199 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
2200 srwm = I965_FIFO_SIZE - entries;
2201 if (srwm < 0)
2202 srwm = 1;
2203 srwm &= 0x1ff;
2204 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
2205 entries, srwm);
2206
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002207 entries = intel_wm_method2(clock, htotal,
2208 crtc->base.cursor->state->crtc_w, 4,
2209 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002210 entries = DIV_ROUND_UP(entries,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002211 i965_cursor_wm_info.cacheline_size) +
2212 i965_cursor_wm_info.guard_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002213
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002214 cursor_sr = i965_cursor_wm_info.fifo_size - entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002215 if (cursor_sr > i965_cursor_wm_info.max_wm)
2216 cursor_sr = i965_cursor_wm_info.max_wm;
2217
2218 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
2219 "cursor %d\n", srwm, cursor_sr);
2220
Imre Deak98584252014-06-13 14:54:20 +03002221 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002222 } else {
Imre Deak98584252014-06-13 14:54:20 +03002223 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002224 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03002225 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002226 }
2227
2228 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2229 srwm);
2230
2231 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002232 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
2233 FW_WM(8, CURSORB) |
2234 FW_WM(8, PLANEB) |
2235 FW_WM(8, PLANEA));
2236 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
2237 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002238 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002239 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03002240
2241 if (cxsr_enabled)
2242 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002243}
2244
Ville Syrjäläf4998962015-03-10 17:02:21 +02002245#undef FW_WM
2246
Ville Syrjälä432081b2016-10-31 22:37:03 +02002247static void i9xx_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002248{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002249 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002250 const struct intel_watermark_params *wm_info;
2251 uint32_t fwater_lo;
2252 uint32_t fwater_hi;
2253 int cwm, srwm = 1;
2254 int fifo_size;
2255 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002256 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002257
Ville Syrjäläa9097be2016-10-31 22:37:20 +02002258 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002259 wm_info = &i945_wm_info;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002260 else if (!IS_GEN2(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002261 wm_info = &i915_wm_info;
2262 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03002263 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002264
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02002265 fifo_size = dev_priv->display.get_fifo_size(dev_priv, 0);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02002266 crtc = intel_get_crtc_for_plane(dev_priv, 0);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002267 if (intel_crtc_active(crtc)) {
2268 const struct drm_display_mode *adjusted_mode =
2269 &crtc->config->base.adjusted_mode;
2270 const struct drm_framebuffer *fb =
2271 crtc->base.primary->state->fb;
2272 int cpp;
2273
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002274 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002275 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002276 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002277 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002278
Damien Lespiau241bfc32013-09-25 16:45:37 +01002279 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002280 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002281 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002282 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002283 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002284 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002285 if (planea_wm > (long)wm_info->max_wm)
2286 planea_wm = wm_info->max_wm;
2287 }
2288
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002289 if (IS_GEN2(dev_priv))
Ville Syrjälä9d539102014-08-15 01:21:53 +03002290 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002291
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02002292 fifo_size = dev_priv->display.get_fifo_size(dev_priv, 1);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02002293 crtc = intel_get_crtc_for_plane(dev_priv, 1);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002294 if (intel_crtc_active(crtc)) {
2295 const struct drm_display_mode *adjusted_mode =
2296 &crtc->config->base.adjusted_mode;
2297 const struct drm_framebuffer *fb =
2298 crtc->base.primary->state->fb;
2299 int cpp;
2300
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002301 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002302 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002303 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002304 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002305
Damien Lespiau241bfc32013-09-25 16:45:37 +01002306 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002307 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002308 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002309 if (enabled == NULL)
2310 enabled = crtc;
2311 else
2312 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002313 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002314 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002315 if (planeb_wm > (long)wm_info->max_wm)
2316 planeb_wm = wm_info->max_wm;
2317 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002318
2319 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
2320
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002321 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07002322 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002323
Ville Syrjäläefc26112016-10-31 22:37:04 +02002324 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002325
2326 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01002327 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002328 enabled = NULL;
2329 }
2330
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002331 /*
2332 * Overlay gets an aggressive default since video jitter is bad.
2333 */
2334 cwm = 2;
2335
2336 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03002337 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002338
2339 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02002340 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002341 /* self-refresh has much higher latency */
2342 static const int sr_latency_ns = 6000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002343 const struct drm_display_mode *adjusted_mode =
2344 &enabled->config->base.adjusted_mode;
2345 const struct drm_framebuffer *fb =
2346 enabled->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002347 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002348 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002349 int hdisplay = enabled->config->pipe_src_w;
2350 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002351 int entries;
2352
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002353 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002354 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002355 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002356 cpp = fb->format->cpp[0];
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002357
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002358 entries = intel_wm_method2(clock, htotal, hdisplay, cpp,
2359 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002360 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
2361 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
2362 srwm = wm_info->fifo_size - entries;
2363 if (srwm < 0)
2364 srwm = 1;
2365
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002366 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002367 I915_WRITE(FW_BLC_SELF,
2368 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03002369 else
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002370 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
2371 }
2372
2373 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
2374 planea_wm, planeb_wm, cwm, srwm);
2375
2376 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2377 fwater_hi = (cwm & 0x1f);
2378
2379 /* Set request length to 8 cachelines per fetch */
2380 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2381 fwater_hi = fwater_hi | (1 << 8);
2382
2383 I915_WRITE(FW_BLC, fwater_lo);
2384 I915_WRITE(FW_BLC2, fwater_hi);
2385
Imre Deak5209b1f2014-07-01 12:36:17 +03002386 if (enabled)
2387 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002388}
2389
Ville Syrjälä432081b2016-10-31 22:37:03 +02002390static void i845_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002391{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002392 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002393 struct intel_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002394 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002395 uint32_t fwater_lo;
2396 int planea_wm;
2397
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002398 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002399 if (crtc == NULL)
2400 return;
2401
Ville Syrjäläefc26112016-10-31 22:37:04 +02002402 adjusted_mode = &crtc->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002403 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02002404 &i845_wm_info,
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02002405 dev_priv->display.get_fifo_size(dev_priv, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01002406 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002407 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
2408 fwater_lo |= (3<<8) | planea_wm;
2409
2410 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
2411
2412 I915_WRITE(FW_BLC, fwater_lo);
2413}
2414
Ville Syrjälä37126462013-08-01 16:18:55 +03002415/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002416static unsigned int ilk_wm_method1(unsigned int pixel_rate,
2417 unsigned int cpp,
2418 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002419{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002420 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002421
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002422 ret = intel_wm_method1(pixel_rate, cpp, latency);
2423 ret = DIV_ROUND_UP(ret, 64) + 2;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002424
2425 return ret;
2426}
2427
Ville Syrjälä37126462013-08-01 16:18:55 +03002428/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002429static unsigned int ilk_wm_method2(unsigned int pixel_rate,
2430 unsigned int htotal,
2431 unsigned int width,
2432 unsigned int cpp,
2433 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002434{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002435 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002436
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002437 ret = intel_wm_method2(pixel_rate, htotal,
2438 width, cpp, latency);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002439 ret = DIV_ROUND_UP(ret, 64) + 2;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002440
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002441 return ret;
2442}
2443
Ville Syrjälä23297042013-07-05 11:57:17 +03002444static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Ville Syrjäläac484962016-01-20 21:05:26 +02002445 uint8_t cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002446{
Matt Roper15126882015-12-03 11:37:40 -08002447 /*
2448 * Neither of these should be possible since this function shouldn't be
2449 * called if the CRTC is off or the plane is invisible. But let's be
2450 * extra paranoid to avoid a potential divide-by-zero if we screw up
2451 * elsewhere in the driver.
2452 */
Ville Syrjäläac484962016-01-20 21:05:26 +02002453 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08002454 return 0;
2455 if (WARN_ON(!horiz_pixels))
2456 return 0;
2457
Ville Syrjäläac484962016-01-20 21:05:26 +02002458 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002459}
2460
Imre Deak820c1982013-12-17 14:46:36 +02002461struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002462 uint16_t pri;
2463 uint16_t spr;
2464 uint16_t cur;
2465 uint16_t fbc;
2466};
2467
Ville Syrjälä37126462013-08-01 16:18:55 +03002468/*
2469 * For both WM_PIPE and WM_LP.
2470 * mem_value must be in 0.1us units.
2471 */
Matt Roper7221fc32015-09-24 15:53:08 -07002472static uint32_t ilk_compute_pri_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002473 const struct intel_plane_state *pstate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03002474 uint32_t mem_value,
2475 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002476{
Paulo Zanonicca32e92013-05-31 11:45:06 -03002477 uint32_t method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002478 int cpp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002479
Ville Syrjälä24304d812017-03-14 17:10:49 +02002480 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002481 return 0;
2482
Ville Syrjälä353c8592016-12-14 23:30:57 +02002483 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002484
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002485 method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002486
2487 if (!is_lp)
2488 return method1;
2489
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002490 method2 = ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002491 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002492 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002493 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002494
2495 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002496}
2497
Ville Syrjälä37126462013-08-01 16:18:55 +03002498/*
2499 * For both WM_PIPE and WM_LP.
2500 * mem_value must be in 0.1us units.
2501 */
Matt Roper7221fc32015-09-24 15:53:08 -07002502static uint32_t ilk_compute_spr_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002503 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002504 uint32_t mem_value)
2505{
2506 uint32_t method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002507 int cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002508
Ville Syrjälä24304d812017-03-14 17:10:49 +02002509 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002510 return 0;
2511
Ville Syrjälä353c8592016-12-14 23:30:57 +02002512 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002513
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002514 method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
2515 method2 = ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002516 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002517 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002518 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002519 return min(method1, method2);
2520}
2521
Ville Syrjälä37126462013-08-01 16:18:55 +03002522/*
2523 * For both WM_PIPE and WM_LP.
2524 * mem_value must be in 0.1us units.
2525 */
Matt Roper7221fc32015-09-24 15:53:08 -07002526static uint32_t ilk_compute_cur_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002527 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002528 uint32_t mem_value)
2529{
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002530 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002531
Ville Syrjälä24304d812017-03-14 17:10:49 +02002532 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002533 return 0;
2534
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002535 cpp = pstate->base.fb->format->cpp[0];
2536
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002537 return ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002538 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002539 pstate->base.crtc_w, cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002540}
2541
Paulo Zanonicca32e92013-05-31 11:45:06 -03002542/* Only for WM_LP. */
Matt Roper7221fc32015-09-24 15:53:08 -07002543static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002544 const struct intel_plane_state *pstate,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03002545 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002546{
Ville Syrjälä83054942016-11-18 21:53:00 +02002547 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002548
Ville Syrjälä24304d812017-03-14 17:10:49 +02002549 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanonicca32e92013-05-31 11:45:06 -03002550 return 0;
2551
Ville Syrjälä353c8592016-12-14 23:30:57 +02002552 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002553
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002554 return ilk_wm_fbc(pri_val, drm_rect_width(&pstate->base.dst), cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002555}
2556
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002557static unsigned int
2558ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002559{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002560 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002561 return 3072;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002562 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002563 return 768;
2564 else
2565 return 512;
2566}
2567
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002568static unsigned int
2569ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2570 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002571{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002572 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002573 /* BDW primary/sprite plane watermarks */
2574 return level == 0 ? 255 : 2047;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002575 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002576 /* IVB/HSW primary/sprite plane watermarks */
2577 return level == 0 ? 127 : 1023;
2578 else if (!is_sprite)
2579 /* ILK/SNB primary plane watermarks */
2580 return level == 0 ? 127 : 511;
2581 else
2582 /* ILK/SNB sprite plane watermarks */
2583 return level == 0 ? 63 : 255;
2584}
2585
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002586static unsigned int
2587ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002588{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002589 if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002590 return level == 0 ? 63 : 255;
2591 else
2592 return level == 0 ? 31 : 63;
2593}
2594
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002595static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002596{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002597 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002598 return 31;
2599 else
2600 return 15;
2601}
2602
Ville Syrjälä158ae642013-08-07 13:28:19 +03002603/* Calculate the maximum primary/sprite plane watermark */
2604static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
2605 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002606 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002607 enum intel_ddb_partitioning ddb_partitioning,
2608 bool is_sprite)
2609{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002610 struct drm_i915_private *dev_priv = to_i915(dev);
2611 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002612
2613 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002614 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002615 return 0;
2616
2617 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002618 if (level == 0 || config->num_pipes_active > 1) {
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002619 fifo_size /= INTEL_INFO(dev_priv)->num_pipes;
Ville Syrjälä158ae642013-08-07 13:28:19 +03002620
2621 /*
2622 * For some reason the non self refresh
2623 * FIFO size is only half of the self
2624 * refresh FIFO size on ILK/SNB.
2625 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002626 if (INTEL_GEN(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002627 fifo_size /= 2;
2628 }
2629
Ville Syrjälä240264f2013-08-07 13:29:12 +03002630 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002631 /* level 0 is always calculated with 1:1 split */
2632 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2633 if (is_sprite)
2634 fifo_size *= 5;
2635 fifo_size /= 6;
2636 } else {
2637 fifo_size /= 2;
2638 }
2639 }
2640
2641 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002642 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002643}
2644
2645/* Calculate the maximum cursor plane watermark */
2646static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002647 int level,
2648 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002649{
2650 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002651 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002652 return 64;
2653
2654 /* otherwise just report max that registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002655 return ilk_cursor_wm_reg_max(to_i915(dev), level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002656}
2657
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002658static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002659 int level,
2660 const struct intel_wm_config *config,
2661 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002662 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002663{
Ville Syrjälä240264f2013-08-07 13:29:12 +03002664 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
2665 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
2666 max->cur = ilk_cursor_wm_max(dev, level, config);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002667 max->fbc = ilk_fbc_wm_reg_max(to_i915(dev));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002668}
2669
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002670static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002671 int level,
2672 struct ilk_wm_maximums *max)
2673{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002674 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2675 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2676 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2677 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002678}
2679
Ville Syrjäläd9395652013-10-09 19:18:10 +03002680static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002681 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002682 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002683{
2684 bool ret;
2685
2686 /* already determined to be invalid? */
2687 if (!result->enable)
2688 return false;
2689
2690 result->enable = result->pri_val <= max->pri &&
2691 result->spr_val <= max->spr &&
2692 result->cur_val <= max->cur;
2693
2694 ret = result->enable;
2695
2696 /*
2697 * HACK until we can pre-compute everything,
2698 * and thus fail gracefully if LP0 watermarks
2699 * are exceeded...
2700 */
2701 if (level == 0 && !result->enable) {
2702 if (result->pri_val > max->pri)
2703 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2704 level, result->pri_val, max->pri);
2705 if (result->spr_val > max->spr)
2706 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2707 level, result->spr_val, max->spr);
2708 if (result->cur_val > max->cur)
2709 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2710 level, result->cur_val, max->cur);
2711
2712 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
2713 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
2714 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
2715 result->enable = true;
2716 }
2717
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002718 return ret;
2719}
2720
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002721static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Matt Roper43d59ed2015-09-24 15:53:07 -07002722 const struct intel_crtc *intel_crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002723 int level,
Matt Roper7221fc32015-09-24 15:53:08 -07002724 struct intel_crtc_state *cstate,
Matt Roper86c8bbb2015-09-24 15:53:16 -07002725 struct intel_plane_state *pristate,
2726 struct intel_plane_state *sprstate,
2727 struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002728 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002729{
2730 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2731 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2732 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2733
2734 /* WM1+ latency values stored in 0.5us units */
2735 if (level > 0) {
2736 pri_latency *= 5;
2737 spr_latency *= 5;
2738 cur_latency *= 5;
2739 }
2740
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002741 if (pristate) {
2742 result->pri_val = ilk_compute_pri_wm(cstate, pristate,
2743 pri_latency, level);
2744 result->fbc_val = ilk_compute_fbc_wm(cstate, pristate, result->pri_val);
2745 }
2746
2747 if (sprstate)
2748 result->spr_val = ilk_compute_spr_wm(cstate, sprstate, spr_latency);
2749
2750 if (curstate)
2751 result->cur_val = ilk_compute_cur_wm(cstate, curstate, cur_latency);
2752
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002753 result->enable = true;
2754}
2755
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002756static uint32_t
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002757hsw_compute_linetime_wm(const struct intel_crtc_state *cstate)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002758{
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002759 const struct intel_atomic_state *intel_state =
2760 to_intel_atomic_state(cstate->base.state);
Matt Roperee91a152015-12-03 11:37:39 -08002761 const struct drm_display_mode *adjusted_mode =
2762 &cstate->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002763 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002764
Matt Roperee91a152015-12-03 11:37:39 -08002765 if (!cstate->base.active)
2766 return 0;
2767 if (WARN_ON(adjusted_mode->crtc_clock == 0))
2768 return 0;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002769 if (WARN_ON(intel_state->cdclk.logical.cdclk == 0))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002770 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002771
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002772 /* The WM are computed with base on how long it takes to fill a single
2773 * row at the given clock rate, multiplied by 8.
2774 * */
Ville Syrjälä124abe02015-09-08 13:40:45 +03002775 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2776 adjusted_mode->crtc_clock);
2777 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002778 intel_state->cdclk.logical.cdclk);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002779
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002780 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2781 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002782}
2783
Ville Syrjäläbb726512016-10-31 22:37:24 +02002784static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
2785 uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002786{
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002787 if (INTEL_GEN(dev_priv) >= 9) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002788 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002789 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002790 int level, max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002791
2792 /* read the first set of memory latencies[0:3] */
2793 val = 0; /* data0 to be programmed to 0 for first set */
2794 mutex_lock(&dev_priv->rps.hw_lock);
2795 ret = sandybridge_pcode_read(dev_priv,
2796 GEN9_PCODE_READ_MEM_LATENCY,
2797 &val);
2798 mutex_unlock(&dev_priv->rps.hw_lock);
2799
2800 if (ret) {
2801 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2802 return;
2803 }
2804
2805 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2806 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2807 GEN9_MEM_LATENCY_LEVEL_MASK;
2808 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2809 GEN9_MEM_LATENCY_LEVEL_MASK;
2810 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2811 GEN9_MEM_LATENCY_LEVEL_MASK;
2812
2813 /* read the second set of memory latencies[4:7] */
2814 val = 1; /* data0 to be programmed to 1 for second set */
2815 mutex_lock(&dev_priv->rps.hw_lock);
2816 ret = sandybridge_pcode_read(dev_priv,
2817 GEN9_PCODE_READ_MEM_LATENCY,
2818 &val);
2819 mutex_unlock(&dev_priv->rps.hw_lock);
2820 if (ret) {
2821 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2822 return;
2823 }
2824
2825 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2826 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2827 GEN9_MEM_LATENCY_LEVEL_MASK;
2828 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2829 GEN9_MEM_LATENCY_LEVEL_MASK;
2830 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2831 GEN9_MEM_LATENCY_LEVEL_MASK;
2832
Vandana Kannan367294b2014-11-04 17:06:46 +00002833 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002834 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2835 * need to be disabled. We make sure to sanitize the values out
2836 * of the punit to satisfy this requirement.
2837 */
2838 for (level = 1; level <= max_level; level++) {
2839 if (wm[level] == 0) {
2840 for (i = level + 1; i <= max_level; i++)
2841 wm[i] = 0;
2842 break;
2843 }
2844 }
2845
2846 /*
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002847 * WaWmMemoryReadLatency:skl+,glk
Damien Lespiau6f972352015-02-09 19:33:07 +00002848 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002849 * punit doesn't take into account the read latency so we need
Paulo Zanoni0727e402016-09-22 18:00:30 -03002850 * to add 2us to the various latency levels we retrieve from the
2851 * punit when level 0 response data us 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002852 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002853 if (wm[0] == 0) {
2854 wm[0] += 2;
2855 for (level = 1; level <= max_level; level++) {
2856 if (wm[level] == 0)
2857 break;
Vandana Kannan367294b2014-11-04 17:06:46 +00002858 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002859 }
Paulo Zanoni0727e402016-09-22 18:00:30 -03002860 }
2861
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002862 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002863 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2864
2865 wm[0] = (sskpd >> 56) & 0xFF;
2866 if (wm[0] == 0)
2867 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002868 wm[1] = (sskpd >> 4) & 0xFF;
2869 wm[2] = (sskpd >> 12) & 0xFF;
2870 wm[3] = (sskpd >> 20) & 0x1FF;
2871 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002872 } else if (INTEL_GEN(dev_priv) >= 6) {
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002873 uint32_t sskpd = I915_READ(MCH_SSKPD);
2874
2875 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2876 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2877 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2878 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002879 } else if (INTEL_GEN(dev_priv) >= 5) {
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002880 uint32_t mltr = I915_READ(MLTR_ILK);
2881
2882 /* ILK primary LP0 latency is 700 ns */
2883 wm[0] = 7;
2884 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2885 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002886 } else {
2887 MISSING_CASE(INTEL_DEVID(dev_priv));
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002888 }
2889}
2890
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002891static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
2892 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002893{
2894 /* ILK sprite LP0 latency is 1300 ns */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002895 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002896 wm[0] = 13;
2897}
2898
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002899static void intel_fixup_cur_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 cursor LP0 latency is 1300 ns */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002903 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002904 wm[0] = 13;
2905
2906 /* WaDoubleCursorLP3Latency:ivb */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002907 if (IS_IVYBRIDGE(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002908 wm[3] *= 2;
2909}
2910
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002911int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002912{
2913 /* how many WM levels are we expecting */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002914 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002915 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002916 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002917 return 4;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002918 else if (INTEL_GEN(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002919 return 3;
2920 else
2921 return 2;
2922}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002923
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002924static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002925 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002926 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002927{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002928 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002929
2930 for (level = 0; level <= max_level; level++) {
2931 unsigned int latency = wm[level];
2932
2933 if (latency == 0) {
2934 DRM_ERROR("%s WM%d latency not provided\n",
2935 name, level);
2936 continue;
2937 }
2938
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002939 /*
2940 * - latencies are in us on gen9.
2941 * - before then, WM1+ latency values are in 0.5us units
2942 */
Paulo Zanonidfc267a2017-08-09 13:52:46 -07002943 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002944 latency *= 10;
2945 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002946 latency *= 5;
2947
2948 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2949 name, level, wm[level],
2950 latency / 10, latency % 10);
2951 }
2952}
2953
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002954static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2955 uint16_t wm[5], uint16_t min)
2956{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002957 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002958
2959 if (wm[0] >= min)
2960 return false;
2961
2962 wm[0] = max(wm[0], min);
2963 for (level = 1; level <= max_level; level++)
2964 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2965
2966 return true;
2967}
2968
Ville Syrjäläbb726512016-10-31 22:37:24 +02002969static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002970{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002971 bool changed;
2972
2973 /*
2974 * The BIOS provided WM memory latency values are often
2975 * inadequate for high resolution displays. Adjust them.
2976 */
2977 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2978 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2979 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2980
2981 if (!changed)
2982 return;
2983
2984 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002985 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
2986 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
2987 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002988}
2989
Ville Syrjäläbb726512016-10-31 22:37:24 +02002990static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002991{
Ville Syrjäläbb726512016-10-31 22:37:24 +02002992 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002993
2994 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2995 sizeof(dev_priv->wm.pri_latency));
2996 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2997 sizeof(dev_priv->wm.pri_latency));
2998
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002999 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003000 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003001
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003002 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3003 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3004 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003005
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003006 if (IS_GEN6(dev_priv))
Ville Syrjäläbb726512016-10-31 22:37:24 +02003007 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03003008}
3009
Ville Syrjäläbb726512016-10-31 22:37:24 +02003010static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003011{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003012 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003013 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003014}
3015
Matt Ropered4a6a72016-02-23 17:20:13 -08003016static bool ilk_validate_pipe_wm(struct drm_device *dev,
3017 struct intel_pipe_wm *pipe_wm)
3018{
3019 /* LP0 watermark maximums depend on this pipe alone */
3020 const struct intel_wm_config config = {
3021 .num_pipes_active = 1,
3022 .sprites_enabled = pipe_wm->sprites_enabled,
3023 .sprites_scaled = pipe_wm->sprites_scaled,
3024 };
3025 struct ilk_wm_maximums max;
3026
3027 /* LP0 watermarks always use 1/2 DDB partitioning */
3028 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
3029
3030 /* At least LP0 must be valid */
3031 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
3032 DRM_DEBUG_KMS("LP0 watermark invalid\n");
3033 return false;
3034 }
3035
3036 return true;
3037}
3038
Matt Roper261a27d2015-10-08 15:28:25 -07003039/* Compute new watermarks for the pipe */
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003040static int ilk_compute_pipe_wm(struct intel_crtc_state *cstate)
Matt Roper261a27d2015-10-08 15:28:25 -07003041{
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003042 struct drm_atomic_state *state = cstate->base.state;
3043 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07003044 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003045 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003046 const struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper43d59ed2015-09-24 15:53:07 -07003047 struct intel_plane *intel_plane;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003048 struct intel_plane_state *pristate = NULL;
Matt Roper43d59ed2015-09-24 15:53:07 -07003049 struct intel_plane_state *sprstate = NULL;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003050 struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003051 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02003052 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003053
Matt Ropere8f1f022016-05-12 07:05:55 -07003054 pipe_wm = &cstate->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003055
Matt Roper43d59ed2015-09-24 15:53:07 -07003056 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003057 struct intel_plane_state *ps;
3058
3059 ps = intel_atomic_get_existing_plane_state(state,
3060 intel_plane);
3061 if (!ps)
3062 continue;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003063
3064 if (intel_plane->base.type == DRM_PLANE_TYPE_PRIMARY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003065 pristate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003066 else if (intel_plane->base.type == DRM_PLANE_TYPE_OVERLAY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003067 sprstate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003068 else if (intel_plane->base.type == DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003069 curstate = ps;
Matt Roper43d59ed2015-09-24 15:53:07 -07003070 }
3071
Matt Ropered4a6a72016-02-23 17:20:13 -08003072 pipe_wm->pipe_enabled = cstate->base.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003073 if (sprstate) {
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003074 pipe_wm->sprites_enabled = sprstate->base.visible;
3075 pipe_wm->sprites_scaled = sprstate->base.visible &&
3076 (drm_rect_width(&sprstate->base.dst) != drm_rect_width(&sprstate->base.src) >> 16 ||
3077 drm_rect_height(&sprstate->base.dst) != drm_rect_height(&sprstate->base.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003078 }
3079
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003080 usable_level = max_level;
3081
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003082 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003083 if (INTEL_GEN(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003084 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003085
3086 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08003087 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003088 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003089
Matt Roper86c8bbb2015-09-24 15:53:16 -07003090 ilk_compute_wm_level(dev_priv, intel_crtc, 0, cstate,
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003091 pristate, sprstate, curstate, &pipe_wm->raw_wm[0]);
3092
3093 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
3094 pipe_wm->wm[0] = pipe_wm->raw_wm[0];
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003095
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003096 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjälä532f7a72016-04-29 17:31:17 +03003097 pipe_wm->linetime = hsw_compute_linetime_wm(cstate);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003098
Matt Ropered4a6a72016-02-23 17:20:13 -08003099 if (!ilk_validate_pipe_wm(dev, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01003100 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003101
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003102 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003103
3104 for (level = 1; level <= max_level; level++) {
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003105 struct intel_wm_level *wm = &pipe_wm->raw_wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003106
Matt Roper86c8bbb2015-09-24 15:53:16 -07003107 ilk_compute_wm_level(dev_priv, intel_crtc, level, cstate,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003108 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003109
3110 /*
3111 * Disable any watermark level that exceeds the
3112 * register maximums since such watermarks are
3113 * always invalid.
3114 */
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003115 if (level > usable_level)
3116 continue;
3117
3118 if (ilk_validate_wm_level(level, &max, wm))
3119 pipe_wm->wm[level] = *wm;
3120 else
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003121 usable_level = level;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003122 }
3123
Matt Roper86c8bbb2015-09-24 15:53:16 -07003124 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003125}
3126
3127/*
Matt Ropered4a6a72016-02-23 17:20:13 -08003128 * Build a set of 'intermediate' watermark values that satisfy both the old
3129 * state and the new state. These can be programmed to the hardware
3130 * immediately.
3131 */
3132static int ilk_compute_intermediate_wm(struct drm_device *dev,
3133 struct intel_crtc *intel_crtc,
3134 struct intel_crtc_state *newstate)
3135{
Matt Ropere8f1f022016-05-12 07:05:55 -07003136 struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08003137 struct intel_pipe_wm *b = &intel_crtc->wm.active.ilk;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003138 int level, max_level = ilk_wm_max_level(to_i915(dev));
Matt Ropered4a6a72016-02-23 17:20:13 -08003139
3140 /*
3141 * Start with the final, target watermarks, then combine with the
3142 * currently active watermarks to get values that are safe both before
3143 * and after the vblank.
3144 */
Matt Ropere8f1f022016-05-12 07:05:55 -07003145 *a = newstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08003146 a->pipe_enabled |= b->pipe_enabled;
3147 a->sprites_enabled |= b->sprites_enabled;
3148 a->sprites_scaled |= b->sprites_scaled;
3149
3150 for (level = 0; level <= max_level; level++) {
3151 struct intel_wm_level *a_wm = &a->wm[level];
3152 const struct intel_wm_level *b_wm = &b->wm[level];
3153
3154 a_wm->enable &= b_wm->enable;
3155 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
3156 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
3157 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
3158 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
3159 }
3160
3161 /*
3162 * We need to make sure that these merged watermark values are
3163 * actually a valid configuration themselves. If they're not,
3164 * there's no safe way to transition from the old state to
3165 * the new state, so we need to fail the atomic transaction.
3166 */
3167 if (!ilk_validate_pipe_wm(dev, a))
3168 return -EINVAL;
3169
3170 /*
3171 * If our intermediate WM are identical to the final WM, then we can
3172 * omit the post-vblank programming; only update if it's different.
3173 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02003174 if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) != 0)
3175 newstate->wm.need_postvbl_update = true;
Matt Ropered4a6a72016-02-23 17:20:13 -08003176
3177 return 0;
3178}
3179
3180/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003181 * Merge the watermarks from all active pipes for a specific level.
3182 */
3183static void ilk_merge_wm_level(struct drm_device *dev,
3184 int level,
3185 struct intel_wm_level *ret_wm)
3186{
3187 const struct intel_crtc *intel_crtc;
3188
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003189 ret_wm->enable = true;
3190
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003191 for_each_intel_crtc(dev, intel_crtc) {
Matt Ropered4a6a72016-02-23 17:20:13 -08003192 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02003193 const struct intel_wm_level *wm = &active->wm[level];
3194
3195 if (!active->pipe_enabled)
3196 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003197
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003198 /*
3199 * The watermark values may have been used in the past,
3200 * so we must maintain them in the registers for some
3201 * time even if the level is now disabled.
3202 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003203 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003204 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003205
3206 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
3207 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
3208 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
3209 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
3210 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003211}
3212
3213/*
3214 * Merge all low power watermarks for all active pipes.
3215 */
3216static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003217 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02003218 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003219 struct intel_pipe_wm *merged)
3220{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003221 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003222 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003223 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003224
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003225 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003226 if ((INTEL_GEN(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003227 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03003228 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003229
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003230 /* ILK: FBC WM must be disabled always */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003231 merged->fbc_wm_enabled = INTEL_GEN(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003232
3233 /* merge each WM1+ level */
3234 for (level = 1; level <= max_level; level++) {
3235 struct intel_wm_level *wm = &merged->wm[level];
3236
3237 ilk_merge_wm_level(dev, level, wm);
3238
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003239 if (level > last_enabled_level)
3240 wm->enable = false;
3241 else if (!ilk_validate_wm_level(level, max, wm))
3242 /* make sure all following levels get disabled */
3243 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003244
3245 /*
3246 * The spec says it is preferred to disable
3247 * FBC WMs instead of disabling a WM level.
3248 */
3249 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003250 if (wm->enable)
3251 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003252 wm->fbc_val = 0;
3253 }
3254 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003255
3256 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
3257 /*
3258 * FIXME this is racy. FBC might get enabled later.
3259 * What we should check here is whether FBC can be
3260 * enabled sometime later.
3261 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003262 if (IS_GEN5(dev_priv) && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03003263 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003264 for (level = 2; level <= max_level; level++) {
3265 struct intel_wm_level *wm = &merged->wm[level];
3266
3267 wm->enable = false;
3268 }
3269 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003270}
3271
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003272static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
3273{
3274 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
3275 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
3276}
3277
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003278/* The value we need to program into the WM_LPx latency field */
3279static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
3280{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003281 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003282
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003283 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003284 return 2 * level;
3285 else
3286 return dev_priv->wm.pri_latency[level];
3287}
3288
Imre Deak820c1982013-12-17 14:46:36 +02003289static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03003290 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03003291 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02003292 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003293{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003294 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003295 struct intel_crtc *intel_crtc;
3296 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003297
Ville Syrjälä0362c782013-10-09 19:17:57 +03003298 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03003299 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003300
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003301 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03003302 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03003303 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003304
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003305 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003306
Ville Syrjälä0362c782013-10-09 19:17:57 +03003307 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03003308
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003309 /*
3310 * Maintain the watermark values even if the level is
3311 * disabled. Doing otherwise could cause underruns.
3312 */
3313 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003314 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07003315 (r->pri_val << WM1_LP_SR_SHIFT) |
3316 r->cur_val;
3317
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003318 if (r->enable)
3319 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
3320
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003321 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07003322 results->wm_lp[wm_lp - 1] |=
3323 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
3324 else
3325 results->wm_lp[wm_lp - 1] |=
3326 r->fbc_val << WM1_LP_FBC_SHIFT;
3327
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003328 /*
3329 * Always set WM1S_LP_EN when spr_val != 0, even if the
3330 * level is disabled. Doing otherwise could cause underruns.
3331 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003332 if (INTEL_GEN(dev_priv) <= 6 && r->spr_val) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003333 WARN_ON(wm_lp != 1);
3334 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
3335 } else
3336 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003337 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003338
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003339 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003340 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003341 enum pipe pipe = intel_crtc->pipe;
Matt Ropered4a6a72016-02-23 17:20:13 -08003342 const struct intel_wm_level *r =
3343 &intel_crtc->wm.active.ilk.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003344
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003345 if (WARN_ON(!r->enable))
3346 continue;
3347
Matt Ropered4a6a72016-02-23 17:20:13 -08003348 results->wm_linetime[pipe] = intel_crtc->wm.active.ilk.linetime;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003349
3350 results->wm_pipe[pipe] =
3351 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
3352 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
3353 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003354 }
3355}
3356
Paulo Zanoni861f3382013-05-31 10:19:21 -03003357/* Find the result with the highest level enabled. Check for enable_fbc_wm in
3358 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02003359static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003360 struct intel_pipe_wm *r1,
3361 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003362{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003363 int level, max_level = ilk_wm_max_level(to_i915(dev));
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003364 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003365
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003366 for (level = 1; level <= max_level; level++) {
3367 if (r1->wm[level].enable)
3368 level1 = level;
3369 if (r2->wm[level].enable)
3370 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003371 }
3372
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003373 if (level1 == level2) {
3374 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003375 return r2;
3376 else
3377 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003378 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03003379 return r1;
3380 } else {
3381 return r2;
3382 }
3383}
3384
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003385/* dirty bits used to track which watermarks need changes */
3386#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
3387#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
3388#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
3389#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
3390#define WM_DIRTY_FBC (1 << 24)
3391#define WM_DIRTY_DDB (1 << 25)
3392
Damien Lespiau055e3932014-08-18 13:49:10 +01003393static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02003394 const struct ilk_wm_values *old,
3395 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003396{
3397 unsigned int dirty = 0;
3398 enum pipe pipe;
3399 int wm_lp;
3400
Damien Lespiau055e3932014-08-18 13:49:10 +01003401 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003402 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
3403 dirty |= WM_DIRTY_LINETIME(pipe);
3404 /* Must disable LP1+ watermarks too */
3405 dirty |= WM_DIRTY_LP_ALL;
3406 }
3407
3408 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
3409 dirty |= WM_DIRTY_PIPE(pipe);
3410 /* Must disable LP1+ watermarks too */
3411 dirty |= WM_DIRTY_LP_ALL;
3412 }
3413 }
3414
3415 if (old->enable_fbc_wm != new->enable_fbc_wm) {
3416 dirty |= WM_DIRTY_FBC;
3417 /* Must disable LP1+ watermarks too */
3418 dirty |= WM_DIRTY_LP_ALL;
3419 }
3420
3421 if (old->partitioning != new->partitioning) {
3422 dirty |= WM_DIRTY_DDB;
3423 /* Must disable LP1+ watermarks too */
3424 dirty |= WM_DIRTY_LP_ALL;
3425 }
3426
3427 /* LP1+ watermarks already deemed dirty, no need to continue */
3428 if (dirty & WM_DIRTY_LP_ALL)
3429 return dirty;
3430
3431 /* Find the lowest numbered LP1+ watermark in need of an update... */
3432 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3433 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
3434 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
3435 break;
3436 }
3437
3438 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
3439 for (; wm_lp <= 3; wm_lp++)
3440 dirty |= WM_DIRTY_LP(wm_lp);
3441
3442 return dirty;
3443}
3444
Ville Syrjälä8553c182013-12-05 15:51:39 +02003445static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
3446 unsigned int dirty)
3447{
Imre Deak820c1982013-12-17 14:46:36 +02003448 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02003449 bool changed = false;
3450
3451 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
3452 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
3453 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
3454 changed = true;
3455 }
3456 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
3457 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
3458 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
3459 changed = true;
3460 }
3461 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
3462 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
3463 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
3464 changed = true;
3465 }
3466
3467 /*
3468 * Don't touch WM1S_LP_EN here.
3469 * Doing so could cause underruns.
3470 */
3471
3472 return changed;
3473}
3474
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003475/*
3476 * The spec says we shouldn't write when we don't need, because every write
3477 * causes WMs to be re-evaluated, expending some power.
3478 */
Imre Deak820c1982013-12-17 14:46:36 +02003479static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
3480 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003481{
Imre Deak820c1982013-12-17 14:46:36 +02003482 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003483 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003484 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003485
Damien Lespiau055e3932014-08-18 13:49:10 +01003486 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003487 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003488 return;
3489
Ville Syrjälä8553c182013-12-05 15:51:39 +02003490 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003491
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003492 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003493 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003494 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003495 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003496 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003497 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
3498
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003499 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003500 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003501 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003502 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003503 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003504 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
3505
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003506 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003507 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003508 val = I915_READ(WM_MISC);
3509 if (results->partitioning == INTEL_DDB_PART_1_2)
3510 val &= ~WM_MISC_DATA_PARTITION_5_6;
3511 else
3512 val |= WM_MISC_DATA_PARTITION_5_6;
3513 I915_WRITE(WM_MISC, val);
3514 } else {
3515 val = I915_READ(DISP_ARB_CTL2);
3516 if (results->partitioning == INTEL_DDB_PART_1_2)
3517 val &= ~DISP_DATA_PARTITION_5_6;
3518 else
3519 val |= DISP_DATA_PARTITION_5_6;
3520 I915_WRITE(DISP_ARB_CTL2, val);
3521 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003522 }
3523
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003524 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03003525 val = I915_READ(DISP_ARB_CTL);
3526 if (results->enable_fbc_wm)
3527 val &= ~DISP_FBC_WM_DIS;
3528 else
3529 val |= DISP_FBC_WM_DIS;
3530 I915_WRITE(DISP_ARB_CTL, val);
3531 }
3532
Imre Deak954911e2013-12-17 14:46:34 +02003533 if (dirty & WM_DIRTY_LP(1) &&
3534 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
3535 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
3536
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003537 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003538 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
3539 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
3540 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
3541 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
3542 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003543
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003544 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003545 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003546 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003547 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003548 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003549 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003550
3551 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003552}
3553
Matt Ropered4a6a72016-02-23 17:20:13 -08003554bool ilk_disable_lp_wm(struct drm_device *dev)
Ville Syrjälä8553c182013-12-05 15:51:39 +02003555{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003556 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003557
3558 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3559}
3560
Matt Roper024c9042015-09-24 15:53:11 -07003561/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003562 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3563 * so assume we'll always need it in order to avoid underruns.
3564 */
3565static bool skl_needs_memory_bw_wa(struct intel_atomic_state *state)
3566{
3567 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3568
Rodrigo Vivib976dc52017-01-23 10:32:37 -08003569 if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv))
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003570 return true;
3571
3572 return false;
3573}
3574
Paulo Zanoni56feca92016-09-22 18:00:28 -03003575static bool
3576intel_has_sagv(struct drm_i915_private *dev_priv)
3577{
Rodrigo Vivi01971812017-08-09 13:52:44 -07003578 if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv) ||
3579 IS_CANNONLAKE(dev_priv))
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003580 return true;
3581
3582 if (IS_SKYLAKE(dev_priv) &&
3583 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED)
3584 return true;
3585
3586 return false;
Paulo Zanoni56feca92016-09-22 18:00:28 -03003587}
3588
Lyude656d1b82016-08-17 15:55:54 -04003589/*
3590 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3591 * depending on power and performance requirements. The display engine access
3592 * to system memory is blocked during the adjustment time. Because of the
3593 * blocking time, having this enabled can cause full system hangs and/or pipe
3594 * underruns if we don't meet all of the following requirements:
3595 *
3596 * - <= 1 pipe enabled
3597 * - All planes can enable watermarks for latencies >= SAGV engine block time
3598 * - We're not using an interlaced display configuration
3599 */
3600int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003601intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003602{
3603 int ret;
3604
Paulo Zanoni56feca92016-09-22 18:00:28 -03003605 if (!intel_has_sagv(dev_priv))
3606 return 0;
3607
3608 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003609 return 0;
3610
3611 DRM_DEBUG_KMS("Enabling the SAGV\n");
3612 mutex_lock(&dev_priv->rps.hw_lock);
3613
3614 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3615 GEN9_SAGV_ENABLE);
3616
3617 /* We don't need to wait for the SAGV when enabling */
3618 mutex_unlock(&dev_priv->rps.hw_lock);
3619
3620 /*
3621 * Some skl systems, pre-release machines in particular,
3622 * don't actually have an SAGV.
3623 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003624 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003625 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003626 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003627 return 0;
3628 } else if (ret < 0) {
3629 DRM_ERROR("Failed to enable the SAGV\n");
3630 return ret;
3631 }
3632
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003633 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003634 return 0;
3635}
3636
Lyude656d1b82016-08-17 15:55:54 -04003637int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003638intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003639{
Imre Deakb3b8e992016-12-05 18:27:38 +02003640 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003641
Paulo Zanoni56feca92016-09-22 18:00:28 -03003642 if (!intel_has_sagv(dev_priv))
3643 return 0;
3644
3645 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003646 return 0;
3647
3648 DRM_DEBUG_KMS("Disabling the SAGV\n");
3649 mutex_lock(&dev_priv->rps.hw_lock);
3650
3651 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003652 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3653 GEN9_SAGV_DISABLE,
3654 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3655 1);
Lyude656d1b82016-08-17 15:55:54 -04003656 mutex_unlock(&dev_priv->rps.hw_lock);
3657
Lyude656d1b82016-08-17 15:55:54 -04003658 /*
3659 * Some skl systems, pre-release machines in particular,
3660 * don't actually have an SAGV.
3661 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003662 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003663 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003664 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003665 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003666 } else if (ret < 0) {
3667 DRM_ERROR("Failed to disable the SAGV (%d)\n", ret);
3668 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003669 }
3670
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003671 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003672 return 0;
3673}
3674
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003675bool intel_can_enable_sagv(struct drm_atomic_state *state)
Lyude656d1b82016-08-17 15:55:54 -04003676{
3677 struct drm_device *dev = state->dev;
3678 struct drm_i915_private *dev_priv = to_i915(dev);
3679 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003680 struct intel_crtc *crtc;
3681 struct intel_plane *plane;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003682 struct intel_crtc_state *cstate;
Lyude656d1b82016-08-17 15:55:54 -04003683 enum pipe pipe;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003684 int level, latency;
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003685 int sagv_block_time_us = IS_GEN9(dev_priv) ? 30 : 20;
Lyude656d1b82016-08-17 15:55:54 -04003686
Paulo Zanoni56feca92016-09-22 18:00:28 -03003687 if (!intel_has_sagv(dev_priv))
3688 return false;
3689
Lyude656d1b82016-08-17 15:55:54 -04003690 /*
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003691 * SKL+ workaround: bspec recommends we disable the SAGV when we have
Lyude656d1b82016-08-17 15:55:54 -04003692 * more then one pipe enabled
3693 *
3694 * If there are no active CRTCs, no additional checks need be performed
3695 */
3696 if (hweight32(intel_state->active_crtcs) == 0)
3697 return true;
3698 else if (hweight32(intel_state->active_crtcs) > 1)
3699 return false;
3700
3701 /* Since we're now guaranteed to only have one active CRTC... */
3702 pipe = ffs(intel_state->active_crtcs) - 1;
Ville Syrjälä98187832016-10-31 22:37:10 +02003703 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003704 cstate = to_intel_crtc_state(crtc->base.state);
Lyude656d1b82016-08-17 15:55:54 -04003705
Paulo Zanonic89cadd2016-10-10 17:30:59 -03003706 if (crtc->base.state->adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003707 return false;
3708
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003709 for_each_intel_plane_on_crtc(dev, crtc, plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003710 struct skl_plane_wm *wm =
3711 &cstate->wm.skl.optimal.planes[plane->id];
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003712
Lyude656d1b82016-08-17 15:55:54 -04003713 /* Skip this plane if it's not enabled */
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003714 if (!wm->wm[0].plane_en)
Lyude656d1b82016-08-17 15:55:54 -04003715 continue;
3716
3717 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003718 for (level = ilk_wm_max_level(dev_priv);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003719 !wm->wm[level].plane_en; --level)
Lyude656d1b82016-08-17 15:55:54 -04003720 { }
3721
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003722 latency = dev_priv->wm.skl_latency[level];
3723
3724 if (skl_needs_memory_bw_wa(intel_state) &&
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003725 plane->base.state->fb->modifier ==
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003726 I915_FORMAT_MOD_X_TILED)
3727 latency += 15;
3728
Lyude656d1b82016-08-17 15:55:54 -04003729 /*
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003730 * If any of the planes on this pipe don't enable wm levels that
3731 * incur memory latencies higher than sagv_block_time_us we
3732 * can't enable the SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003733 */
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003734 if (latency < sagv_block_time_us)
Lyude656d1b82016-08-17 15:55:54 -04003735 return false;
3736 }
3737
3738 return true;
3739}
3740
Damien Lespiaub9cec072014-11-04 17:06:43 +00003741static void
3742skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
Matt Roper024c9042015-09-24 15:53:11 -07003743 const struct intel_crtc_state *cstate,
Matt Roperc107acf2016-05-12 07:06:01 -07003744 struct skl_ddb_entry *alloc, /* out */
3745 int *num_active /* out */)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003746{
Matt Roperc107acf2016-05-12 07:06:01 -07003747 struct drm_atomic_state *state = cstate->base.state;
3748 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
3749 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper024c9042015-09-24 15:53:11 -07003750 struct drm_crtc *for_crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003751 unsigned int pipe_size, ddb_size;
3752 int nth_active_pipe;
Matt Roperc107acf2016-05-12 07:06:01 -07003753
Matt Ropera6d3460e2016-05-12 07:06:04 -07003754 if (WARN_ON(!state) || !cstate->base.active) {
Damien Lespiaub9cec072014-11-04 17:06:43 +00003755 alloc->start = 0;
3756 alloc->end = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003757 *num_active = hweight32(dev_priv->active_crtcs);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003758 return;
3759 }
3760
Matt Ropera6d3460e2016-05-12 07:06:04 -07003761 if (intel_state->active_pipe_changes)
3762 *num_active = hweight32(intel_state->active_crtcs);
3763 else
3764 *num_active = hweight32(dev_priv->active_crtcs);
3765
Deepak M6f3fff62016-09-15 15:01:10 +05303766 ddb_size = INTEL_INFO(dev_priv)->ddb_size;
3767 WARN_ON(ddb_size == 0);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003768
3769 ddb_size -= 4; /* 4 blocks for bypass path allocation */
3770
Matt Roperc107acf2016-05-12 07:06:01 -07003771 /*
Matt Ropera6d3460e2016-05-12 07:06:04 -07003772 * If the state doesn't change the active CRTC's, then there's
3773 * no need to recalculate; the existing pipe allocation limits
3774 * should remain unchanged. Note that we're safe from racing
3775 * commits since any racing commit that changes the active CRTC
3776 * list would need to grab _all_ crtc locks, including the one
3777 * we currently hold.
Matt Roperc107acf2016-05-12 07:06:01 -07003778 */
Matt Ropera6d3460e2016-05-12 07:06:04 -07003779 if (!intel_state->active_pipe_changes) {
Maarten Lankhorst512b5522016-11-08 13:55:34 +01003780 /*
3781 * alloc may be cleared by clear_intel_crtc_state,
3782 * copy from old state to be sure
3783 */
3784 *alloc = to_intel_crtc_state(for_crtc->state)->wm.skl.ddb;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003785 return;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003786 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07003787
3788 nth_active_pipe = hweight32(intel_state->active_crtcs &
3789 (drm_crtc_mask(for_crtc) - 1));
3790 pipe_size = ddb_size / hweight32(intel_state->active_crtcs);
3791 alloc->start = nth_active_pipe * ddb_size / *num_active;
3792 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003793}
3794
Matt Roperc107acf2016-05-12 07:06:01 -07003795static unsigned int skl_cursor_allocation(int num_active)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003796{
Matt Roperc107acf2016-05-12 07:06:01 -07003797 if (num_active == 1)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003798 return 32;
3799
3800 return 8;
3801}
3802
Damien Lespiaua269c582014-11-04 17:06:49 +00003803static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
3804{
3805 entry->start = reg & 0x3ff;
3806 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00003807 if (entry->end)
3808 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00003809}
3810
Damien Lespiau08db6652014-11-04 17:06:52 +00003811void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
3812 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00003813{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003814 struct intel_crtc *crtc;
Damien Lespiaua269c582014-11-04 17:06:49 +00003815
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003816 memset(ddb, 0, sizeof(*ddb));
3817
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003818 for_each_intel_crtc(&dev_priv->drm, crtc) {
Imre Deak4d800032016-02-17 16:31:29 +02003819 enum intel_display_power_domain power_domain;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003820 enum plane_id plane_id;
3821 enum pipe pipe = crtc->pipe;
Imre Deak4d800032016-02-17 16:31:29 +02003822
3823 power_domain = POWER_DOMAIN_PIPE(pipe);
3824 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003825 continue;
3826
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003827 for_each_plane_id_on_crtc(crtc, plane_id) {
3828 u32 val;
Damien Lespiaua269c582014-11-04 17:06:49 +00003829
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003830 if (plane_id != PLANE_CURSOR)
3831 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
3832 else
3833 val = I915_READ(CUR_BUF_CFG(pipe));
3834
3835 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane_id], val);
3836 }
Imre Deak4d800032016-02-17 16:31:29 +02003837
3838 intel_display_power_put(dev_priv, power_domain);
Damien Lespiaua269c582014-11-04 17:06:49 +00003839 }
3840}
3841
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003842/*
3843 * Determines the downscale amount of a plane for the purposes of watermark calculations.
3844 * The bspec defines downscale amount as:
3845 *
3846 * """
3847 * Horizontal down scale amount = maximum[1, Horizontal source size /
3848 * Horizontal destination size]
3849 * Vertical down scale amount = maximum[1, Vertical source size /
3850 * Vertical destination size]
3851 * Total down scale amount = Horizontal down scale amount *
3852 * Vertical down scale amount
3853 * """
3854 *
3855 * Return value is provided in 16.16 fixed point form to retain fractional part.
3856 * Caller should take care of dividing & rounding off the value.
3857 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303858static uint_fixed_16_16_t
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003859skl_plane_downscale_amount(const struct intel_crtc_state *cstate,
3860 const struct intel_plane_state *pstate)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003861{
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003862 struct intel_plane *plane = to_intel_plane(pstate->base.plane);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003863 uint32_t src_w, src_h, dst_w, dst_h;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303864 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
3865 uint_fixed_16_16_t downscale_h, downscale_w;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003866
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003867 if (WARN_ON(!intel_wm_plane_visible(cstate, pstate)))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303868 return u32_to_fixed16(0);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003869
3870 /* n.b., src is 16.16 fixed point, dst is whole integer */
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003871 if (plane->id == PLANE_CURSOR) {
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03003872 /*
3873 * Cursors only support 0/180 degree rotation,
3874 * hence no need to account for rotation here.
3875 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303876 src_w = pstate->base.src_w >> 16;
3877 src_h = pstate->base.src_h >> 16;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003878 dst_w = pstate->base.crtc_w;
3879 dst_h = pstate->base.crtc_h;
3880 } else {
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03003881 /*
3882 * Src coordinates are already rotated by 270 degrees for
3883 * the 90/270 degree plane rotation cases (to match the
3884 * GTT mapping), hence no need to account for rotation here.
3885 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303886 src_w = drm_rect_width(&pstate->base.src) >> 16;
3887 src_h = drm_rect_height(&pstate->base.src) >> 16;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003888 dst_w = drm_rect_width(&pstate->base.dst);
3889 dst_h = drm_rect_height(&pstate->base.dst);
3890 }
3891
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303892 fp_w_ratio = div_fixed16(src_w, dst_w);
3893 fp_h_ratio = div_fixed16(src_h, dst_h);
3894 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
3895 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003896
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303897 return mul_fixed16(downscale_w, downscale_h);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003898}
3899
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303900static uint_fixed_16_16_t
3901skl_pipe_downscale_amount(const struct intel_crtc_state *crtc_state)
3902{
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303903 uint_fixed_16_16_t pipe_downscale = u32_to_fixed16(1);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303904
3905 if (!crtc_state->base.enable)
3906 return pipe_downscale;
3907
3908 if (crtc_state->pch_pfit.enabled) {
3909 uint32_t src_w, src_h, dst_w, dst_h;
3910 uint32_t pfit_size = crtc_state->pch_pfit.size;
3911 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
3912 uint_fixed_16_16_t downscale_h, downscale_w;
3913
3914 src_w = crtc_state->pipe_src_w;
3915 src_h = crtc_state->pipe_src_h;
3916 dst_w = pfit_size >> 16;
3917 dst_h = pfit_size & 0xffff;
3918
3919 if (!dst_w || !dst_h)
3920 return pipe_downscale;
3921
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303922 fp_w_ratio = div_fixed16(src_w, dst_w);
3923 fp_h_ratio = div_fixed16(src_h, dst_h);
3924 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
3925 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303926
3927 pipe_downscale = mul_fixed16(downscale_w, downscale_h);
3928 }
3929
3930 return pipe_downscale;
3931}
3932
3933int skl_check_pipe_max_pixel_rate(struct intel_crtc *intel_crtc,
3934 struct intel_crtc_state *cstate)
3935{
3936 struct drm_crtc_state *crtc_state = &cstate->base;
3937 struct drm_atomic_state *state = crtc_state->state;
3938 struct drm_plane *plane;
3939 const struct drm_plane_state *pstate;
3940 struct intel_plane_state *intel_pstate;
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02003941 int crtc_clock, dotclk;
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303942 uint32_t pipe_max_pixel_rate;
3943 uint_fixed_16_16_t pipe_downscale;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303944 uint_fixed_16_16_t max_downscale = u32_to_fixed16(1);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303945
3946 if (!cstate->base.enable)
3947 return 0;
3948
3949 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, crtc_state) {
3950 uint_fixed_16_16_t plane_downscale;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303951 uint_fixed_16_16_t fp_9_div_8 = div_fixed16(9, 8);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303952 int bpp;
3953
3954 if (!intel_wm_plane_visible(cstate,
3955 to_intel_plane_state(pstate)))
3956 continue;
3957
3958 if (WARN_ON(!pstate->fb))
3959 return -EINVAL;
3960
3961 intel_pstate = to_intel_plane_state(pstate);
3962 plane_downscale = skl_plane_downscale_amount(cstate,
3963 intel_pstate);
3964 bpp = pstate->fb->format->cpp[0] * 8;
3965 if (bpp == 64)
3966 plane_downscale = mul_fixed16(plane_downscale,
3967 fp_9_div_8);
3968
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303969 max_downscale = max_fixed16(plane_downscale, max_downscale);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303970 }
3971 pipe_downscale = skl_pipe_downscale_amount(cstate);
3972
3973 pipe_downscale = mul_fixed16(pipe_downscale, max_downscale);
3974
3975 crtc_clock = crtc_state->adjusted_mode.crtc_clock;
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02003976 dotclk = to_intel_atomic_state(state)->cdclk.logical.cdclk;
3977
3978 if (IS_GEMINILAKE(to_i915(intel_crtc->base.dev)))
3979 dotclk *= 2;
3980
3981 pipe_max_pixel_rate = div_round_up_u32_fixed16(dotclk, pipe_downscale);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303982
3983 if (pipe_max_pixel_rate < crtc_clock) {
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02003984 DRM_DEBUG_KMS("Max supported pixel clock with scaling exceeded\n");
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303985 return -EINVAL;
3986 }
3987
3988 return 0;
3989}
3990
Damien Lespiaub9cec072014-11-04 17:06:43 +00003991static unsigned int
Matt Roper024c9042015-09-24 15:53:11 -07003992skl_plane_relative_data_rate(const struct intel_crtc_state *cstate,
3993 const struct drm_plane_state *pstate,
3994 int y)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003995{
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003996 struct intel_plane *plane = to_intel_plane(pstate->plane);
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003997 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303998 uint32_t data_rate;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003999 uint32_t width = 0, height = 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02004000 struct drm_framebuffer *fb;
4001 u32 format;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304002 uint_fixed_16_16_t down_scale_amount;
Matt Ropera1de91e2016-05-12 07:05:57 -07004003
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004004 if (!intel_pstate->base.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07004005 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02004006
4007 fb = pstate->fb;
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004008 format = fb->format->format;
Ville Syrjälä83054942016-11-18 21:53:00 +02004009
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004010 if (plane->id == PLANE_CURSOR)
Matt Ropera1de91e2016-05-12 07:05:57 -07004011 return 0;
4012 if (y && format != DRM_FORMAT_NV12)
4013 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004014
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004015 /*
4016 * Src coordinates are already rotated by 270 degrees for
4017 * the 90/270 degree plane rotation cases (to match the
4018 * GTT mapping), hence no need to account for rotation here.
4019 */
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004020 width = drm_rect_width(&intel_pstate->base.src) >> 16;
4021 height = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004022
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004023 /* for planar format */
Matt Ropera1de91e2016-05-12 07:05:57 -07004024 if (format == DRM_FORMAT_NV12) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004025 if (y) /* y-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004026 data_rate = width * height *
Ville Syrjälä353c8592016-12-14 23:30:57 +02004027 fb->format->cpp[0];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004028 else /* uv-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004029 data_rate = (width / 2) * (height / 2) *
Ville Syrjälä353c8592016-12-14 23:30:57 +02004030 fb->format->cpp[1];
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004031 } else {
4032 /* for packed formats */
Ville Syrjälä353c8592016-12-14 23:30:57 +02004033 data_rate = width * height * fb->format->cpp[0];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004034 }
4035
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004036 down_scale_amount = skl_plane_downscale_amount(cstate, intel_pstate);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004037
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304038 return mul_round_up_u32_fixed16(data_rate, down_scale_amount);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004039}
4040
4041/*
4042 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
4043 * a 8192x4096@32bpp framebuffer:
4044 * 3 * 4096 * 8192 * 4 < 2^32
4045 */
4046static unsigned int
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004047skl_get_total_relative_data_rate(struct intel_crtc_state *intel_cstate,
4048 unsigned *plane_data_rate,
4049 unsigned *plane_y_data_rate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004050{
Matt Roper9c74d822016-05-12 07:05:58 -07004051 struct drm_crtc_state *cstate = &intel_cstate->base;
4052 struct drm_atomic_state *state = cstate->state;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02004053 struct drm_plane *plane;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02004054 const struct drm_plane_state *pstate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004055 unsigned int total_data_rate = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07004056
4057 if (WARN_ON(!state))
4058 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004059
Matt Ropera1de91e2016-05-12 07:05:57 -07004060 /* Calculate and cache data rate for each plane */
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02004061 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, cstate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004062 enum plane_id plane_id = to_intel_plane(plane)->id;
4063 unsigned int rate;
Matt Roper024c9042015-09-24 15:53:11 -07004064
Matt Ropera6d3460e2016-05-12 07:06:04 -07004065 /* packed/uv */
4066 rate = skl_plane_relative_data_rate(intel_cstate,
4067 pstate, 0);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004068 plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004069
4070 total_data_rate += rate;
Matt Roper9c74d822016-05-12 07:05:58 -07004071
Matt Ropera6d3460e2016-05-12 07:06:04 -07004072 /* y-plane */
4073 rate = skl_plane_relative_data_rate(intel_cstate,
4074 pstate, 1);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004075 plane_y_data_rate[plane_id] = rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07004076
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004077 total_data_rate += rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004078 }
4079
4080 return total_data_rate;
4081}
4082
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004083static uint16_t
4084skl_ddb_min_alloc(const struct drm_plane_state *pstate,
4085 const int y)
4086{
4087 struct drm_framebuffer *fb = pstate->fb;
4088 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
4089 uint32_t src_w, src_h;
4090 uint32_t min_scanlines = 8;
4091 uint8_t plane_bpp;
4092
4093 if (WARN_ON(!fb))
4094 return 0;
4095
4096 /* For packed formats, no y-plane, return 0 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004097 if (y && fb->format->format != DRM_FORMAT_NV12)
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004098 return 0;
4099
4100 /* For Non Y-tile return 8-blocks */
Ville Syrjäläbae781b2016-11-16 13:33:16 +02004101 if (fb->modifier != I915_FORMAT_MOD_Y_TILED &&
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07004102 fb->modifier != I915_FORMAT_MOD_Yf_TILED &&
4103 fb->modifier != I915_FORMAT_MOD_Y_TILED_CCS &&
4104 fb->modifier != I915_FORMAT_MOD_Yf_TILED_CCS)
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004105 return 8;
4106
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004107 /*
4108 * Src coordinates are already rotated by 270 degrees for
4109 * the 90/270 degree plane rotation cases (to match the
4110 * GTT mapping), hence no need to account for rotation here.
4111 */
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004112 src_w = drm_rect_width(&intel_pstate->base.src) >> 16;
4113 src_h = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004114
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004115 /* Halve UV plane width and height for NV12 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004116 if (fb->format->format == DRM_FORMAT_NV12 && !y) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004117 src_w /= 2;
4118 src_h /= 2;
4119 }
4120
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004121 if (fb->format->format == DRM_FORMAT_NV12 && !y)
Ville Syrjälä353c8592016-12-14 23:30:57 +02004122 plane_bpp = fb->format->cpp[1];
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004123 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02004124 plane_bpp = fb->format->cpp[0];
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004125
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03004126 if (drm_rotation_90_or_270(pstate->rotation)) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004127 switch (plane_bpp) {
4128 case 1:
4129 min_scanlines = 32;
4130 break;
4131 case 2:
4132 min_scanlines = 16;
4133 break;
4134 case 4:
4135 min_scanlines = 8;
4136 break;
4137 case 8:
4138 min_scanlines = 4;
4139 break;
4140 default:
4141 WARN(1, "Unsupported pixel depth %u for rotation",
4142 plane_bpp);
4143 min_scanlines = 32;
4144 }
4145 }
4146
4147 return DIV_ROUND_UP((4 * src_w * plane_bpp), 512) * min_scanlines/4 + 3;
4148}
4149
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004150static void
4151skl_ddb_calc_min(const struct intel_crtc_state *cstate, int num_active,
4152 uint16_t *minimum, uint16_t *y_minimum)
4153{
4154 const struct drm_plane_state *pstate;
4155 struct drm_plane *plane;
4156
4157 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, &cstate->base) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004158 enum plane_id plane_id = to_intel_plane(plane)->id;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004159
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004160 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004161 continue;
4162
4163 if (!pstate->visible)
4164 continue;
4165
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004166 minimum[plane_id] = skl_ddb_min_alloc(pstate, 0);
4167 y_minimum[plane_id] = skl_ddb_min_alloc(pstate, 1);
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004168 }
4169
4170 minimum[PLANE_CURSOR] = skl_cursor_allocation(num_active);
4171}
4172
Matt Roperc107acf2016-05-12 07:06:01 -07004173static int
Matt Roper024c9042015-09-24 15:53:11 -07004174skl_allocate_pipe_ddb(struct intel_crtc_state *cstate,
Damien Lespiaub9cec072014-11-04 17:06:43 +00004175 struct skl_ddb_allocation *ddb /* out */)
4176{
Matt Roperc107acf2016-05-12 07:06:01 -07004177 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07004178 struct drm_crtc *crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004179 struct drm_device *dev = crtc->dev;
4180 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4181 enum pipe pipe = intel_crtc->pipe;
Lyudece0ba282016-09-15 10:46:35 -04004182 struct skl_ddb_entry *alloc = &cstate->wm.skl.ddb;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004183 uint16_t alloc_size, start;
Maarten Lankhorstfefdd812016-10-26 15:41:33 +02004184 uint16_t minimum[I915_MAX_PLANES] = {};
4185 uint16_t y_minimum[I915_MAX_PLANES] = {};
Damien Lespiaub9cec072014-11-04 17:06:43 +00004186 unsigned int total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004187 enum plane_id plane_id;
Matt Roperc107acf2016-05-12 07:06:01 -07004188 int num_active;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004189 unsigned plane_data_rate[I915_MAX_PLANES] = {};
4190 unsigned plane_y_data_rate[I915_MAX_PLANES] = {};
Kumar, Mahesh5ba6faa2017-05-17 17:28:26 +05304191 uint16_t total_min_blocks = 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004192
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004193 /* Clear the partitioning for disabled planes. */
4194 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
4195 memset(ddb->y_plane[pipe], 0, sizeof(ddb->y_plane[pipe]));
4196
Matt Ropera6d3460e2016-05-12 07:06:04 -07004197 if (WARN_ON(!state))
4198 return 0;
4199
Matt Roperc107acf2016-05-12 07:06:01 -07004200 if (!cstate->base.active) {
Lyudece0ba282016-09-15 10:46:35 -04004201 alloc->start = alloc->end = 0;
Matt Roperc107acf2016-05-12 07:06:01 -07004202 return 0;
4203 }
4204
Matt Ropera6d3460e2016-05-12 07:06:04 -07004205 skl_ddb_get_pipe_allocation_limits(dev, cstate, alloc, &num_active);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00004206 alloc_size = skl_ddb_entry_size(alloc);
Kumar, Mahesh336031e2017-05-17 17:28:25 +05304207 if (alloc_size == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004208 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004209
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004210 skl_ddb_calc_min(cstate, num_active, minimum, y_minimum);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004211
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004212 /*
4213 * 1. Allocate the mininum required blocks for each active plane
4214 * and allocate the cursor, it doesn't require extra allocation
4215 * proportional to the data rate.
4216 */
Damien Lespiaub9cec072014-11-04 17:06:43 +00004217
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004218 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Kumar, Mahesh5ba6faa2017-05-17 17:28:26 +05304219 total_min_blocks += minimum[plane_id];
4220 total_min_blocks += y_minimum[plane_id];
Damien Lespiau80958152015-02-09 13:35:10 +00004221 }
4222
Kumar, Mahesh5ba6faa2017-05-17 17:28:26 +05304223 if (total_min_blocks > alloc_size) {
4224 DRM_DEBUG_KMS("Requested display configuration exceeds system DDB limitations");
4225 DRM_DEBUG_KMS("minimum required %d/%d\n", total_min_blocks,
4226 alloc_size);
4227 return -EINVAL;
4228 }
4229
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004230 alloc_size -= total_min_blocks;
4231 ddb->plane[pipe][PLANE_CURSOR].start = alloc->end - minimum[PLANE_CURSOR];
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004232 ddb->plane[pipe][PLANE_CURSOR].end = alloc->end;
4233
Damien Lespiaub9cec072014-11-04 17:06:43 +00004234 /*
Damien Lespiau80958152015-02-09 13:35:10 +00004235 * 2. Distribute the remaining space in proportion to the amount of
4236 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00004237 *
4238 * FIXME: we may not allocate every single block here.
4239 */
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004240 total_data_rate = skl_get_total_relative_data_rate(cstate,
4241 plane_data_rate,
4242 plane_y_data_rate);
Matt Ropera1de91e2016-05-12 07:05:57 -07004243 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004244 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004245
Damien Lespiau34bb56a2014-11-04 17:07:01 +00004246 start = alloc->start;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004247 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004248 unsigned int data_rate, y_data_rate;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004249 uint16_t plane_blocks, y_plane_blocks = 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004250
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004251 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004252 continue;
4253
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004254 data_rate = plane_data_rate[plane_id];
Damien Lespiaub9cec072014-11-04 17:06:43 +00004255
4256 /*
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004257 * allocation for (packed formats) or (uv-plane part of planar format):
Damien Lespiaub9cec072014-11-04 17:06:43 +00004258 * promote the expression to 64 bits to avoid overflowing, the
4259 * result is < available as data_rate / total_data_rate < 1
4260 */
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004261 plane_blocks = minimum[plane_id];
4262 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
4263 total_data_rate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004264
Matt Roperc107acf2016-05-12 07:06:01 -07004265 /* Leave disabled planes at (0,0) */
4266 if (data_rate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004267 ddb->plane[pipe][plane_id].start = start;
4268 ddb->plane[pipe][plane_id].end = start + plane_blocks;
Matt Roperc107acf2016-05-12 07:06:01 -07004269 }
Damien Lespiaub9cec072014-11-04 17:06:43 +00004270
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004271 start += plane_blocks;
4272
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004273 /*
4274 * allocation for y_plane part of planar format:
4275 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004276 y_data_rate = plane_y_data_rate[plane_id];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004277
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004278 y_plane_blocks = y_minimum[plane_id];
4279 y_plane_blocks += div_u64((uint64_t)alloc_size * y_data_rate,
4280 total_data_rate);
4281
Matt Roperc107acf2016-05-12 07:06:01 -07004282 if (y_data_rate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004283 ddb->y_plane[pipe][plane_id].start = start;
4284 ddb->y_plane[pipe][plane_id].end = start + y_plane_blocks;
Matt Roperc107acf2016-05-12 07:06:01 -07004285 }
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004286
4287 start += y_plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004288 }
4289
Matt Roperc107acf2016-05-12 07:06:01 -07004290 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004291}
4292
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004293/*
4294 * 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 +02004295 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004296 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
4297 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
4298*/
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07004299static uint_fixed_16_16_t
4300skl_wm_method1(const struct drm_i915_private *dev_priv, uint32_t pixel_rate,
4301 uint8_t cpp, uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004302{
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304303 uint32_t wm_intermediate_val;
4304 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004305
4306 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304307 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004308
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304309 wm_intermediate_val = latency * pixel_rate * cpp;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304310 ret = div_fixed16(wm_intermediate_val, 1000 * 512);
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07004311
4312 if (INTEL_GEN(dev_priv) >= 10)
4313 ret = add_fixed16_u32(ret, 1);
4314
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004315 return ret;
4316}
4317
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304318static uint_fixed_16_16_t skl_wm_method2(uint32_t pixel_rate,
4319 uint32_t pipe_htotal,
4320 uint32_t latency,
4321 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004322{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004323 uint32_t wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304324 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004325
4326 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304327 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004328
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004329 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304330 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
4331 pipe_htotal * 1000);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304332 ret = mul_u32_fixed16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004333 return ret;
4334}
4335
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304336static uint_fixed_16_16_t
4337intel_get_linetime_us(struct intel_crtc_state *cstate)
4338{
4339 uint32_t pixel_rate;
4340 uint32_t crtc_htotal;
4341 uint_fixed_16_16_t linetime_us;
4342
4343 if (!cstate->base.active)
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304344 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304345
4346 pixel_rate = cstate->pixel_rate;
4347
4348 if (WARN_ON(pixel_rate == 0))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304349 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304350
4351 crtc_htotal = cstate->base.adjusted_mode.crtc_htotal;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304352 linetime_us = div_fixed16(crtc_htotal * 1000, pixel_rate);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304353
4354 return linetime_us;
4355}
4356
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304357static uint32_t
4358skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *cstate,
4359 const struct intel_plane_state *pstate)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004360{
4361 uint64_t adjusted_pixel_rate;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304362 uint_fixed_16_16_t downscale_amount;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004363
4364 /* Shouldn't reach here on disabled planes... */
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004365 if (WARN_ON(!intel_wm_plane_visible(cstate, pstate)))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004366 return 0;
4367
4368 /*
4369 * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
4370 * with additional adjustments for plane-specific scaling.
4371 */
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02004372 adjusted_pixel_rate = cstate->pixel_rate;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004373 downscale_amount = skl_plane_downscale_amount(cstate, pstate);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004374
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304375 return mul_round_up_u32_fixed16(adjusted_pixel_rate,
4376 downscale_amount);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004377}
4378
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304379static int
4380skl_compute_plane_wm_params(const struct drm_i915_private *dev_priv,
4381 struct intel_crtc_state *cstate,
4382 const struct intel_plane_state *intel_pstate,
4383 struct skl_wm_params *wp)
4384{
4385 struct intel_plane *plane = to_intel_plane(intel_pstate->base.plane);
4386 const struct drm_plane_state *pstate = &intel_pstate->base;
4387 const struct drm_framebuffer *fb = pstate->fb;
4388 uint32_t interm_pbpl;
4389 struct intel_atomic_state *state =
4390 to_intel_atomic_state(cstate->base.state);
4391 bool apply_memory_bw_wa = skl_needs_memory_bw_wa(state);
4392
4393 if (!intel_wm_plane_visible(cstate, intel_pstate))
4394 return 0;
4395
4396 wp->y_tiled = fb->modifier == I915_FORMAT_MOD_Y_TILED ||
4397 fb->modifier == I915_FORMAT_MOD_Yf_TILED ||
4398 fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
4399 fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
4400 wp->x_tiled = fb->modifier == I915_FORMAT_MOD_X_TILED;
4401 wp->rc_surface = fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
4402 fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
4403
4404 if (plane->id == PLANE_CURSOR) {
4405 wp->width = intel_pstate->base.crtc_w;
4406 } else {
4407 /*
4408 * Src coordinates are already rotated by 270 degrees for
4409 * the 90/270 degree plane rotation cases (to match the
4410 * GTT mapping), hence no need to account for rotation here.
4411 */
4412 wp->width = drm_rect_width(&intel_pstate->base.src) >> 16;
4413 }
4414
4415 wp->cpp = (fb->format->format == DRM_FORMAT_NV12) ? fb->format->cpp[1] :
4416 fb->format->cpp[0];
4417 wp->plane_pixel_rate = skl_adjusted_plane_pixel_rate(cstate,
4418 intel_pstate);
4419
4420 if (drm_rotation_90_or_270(pstate->rotation)) {
4421
4422 switch (wp->cpp) {
4423 case 1:
4424 wp->y_min_scanlines = 16;
4425 break;
4426 case 2:
4427 wp->y_min_scanlines = 8;
4428 break;
4429 case 4:
4430 wp->y_min_scanlines = 4;
4431 break;
4432 default:
4433 MISSING_CASE(wp->cpp);
4434 return -EINVAL;
4435 }
4436 } else {
4437 wp->y_min_scanlines = 4;
4438 }
4439
4440 if (apply_memory_bw_wa)
4441 wp->y_min_scanlines *= 2;
4442
4443 wp->plane_bytes_per_line = wp->width * wp->cpp;
4444 if (wp->y_tiled) {
4445 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line *
4446 wp->y_min_scanlines, 512);
4447
4448 if (INTEL_GEN(dev_priv) >= 10)
4449 interm_pbpl++;
4450
4451 wp->plane_blocks_per_line = div_fixed16(interm_pbpl,
4452 wp->y_min_scanlines);
4453 } else if (wp->x_tiled && IS_GEN9(dev_priv)) {
4454 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line, 512);
4455 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
4456 } else {
4457 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line, 512) + 1;
4458 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
4459 }
4460
4461 wp->y_tile_minimum = mul_u32_fixed16(wp->y_min_scanlines,
4462 wp->plane_blocks_per_line);
4463 wp->linetime_us = fixed16_to_u32_round_up(
4464 intel_get_linetime_us(cstate));
4465
4466 return 0;
4467}
4468
Matt Roper55994c22016-05-12 07:06:08 -07004469static int skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
4470 struct intel_crtc_state *cstate,
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304471 const struct intel_plane_state *intel_pstate,
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004472 uint16_t ddb_allocation,
Matt Roper55994c22016-05-12 07:06:08 -07004473 int level,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304474 const struct skl_wm_params *wp,
Matt Roper55994c22016-05-12 07:06:08 -07004475 uint16_t *out_blocks, /* out */
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004476 uint8_t *out_lines, /* out */
4477 bool *enabled /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004478{
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304479 const struct drm_plane_state *pstate = &intel_pstate->base;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004480 uint32_t latency = dev_priv->wm.skl_latency[level];
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304481 uint_fixed_16_16_t method1, method2;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304482 uint_fixed_16_16_t selected_result;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004483 uint32_t res_blocks, res_lines;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03004484 struct intel_atomic_state *state =
4485 to_intel_atomic_state(cstate->base.state);
4486 bool apply_memory_bw_wa = skl_needs_memory_bw_wa(state);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004487
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004488 if (latency == 0 ||
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004489 !intel_wm_plane_visible(cstate, intel_pstate)) {
4490 *enabled = false;
Matt Roper55994c22016-05-12 07:06:08 -07004491 return 0;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004492 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004493
Rodrigo Vivi82525c12017-06-08 08:50:00 -07004494 /* Display WA #1141: kbl,cfl */
Kumar, Maheshd86ba622017-08-17 19:15:26 +05304495 if ((IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv) ||
4496 IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_B0)) &&
Rodrigo Vivi82525c12017-06-08 08:50:00 -07004497 dev_priv->ipc_enabled)
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05304498 latency += 4;
4499
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304500 if (apply_memory_bw_wa && wp->x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03004501 latency += 15;
4502
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304503 method1 = skl_wm_method1(dev_priv, wp->plane_pixel_rate,
4504 wp->cpp, latency);
4505 method2 = skl_wm_method2(wp->plane_pixel_rate,
Matt Roper024c9042015-09-24 15:53:11 -07004506 cstate->base.adjusted_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004507 latency,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304508 wp->plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004509
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304510 if (wp->y_tiled) {
4511 selected_result = max_fixed16(method2, wp->y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004512 } else {
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304513 if ((wp->cpp * cstate->base.adjusted_mode.crtc_htotal /
4514 512 < 1) && (wp->plane_bytes_per_line / 512 < 1))
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03004515 selected_result = method2;
Maarten Lankhorst54d20ed2017-07-17 14:02:30 +02004516 else if (ddb_allocation >=
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304517 fixed16_to_u32_round_up(wp->plane_blocks_per_line))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304518 selected_result = min_fixed16(method1, method2);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304519 else if (latency >= wp->linetime_us)
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304520 selected_result = min_fixed16(method1, method2);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004521 else
4522 selected_result = method1;
4523 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004524
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304525 res_blocks = fixed16_to_u32_round_up(selected_result) + 1;
Kumar, Maheshd273ecc2017-05-17 17:28:22 +05304526 res_lines = div_round_up_fixed16(selected_result,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304527 wp->plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00004528
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07004529 /* Display WA #1125: skl,bxt,kbl,glk */
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304530 if (level == 0 && wp->rc_surface)
4531 res_blocks += fixed16_to_u32_round_up(wp->y_tile_minimum);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07004532
4533 /* Display WA #1126: skl,bxt,kbl,glk */
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004534 if (level >= 1 && level <= 7) {
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304535 if (wp->y_tiled) {
4536 res_blocks += fixed16_to_u32_round_up(
4537 wp->y_tile_minimum);
4538 res_lines += wp->y_min_scanlines;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004539 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004540 res_blocks++;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004541 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004542 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004543
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004544 if (res_blocks >= ddb_allocation || res_lines > 31) {
4545 *enabled = false;
Matt Roper6b6bada2016-05-12 07:06:10 -07004546
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004547 /*
4548 * If there are no valid level 0 watermarks, then we can't
4549 * support this display configuration.
4550 */
4551 if (level) {
4552 return 0;
4553 } else {
4554 struct drm_plane *plane = pstate->plane;
4555
4556 DRM_DEBUG_KMS("Requested display configuration exceeds system watermark limitations\n");
4557 DRM_DEBUG_KMS("[PLANE:%d:%s] blocks required = %u/%u, lines required = %u/31\n",
4558 plane->base.id, plane->name,
4559 res_blocks, ddb_allocation, res_lines);
4560 return -EINVAL;
4561 }
Matt Roper55994c22016-05-12 07:06:08 -07004562 }
Damien Lespiaue6d66172014-11-04 17:06:55 +00004563
4564 *out_blocks = res_blocks;
4565 *out_lines = res_lines;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004566 *enabled = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004567
Matt Roper55994c22016-05-12 07:06:08 -07004568 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004569}
4570
Matt Roperf4a96752016-05-12 07:06:06 -07004571static int
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304572skl_compute_wm_levels(const struct drm_i915_private *dev_priv,
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004573 struct skl_ddb_allocation *ddb,
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304574 struct intel_crtc_state *cstate,
4575 const struct intel_plane_state *intel_pstate,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304576 const struct skl_wm_params *wm_params,
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304577 struct skl_plane_wm *wm)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004578{
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004579 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
4580 struct drm_plane *plane = intel_pstate->base.plane;
4581 struct intel_plane *intel_plane = to_intel_plane(plane);
4582 uint16_t ddb_blocks;
4583 enum pipe pipe = intel_crtc->pipe;
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304584 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Roper55994c22016-05-12 07:06:08 -07004585 int ret;
Lyudea62163e2016-10-04 14:28:20 -04004586
Kumar, Mahesh7b751192017-05-17 17:28:24 +05304587 if (WARN_ON(!intel_pstate->base.fb))
4588 return -EINVAL;
Matt Roper024c9042015-09-24 15:53:11 -07004589
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004590 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][intel_plane->id]);
4591
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304592 for (level = 0; level <= max_level; level++) {
4593 struct skl_wm_level *result = &wm->wm[level];
4594
4595 ret = skl_compute_plane_wm(dev_priv,
4596 cstate,
4597 intel_pstate,
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004598 ddb_blocks,
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304599 level,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304600 wm_params,
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304601 &result->plane_res_b,
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004602 &result->plane_res_l,
4603 &result->plane_en);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304604 if (ret)
4605 return ret;
4606 }
Matt Roperf4a96752016-05-12 07:06:06 -07004607
4608 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004609}
4610
Damien Lespiau407b50f2014-11-04 17:06:57 +00004611static uint32_t
Matt Roper024c9042015-09-24 15:53:11 -07004612skl_compute_linetime_wm(struct intel_crtc_state *cstate)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004613{
Mahesh Kumara3a89862016-12-01 21:19:34 +05304614 struct drm_atomic_state *state = cstate->base.state;
4615 struct drm_i915_private *dev_priv = to_i915(state->dev);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304616 uint_fixed_16_16_t linetime_us;
Mahesh Kumara3a89862016-12-01 21:19:34 +05304617 uint32_t linetime_wm;
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03004618
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304619 linetime_us = intel_get_linetime_us(cstate);
4620
4621 if (is_fixed16_zero(linetime_us))
Damien Lespiau407b50f2014-11-04 17:06:57 +00004622 return 0;
4623
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304624 linetime_wm = fixed16_to_u32_round_up(mul_u32_fixed16(8, linetime_us));
Mahesh Kumara3a89862016-12-01 21:19:34 +05304625
Kumar, Mahesh446e8502017-08-17 19:15:25 +05304626 /* Display WA #1135: bxt:ALL GLK:ALL */
4627 if ((IS_BROXTON(dev_priv) || IS_GEMINILAKE(dev_priv)) &&
4628 dev_priv->ipc_enabled)
4629 linetime_wm /= 2;
Mahesh Kumara3a89862016-12-01 21:19:34 +05304630
4631 return linetime_wm;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004632}
4633
Matt Roper024c9042015-09-24 15:53:11 -07004634static void skl_compute_transition_wm(struct intel_crtc_state *cstate,
Kumar, Maheshca476672017-08-17 19:15:24 +05304635 struct skl_wm_params *wp,
4636 struct skl_wm_level *wm_l0,
4637 uint16_t ddb_allocation,
Damien Lespiau9414f562014-11-04 17:06:58 +00004638 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004639{
Kumar, Maheshca476672017-08-17 19:15:24 +05304640 struct drm_device *dev = cstate->base.crtc->dev;
4641 const struct drm_i915_private *dev_priv = to_i915(dev);
4642 uint16_t trans_min, trans_y_tile_min;
4643 const uint16_t trans_amount = 10; /* This is configurable amount */
4644 uint16_t trans_offset_b, res_blocks;
Damien Lespiau9414f562014-11-04 17:06:58 +00004645
Kumar, Maheshca476672017-08-17 19:15:24 +05304646 if (!cstate->base.active)
4647 goto exit;
4648
4649 /* Transition WM are not recommended by HW team for GEN9 */
4650 if (INTEL_GEN(dev_priv) <= 9)
4651 goto exit;
4652
4653 /* Transition WM don't make any sense if ipc is disabled */
4654 if (!dev_priv->ipc_enabled)
4655 goto exit;
4656
4657 if (INTEL_GEN(dev_priv) >= 10)
4658 trans_min = 4;
4659
4660 trans_offset_b = trans_min + trans_amount;
4661
4662 if (wp->y_tiled) {
4663 trans_y_tile_min = (uint16_t) mul_round_up_u32_fixed16(2,
4664 wp->y_tile_minimum);
4665 res_blocks = max(wm_l0->plane_res_b, trans_y_tile_min) +
4666 trans_offset_b;
4667 } else {
4668 res_blocks = wm_l0->plane_res_b + trans_offset_b;
4669
4670 /* WA BUG:1938466 add one block for non y-tile planes */
4671 if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_A0))
4672 res_blocks += 1;
4673
4674 }
4675
4676 res_blocks += 1;
4677
4678 if (res_blocks < ddb_allocation) {
4679 trans_wm->plane_res_b = res_blocks;
4680 trans_wm->plane_en = true;
4681 return;
4682 }
4683
4684exit:
Lyudea62163e2016-10-04 14:28:20 -04004685 trans_wm->plane_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004686}
4687
Matt Roper55994c22016-05-12 07:06:08 -07004688static int skl_build_pipe_wm(struct intel_crtc_state *cstate,
4689 struct skl_ddb_allocation *ddb,
4690 struct skl_pipe_wm *pipe_wm)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004691{
Matt Roper024c9042015-09-24 15:53:11 -07004692 struct drm_device *dev = cstate->base.crtc->dev;
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304693 struct drm_crtc_state *crtc_state = &cstate->base;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004694 const struct drm_i915_private *dev_priv = to_i915(dev);
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304695 struct drm_plane *plane;
4696 const struct drm_plane_state *pstate;
Lyudea62163e2016-10-04 14:28:20 -04004697 struct skl_plane_wm *wm;
Matt Roper55994c22016-05-12 07:06:08 -07004698 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004699
Lyudea62163e2016-10-04 14:28:20 -04004700 /*
4701 * We'll only calculate watermarks for planes that are actually
4702 * enabled, so make sure all other planes are set as disabled.
4703 */
4704 memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
4705
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304706 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, crtc_state) {
4707 const struct intel_plane_state *intel_pstate =
4708 to_intel_plane_state(pstate);
4709 enum plane_id plane_id = to_intel_plane(plane)->id;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304710 struct skl_wm_params wm_params;
Kumar, Maheshca476672017-08-17 19:15:24 +05304711 enum pipe pipe = to_intel_crtc(cstate->base.crtc)->pipe;
4712 uint16_t ddb_blocks;
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304713
4714 wm = &pipe_wm->planes[plane_id];
Kumar, Maheshca476672017-08-17 19:15:24 +05304715 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][plane_id]);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304716 memset(&wm_params, 0, sizeof(struct skl_wm_params));
4717
4718 ret = skl_compute_plane_wm_params(dev_priv, cstate,
4719 intel_pstate, &wm_params);
4720 if (ret)
4721 return ret;
Lyudea62163e2016-10-04 14:28:20 -04004722
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004723 ret = skl_compute_wm_levels(dev_priv, ddb, cstate,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304724 intel_pstate, &wm_params, wm);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304725 if (ret)
4726 return ret;
Kumar, Maheshca476672017-08-17 19:15:24 +05304727 skl_compute_transition_wm(cstate, &wm_params, &wm->wm[0],
4728 ddb_blocks, &wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004729 }
Matt Roper024c9042015-09-24 15:53:11 -07004730 pipe_wm->linetime = skl_compute_linetime_wm(cstate);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004731
Matt Roper55994c22016-05-12 07:06:08 -07004732 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004733}
4734
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004735static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
4736 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00004737 const struct skl_ddb_entry *entry)
4738{
4739 if (entry->end)
4740 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
4741 else
4742 I915_WRITE(reg, 0);
4743}
4744
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004745static void skl_write_wm_level(struct drm_i915_private *dev_priv,
4746 i915_reg_t reg,
4747 const struct skl_wm_level *level)
4748{
4749 uint32_t val = 0;
4750
4751 if (level->plane_en) {
4752 val |= PLANE_WM_EN;
4753 val |= level->plane_res_b;
4754 val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
4755 }
4756
4757 I915_WRITE(reg, val);
4758}
4759
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004760static void skl_write_plane_wm(struct intel_crtc *intel_crtc,
4761 const struct skl_plane_wm *wm,
4762 const struct skl_ddb_allocation *ddb,
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004763 enum plane_id plane_id)
Lyude62e0fb82016-08-22 12:50:08 -04004764{
4765 struct drm_crtc *crtc = &intel_crtc->base;
4766 struct drm_device *dev = crtc->dev;
4767 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004768 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004769 enum pipe pipe = intel_crtc->pipe;
4770
4771 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004772 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004773 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004774 }
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004775 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004776 &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004777
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004778 skl_ddb_entry_write(dev_priv, PLANE_BUF_CFG(pipe, plane_id),
4779 &ddb->plane[pipe][plane_id]);
4780 skl_ddb_entry_write(dev_priv, PLANE_NV12_BUF_CFG(pipe, plane_id),
4781 &ddb->y_plane[pipe][plane_id]);
Lyude62e0fb82016-08-22 12:50:08 -04004782}
4783
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004784static void skl_write_cursor_wm(struct intel_crtc *intel_crtc,
4785 const struct skl_plane_wm *wm,
4786 const struct skl_ddb_allocation *ddb)
Lyude62e0fb82016-08-22 12:50:08 -04004787{
4788 struct drm_crtc *crtc = &intel_crtc->base;
4789 struct drm_device *dev = crtc->dev;
4790 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004791 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004792 enum pipe pipe = intel_crtc->pipe;
4793
4794 for (level = 0; level <= max_level; level++) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004795 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
4796 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004797 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004798 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004799
4800 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004801 &ddb->plane[pipe][PLANE_CURSOR]);
Lyude62e0fb82016-08-22 12:50:08 -04004802}
4803
cpaul@redhat.com45ece232016-10-14 17:31:56 -04004804bool skl_wm_level_equals(const struct skl_wm_level *l1,
4805 const struct skl_wm_level *l2)
4806{
4807 if (l1->plane_en != l2->plane_en)
4808 return false;
4809
4810 /* If both planes aren't enabled, the rest shouldn't matter */
4811 if (!l1->plane_en)
4812 return true;
4813
4814 return (l1->plane_res_l == l2->plane_res_l &&
4815 l1->plane_res_b == l2->plane_res_b);
4816}
4817
Lyude27082492016-08-24 07:48:10 +02004818static inline bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
4819 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004820{
Lyude27082492016-08-24 07:48:10 +02004821 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004822}
4823
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01004824bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry **entries,
4825 const struct skl_ddb_entry *ddb,
4826 int ignore)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004827{
Lyudece0ba282016-09-15 10:46:35 -04004828 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004829
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01004830 for (i = 0; i < I915_MAX_PIPES; i++)
4831 if (i != ignore && entries[i] &&
4832 skl_ddb_entries_overlap(ddb, entries[i]))
Lyude27082492016-08-24 07:48:10 +02004833 return true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004834
Lyude27082492016-08-24 07:48:10 +02004835 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004836}
4837
Matt Roper55994c22016-05-12 07:06:08 -07004838static int skl_update_pipe_wm(struct drm_crtc_state *cstate,
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004839 const struct skl_pipe_wm *old_pipe_wm,
Matt Roper55994c22016-05-12 07:06:08 -07004840 struct skl_pipe_wm *pipe_wm, /* out */
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004841 struct skl_ddb_allocation *ddb, /* out */
Matt Roper55994c22016-05-12 07:06:08 -07004842 bool *changed /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004843{
Matt Roperf4a96752016-05-12 07:06:06 -07004844 struct intel_crtc_state *intel_cstate = to_intel_crtc_state(cstate);
Matt Roper55994c22016-05-12 07:06:08 -07004845 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004846
Matt Roper55994c22016-05-12 07:06:08 -07004847 ret = skl_build_pipe_wm(intel_cstate, ddb, pipe_wm);
4848 if (ret)
4849 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004850
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004851 if (!memcmp(old_pipe_wm, pipe_wm, sizeof(*pipe_wm)))
Matt Roper55994c22016-05-12 07:06:08 -07004852 *changed = false;
4853 else
4854 *changed = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004855
Matt Roper55994c22016-05-12 07:06:08 -07004856 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004857}
4858
Matt Roper9b613022016-06-27 16:42:44 -07004859static uint32_t
4860pipes_modified(struct drm_atomic_state *state)
4861{
4862 struct drm_crtc *crtc;
4863 struct drm_crtc_state *cstate;
4864 uint32_t i, ret = 0;
4865
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004866 for_each_new_crtc_in_state(state, crtc, cstate, i)
Matt Roper9b613022016-06-27 16:42:44 -07004867 ret |= drm_crtc_mask(crtc);
4868
4869 return ret;
4870}
4871
Jani Nikulabb7791b2016-10-04 12:29:17 +03004872static int
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004873skl_ddb_add_affected_planes(struct intel_crtc_state *cstate)
4874{
4875 struct drm_atomic_state *state = cstate->base.state;
4876 struct drm_device *dev = state->dev;
4877 struct drm_crtc *crtc = cstate->base.crtc;
4878 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4879 struct drm_i915_private *dev_priv = to_i915(dev);
4880 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4881 struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
4882 struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
4883 struct drm_plane_state *plane_state;
4884 struct drm_plane *plane;
4885 enum pipe pipe = intel_crtc->pipe;
4886
4887 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
4888
4889 drm_for_each_plane_mask(plane, dev, cstate->base.plane_mask) {
4890 enum plane_id plane_id = to_intel_plane(plane)->id;
4891
4892 if (skl_ddb_entry_equal(&cur_ddb->plane[pipe][plane_id],
4893 &new_ddb->plane[pipe][plane_id]) &&
4894 skl_ddb_entry_equal(&cur_ddb->y_plane[pipe][plane_id],
4895 &new_ddb->y_plane[pipe][plane_id]))
4896 continue;
4897
4898 plane_state = drm_atomic_get_plane_state(state, plane);
4899 if (IS_ERR(plane_state))
4900 return PTR_ERR(plane_state);
4901 }
4902
4903 return 0;
4904}
4905
4906static int
4907skl_compute_ddb(struct drm_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07004908{
4909 struct drm_device *dev = state->dev;
4910 struct drm_i915_private *dev_priv = to_i915(dev);
4911 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4912 struct intel_crtc *intel_crtc;
Matt Roper734fa012016-05-12 15:11:40 -07004913 struct skl_ddb_allocation *ddb = &intel_state->wm_results.ddb;
Matt Roper9b613022016-06-27 16:42:44 -07004914 uint32_t realloc_pipes = pipes_modified(state);
Matt Roper98d39492016-05-12 07:06:03 -07004915 int ret;
4916
4917 /*
4918 * If this is our first atomic update following hardware readout,
4919 * we can't trust the DDB that the BIOS programmed for us. Let's
4920 * pretend that all pipes switched active status so that we'll
4921 * ensure a full DDB recompute.
4922 */
Matt Roper1b54a882016-06-17 13:42:18 -07004923 if (dev_priv->wm.distrust_bios_wm) {
4924 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
4925 state->acquire_ctx);
4926 if (ret)
4927 return ret;
4928
Matt Roper98d39492016-05-12 07:06:03 -07004929 intel_state->active_pipe_changes = ~0;
4930
Matt Roper1b54a882016-06-17 13:42:18 -07004931 /*
4932 * We usually only initialize intel_state->active_crtcs if we
4933 * we're doing a modeset; make sure this field is always
4934 * initialized during the sanitization process that happens
4935 * on the first commit too.
4936 */
4937 if (!intel_state->modeset)
4938 intel_state->active_crtcs = dev_priv->active_crtcs;
4939 }
4940
Matt Roper98d39492016-05-12 07:06:03 -07004941 /*
4942 * If the modeset changes which CRTC's are active, we need to
4943 * recompute the DDB allocation for *all* active pipes, even
4944 * those that weren't otherwise being modified in any way by this
4945 * atomic commit. Due to the shrinking of the per-pipe allocations
4946 * when new active CRTC's are added, it's possible for a pipe that
4947 * we were already using and aren't changing at all here to suddenly
4948 * become invalid if its DDB needs exceeds its new allocation.
4949 *
4950 * Note that if we wind up doing a full DDB recompute, we can't let
4951 * any other display updates race with this transaction, so we need
4952 * to grab the lock on *all* CRTC's.
4953 */
Matt Roper734fa012016-05-12 15:11:40 -07004954 if (intel_state->active_pipe_changes) {
Matt Roper98d39492016-05-12 07:06:03 -07004955 realloc_pipes = ~0;
Matt Roper734fa012016-05-12 15:11:40 -07004956 intel_state->wm_results.dirty_pipes = ~0;
4957 }
Matt Roper98d39492016-05-12 07:06:03 -07004958
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004959 /*
4960 * We're not recomputing for the pipes not included in the commit, so
4961 * make sure we start with the current state.
4962 */
4963 memcpy(ddb, &dev_priv->wm.skl_hw.ddb, sizeof(*ddb));
4964
Matt Roper98d39492016-05-12 07:06:03 -07004965 for_each_intel_crtc_mask(dev, intel_crtc, realloc_pipes) {
4966 struct intel_crtc_state *cstate;
4967
4968 cstate = intel_atomic_get_crtc_state(state, intel_crtc);
4969 if (IS_ERR(cstate))
4970 return PTR_ERR(cstate);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004971
4972 ret = skl_allocate_pipe_ddb(cstate, ddb);
4973 if (ret)
4974 return ret;
4975
4976 ret = skl_ddb_add_affected_planes(cstate);
4977 if (ret)
4978 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07004979 }
4980
4981 return 0;
4982}
4983
Matt Roper2722efb2016-08-17 15:55:55 -04004984static void
4985skl_copy_wm_for_pipe(struct skl_wm_values *dst,
4986 struct skl_wm_values *src,
4987 enum pipe pipe)
4988{
Matt Roper2722efb2016-08-17 15:55:55 -04004989 memcpy(dst->ddb.y_plane[pipe], src->ddb.y_plane[pipe],
4990 sizeof(dst->ddb.y_plane[pipe]));
4991 memcpy(dst->ddb.plane[pipe], src->ddb.plane[pipe],
4992 sizeof(dst->ddb.plane[pipe]));
4993}
4994
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004995static void
4996skl_print_wm_changes(const struct drm_atomic_state *state)
4997{
4998 const struct drm_device *dev = state->dev;
4999 const struct drm_i915_private *dev_priv = to_i915(dev);
5000 const struct intel_atomic_state *intel_state =
5001 to_intel_atomic_state(state);
5002 const struct drm_crtc *crtc;
5003 const struct drm_crtc_state *cstate;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005004 const struct intel_plane *intel_plane;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005005 const struct skl_ddb_allocation *old_ddb = &dev_priv->wm.skl_hw.ddb;
5006 const struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
Maarten Lankhorst75704982016-11-01 12:04:10 +01005007 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005008
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01005009 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst75704982016-11-01 12:04:10 +01005010 const struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5011 enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005012
Maarten Lankhorst75704982016-11-01 12:04:10 +01005013 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005014 enum plane_id plane_id = intel_plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005015 const struct skl_ddb_entry *old, *new;
5016
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005017 old = &old_ddb->plane[pipe][plane_id];
5018 new = &new_ddb->plane[pipe][plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005019
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005020 if (skl_ddb_entry_equal(old, new))
5021 continue;
5022
Maarten Lankhorst75704982016-11-01 12:04:10 +01005023 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] ddb (%d - %d) -> (%d - %d)\n",
5024 intel_plane->base.base.id,
5025 intel_plane->base.name,
5026 old->start, old->end,
5027 new->start, new->end);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005028 }
5029 }
5030}
5031
Matt Roper98d39492016-05-12 07:06:03 -07005032static int
5033skl_compute_wm(struct drm_atomic_state *state)
5034{
5035 struct drm_crtc *crtc;
5036 struct drm_crtc_state *cstate;
Matt Roper734fa012016-05-12 15:11:40 -07005037 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
5038 struct skl_wm_values *results = &intel_state->wm_results;
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02005039 struct drm_device *dev = state->dev;
Matt Roper734fa012016-05-12 15:11:40 -07005040 struct skl_pipe_wm *pipe_wm;
Matt Roper98d39492016-05-12 07:06:03 -07005041 bool changed = false;
Matt Roper734fa012016-05-12 15:11:40 -07005042 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07005043
5044 /*
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02005045 * When we distrust bios wm we always need to recompute to set the
5046 * expected DDB allocations for each CRTC.
5047 */
5048 if (to_i915(dev)->wm.distrust_bios_wm)
5049 changed = true;
5050
5051 /*
Matt Roper98d39492016-05-12 07:06:03 -07005052 * If this transaction isn't actually touching any CRTC's, don't
5053 * bother with watermark calculation. Note that if we pass this
5054 * test, we're guaranteed to hold at least one CRTC state mutex,
5055 * which means we can safely use values like dev_priv->active_crtcs
5056 * since any racing commits that want to update them would need to
5057 * hold _all_ CRTC state mutexes.
5058 */
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01005059 for_each_new_crtc_in_state(state, crtc, cstate, i)
Matt Roper98d39492016-05-12 07:06:03 -07005060 changed = true;
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02005061
Matt Roper98d39492016-05-12 07:06:03 -07005062 if (!changed)
5063 return 0;
5064
Matt Roper734fa012016-05-12 15:11:40 -07005065 /* Clear all dirty flags */
5066 results->dirty_pipes = 0;
5067
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005068 ret = skl_compute_ddb(state);
Matt Roper98d39492016-05-12 07:06:03 -07005069 if (ret)
5070 return ret;
5071
Matt Roper734fa012016-05-12 15:11:40 -07005072 /*
5073 * Calculate WM's for all pipes that are part of this transaction.
5074 * Note that the DDB allocation above may have added more CRTC's that
5075 * weren't otherwise being modified (and set bits in dirty_pipes) if
5076 * pipe allocations had to change.
5077 *
5078 * FIXME: Now that we're doing this in the atomic check phase, we
5079 * should allow skl_update_pipe_wm() to return failure in cases where
5080 * no suitable watermark values can be found.
5081 */
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01005082 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Matt Roper734fa012016-05-12 15:11:40 -07005083 struct intel_crtc_state *intel_cstate =
5084 to_intel_crtc_state(cstate);
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02005085 const struct skl_pipe_wm *old_pipe_wm =
5086 &to_intel_crtc_state(crtc->state)->wm.skl.optimal;
Matt Roper734fa012016-05-12 15:11:40 -07005087
5088 pipe_wm = &intel_cstate->wm.skl.optimal;
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02005089 ret = skl_update_pipe_wm(cstate, old_pipe_wm, pipe_wm,
5090 &results->ddb, &changed);
Matt Roper734fa012016-05-12 15:11:40 -07005091 if (ret)
5092 return ret;
5093
5094 if (changed)
5095 results->dirty_pipes |= drm_crtc_mask(crtc);
5096
5097 if ((results->dirty_pipes & drm_crtc_mask(crtc)) == 0)
5098 /* This pipe's WM's did not change */
5099 continue;
5100
5101 intel_cstate->update_wm_pre = true;
Matt Roper734fa012016-05-12 15:11:40 -07005102 }
5103
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005104 skl_print_wm_changes(state);
5105
Matt Roper98d39492016-05-12 07:06:03 -07005106 return 0;
5107}
5108
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005109static void skl_atomic_update_crtc_wm(struct intel_atomic_state *state,
5110 struct intel_crtc_state *cstate)
5111{
5112 struct intel_crtc *crtc = to_intel_crtc(cstate->base.crtc);
5113 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5114 struct skl_pipe_wm *pipe_wm = &cstate->wm.skl.optimal;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005115 const struct skl_ddb_allocation *ddb = &state->wm_results.ddb;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005116 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005117 enum plane_id plane_id;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005118
5119 if (!(state->wm_results.dirty_pipes & drm_crtc_mask(&crtc->base)))
5120 return;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005121
5122 I915_WRITE(PIPE_WM_LINETIME(pipe), pipe_wm->linetime);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005123
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005124 for_each_plane_id_on_crtc(crtc, plane_id) {
5125 if (plane_id != PLANE_CURSOR)
5126 skl_write_plane_wm(crtc, &pipe_wm->planes[plane_id],
5127 ddb, plane_id);
5128 else
5129 skl_write_cursor_wm(crtc, &pipe_wm->planes[plane_id],
5130 ddb);
5131 }
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005132}
5133
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005134static void skl_initial_wm(struct intel_atomic_state *state,
5135 struct intel_crtc_state *cstate)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005136{
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005137 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02005138 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005139 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005140 struct skl_wm_values *results = &state->wm_results;
Matt Roper2722efb2016-08-17 15:55:55 -04005141 struct skl_wm_values *hw_vals = &dev_priv->wm.skl_hw;
Lyude27082492016-08-24 07:48:10 +02005142 enum pipe pipe = intel_crtc->pipe;
Bob Paauweadda50b2015-07-21 10:42:53 -07005143
Ville Syrjälä432081b2016-10-31 22:37:03 +02005144 if ((results->dirty_pipes & drm_crtc_mask(&intel_crtc->base)) == 0)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005145 return;
5146
Matt Roper734fa012016-05-12 15:11:40 -07005147 mutex_lock(&dev_priv->wm.wm_mutex);
5148
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005149 if (cstate->base.active_changed)
5150 skl_atomic_update_crtc_wm(state, cstate);
Lyude27082492016-08-24 07:48:10 +02005151
5152 skl_copy_wm_for_pipe(hw_vals, results, pipe);
Matt Roper734fa012016-05-12 15:11:40 -07005153
5154 mutex_unlock(&dev_priv->wm.wm_mutex);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005155}
5156
Ville Syrjäläd8905652016-01-14 14:53:35 +02005157static void ilk_compute_wm_config(struct drm_device *dev,
5158 struct intel_wm_config *config)
5159{
5160 struct intel_crtc *crtc;
5161
5162 /* Compute the currently _active_ config */
5163 for_each_intel_crtc(dev, crtc) {
5164 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
5165
5166 if (!wm->pipe_enabled)
5167 continue;
5168
5169 config->sprites_enabled |= wm->sprites_enabled;
5170 config->sprites_scaled |= wm->sprites_scaled;
5171 config->num_pipes_active++;
5172 }
5173}
5174
Matt Ropered4a6a72016-02-23 17:20:13 -08005175static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03005176{
Chris Wilson91c8a322016-07-05 10:40:23 +01005177 struct drm_device *dev = &dev_priv->drm;
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005178 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02005179 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02005180 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02005181 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005182 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07005183
Ville Syrjäläd8905652016-01-14 14:53:35 +02005184 ilk_compute_wm_config(dev, &config);
5185
5186 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
5187 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03005188
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005189 /* 5/6 split only in single pipe config on IVB+ */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00005190 if (INTEL_GEN(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02005191 config.num_pipes_active == 1 && config.sprites_enabled) {
5192 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
5193 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005194
Imre Deak820c1982013-12-17 14:46:36 +02005195 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03005196 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005197 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005198 }
5199
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005200 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005201 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005202
Imre Deak820c1982013-12-17 14:46:36 +02005203 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03005204
Imre Deak820c1982013-12-17 14:46:36 +02005205 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03005206}
5207
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005208static void ilk_initial_watermarks(struct intel_atomic_state *state,
5209 struct intel_crtc_state *cstate)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005210{
Matt Ropered4a6a72016-02-23 17:20:13 -08005211 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
5212 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005213
Matt Ropered4a6a72016-02-23 17:20:13 -08005214 mutex_lock(&dev_priv->wm.wm_mutex);
Matt Ropere8f1f022016-05-12 07:05:55 -07005215 intel_crtc->wm.active.ilk = cstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08005216 ilk_program_watermarks(dev_priv);
5217 mutex_unlock(&dev_priv->wm.wm_mutex);
5218}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005219
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005220static void ilk_optimize_watermarks(struct intel_atomic_state *state,
5221 struct intel_crtc_state *cstate)
Matt Ropered4a6a72016-02-23 17:20:13 -08005222{
5223 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
5224 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
5225
5226 mutex_lock(&dev_priv->wm.wm_mutex);
5227 if (cstate->wm.need_postvbl_update) {
Matt Ropere8f1f022016-05-12 07:05:55 -07005228 intel_crtc->wm.active.ilk = cstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08005229 ilk_program_watermarks(dev_priv);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005230 }
Matt Ropered4a6a72016-02-23 17:20:13 -08005231 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005232}
5233
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005234static inline void skl_wm_level_from_reg_val(uint32_t val,
5235 struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00005236{
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005237 level->plane_en = val & PLANE_WM_EN;
5238 level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
5239 level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
5240 PLANE_WM_LINES_MASK;
Pradeep Bhat30789992014-11-04 17:06:45 +00005241}
5242
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005243void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc,
5244 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00005245{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005246 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Pradeep Bhat30789992014-11-04 17:06:45 +00005247 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Pradeep Bhat30789992014-11-04 17:06:45 +00005248 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005249 int level, max_level;
5250 enum plane_id plane_id;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005251 uint32_t val;
Pradeep Bhat30789992014-11-04 17:06:45 +00005252
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005253 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00005254
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005255 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
5256 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00005257
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005258 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005259 if (plane_id != PLANE_CURSOR)
5260 val = I915_READ(PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005261 else
5262 val = I915_READ(CUR_WM(pipe, level));
5263
5264 skl_wm_level_from_reg_val(val, &wm->wm[level]);
5265 }
5266
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005267 if (plane_id != PLANE_CURSOR)
5268 val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005269 else
5270 val = I915_READ(CUR_WM_TRANS(pipe));
5271
5272 skl_wm_level_from_reg_val(val, &wm->trans_wm);
5273 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005274
Matt Roper3ef00282015-03-09 10:19:24 -07005275 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00005276 return;
5277
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005278 out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00005279}
5280
5281void skl_wm_get_hw_state(struct drm_device *dev)
5282{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005283 struct drm_i915_private *dev_priv = to_i915(dev);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005284 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
Damien Lespiaua269c582014-11-04 17:06:49 +00005285 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00005286 struct drm_crtc *crtc;
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005287 struct intel_crtc *intel_crtc;
5288 struct intel_crtc_state *cstate;
Pradeep Bhat30789992014-11-04 17:06:45 +00005289
Damien Lespiaua269c582014-11-04 17:06:49 +00005290 skl_ddb_get_hw_state(dev_priv, ddb);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005291 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5292 intel_crtc = to_intel_crtc(crtc);
5293 cstate = to_intel_crtc_state(crtc->state);
5294
5295 skl_pipe_wm_get_hw_state(crtc, &cstate->wm.skl.optimal);
5296
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02005297 if (intel_crtc->active)
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005298 hw->dirty_pipes |= drm_crtc_mask(crtc);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005299 }
Matt Ropera1de91e2016-05-12 07:05:57 -07005300
Matt Roper279e99d2016-05-12 07:06:02 -07005301 if (dev_priv->active_crtcs) {
5302 /* Fully recompute DDB on first atomic commit */
5303 dev_priv->wm.distrust_bios_wm = true;
5304 } else {
5305 /* Easy/common case; just sanitize DDB now if everything off */
5306 memset(ddb, 0, sizeof(*ddb));
5307 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005308}
5309
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005310static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
5311{
5312 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005313 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02005314 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005315 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper4e0963c2015-09-24 15:53:15 -07005316 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Matt Ropere8f1f022016-05-12 07:05:55 -07005317 struct intel_pipe_wm *active = &cstate->wm.ilk.optimal;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005318 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005319 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005320 [PIPE_A] = WM0_PIPEA_ILK,
5321 [PIPE_B] = WM0_PIPEB_ILK,
5322 [PIPE_C] = WM0_PIPEC_IVB,
5323 };
5324
5325 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005326 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02005327 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005328
Ville Syrjälä15606532016-05-13 17:55:17 +03005329 memset(active, 0, sizeof(*active));
5330
Matt Roper3ef00282015-03-09 10:19:24 -07005331 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02005332
5333 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005334 u32 tmp = hw->wm_pipe[pipe];
5335
5336 /*
5337 * For active pipes LP0 watermark is marked as
5338 * enabled, and LP1+ watermaks as disabled since
5339 * we can't really reverse compute them in case
5340 * multiple pipes are active.
5341 */
5342 active->wm[0].enable = true;
5343 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
5344 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
5345 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
5346 active->linetime = hw->wm_linetime[pipe];
5347 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005348 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005349
5350 /*
5351 * For inactive pipes, all watermark levels
5352 * should be marked as enabled but zeroed,
5353 * which is what we'd compute them to.
5354 */
5355 for (level = 0; level <= max_level; level++)
5356 active->wm[level].enable = true;
5357 }
Matt Roper4e0963c2015-09-24 15:53:15 -07005358
5359 intel_crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005360}
5361
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005362#define _FW_WM(value, plane) \
5363 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
5364#define _FW_WM_VLV(value, plane) \
5365 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
5366
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005367static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
5368 struct g4x_wm_values *wm)
5369{
5370 uint32_t tmp;
5371
5372 tmp = I915_READ(DSPFW1);
5373 wm->sr.plane = _FW_WM(tmp, SR);
5374 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5375 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
5376 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
5377
5378 tmp = I915_READ(DSPFW2);
5379 wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
5380 wm->sr.fbc = _FW_WM(tmp, FBC_SR);
5381 wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
5382 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
5383 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5384 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
5385
5386 tmp = I915_READ(DSPFW3);
5387 wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
5388 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5389 wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
5390 wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
5391}
5392
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005393static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
5394 struct vlv_wm_values *wm)
5395{
5396 enum pipe pipe;
5397 uint32_t tmp;
5398
5399 for_each_pipe(dev_priv, pipe) {
5400 tmp = I915_READ(VLV_DDL(pipe));
5401
Ville Syrjälä1b313892016-11-28 19:37:08 +02005402 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005403 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005404 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005405 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005406 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005407 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005408 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005409 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
5410 }
5411
5412 tmp = I915_READ(DSPFW1);
5413 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005414 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5415 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
5416 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005417
5418 tmp = I915_READ(DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005419 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
5420 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5421 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005422
5423 tmp = I915_READ(DSPFW3);
5424 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5425
5426 if (IS_CHERRYVIEW(dev_priv)) {
5427 tmp = I915_READ(DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005428 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5429 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005430
5431 tmp = I915_READ(DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005432 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
5433 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005434
5435 tmp = I915_READ(DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005436 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
5437 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005438
5439 tmp = I915_READ(DSPHOWM);
5440 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005441 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
5442 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
5443 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
5444 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5445 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5446 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5447 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5448 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5449 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005450 } else {
5451 tmp = I915_READ(DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005452 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5453 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005454
5455 tmp = I915_READ(DSPHOWM);
5456 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005457 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5458 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5459 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5460 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5461 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5462 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005463 }
5464}
5465
5466#undef _FW_WM
5467#undef _FW_WM_VLV
5468
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005469void g4x_wm_get_hw_state(struct drm_device *dev)
5470{
5471 struct drm_i915_private *dev_priv = to_i915(dev);
5472 struct g4x_wm_values *wm = &dev_priv->wm.g4x;
5473 struct intel_crtc *crtc;
5474
5475 g4x_read_wm_values(dev_priv, wm);
5476
5477 wm->cxsr = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
5478
5479 for_each_intel_crtc(dev, crtc) {
5480 struct intel_crtc_state *crtc_state =
5481 to_intel_crtc_state(crtc->base.state);
5482 struct g4x_wm_state *active = &crtc->wm.active.g4x;
5483 struct g4x_pipe_wm *raw;
5484 enum pipe pipe = crtc->pipe;
5485 enum plane_id plane_id;
5486 int level, max_level;
5487
5488 active->cxsr = wm->cxsr;
5489 active->hpll_en = wm->hpll_en;
5490 active->fbc_en = wm->fbc_en;
5491
5492 active->sr = wm->sr;
5493 active->hpll = wm->hpll;
5494
5495 for_each_plane_id_on_crtc(crtc, plane_id) {
5496 active->wm.plane[plane_id] =
5497 wm->pipe[pipe].plane[plane_id];
5498 }
5499
5500 if (wm->cxsr && wm->hpll_en)
5501 max_level = G4X_WM_LEVEL_HPLL;
5502 else if (wm->cxsr)
5503 max_level = G4X_WM_LEVEL_SR;
5504 else
5505 max_level = G4X_WM_LEVEL_NORMAL;
5506
5507 level = G4X_WM_LEVEL_NORMAL;
5508 raw = &crtc_state->wm.g4x.raw[level];
5509 for_each_plane_id_on_crtc(crtc, plane_id)
5510 raw->plane[plane_id] = active->wm.plane[plane_id];
5511
5512 if (++level > max_level)
5513 goto out;
5514
5515 raw = &crtc_state->wm.g4x.raw[level];
5516 raw->plane[PLANE_PRIMARY] = active->sr.plane;
5517 raw->plane[PLANE_CURSOR] = active->sr.cursor;
5518 raw->plane[PLANE_SPRITE0] = 0;
5519 raw->fbc = active->sr.fbc;
5520
5521 if (++level > max_level)
5522 goto out;
5523
5524 raw = &crtc_state->wm.g4x.raw[level];
5525 raw->plane[PLANE_PRIMARY] = active->hpll.plane;
5526 raw->plane[PLANE_CURSOR] = active->hpll.cursor;
5527 raw->plane[PLANE_SPRITE0] = 0;
5528 raw->fbc = active->hpll.fbc;
5529
5530 out:
5531 for_each_plane_id_on_crtc(crtc, plane_id)
5532 g4x_raw_plane_wm_set(crtc_state, level,
5533 plane_id, USHRT_MAX);
5534 g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
5535
5536 crtc_state->wm.g4x.optimal = *active;
5537 crtc_state->wm.g4x.intermediate = *active;
5538
5539 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
5540 pipe_name(pipe),
5541 wm->pipe[pipe].plane[PLANE_PRIMARY],
5542 wm->pipe[pipe].plane[PLANE_CURSOR],
5543 wm->pipe[pipe].plane[PLANE_SPRITE0]);
5544 }
5545
5546 DRM_DEBUG_KMS("Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
5547 wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
5548 DRM_DEBUG_KMS("Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
5549 wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
5550 DRM_DEBUG_KMS("Initial SR=%s HPLL=%s FBC=%s\n",
5551 yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
5552}
5553
5554void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
5555{
5556 struct intel_plane *plane;
5557 struct intel_crtc *crtc;
5558
5559 mutex_lock(&dev_priv->wm.wm_mutex);
5560
5561 for_each_intel_plane(&dev_priv->drm, plane) {
5562 struct intel_crtc *crtc =
5563 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
5564 struct intel_crtc_state *crtc_state =
5565 to_intel_crtc_state(crtc->base.state);
5566 struct intel_plane_state *plane_state =
5567 to_intel_plane_state(plane->base.state);
5568 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
5569 enum plane_id plane_id = plane->id;
5570 int level;
5571
5572 if (plane_state->base.visible)
5573 continue;
5574
5575 for (level = 0; level < 3; level++) {
5576 struct g4x_pipe_wm *raw =
5577 &crtc_state->wm.g4x.raw[level];
5578
5579 raw->plane[plane_id] = 0;
5580 wm_state->wm.plane[plane_id] = 0;
5581 }
5582
5583 if (plane_id == PLANE_PRIMARY) {
5584 for (level = 0; level < 3; level++) {
5585 struct g4x_pipe_wm *raw =
5586 &crtc_state->wm.g4x.raw[level];
5587 raw->fbc = 0;
5588 }
5589
5590 wm_state->sr.fbc = 0;
5591 wm_state->hpll.fbc = 0;
5592 wm_state->fbc_en = false;
5593 }
5594 }
5595
5596 for_each_intel_crtc(&dev_priv->drm, crtc) {
5597 struct intel_crtc_state *crtc_state =
5598 to_intel_crtc_state(crtc->base.state);
5599
5600 crtc_state->wm.g4x.intermediate =
5601 crtc_state->wm.g4x.optimal;
5602 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
5603 }
5604
5605 g4x_program_watermarks(dev_priv);
5606
5607 mutex_unlock(&dev_priv->wm.wm_mutex);
5608}
5609
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005610void vlv_wm_get_hw_state(struct drm_device *dev)
5611{
5612 struct drm_i915_private *dev_priv = to_i915(dev);
5613 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02005614 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005615 u32 val;
5616
5617 vlv_read_wm_values(dev_priv, wm);
5618
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005619 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
5620 wm->level = VLV_WM_LEVEL_PM2;
5621
5622 if (IS_CHERRYVIEW(dev_priv)) {
5623 mutex_lock(&dev_priv->rps.hw_lock);
5624
5625 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5626 if (val & DSP_MAXFIFO_PM5_ENABLE)
5627 wm->level = VLV_WM_LEVEL_PM5;
5628
Ville Syrjälä58590c12015-09-08 21:05:12 +03005629 /*
5630 * If DDR DVFS is disabled in the BIOS, Punit
5631 * will never ack the request. So if that happens
5632 * assume we don't have to enable/disable DDR DVFS
5633 * dynamically. To test that just set the REQ_ACK
5634 * bit to poke the Punit, but don't change the
5635 * HIGH/LOW bits so that we don't actually change
5636 * the current state.
5637 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005638 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03005639 val |= FORCE_DDR_FREQ_REQ_ACK;
5640 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
5641
5642 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
5643 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
5644 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
5645 "assuming DDR DVFS is disabled\n");
5646 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
5647 } else {
5648 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
5649 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
5650 wm->level = VLV_WM_LEVEL_DDR_DVFS;
5651 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005652
5653 mutex_unlock(&dev_priv->rps.hw_lock);
5654 }
5655
Ville Syrjäläff32c542017-03-02 19:14:57 +02005656 for_each_intel_crtc(dev, crtc) {
5657 struct intel_crtc_state *crtc_state =
5658 to_intel_crtc_state(crtc->base.state);
5659 struct vlv_wm_state *active = &crtc->wm.active.vlv;
5660 const struct vlv_fifo_state *fifo_state =
5661 &crtc_state->wm.vlv.fifo_state;
5662 enum pipe pipe = crtc->pipe;
5663 enum plane_id plane_id;
5664 int level;
5665
5666 vlv_get_fifo_size(crtc_state);
5667
5668 active->num_levels = wm->level + 1;
5669 active->cxsr = wm->cxsr;
5670
Ville Syrjäläff32c542017-03-02 19:14:57 +02005671 for (level = 0; level < active->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03005672 struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02005673 &crtc_state->wm.vlv.raw[level];
5674
5675 active->sr[level].plane = wm->sr.plane;
5676 active->sr[level].cursor = wm->sr.cursor;
5677
5678 for_each_plane_id_on_crtc(crtc, plane_id) {
5679 active->wm[level].plane[plane_id] =
5680 wm->pipe[pipe].plane[plane_id];
5681
5682 raw->plane[plane_id] =
5683 vlv_invert_wm_value(active->wm[level].plane[plane_id],
5684 fifo_state->plane[plane_id]);
5685 }
5686 }
5687
5688 for_each_plane_id_on_crtc(crtc, plane_id)
5689 vlv_raw_plane_wm_set(crtc_state, level,
5690 plane_id, USHRT_MAX);
5691 vlv_invalidate_wms(crtc, active, level);
5692
5693 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02005694 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02005695
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005696 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 +02005697 pipe_name(pipe),
5698 wm->pipe[pipe].plane[PLANE_PRIMARY],
5699 wm->pipe[pipe].plane[PLANE_CURSOR],
5700 wm->pipe[pipe].plane[PLANE_SPRITE0],
5701 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02005702 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005703
5704 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
5705 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
5706}
5707
Ville Syrjälä602ae832017-03-02 19:15:02 +02005708void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
5709{
5710 struct intel_plane *plane;
5711 struct intel_crtc *crtc;
5712
5713 mutex_lock(&dev_priv->wm.wm_mutex);
5714
5715 for_each_intel_plane(&dev_priv->drm, plane) {
5716 struct intel_crtc *crtc =
5717 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
5718 struct intel_crtc_state *crtc_state =
5719 to_intel_crtc_state(crtc->base.state);
5720 struct intel_plane_state *plane_state =
5721 to_intel_plane_state(plane->base.state);
5722 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
5723 const struct vlv_fifo_state *fifo_state =
5724 &crtc_state->wm.vlv.fifo_state;
5725 enum plane_id plane_id = plane->id;
5726 int level;
5727
5728 if (plane_state->base.visible)
5729 continue;
5730
5731 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03005732 struct g4x_pipe_wm *raw =
Ville Syrjälä602ae832017-03-02 19:15:02 +02005733 &crtc_state->wm.vlv.raw[level];
5734
5735 raw->plane[plane_id] = 0;
5736
5737 wm_state->wm[level].plane[plane_id] =
5738 vlv_invert_wm_value(raw->plane[plane_id],
5739 fifo_state->plane[plane_id]);
5740 }
5741 }
5742
5743 for_each_intel_crtc(&dev_priv->drm, crtc) {
5744 struct intel_crtc_state *crtc_state =
5745 to_intel_crtc_state(crtc->base.state);
5746
5747 crtc_state->wm.vlv.intermediate =
5748 crtc_state->wm.vlv.optimal;
5749 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
5750 }
5751
5752 vlv_program_watermarks(dev_priv);
5753
5754 mutex_unlock(&dev_priv->wm.wm_mutex);
5755}
5756
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005757void ilk_wm_get_hw_state(struct drm_device *dev)
5758{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005759 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02005760 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005761 struct drm_crtc *crtc;
5762
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01005763 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005764 ilk_pipe_wm_get_hw_state(crtc);
5765
5766 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
5767 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
5768 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
5769
5770 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00005771 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjäläcfa76982014-03-07 18:32:08 +02005772 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
5773 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
5774 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005775
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005776 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02005777 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
5778 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01005779 else if (IS_IVYBRIDGE(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02005780 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
5781 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005782
5783 hw->enable_fbc_wm =
5784 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
5785}
5786
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005787/**
5788 * intel_update_watermarks - update FIFO watermark values based on current modes
5789 *
5790 * Calculate watermark values for the various WM regs based on current mode
5791 * and plane configuration.
5792 *
5793 * There are several cases to deal with here:
5794 * - normal (i.e. non-self-refresh)
5795 * - self-refresh (SR) mode
5796 * - lines are large relative to FIFO size (buffer can hold up to 2)
5797 * - lines are small relative to FIFO size (buffer can hold more than 2
5798 * lines), so need to account for TLB latency
5799 *
5800 * The normal calculation is:
5801 * watermark = dotclock * bytes per pixel * latency
5802 * where latency is platform & configuration dependent (we assume pessimal
5803 * values here).
5804 *
5805 * The SR calculation is:
5806 * watermark = (trunc(latency/line time)+1) * surface width *
5807 * bytes per pixel
5808 * where
5809 * line time = htotal / dotclock
5810 * surface width = hdisplay for normal plane and 64 for cursor
5811 * and latency is assumed to be high, as above.
5812 *
5813 * The final value programmed to the register should always be rounded up,
5814 * and include an extra 2 entries to account for clock crossings.
5815 *
5816 * We don't use the sprite, so we can ignore that. And on Crestline we have
5817 * to set the non-SR watermarks to 8.
5818 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02005819void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005820{
Ville Syrjälä432081b2016-10-31 22:37:03 +02005821 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005822
5823 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005824 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005825}
5826
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05305827void intel_enable_ipc(struct drm_i915_private *dev_priv)
5828{
5829 u32 val;
5830
5831 val = I915_READ(DISP_ARB_CTL2);
5832
5833 if (dev_priv->ipc_enabled)
5834 val |= DISP_IPC_ENABLE;
5835 else
5836 val &= ~DISP_IPC_ENABLE;
5837
5838 I915_WRITE(DISP_ARB_CTL2, val);
5839}
5840
5841void intel_init_ipc(struct drm_i915_private *dev_priv)
5842{
5843 dev_priv->ipc_enabled = false;
5844 if (!HAS_IPC(dev_priv))
5845 return;
5846
5847 dev_priv->ipc_enabled = true;
5848 intel_enable_ipc(dev_priv);
5849}
5850
Jani Nikulae2828912016-01-18 09:19:47 +02005851/*
Daniel Vetter92703882012-08-09 16:46:01 +02005852 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02005853 */
5854DEFINE_SPINLOCK(mchdev_lock);
5855
5856/* Global for IPS driver to get at the current i915 device. Protected by
5857 * mchdev_lock. */
5858static struct drm_i915_private *i915_mch_dev;
5859
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005860bool ironlake_set_drps(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005861{
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005862 u16 rgvswctl;
5863
Chris Wilson67520412017-03-02 13:28:01 +00005864 lockdep_assert_held(&mchdev_lock);
Daniel Vetter92703882012-08-09 16:46:01 +02005865
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005866 rgvswctl = I915_READ16(MEMSWCTL);
5867 if (rgvswctl & MEMCTL_CMD_STS) {
5868 DRM_DEBUG("gpu busy, RCS change rejected\n");
5869 return false; /* still busy with another command */
5870 }
5871
5872 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
5873 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
5874 I915_WRITE16(MEMSWCTL, rgvswctl);
5875 POSTING_READ16(MEMSWCTL);
5876
5877 rgvswctl |= MEMCTL_CMD_STS;
5878 I915_WRITE16(MEMSWCTL, rgvswctl);
5879
5880 return true;
5881}
5882
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005883static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005884{
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00005885 u32 rgvmodectl;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005886 u8 fmax, fmin, fstart, vstart;
5887
Daniel Vetter92703882012-08-09 16:46:01 +02005888 spin_lock_irq(&mchdev_lock);
5889
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00005890 rgvmodectl = I915_READ(MEMMODECTL);
5891
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005892 /* Enable temp reporting */
5893 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
5894 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
5895
5896 /* 100ms RC evaluation intervals */
5897 I915_WRITE(RCUPEI, 100000);
5898 I915_WRITE(RCDNEI, 100000);
5899
5900 /* Set max/min thresholds to 90ms and 80ms respectively */
5901 I915_WRITE(RCBMAXAVG, 90000);
5902 I915_WRITE(RCBMINAVG, 80000);
5903
5904 I915_WRITE(MEMIHYST, 1);
5905
5906 /* Set up min, max, and cur for interrupt handling */
5907 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
5908 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
5909 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
5910 MEMMODE_FSTART_SHIFT;
5911
Ville Syrjälä616847e2015-09-18 20:03:19 +03005912 vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005913 PXVFREQ_PX_SHIFT;
5914
Daniel Vetter20e4d402012-08-08 23:35:39 +02005915 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
5916 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005917
Daniel Vetter20e4d402012-08-08 23:35:39 +02005918 dev_priv->ips.max_delay = fstart;
5919 dev_priv->ips.min_delay = fmin;
5920 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005921
5922 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
5923 fmax, fmin, fstart);
5924
5925 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
5926
5927 /*
5928 * Interrupts will be enabled in ironlake_irq_postinstall
5929 */
5930
5931 I915_WRITE(VIDSTART, vstart);
5932 POSTING_READ(VIDSTART);
5933
5934 rgvmodectl |= MEMMODE_SWMODE_EN;
5935 I915_WRITE(MEMMODECTL, rgvmodectl);
5936
Daniel Vetter92703882012-08-09 16:46:01 +02005937 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005938 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005939 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005940
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005941 ironlake_set_drps(dev_priv, fstart);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005942
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03005943 dev_priv->ips.last_count1 = I915_READ(DMIEC) +
5944 I915_READ(DDREC) + I915_READ(CSIEC);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005945 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03005946 dev_priv->ips.last_count2 = I915_READ(GFXEC);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005947 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02005948
5949 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005950}
5951
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005952static void ironlake_disable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005953{
Daniel Vetter92703882012-08-09 16:46:01 +02005954 u16 rgvswctl;
5955
5956 spin_lock_irq(&mchdev_lock);
5957
5958 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005959
5960 /* Ack interrupts, disable EFC interrupt */
5961 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
5962 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
5963 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
5964 I915_WRITE(DEIIR, DE_PCU_EVENT);
5965 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
5966
5967 /* Go back to the starting frequency */
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005968 ironlake_set_drps(dev_priv, dev_priv->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005969 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005970 rgvswctl |= MEMCTL_CMD_STS;
5971 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005972 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005973
Daniel Vetter92703882012-08-09 16:46:01 +02005974 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005975}
5976
Daniel Vetteracbe9472012-07-26 11:50:05 +02005977/* There's a funny hw issue where the hw returns all 0 when reading from
5978 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
5979 * ourselves, instead of doing a rmw cycle (which might result in us clearing
5980 * all limits and the gpu stuck at whatever frequency it is at atm).
5981 */
Akash Goel74ef1172015-03-06 11:07:19 +05305982static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005983{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005984 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005985
Daniel Vetter20b46e52012-07-26 11:16:14 +02005986 /* Only set the down limit when we've reached the lowest level to avoid
5987 * getting more interrupts, otherwise leave this clear. This prevents a
5988 * race in the hw when coming out of rc6: There's a tiny window where
5989 * the hw runs at the minimal clock before selecting the desired
5990 * frequency, if the down threshold expires in that window we will not
5991 * receive a down interrupt. */
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07005992 if (INTEL_GEN(dev_priv) >= 9) {
Akash Goel74ef1172015-03-06 11:07:19 +05305993 limits = (dev_priv->rps.max_freq_softlimit) << 23;
5994 if (val <= dev_priv->rps.min_freq_softlimit)
5995 limits |= (dev_priv->rps.min_freq_softlimit) << 14;
5996 } else {
5997 limits = dev_priv->rps.max_freq_softlimit << 24;
5998 if (val <= dev_priv->rps.min_freq_softlimit)
5999 limits |= dev_priv->rps.min_freq_softlimit << 16;
6000 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02006001
6002 return limits;
6003}
6004
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006005static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
6006{
6007 int new_power;
Akash Goel8a586432015-03-06 11:07:18 +05306008 u32 threshold_up = 0, threshold_down = 0; /* in % */
6009 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006010
6011 new_power = dev_priv->rps.power;
6012 switch (dev_priv->rps.power) {
6013 case LOW_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01006014 if (val > dev_priv->rps.efficient_freq + 1 &&
6015 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006016 new_power = BETWEEN;
6017 break;
6018
6019 case BETWEEN:
Chris Wilsona72b5622016-07-02 15:35:59 +01006020 if (val <= dev_priv->rps.efficient_freq &&
6021 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006022 new_power = LOW_POWER;
Chris Wilsona72b5622016-07-02 15:35:59 +01006023 else if (val >= dev_priv->rps.rp0_freq &&
6024 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006025 new_power = HIGH_POWER;
6026 break;
6027
6028 case HIGH_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01006029 if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 &&
6030 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006031 new_power = BETWEEN;
6032 break;
6033 }
6034 /* Max/min bins are special */
Chris Wilsonaed242f2015-03-18 09:48:21 +00006035 if (val <= dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006036 new_power = LOW_POWER;
Chris Wilsonaed242f2015-03-18 09:48:21 +00006037 if (val >= dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006038 new_power = HIGH_POWER;
6039 if (new_power == dev_priv->rps.power)
6040 return;
6041
6042 /* Note the units here are not exactly 1us, but 1280ns. */
6043 switch (new_power) {
6044 case LOW_POWER:
6045 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05306046 ei_up = 16000;
6047 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006048
6049 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306050 ei_down = 32000;
6051 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006052 break;
6053
6054 case BETWEEN:
6055 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05306056 ei_up = 13000;
6057 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006058
6059 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306060 ei_down = 32000;
6061 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006062 break;
6063
6064 case HIGH_POWER:
6065 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05306066 ei_up = 10000;
6067 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006068
6069 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306070 ei_down = 32000;
6071 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006072 break;
6073 }
6074
Mika Kuoppala6067a272017-02-15 15:52:59 +02006075 /* When byt can survive without system hang with dynamic
6076 * sw freq adjustments, this restriction can be lifted.
6077 */
6078 if (IS_VALLEYVIEW(dev_priv))
6079 goto skip_hw_write;
6080
Akash Goel8a586432015-03-06 11:07:18 +05306081 I915_WRITE(GEN6_RP_UP_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01006082 GT_INTERVAL_FROM_US(dev_priv, ei_up));
Akash Goel8a586432015-03-06 11:07:18 +05306083 I915_WRITE(GEN6_RP_UP_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01006084 GT_INTERVAL_FROM_US(dev_priv,
6085 ei_up * threshold_up / 100));
Akash Goel8a586432015-03-06 11:07:18 +05306086
6087 I915_WRITE(GEN6_RP_DOWN_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01006088 GT_INTERVAL_FROM_US(dev_priv, ei_down));
Akash Goel8a586432015-03-06 11:07:18 +05306089 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01006090 GT_INTERVAL_FROM_US(dev_priv,
6091 ei_down * threshold_down / 100));
Akash Goel8a586432015-03-06 11:07:18 +05306092
Chris Wilsona72b5622016-07-02 15:35:59 +01006093 I915_WRITE(GEN6_RP_CONTROL,
6094 GEN6_RP_MEDIA_TURBO |
6095 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6096 GEN6_RP_MEDIA_IS_GFX |
6097 GEN6_RP_ENABLE |
6098 GEN6_RP_UP_BUSY_AVG |
6099 GEN6_RP_DOWN_IDLE_AVG);
Akash Goel8a586432015-03-06 11:07:18 +05306100
Mika Kuoppala6067a272017-02-15 15:52:59 +02006101skip_hw_write:
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006102 dev_priv->rps.power = new_power;
Chris Wilson8fb55192015-04-07 16:20:28 +01006103 dev_priv->rps.up_threshold = threshold_up;
6104 dev_priv->rps.down_threshold = threshold_down;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006105 dev_priv->rps.last_adj = 0;
6106}
6107
Chris Wilson2876ce72014-03-28 08:03:34 +00006108static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
6109{
6110 u32 mask = 0;
6111
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006112 /* We use UP_EI_EXPIRED interupts for both up/down in manual mode */
Chris Wilson2876ce72014-03-28 08:03:34 +00006113 if (val > dev_priv->rps.min_freq_softlimit)
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006114 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
Chris Wilson2876ce72014-03-28 08:03:34 +00006115 if (val < dev_priv->rps.max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00006116 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00006117
Chris Wilson7b3c29f2014-07-10 20:31:19 +01006118 mask &= dev_priv->pm_rps_events;
6119
Imre Deak59d02a12014-12-19 19:33:26 +02006120 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00006121}
6122
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006123/* gen6_set_rps is called to update the frequency request, but should also be
6124 * called when the range (min_delay and max_delay) is modified so that we can
6125 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006126static int gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02006127{
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006128 /* min/max delay may still have been modified so be sure to
6129 * write the limits value.
6130 */
6131 if (val != dev_priv->rps.cur_freq) {
6132 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006133
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006134 if (INTEL_GEN(dev_priv) >= 9)
Akash Goel57041952015-03-06 11:07:17 +05306135 I915_WRITE(GEN6_RPNSWREQ,
6136 GEN9_FREQUENCY(val));
Chris Wilsondc979972016-05-10 14:10:04 +01006137 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006138 I915_WRITE(GEN6_RPNSWREQ,
6139 HSW_FREQUENCY(val));
6140 else
6141 I915_WRITE(GEN6_RPNSWREQ,
6142 GEN6_FREQUENCY(val) |
6143 GEN6_OFFSET(0) |
6144 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006145 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006146
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006147 /* Make sure we continue to get interrupts
6148 * until we hit the minimum or maximum frequencies.
6149 */
Akash Goel74ef1172015-03-06 11:07:19 +05306150 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00006151 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006152
Ben Widawskyb39fb292014-03-19 18:31:11 -07006153 dev_priv->rps.cur_freq = val;
Mika Kuoppala0f945922015-11-17 18:14:26 +02006154 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006155
6156 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006157}
6158
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006159static int valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006160{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006161 int err;
6162
Chris Wilsondc979972016-05-10 14:10:04 +01006163 if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006164 "Odd GPU freq value\n"))
6165 val &= ~1;
6166
Deepak Scd25dd52015-07-10 18:31:40 +05306167 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
6168
Chris Wilson8fb55192015-04-07 16:20:28 +01006169 if (val != dev_priv->rps.cur_freq) {
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006170 err = vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
6171 if (err)
6172 return err;
6173
Chris Wilsondb4c5e02017-02-10 15:03:46 +00006174 gen6_set_rps_thresholds(dev_priv, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01006175 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006176
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006177 dev_priv->rps.cur_freq = val;
6178 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006179
6180 return 0;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006181}
6182
Deepak Sa7f6e232015-05-09 18:04:44 +05306183/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05306184 *
6185 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05306186 * 1. Forcewake Media well.
6187 * 2. Request idle freq.
6188 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05306189*/
6190static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
6191{
Chris Wilsonaed242f2015-03-18 09:48:21 +00006192 u32 val = dev_priv->rps.idle_freq;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006193 int err;
Deepak S5549d252014-06-28 11:26:11 +05306194
Chris Wilsonaed242f2015-03-18 09:48:21 +00006195 if (dev_priv->rps.cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05306196 return;
6197
Chris Wilsonc9efef72017-01-02 15:28:45 +00006198 /* The punit delays the write of the frequency and voltage until it
6199 * determines the GPU is awake. During normal usage we don't want to
6200 * waste power changing the frequency if the GPU is sleeping (rc6).
6201 * However, the GPU and driver is now idle and we do not want to delay
6202 * switching to minimum voltage (reducing power whilst idle) as we do
6203 * not expect to be woken in the near future and so must flush the
6204 * change by waking the device.
6205 *
6206 * We choose to take the media powerwell (either would do to trick the
6207 * punit into committing the voltage change) as that takes a lot less
6208 * power than the render powerwell.
6209 */
Deepak Sa7f6e232015-05-09 18:04:44 +05306210 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006211 err = valleyview_set_rps(dev_priv, val);
Deepak Sa7f6e232015-05-09 18:04:44 +05306212 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006213
6214 if (err)
6215 DRM_ERROR("Failed to set RPS for idle\n");
Deepak S76c3552f2014-01-30 23:08:16 +05306216}
6217
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006218void gen6_rps_busy(struct drm_i915_private *dev_priv)
6219{
6220 mutex_lock(&dev_priv->rps.hw_lock);
6221 if (dev_priv->rps.enabled) {
Chris Wilsonbd648182017-02-10 15:03:48 +00006222 u8 freq;
6223
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006224 if (dev_priv->pm_rps_events & GEN6_PM_RP_UP_EI_EXPIRED)
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006225 gen6_rps_reset_ei(dev_priv);
6226 I915_WRITE(GEN6_PMINTRMSK,
6227 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Michał Winiarski2b83c4c2016-06-20 11:58:27 +02006228
Chris Wilsonc33d2472016-07-04 08:08:36 +01006229 gen6_enable_rps_interrupts(dev_priv);
6230
Chris Wilsonbd648182017-02-10 15:03:48 +00006231 /* Use the user's desired frequency as a guide, but for better
6232 * performance, jump directly to RPe as our starting frequency.
6233 */
6234 freq = max(dev_priv->rps.cur_freq,
6235 dev_priv->rps.efficient_freq);
6236
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006237 if (intel_set_rps(dev_priv,
Chris Wilsonbd648182017-02-10 15:03:48 +00006238 clamp(freq,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006239 dev_priv->rps.min_freq_softlimit,
6240 dev_priv->rps.max_freq_softlimit)))
6241 DRM_DEBUG_DRIVER("Failed to set idle frequency\n");
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006242 }
6243 mutex_unlock(&dev_priv->rps.hw_lock);
6244}
6245
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006246void gen6_rps_idle(struct drm_i915_private *dev_priv)
6247{
Chris Wilsonc33d2472016-07-04 08:08:36 +01006248 /* Flush our bottom-half so that it does not race with us
6249 * setting the idle frequency and so that it is bounded by
6250 * our rpm wakeref. And then disable the interrupts to stop any
6251 * futher RPS reclocking whilst we are asleep.
6252 */
6253 gen6_disable_rps_interrupts(dev_priv);
6254
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006255 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01006256 if (dev_priv->rps.enabled) {
Chris Wilsondc979972016-05-10 14:10:04 +01006257 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Deepak S76c3552f2014-01-30 23:08:16 +05306258 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02006259 else
Chris Wilsondc979972016-05-10 14:10:04 +01006260 gen6_set_rps(dev_priv, dev_priv->rps.idle_freq);
Chris Wilsonc0951f02013-10-10 21:58:50 +01006261 dev_priv->rps.last_adj = 0;
Ville Syrjälä12c100b2016-05-23 17:42:48 +03006262 I915_WRITE(GEN6_PMINTRMSK,
6263 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Chris Wilsonc0951f02013-10-10 21:58:50 +01006264 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01006265 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006266}
6267
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006268void gen6_rps_boost(struct drm_i915_gem_request *rq,
6269 struct intel_rps_client *rps)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006270{
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006271 struct drm_i915_private *i915 = rq->i915;
Chris Wilson74d290f2017-08-17 13:37:06 +01006272 unsigned long flags;
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006273 bool boost;
6274
Chris Wilson8d3afd72015-05-21 21:01:47 +01006275 /* This is intentionally racy! We peek at the state here, then
6276 * validate inside the RPS worker.
6277 */
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006278 if (!i915->rps.enabled)
Chris Wilson8d3afd72015-05-21 21:01:47 +01006279 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006280
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006281 boost = false;
Chris Wilson74d290f2017-08-17 13:37:06 +01006282 spin_lock_irqsave(&rq->lock, flags);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006283 if (!rq->waitboost && !i915_gem_request_completed(rq)) {
6284 atomic_inc(&i915->rps.num_waiters);
6285 rq->waitboost = true;
6286 boost = true;
Chris Wilsonc0951f02013-10-10 21:58:50 +01006287 }
Chris Wilson74d290f2017-08-17 13:37:06 +01006288 spin_unlock_irqrestore(&rq->lock, flags);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006289 if (!boost)
6290 return;
6291
6292 if (READ_ONCE(i915->rps.cur_freq) < i915->rps.boost_freq)
6293 schedule_work(&i915->rps.work);
6294
6295 atomic_inc(rps ? &rps->boosts : &i915->rps.boosts);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006296}
6297
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006298int intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006299{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006300 int err;
6301
Chris Wilsoncfd1c482017-02-20 09:47:07 +00006302 lockdep_assert_held(&dev_priv->rps.hw_lock);
6303 GEM_BUG_ON(val > dev_priv->rps.max_freq);
6304 GEM_BUG_ON(val < dev_priv->rps.min_freq);
6305
Chris Wilson76e4e4b2017-02-20 09:47:08 +00006306 if (!dev_priv->rps.enabled) {
6307 dev_priv->rps.cur_freq = val;
6308 return 0;
6309 }
6310
Chris Wilsondc979972016-05-10 14:10:04 +01006311 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006312 err = valleyview_set_rps(dev_priv, val);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006313 else
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006314 err = gen6_set_rps(dev_priv, val);
6315
6316 return err;
Jesse Barnes0a073b82013-04-17 15:54:58 -07006317}
6318
Chris Wilsondc979972016-05-10 14:10:04 +01006319static void gen9_disable_rc6(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00006320{
Zhe Wang20e49362014-11-04 17:07:05 +00006321 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00006322 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00006323}
6324
Chris Wilsondc979972016-05-10 14:10:04 +01006325static void gen9_disable_rps(struct drm_i915_private *dev_priv)
Akash Goel2030d682016-04-23 00:05:45 +05306326{
Akash Goel2030d682016-04-23 00:05:45 +05306327 I915_WRITE(GEN6_RP_CONTROL, 0);
6328}
6329
Chris Wilsondc979972016-05-10 14:10:04 +01006330static void gen6_disable_rps(struct drm_i915_private *dev_priv)
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006331{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006332 I915_WRITE(GEN6_RC_CONTROL, 0);
6333 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Akash Goel2030d682016-04-23 00:05:45 +05306334 I915_WRITE(GEN6_RP_CONTROL, 0);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006335}
6336
Chris Wilsondc979972016-05-10 14:10:04 +01006337static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306338{
Deepak S38807742014-05-23 21:00:15 +05306339 I915_WRITE(GEN6_RC_CONTROL, 0);
6340}
6341
Chris Wilsondc979972016-05-10 14:10:04 +01006342static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006343{
Deepak S98a2e5f2014-08-18 10:35:27 -07006344 /* we're doing forcewake before Disabling RC6,
6345 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02006346 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07006347
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006348 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006349
Mika Kuoppala59bad942015-01-16 11:34:40 +02006350 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006351}
6352
Chris Wilsondc979972016-05-10 14:10:04 +01006353static void intel_print_rc6_info(struct drm_i915_private *dev_priv, u32 mode)
Ben Widawskydc39fff2013-10-18 12:32:07 -07006354{
Chris Wilsondc979972016-05-10 14:10:04 +01006355 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Imre Deak91ca6892014-04-14 20:24:25 +03006356 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
6357 mode = GEN6_RC_CTL_RC6_ENABLE;
6358 else
6359 mode = 0;
6360 }
Chris Wilsondc979972016-05-10 14:10:04 +01006361 if (HAS_RC6p(dev_priv))
Imre Deakb99d49c2016-06-29 19:13:54 +03006362 DRM_DEBUG_DRIVER("Enabling RC6 states: "
6363 "RC6 %s RC6p %s RC6pp %s\n",
6364 onoff(mode & GEN6_RC_CTL_RC6_ENABLE),
6365 onoff(mode & GEN6_RC_CTL_RC6p_ENABLE),
6366 onoff(mode & GEN6_RC_CTL_RC6pp_ENABLE));
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07006367
6368 else
Imre Deakb99d49c2016-06-29 19:13:54 +03006369 DRM_DEBUG_DRIVER("Enabling RC6 states: RC6 %s\n",
6370 onoff(mode & GEN6_RC_CTL_RC6_ENABLE));
Ben Widawskydc39fff2013-10-18 12:32:07 -07006371}
6372
Chris Wilsondc979972016-05-10 14:10:04 +01006373static bool bxt_check_bios_rc6_setup(struct drm_i915_private *dev_priv)
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306374{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006375 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306376 bool enable_rc6 = true;
6377 unsigned long rc6_ctx_base;
Imre Deakfc619842016-06-29 19:13:55 +03006378 u32 rc_ctl;
6379 int rc_sw_target;
6380
6381 rc_ctl = I915_READ(GEN6_RC_CONTROL);
6382 rc_sw_target = (I915_READ(GEN6_RC_STATE) & RC_SW_TARGET_STATE_MASK) >>
6383 RC_SW_TARGET_STATE_SHIFT;
6384 DRM_DEBUG_DRIVER("BIOS enabled RC states: "
6385 "HW_CTRL %s HW_RC6 %s SW_TARGET_STATE %x\n",
6386 onoff(rc_ctl & GEN6_RC_CTL_HW_ENABLE),
6387 onoff(rc_ctl & GEN6_RC_CTL_RC6_ENABLE),
6388 rc_sw_target);
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306389
6390 if (!(I915_READ(RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006391 DRM_DEBUG_DRIVER("RC6 Base location not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306392 enable_rc6 = false;
6393 }
6394
6395 /*
6396 * The exact context size is not known for BXT, so assume a page size
6397 * for this check.
6398 */
6399 rc6_ctx_base = I915_READ(RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006400 if (!((rc6_ctx_base >= ggtt->stolen_reserved_base) &&
6401 (rc6_ctx_base + PAGE_SIZE <= ggtt->stolen_reserved_base +
6402 ggtt->stolen_reserved_size))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006403 DRM_DEBUG_DRIVER("RC6 Base address not as expected.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306404 enable_rc6 = false;
6405 }
6406
6407 if (!(((I915_READ(PWRCTX_MAXCNT_RCSUNIT) & IDLE_TIME_MASK) > 1) &&
6408 ((I915_READ(PWRCTX_MAXCNT_VCSUNIT0) & IDLE_TIME_MASK) > 1) &&
6409 ((I915_READ(PWRCTX_MAXCNT_BCSUNIT) & IDLE_TIME_MASK) > 1) &&
6410 ((I915_READ(PWRCTX_MAXCNT_VECSUNIT) & IDLE_TIME_MASK) > 1))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006411 DRM_DEBUG_DRIVER("Engine Idle wait time not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306412 enable_rc6 = false;
6413 }
6414
Imre Deakfc619842016-06-29 19:13:55 +03006415 if (!I915_READ(GEN8_PUSHBUS_CONTROL) ||
6416 !I915_READ(GEN8_PUSHBUS_ENABLE) ||
6417 !I915_READ(GEN8_PUSHBUS_SHIFT)) {
6418 DRM_DEBUG_DRIVER("Pushbus not setup properly.\n");
6419 enable_rc6 = false;
6420 }
6421
6422 if (!I915_READ(GEN6_GFXPAUSE)) {
6423 DRM_DEBUG_DRIVER("GFX pause not setup properly.\n");
6424 enable_rc6 = false;
6425 }
6426
6427 if (!I915_READ(GEN8_MISC_CTRL0)) {
6428 DRM_DEBUG_DRIVER("GPM control not setup properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306429 enable_rc6 = false;
6430 }
6431
6432 return enable_rc6;
6433}
6434
Chris Wilsondc979972016-05-10 14:10:04 +01006435int sanitize_rc6_option(struct drm_i915_private *dev_priv, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006436{
Daniel Vettere7d66d82015-06-15 23:23:54 +02006437 /* No RC6 before Ironlake and code is gone for ilk. */
Chris Wilsondc979972016-05-10 14:10:04 +01006438 if (INTEL_INFO(dev_priv)->gen < 6)
Imre Deake6069ca2014-04-18 16:01:02 +03006439 return 0;
6440
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306441 if (!enable_rc6)
6442 return 0;
6443
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02006444 if (IS_GEN9_LP(dev_priv) && !bxt_check_bios_rc6_setup(dev_priv)) {
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306445 DRM_INFO("RC6 disabled by BIOS\n");
6446 return 0;
6447 }
6448
Daniel Vetter456470e2012-08-08 23:35:40 +02006449 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03006450 if (enable_rc6 >= 0) {
6451 int mask;
6452
Chris Wilsondc979972016-05-10 14:10:04 +01006453 if (HAS_RC6p(dev_priv))
Imre Deake6069ca2014-04-18 16:01:02 +03006454 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
6455 INTEL_RC6pp_ENABLE;
6456 else
6457 mask = INTEL_RC6_ENABLE;
6458
6459 if ((enable_rc6 & mask) != enable_rc6)
Imre Deakb99d49c2016-06-29 19:13:54 +03006460 DRM_DEBUG_DRIVER("Adjusting RC6 mask to %d "
6461 "(requested %d, valid %d)\n",
6462 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03006463
6464 return enable_rc6 & mask;
6465 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006466
Chris Wilsondc979972016-05-10 14:10:04 +01006467 if (IS_IVYBRIDGE(dev_priv))
Ben Widawskycca84a12014-01-28 20:25:38 -08006468 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08006469
6470 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006471}
6472
Chris Wilsondc979972016-05-10 14:10:04 +01006473static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
Imre Deake6069ca2014-04-18 16:01:02 +03006474{
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006475 /* All of these values are in units of 50MHz */
Chris Wilson773ea9a2016-07-13 09:10:33 +01006476
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006477 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02006478 if (IS_GEN9_LP(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006479 u32 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07006480 dev_priv->rps.rp0_freq = (rp_state_cap >> 16) & 0xff;
6481 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
6482 dev_priv->rps.min_freq = (rp_state_cap >> 0) & 0xff;
6483 } else {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006484 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07006485 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
6486 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
6487 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
6488 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006489 /* hw_max = RP0 until we check for overclocking */
Chris Wilson773ea9a2016-07-13 09:10:33 +01006490 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006491
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006492 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01006493 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006494 IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006495 u32 ddcc_status = 0;
6496
6497 if (sandybridge_pcode_read(dev_priv,
6498 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
6499 &ddcc_status) == 0)
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006500 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08006501 clamp_t(u8,
6502 ((ddcc_status >> 8) & 0xff),
6503 dev_priv->rps.min_freq,
6504 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006505 }
6506
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006507 if (IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Akash Goelc5e06882015-06-29 14:50:19 +05306508 /* Store the frequency values in 16.66 MHZ units, which is
Chris Wilson773ea9a2016-07-13 09:10:33 +01006509 * the natural hardware unit for SKL
6510 */
Akash Goelc5e06882015-06-29 14:50:19 +05306511 dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
6512 dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
6513 dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
6514 dev_priv->rps.max_freq *= GEN9_FREQ_SCALER;
6515 dev_priv->rps.efficient_freq *= GEN9_FREQ_SCALER;
6516 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006517}
6518
Chris Wilson3a45b052016-07-13 09:10:32 +01006519static void reset_rps(struct drm_i915_private *dev_priv,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006520 int (*set)(struct drm_i915_private *, u8))
Chris Wilson3a45b052016-07-13 09:10:32 +01006521{
6522 u8 freq = dev_priv->rps.cur_freq;
6523
6524 /* force a reset */
6525 dev_priv->rps.power = -1;
6526 dev_priv->rps.cur_freq = -1;
6527
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006528 if (set(dev_priv, freq))
6529 DRM_ERROR("Failed to reset RPS to initial values\n");
Chris Wilson3a45b052016-07-13 09:10:32 +01006530}
6531
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006532/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Chris Wilsondc979972016-05-10 14:10:04 +01006533static void gen9_enable_rps(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00006534{
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006535 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
6536
Akash Goel0beb0592015-03-06 11:07:20 +05306537 /* Program defaults and thresholds for RPS*/
6538 I915_WRITE(GEN6_RC_VIDEO_FREQ,
6539 GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006540
Akash Goel0beb0592015-03-06 11:07:20 +05306541 /* 1 second timeout*/
6542 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
6543 GT_INTERVAL_FROM_US(dev_priv, 1000000));
6544
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006545 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006546
Akash Goel0beb0592015-03-06 11:07:20 +05306547 /* Leaning on the below call to gen6_set_rps to program/setup the
6548 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
6549 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
Chris Wilson3a45b052016-07-13 09:10:32 +01006550 reset_rps(dev_priv, gen6_set_rps);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006551
6552 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
6553}
6554
Chris Wilsondc979972016-05-10 14:10:04 +01006555static void gen9_enable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006556{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006557 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306558 enum intel_engine_id id;
Zhe Wang20e49362014-11-04 17:07:05 +00006559 uint32_t rc6_mask = 0;
Zhe Wang20e49362014-11-04 17:07:05 +00006560
6561 /* 1a: Software RC state - RC0 */
6562 I915_WRITE(GEN6_RC_STATE, 0);
6563
6564 /* 1b: Get forcewake during program sequence. Although the driver
6565 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006566 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00006567
6568 /* 2a: Disable RC states. */
6569 I915_WRITE(GEN6_RC_CONTROL, 0);
6570
6571 /* 2b: Program RC6 thresholds.*/
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05306572
6573 /* WaRsDoubleRc6WrlWithCoarsePowerGating: Doubling WRL only when CPG is enabled */
Chris Wilsondc979972016-05-10 14:10:04 +01006574 if (IS_SKYLAKE(dev_priv))
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05306575 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
6576 else
6577 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
Zhe Wang20e49362014-11-04 17:07:05 +00006578 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6579 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05306580 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006581 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05306582
Dave Gordon1a3d1892016-05-13 15:36:30 +01006583 if (HAS_GUC(dev_priv))
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05306584 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
6585
Zhe Wang20e49362014-11-04 17:07:05 +00006586 I915_WRITE(GEN6_RC_SLEEP, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00006587
Zhe Wang38c23522015-01-20 12:23:04 +00006588 /* 2c: Program Coarse Power Gating Policies. */
6589 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
6590 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
6591
Zhe Wang20e49362014-11-04 17:07:05 +00006592 /* 3a: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006593 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Zhe Wang20e49362014-11-04 17:07:05 +00006594 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Jani Nikula87ad3212016-01-14 12:53:34 +02006595 DRM_INFO("RC6 %s\n", onoff(rc6_mask & GEN6_RC_CTL_RC6_ENABLE));
Chris Wilson1c044f92017-01-25 17:26:01 +00006596 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
6597 I915_WRITE(GEN6_RC_CONTROL,
6598 GEN6_RC_CTL_HW_ENABLE | GEN6_RC_CTL_EI_MODE(1) | rc6_mask);
Zhe Wang20e49362014-11-04 17:07:05 +00006599
Sagar Kamblecb07bae2015-04-12 11:28:14 +05306600 /*
6601 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05306602 * WaRsDisableCoarsePowerGating:skl,bxt - Render/Media PG need to be disabled with RC6.
Sagar Kamblecb07bae2015-04-12 11:28:14 +05306603 */
Chris Wilsondc979972016-05-10 14:10:04 +01006604 if (NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05306605 I915_WRITE(GEN9_PG_ENABLE, 0);
6606 else
6607 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
6608 (GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE) : 0);
Zhe Wang38c23522015-01-20 12:23:04 +00006609
Mika Kuoppala59bad942015-01-16 11:34:40 +02006610 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00006611}
6612
Chris Wilsondc979972016-05-10 14:10:04 +01006613static void gen8_enable_rps(struct drm_i915_private *dev_priv)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006614{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006615 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306616 enum intel_engine_id id;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006617 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006618
6619 /* 1a: Software RC state - RC0 */
6620 I915_WRITE(GEN6_RC_STATE, 0);
6621
6622 /* 1c & 1d: Get forcewake during program sequence. Although the driver
6623 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006624 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006625
6626 /* 2a: Disable RC states. */
6627 I915_WRITE(GEN6_RC_CONTROL, 0);
6628
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006629 /* 2b: Program RC6 thresholds.*/
6630 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
6631 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6632 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05306633 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006634 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006635 I915_WRITE(GEN6_RC_SLEEP, 0);
Chris Wilsondc979972016-05-10 14:10:04 +01006636 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07006637 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
6638 else
6639 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006640
6641 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006642 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006643 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Chris Wilsondc979972016-05-10 14:10:04 +01006644 intel_print_rc6_info(dev_priv, rc6_mask);
6645 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07006646 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
6647 GEN7_RC_CTL_TO_MODE |
6648 rc6_mask);
6649 else
6650 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
6651 GEN6_RC_CTL_EI_MODE(1) |
6652 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006653
6654 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07006655 I915_WRITE(GEN6_RPNSWREQ,
6656 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
6657 I915_WRITE(GEN6_RC_VIDEO_FREQ,
6658 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02006659 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
6660 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006661
Daniel Vetter7526ed72014-09-29 15:07:19 +02006662 /* Docs recommend 900MHz, and 300 MHz respectively */
6663 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
6664 dev_priv->rps.max_freq_softlimit << 24 |
6665 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006666
Daniel Vetter7526ed72014-09-29 15:07:19 +02006667 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
6668 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
6669 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
6670 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006671
Daniel Vetter7526ed72014-09-29 15:07:19 +02006672 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006673
6674 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02006675 I915_WRITE(GEN6_RP_CONTROL,
6676 GEN6_RP_MEDIA_TURBO |
6677 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6678 GEN6_RP_MEDIA_IS_GFX |
6679 GEN6_RP_ENABLE |
6680 GEN6_RP_UP_BUSY_AVG |
6681 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006682
Daniel Vetter7526ed72014-09-29 15:07:19 +02006683 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006684
Chris Wilson3a45b052016-07-13 09:10:32 +01006685 reset_rps(dev_priv, gen6_set_rps);
Daniel Vetter7526ed72014-09-29 15:07:19 +02006686
Mika Kuoppala59bad942015-01-16 11:34:40 +02006687 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006688}
6689
Chris Wilsondc979972016-05-10 14:10:04 +01006690static void gen6_enable_rps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006691{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006692 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306693 enum intel_engine_id id;
Chris Wilson99ac9612016-07-13 09:10:34 +01006694 u32 rc6vids, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006695 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006696 int rc6_mode;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00006697 int ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006698
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006699 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02006700
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006701 /* Here begins a magic sequence of register writes to enable
6702 * auto-downclocking.
6703 *
6704 * Perhaps there might be some value in exposing these to
6705 * userspace...
6706 */
6707 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006708
6709 /* Clear the DBG now so we don't confuse earlier errors */
Ville Syrjälä297b32e2016-04-13 21:09:30 +03006710 gtfifodbg = I915_READ(GTFIFODBG);
6711 if (gtfifodbg) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006712 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
6713 I915_WRITE(GTFIFODBG, gtfifodbg);
6714 }
6715
Mika Kuoppala59bad942015-01-16 11:34:40 +02006716 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006717
6718 /* disable the counters and set deterministic thresholds */
6719 I915_WRITE(GEN6_RC_CONTROL, 0);
6720
6721 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
6722 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
6723 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
6724 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
6725 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
6726
Akash Goel3b3f1652016-10-13 22:44:48 +05306727 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006728 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006729
6730 I915_WRITE(GEN6_RC_SLEEP, 0);
6731 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Chris Wilsondc979972016-05-10 14:10:04 +01006732 if (IS_IVYBRIDGE(dev_priv))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07006733 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
6734 else
6735 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08006736 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006737 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
6738
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006739 /* Check if we are enabling RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006740 rc6_mode = intel_enable_rc6();
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006741 if (rc6_mode & INTEL_RC6_ENABLE)
6742 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
6743
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006744 /* We don't use those on Haswell */
Chris Wilsondc979972016-05-10 14:10:04 +01006745 if (!IS_HASWELL(dev_priv)) {
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006746 if (rc6_mode & INTEL_RC6p_ENABLE)
6747 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006748
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006749 if (rc6_mode & INTEL_RC6pp_ENABLE)
6750 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
6751 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006752
Chris Wilsondc979972016-05-10 14:10:04 +01006753 intel_print_rc6_info(dev_priv, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006754
6755 I915_WRITE(GEN6_RC_CONTROL,
6756 rc6_mask |
6757 GEN6_RC_CTL_EI_MODE(1) |
6758 GEN6_RC_CTL_HW_ENABLE);
6759
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006760 /* Power down if completely idle for over 50ms */
6761 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006762 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006763
Chris Wilson3a45b052016-07-13 09:10:32 +01006764 reset_rps(dev_priv, gen6_set_rps);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006765
Ben Widawsky31643d52012-09-26 10:34:01 -07006766 rc6vids = 0;
6767 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
Chris Wilsondc979972016-05-10 14:10:04 +01006768 if (IS_GEN6(dev_priv) && ret) {
Ben Widawsky31643d52012-09-26 10:34:01 -07006769 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
Chris Wilsondc979972016-05-10 14:10:04 +01006770 } else if (IS_GEN6(dev_priv) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
Ben Widawsky31643d52012-09-26 10:34:01 -07006771 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
6772 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
6773 rc6vids &= 0xffff00;
6774 rc6vids |= GEN6_ENCODE_RC6_VID(450);
6775 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
6776 if (ret)
6777 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
6778 }
6779
Mika Kuoppala59bad942015-01-16 11:34:40 +02006780 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006781}
6782
Chris Wilsonfb7404e2016-07-13 09:10:38 +01006783static void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006784{
6785 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01006786 unsigned int gpu_freq;
6787 unsigned int max_ia_freq, min_ring_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05306788 unsigned int max_gpu_freq, min_gpu_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006789 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03006790 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006791
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006792 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02006793
Ben Widawskyeda79642013-10-07 17:15:48 -03006794 policy = cpufreq_cpu_get(0);
6795 if (policy) {
6796 max_ia_freq = policy->cpuinfo.max_freq;
6797 cpufreq_cpu_put(policy);
6798 } else {
6799 /*
6800 * Default to measured freq if none found, PCU will ensure we
6801 * don't go over
6802 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006803 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03006804 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006805
6806 /* Convert from kHz to MHz */
6807 max_ia_freq /= 1000;
6808
Ben Widawsky153b4b952013-10-22 22:05:09 -07006809 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07006810 /* convert DDR frequency from units of 266.6MHz to bandwidth */
6811 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01006812
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006813 if (IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05306814 /* Convert GT frequency to 50 HZ units */
6815 min_gpu_freq = dev_priv->rps.min_freq / GEN9_FREQ_SCALER;
6816 max_gpu_freq = dev_priv->rps.max_freq / GEN9_FREQ_SCALER;
6817 } else {
6818 min_gpu_freq = dev_priv->rps.min_freq;
6819 max_gpu_freq = dev_priv->rps.max_freq;
6820 }
6821
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006822 /*
6823 * For each potential GPU frequency, load a ring frequency we'd like
6824 * to use for memory access. We do this by specifying the IA frequency
6825 * the PCU should use as a reference to determine the ring frequency.
6826 */
Akash Goel4c8c7742015-06-29 14:50:20 +05306827 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
6828 int diff = max_gpu_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01006829 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006830
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006831 if (IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05306832 /*
6833 * ring_freq = 2 * GT. ring_freq is in 100MHz units
6834 * No floor required for ring frequency on SKL.
6835 */
6836 ring_freq = gpu_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01006837 } else if (INTEL_INFO(dev_priv)->gen >= 8) {
Ben Widawsky46c764d2013-11-02 21:07:49 -07006838 /* max(2 * GT, DDR). NB: GT is 50MHz units */
6839 ring_freq = max(min_ring_freq, gpu_freq);
Chris Wilsondc979972016-05-10 14:10:04 +01006840 } else if (IS_HASWELL(dev_priv)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07006841 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01006842 ring_freq = max(min_ring_freq, ring_freq);
6843 /* leave ia_freq as the default, chosen by cpufreq */
6844 } else {
6845 /* On older processors, there is no separate ring
6846 * clock domain, so in order to boost the bandwidth
6847 * of the ring, we need to upclock the CPU (ia_freq).
6848 *
6849 * For GPU frequencies less than 750MHz,
6850 * just use the lowest ring freq.
6851 */
6852 if (gpu_freq < min_freq)
6853 ia_freq = 800;
6854 else
6855 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
6856 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
6857 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006858
Ben Widawsky42c05262012-09-26 10:34:00 -07006859 sandybridge_pcode_write(dev_priv,
6860 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01006861 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
6862 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
6863 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006864 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006865}
6866
Ville Syrjälä03af2042014-06-28 02:03:53 +03006867static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05306868{
6869 u32 val, rp0;
6870
Jani Nikula5b5929c2015-10-07 11:17:46 +03006871 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05306872
Imre Deak43b67992016-08-31 19:13:02 +03006873 switch (INTEL_INFO(dev_priv)->sseu.eu_total) {
Jani Nikula5b5929c2015-10-07 11:17:46 +03006874 case 8:
6875 /* (2 * 4) config */
6876 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
6877 break;
6878 case 12:
6879 /* (2 * 6) config */
6880 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
6881 break;
6882 case 16:
6883 /* (2 * 8) config */
6884 default:
6885 /* Setting (2 * 8) Min RP0 for any other combination */
6886 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
6887 break;
Deepak S095acd52015-01-17 11:05:59 +05306888 }
Jani Nikula5b5929c2015-10-07 11:17:46 +03006889
6890 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
6891
Deepak S2b6b3a02014-05-27 15:59:30 +05306892 return rp0;
6893}
6894
6895static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
6896{
6897 u32 val, rpe;
6898
6899 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
6900 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
6901
6902 return rpe;
6903}
6904
Deepak S7707df42014-07-12 18:46:14 +05306905static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
6906{
6907 u32 val, rp1;
6908
Jani Nikula5b5929c2015-10-07 11:17:46 +03006909 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
6910 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
6911
Deepak S7707df42014-07-12 18:46:14 +05306912 return rp1;
6913}
6914
Deepak S96676fe2016-08-12 18:46:41 +05306915static u32 cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
6916{
6917 u32 val, rpn;
6918
6919 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
6920 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
6921 FB_GFX_FREQ_FUSE_MASK);
6922
6923 return rpn;
6924}
6925
Deepak Sf8f2b002014-07-10 13:16:21 +05306926static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
6927{
6928 u32 val, rp1;
6929
6930 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
6931
6932 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
6933
6934 return rp1;
6935}
6936
Ville Syrjälä03af2042014-06-28 02:03:53 +03006937static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006938{
6939 u32 val, rp0;
6940
Jani Nikula64936252013-05-22 15:36:20 +03006941 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006942
6943 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
6944 /* Clamp to max */
6945 rp0 = min_t(u32, rp0, 0xea);
6946
6947 return rp0;
6948}
6949
6950static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
6951{
6952 u32 val, rpe;
6953
Jani Nikula64936252013-05-22 15:36:20 +03006954 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006955 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03006956 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006957 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
6958
6959 return rpe;
6960}
6961
Ville Syrjälä03af2042014-06-28 02:03:53 +03006962static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006963{
Imre Deak36146032014-12-04 18:39:35 +02006964 u32 val;
6965
6966 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
6967 /*
6968 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
6969 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
6970 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
6971 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
6972 * to make sure it matches what Punit accepts.
6973 */
6974 return max_t(u32, val, 0xc0);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006975}
6976
Imre Deakae484342014-03-31 15:10:44 +03006977/* Check that the pctx buffer wasn't move under us. */
6978static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
6979{
6980 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
6981
6982 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
6983 dev_priv->vlv_pctx->stolen->start);
6984}
6985
Deepak S38807742014-05-23 21:00:15 +05306986
6987/* Check that the pcbr address is not empty. */
6988static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
6989{
6990 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
6991
6992 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
6993}
6994
Chris Wilsondc979972016-05-10 14:10:04 +01006995static void cherryview_setup_pctx(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306996{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02006997 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006998 unsigned long pctx_paddr, paddr;
Deepak S38807742014-05-23 21:00:15 +05306999 u32 pcbr;
7000 int pctx_size = 32*1024;
7001
Deepak S38807742014-05-23 21:00:15 +05307002 pcbr = I915_READ(VLV_PCBR);
7003 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02007004 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05307005 paddr = (dev_priv->mm.stolen_base +
Joonas Lahtinen62106b42016-03-18 10:42:57 +02007006 (ggtt->stolen_size - pctx_size));
Deepak S38807742014-05-23 21:00:15 +05307007
7008 pctx_paddr = (paddr & (~4095));
7009 I915_WRITE(VLV_PCBR, pctx_paddr);
7010 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02007011
7012 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05307013}
7014
Chris Wilsondc979972016-05-10 14:10:04 +01007015static void valleyview_setup_pctx(struct drm_i915_private *dev_priv)
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007016{
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007017 struct drm_i915_gem_object *pctx;
7018 unsigned long pctx_paddr;
7019 u32 pcbr;
7020 int pctx_size = 24*1024;
7021
7022 pcbr = I915_READ(VLV_PCBR);
7023 if (pcbr) {
7024 /* BIOS set it up already, grab the pre-alloc'd space */
7025 int pcbr_offset;
7026
7027 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00007028 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007029 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02007030 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007031 pctx_size);
7032 goto out;
7033 }
7034
Ville Syrjäläce611ef2014-11-07 21:33:46 +02007035 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
7036
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007037 /*
7038 * From the Gunit register HAS:
7039 * The Gfx driver is expected to program this register and ensure
7040 * proper allocation within Gfx stolen memory. For example, this
7041 * register should be programmed such than the PCBR range does not
7042 * overlap with other ranges, such as the frame buffer, protected
7043 * memory, or any other relevant ranges.
7044 */
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00007045 pctx = i915_gem_object_create_stolen(dev_priv, pctx_size);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007046 if (!pctx) {
7047 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
Tvrtko Ursulinee504892016-02-11 10:27:30 +00007048 goto out;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007049 }
7050
7051 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
7052 I915_WRITE(VLV_PCBR, pctx_paddr);
7053
7054out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02007055 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007056 dev_priv->vlv_pctx = pctx;
7057}
7058
Chris Wilsondc979972016-05-10 14:10:04 +01007059static void valleyview_cleanup_pctx(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007060{
Imre Deakae484342014-03-31 15:10:44 +03007061 if (WARN_ON(!dev_priv->vlv_pctx))
7062 return;
7063
Chris Wilsonf0cd5182016-10-28 13:58:43 +01007064 i915_gem_object_put(dev_priv->vlv_pctx);
Imre Deakae484342014-03-31 15:10:44 +03007065 dev_priv->vlv_pctx = NULL;
7066}
7067
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007068static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
7069{
7070 dev_priv->rps.gpll_ref_freq =
7071 vlv_get_cck_clock(dev_priv, "GPLL ref",
7072 CCK_GPLL_CLOCK_CONTROL,
7073 dev_priv->czclk_freq);
7074
7075 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
7076 dev_priv->rps.gpll_ref_freq);
7077}
7078
Chris Wilsondc979972016-05-10 14:10:04 +01007079static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03007080{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007081 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03007082
Chris Wilsondc979972016-05-10 14:10:04 +01007083 valleyview_setup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007084
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007085 vlv_init_gpll_ref_freq(dev_priv);
7086
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007087 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
7088 switch ((val >> 6) & 3) {
7089 case 0:
7090 case 1:
7091 dev_priv->mem_freq = 800;
7092 break;
7093 case 2:
7094 dev_priv->mem_freq = 1066;
7095 break;
7096 case 3:
7097 dev_priv->mem_freq = 1333;
7098 break;
7099 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02007100 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007101
Imre Deak4e805192014-04-14 20:24:41 +03007102 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
7103 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
7104 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007105 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03007106 dev_priv->rps.max_freq);
7107
7108 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
7109 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007110 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03007111 dev_priv->rps.efficient_freq);
7112
Deepak Sf8f2b002014-07-10 13:16:21 +05307113 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
7114 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007115 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05307116 dev_priv->rps.rp1_freq);
7117
Imre Deak4e805192014-04-14 20:24:41 +03007118 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
7119 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007120 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03007121 dev_priv->rps.min_freq);
Imre Deak4e805192014-04-14 20:24:41 +03007122}
7123
Chris Wilsondc979972016-05-10 14:10:04 +01007124static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05307125{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007126 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05307127
Chris Wilsondc979972016-05-10 14:10:04 +01007128 cherryview_setup_pctx(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307129
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007130 vlv_init_gpll_ref_freq(dev_priv);
7131
Ville Syrjäläa5805162015-05-26 20:42:30 +03007132 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02007133 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007134 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02007135
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007136 switch ((val >> 2) & 0x7) {
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007137 case 3:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007138 dev_priv->mem_freq = 2000;
7139 break;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03007140 default:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007141 dev_priv->mem_freq = 1600;
7142 break;
7143 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02007144 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007145
Deepak S2b6b3a02014-05-27 15:59:30 +05307146 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
7147 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
7148 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007149 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05307150 dev_priv->rps.max_freq);
7151
7152 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
7153 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007154 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05307155 dev_priv->rps.efficient_freq);
7156
Deepak S7707df42014-07-12 18:46:14 +05307157 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
7158 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007159 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05307160 dev_priv->rps.rp1_freq);
7161
Deepak S96676fe2016-08-12 18:46:41 +05307162 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307163 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007164 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05307165 dev_priv->rps.min_freq);
7166
Ville Syrjälä1c147622014-08-18 14:42:43 +03007167 WARN_ONCE((dev_priv->rps.max_freq |
7168 dev_priv->rps.efficient_freq |
7169 dev_priv->rps.rp1_freq |
7170 dev_priv->rps.min_freq) & 1,
7171 "Odd GPU freq values\n");
Deepak S38807742014-05-23 21:00:15 +05307172}
7173
Chris Wilsondc979972016-05-10 14:10:04 +01007174static void valleyview_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03007175{
Chris Wilsondc979972016-05-10 14:10:04 +01007176 valleyview_cleanup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007177}
7178
Chris Wilsondc979972016-05-10 14:10:04 +01007179static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05307180{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007181 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05307182 enum intel_engine_id id;
Deepak S2b6b3a02014-05-27 15:59:30 +05307183 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05307184
7185 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
7186
Ville Syrjälä297b32e2016-04-13 21:09:30 +03007187 gtfifodbg = I915_READ(GTFIFODBG) & ~(GT_FIFO_SBDEDICATE_FREE_ENTRY_CHV |
7188 GT_FIFO_FREE_ENTRIES_CHV);
Deepak S38807742014-05-23 21:00:15 +05307189 if (gtfifodbg) {
7190 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
7191 gtfifodbg);
7192 I915_WRITE(GTFIFODBG, gtfifodbg);
7193 }
7194
7195 cherryview_check_pctx(dev_priv);
7196
7197 /* 1a & 1b: Get forcewake during program sequence. Although the driver
7198 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02007199 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05307200
Ville Syrjälä160614a2015-01-19 13:50:47 +02007201 /* Disable RC states. */
7202 I915_WRITE(GEN6_RC_CONTROL, 0);
7203
Deepak S38807742014-05-23 21:00:15 +05307204 /* 2a: Program RC6 thresholds.*/
7205 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
7206 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
7207 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
7208
Akash Goel3b3f1652016-10-13 22:44:48 +05307209 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007210 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Deepak S38807742014-05-23 21:00:15 +05307211 I915_WRITE(GEN6_RC_SLEEP, 0);
7212
Deepak Sf4f71c72015-03-28 15:23:35 +05307213 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
7214 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
Deepak S38807742014-05-23 21:00:15 +05307215
7216 /* allows RC6 residency counter to work */
7217 I915_WRITE(VLV_COUNTER_CONTROL,
7218 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
7219 VLV_MEDIA_RC6_COUNT_EN |
7220 VLV_RENDER_RC6_COUNT_EN));
7221
7222 /* For now we assume BIOS is allocating and populating the PCBR */
7223 pcbr = I915_READ(VLV_PCBR);
7224
Deepak S38807742014-05-23 21:00:15 +05307225 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01007226 if ((intel_enable_rc6() & INTEL_RC6_ENABLE) &&
7227 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02007228 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05307229
7230 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
7231
Deepak S2b6b3a02014-05-27 15:59:30 +05307232 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02007233 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05307234 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
7235 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
7236 I915_WRITE(GEN6_RP_UP_EI, 66000);
7237 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
7238
7239 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7240
7241 /* 5: Enable RPS */
7242 I915_WRITE(GEN6_RP_CONTROL,
7243 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02007244 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05307245 GEN6_RP_ENABLE |
7246 GEN6_RP_UP_BUSY_AVG |
7247 GEN6_RP_DOWN_IDLE_AVG);
7248
Deepak S3ef62342015-04-29 08:36:24 +05307249 /* Setting Fixed Bias */
7250 val = VLV_OVERRIDE_EN |
7251 VLV_SOC_TDP_EN |
7252 CHV_BIAS_CPU_50_SOC_50;
7253 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
7254
Deepak S2b6b3a02014-05-27 15:59:30 +05307255 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
7256
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02007257 /* RPS code assumes GPLL is used */
7258 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
7259
Jani Nikula742f4912015-09-03 11:16:09 +03007260 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05307261 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7262
Chris Wilson3a45b052016-07-13 09:10:32 +01007263 reset_rps(dev_priv, valleyview_set_rps);
Deepak S2b6b3a02014-05-27 15:59:30 +05307264
Mika Kuoppala59bad942015-01-16 11:34:40 +02007265 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05307266}
7267
Chris Wilsondc979972016-05-10 14:10:04 +01007268static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07007269{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007270 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05307271 enum intel_engine_id id;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07007272 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07007273
7274 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
7275
Imre Deakae484342014-03-31 15:10:44 +03007276 valleyview_check_pctx(dev_priv);
7277
Ville Syrjälä297b32e2016-04-13 21:09:30 +03007278 gtfifodbg = I915_READ(GTFIFODBG);
7279 if (gtfifodbg) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07007280 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
7281 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007282 I915_WRITE(GTFIFODBG, gtfifodbg);
7283 }
7284
Deepak Sc8d9a592013-11-23 14:55:42 +05307285 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02007286 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007287
Ville Syrjälä160614a2015-01-19 13:50:47 +02007288 /* Disable RC states. */
7289 I915_WRITE(GEN6_RC_CONTROL, 0);
7290
Ville Syrjäläcad725f2015-01-19 13:50:48 +02007291 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007292 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
7293 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
7294 I915_WRITE(GEN6_RP_UP_EI, 66000);
7295 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
7296
7297 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7298
7299 I915_WRITE(GEN6_RP_CONTROL,
7300 GEN6_RP_MEDIA_TURBO |
7301 GEN6_RP_MEDIA_HW_NORMAL_MODE |
7302 GEN6_RP_MEDIA_IS_GFX |
7303 GEN6_RP_ENABLE |
7304 GEN6_RP_UP_BUSY_AVG |
7305 GEN6_RP_DOWN_IDLE_CONT);
7306
7307 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
7308 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
7309 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
7310
Akash Goel3b3f1652016-10-13 22:44:48 +05307311 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007312 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007313
Jesse Barnes2f0aa3042013-11-15 09:32:11 -08007314 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007315
7316 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07007317 I915_WRITE(VLV_COUNTER_CONTROL,
Mika Kuoppala6b7f6aa2017-03-15 18:12:59 +02007318 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
7319 VLV_MEDIA_RC0_COUNT_EN |
Deepak S31685c22014-07-03 17:33:01 -04007320 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07007321 VLV_MEDIA_RC6_COUNT_EN |
7322 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04007323
Chris Wilsondc979972016-05-10 14:10:04 +01007324 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08007325 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07007326
Chris Wilsondc979972016-05-10 14:10:04 +01007327 intel_print_rc6_info(dev_priv, rc6_mode);
Ben Widawskydc39fff2013-10-18 12:32:07 -07007328
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07007329 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007330
Deepak S3ef62342015-04-29 08:36:24 +05307331 /* Setting Fixed Bias */
7332 val = VLV_OVERRIDE_EN |
7333 VLV_SOC_TDP_EN |
7334 VLV_BIAS_CPU_125_SOC_875;
7335 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
7336
Jani Nikula64936252013-05-22 15:36:20 +03007337 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007338
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02007339 /* RPS code assumes GPLL is used */
7340 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
7341
Jani Nikula742f4912015-09-03 11:16:09 +03007342 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07007343 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7344
Chris Wilson3a45b052016-07-13 09:10:32 +01007345 reset_rps(dev_priv, valleyview_set_rps);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007346
Mika Kuoppala59bad942015-01-16 11:34:40 +02007347 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007348}
7349
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007350static unsigned long intel_pxfreq(u32 vidfreq)
7351{
7352 unsigned long freq;
7353 int div = (vidfreq & 0x3f0000) >> 16;
7354 int post = (vidfreq & 0x3000) >> 12;
7355 int pre = (vidfreq & 0x7);
7356
7357 if (!pre)
7358 return 0;
7359
7360 freq = ((div * 133333) / ((1<<post) * pre));
7361
7362 return freq;
7363}
7364
Daniel Vettereb48eb02012-04-26 23:28:12 +02007365static const struct cparams {
7366 u16 i;
7367 u16 t;
7368 u16 m;
7369 u16 c;
7370} cparams[] = {
7371 { 1, 1333, 301, 28664 },
7372 { 1, 1066, 294, 24460 },
7373 { 1, 800, 294, 25192 },
7374 { 0, 1333, 276, 27605 },
7375 { 0, 1066, 276, 27605 },
7376 { 0, 800, 231, 23784 },
7377};
7378
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007379static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007380{
7381 u64 total_count, diff, ret;
7382 u32 count1, count2, count3, m = 0, c = 0;
7383 unsigned long now = jiffies_to_msecs(jiffies), diff1;
7384 int i;
7385
Chris Wilson67520412017-03-02 13:28:01 +00007386 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007387
Daniel Vetter20e4d402012-08-08 23:35:39 +02007388 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007389
7390 /* Prevent division-by-zero if we are asking too fast.
7391 * Also, we don't get interesting results if we are polling
7392 * faster than once in 10ms, so just return the saved value
7393 * in such cases.
7394 */
7395 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02007396 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007397
7398 count1 = I915_READ(DMIEC);
7399 count2 = I915_READ(DDREC);
7400 count3 = I915_READ(CSIEC);
7401
7402 total_count = count1 + count2 + count3;
7403
7404 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02007405 if (total_count < dev_priv->ips.last_count1) {
7406 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007407 diff += total_count;
7408 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007409 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007410 }
7411
7412 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007413 if (cparams[i].i == dev_priv->ips.c_m &&
7414 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02007415 m = cparams[i].m;
7416 c = cparams[i].c;
7417 break;
7418 }
7419 }
7420
7421 diff = div_u64(diff, diff1);
7422 ret = ((m * diff) + c);
7423 ret = div_u64(ret, 10);
7424
Daniel Vetter20e4d402012-08-08 23:35:39 +02007425 dev_priv->ips.last_count1 = total_count;
7426 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007427
Daniel Vetter20e4d402012-08-08 23:35:39 +02007428 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007429
7430 return ret;
7431}
7432
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007433unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
7434{
7435 unsigned long val;
7436
Chris Wilsondc979972016-05-10 14:10:04 +01007437 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007438 return 0;
7439
7440 spin_lock_irq(&mchdev_lock);
7441
7442 val = __i915_chipset_val(dev_priv);
7443
7444 spin_unlock_irq(&mchdev_lock);
7445
7446 return val;
7447}
7448
Daniel Vettereb48eb02012-04-26 23:28:12 +02007449unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
7450{
7451 unsigned long m, x, b;
7452 u32 tsfs;
7453
7454 tsfs = I915_READ(TSFS);
7455
7456 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
7457 x = I915_READ8(TR1);
7458
7459 b = tsfs & TSFS_INTR_MASK;
7460
7461 return ((m * x) / 127) - b;
7462}
7463
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007464static int _pxvid_to_vd(u8 pxvid)
7465{
7466 if (pxvid == 0)
7467 return 0;
7468
7469 if (pxvid >= 8 && pxvid < 31)
7470 pxvid = 31;
7471
7472 return (pxvid + 2) * 125;
7473}
7474
7475static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007476{
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007477 const int vd = _pxvid_to_vd(pxvid);
7478 const int vm = vd - 1125;
7479
Chris Wilsondc979972016-05-10 14:10:04 +01007480 if (INTEL_INFO(dev_priv)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007481 return vm > 0 ? vm : 0;
7482
7483 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007484}
7485
Daniel Vetter02d71952012-08-09 16:44:54 +02007486static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007487{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00007488 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007489 u32 count;
7490
Chris Wilson67520412017-03-02 13:28:01 +00007491 lockdep_assert_held(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007492
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00007493 now = ktime_get_raw_ns();
7494 diffms = now - dev_priv->ips.last_time2;
7495 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007496
7497 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02007498 if (!diffms)
7499 return;
7500
7501 count = I915_READ(GFXEC);
7502
Daniel Vetter20e4d402012-08-08 23:35:39 +02007503 if (count < dev_priv->ips.last_count2) {
7504 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007505 diff += count;
7506 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007507 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007508 }
7509
Daniel Vetter20e4d402012-08-08 23:35:39 +02007510 dev_priv->ips.last_count2 = count;
7511 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007512
7513 /* More magic constants... */
7514 diff = diff * 1181;
7515 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02007516 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007517}
7518
Daniel Vetter02d71952012-08-09 16:44:54 +02007519void i915_update_gfx_val(struct drm_i915_private *dev_priv)
7520{
Chris Wilsondc979972016-05-10 14:10:04 +01007521 if (INTEL_INFO(dev_priv)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02007522 return;
7523
Daniel Vetter92703882012-08-09 16:46:01 +02007524 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007525
7526 __i915_update_gfx_val(dev_priv);
7527
Daniel Vetter92703882012-08-09 16:46:01 +02007528 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007529}
7530
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007531static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007532{
7533 unsigned long t, corr, state1, corr2, state2;
7534 u32 pxvid, ext_v;
7535
Chris Wilson67520412017-03-02 13:28:01 +00007536 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007537
Ville Syrjälä616847e2015-09-18 20:03:19 +03007538 pxvid = I915_READ(PXVFREQ(dev_priv->rps.cur_freq));
Daniel Vettereb48eb02012-04-26 23:28:12 +02007539 pxvid = (pxvid >> 24) & 0x7f;
7540 ext_v = pvid_to_extvid(dev_priv, pxvid);
7541
7542 state1 = ext_v;
7543
7544 t = i915_mch_val(dev_priv);
7545
7546 /* Revel in the empirically derived constants */
7547
7548 /* Correction factor in 1/100000 units */
7549 if (t > 80)
7550 corr = ((t * 2349) + 135940);
7551 else if (t >= 50)
7552 corr = ((t * 964) + 29317);
7553 else /* < 50 */
7554 corr = ((t * 301) + 1004);
7555
7556 corr = corr * ((150142 * state1) / 10000 - 78642);
7557 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02007558 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007559
7560 state2 = (corr2 * state1) / 10000;
7561 state2 /= 100; /* convert to mW */
7562
Daniel Vetter02d71952012-08-09 16:44:54 +02007563 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007564
Daniel Vetter20e4d402012-08-08 23:35:39 +02007565 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007566}
7567
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007568unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
7569{
7570 unsigned long val;
7571
Chris Wilsondc979972016-05-10 14:10:04 +01007572 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007573 return 0;
7574
7575 spin_lock_irq(&mchdev_lock);
7576
7577 val = __i915_gfx_val(dev_priv);
7578
7579 spin_unlock_irq(&mchdev_lock);
7580
7581 return val;
7582}
7583
Daniel Vettereb48eb02012-04-26 23:28:12 +02007584/**
7585 * i915_read_mch_val - return value for IPS use
7586 *
7587 * Calculate and return a value for the IPS driver to use when deciding whether
7588 * we have thermal and power headroom to increase CPU or GPU power budget.
7589 */
7590unsigned long i915_read_mch_val(void)
7591{
7592 struct drm_i915_private *dev_priv;
7593 unsigned long chipset_val, graphics_val, ret = 0;
7594
Daniel Vetter92703882012-08-09 16:46:01 +02007595 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007596 if (!i915_mch_dev)
7597 goto out_unlock;
7598 dev_priv = i915_mch_dev;
7599
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007600 chipset_val = __i915_chipset_val(dev_priv);
7601 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007602
7603 ret = chipset_val + graphics_val;
7604
7605out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007606 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007607
7608 return ret;
7609}
7610EXPORT_SYMBOL_GPL(i915_read_mch_val);
7611
7612/**
7613 * i915_gpu_raise - raise GPU frequency limit
7614 *
7615 * Raise the limit; IPS indicates we have thermal headroom.
7616 */
7617bool i915_gpu_raise(void)
7618{
7619 struct drm_i915_private *dev_priv;
7620 bool ret = true;
7621
Daniel Vetter92703882012-08-09 16:46:01 +02007622 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007623 if (!i915_mch_dev) {
7624 ret = false;
7625 goto out_unlock;
7626 }
7627 dev_priv = i915_mch_dev;
7628
Daniel Vetter20e4d402012-08-08 23:35:39 +02007629 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
7630 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007631
7632out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007633 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007634
7635 return ret;
7636}
7637EXPORT_SYMBOL_GPL(i915_gpu_raise);
7638
7639/**
7640 * i915_gpu_lower - lower GPU frequency limit
7641 *
7642 * IPS indicates we're close to a thermal limit, so throttle back the GPU
7643 * frequency maximum.
7644 */
7645bool i915_gpu_lower(void)
7646{
7647 struct drm_i915_private *dev_priv;
7648 bool ret = true;
7649
Daniel Vetter92703882012-08-09 16:46:01 +02007650 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007651 if (!i915_mch_dev) {
7652 ret = false;
7653 goto out_unlock;
7654 }
7655 dev_priv = i915_mch_dev;
7656
Daniel Vetter20e4d402012-08-08 23:35:39 +02007657 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
7658 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007659
7660out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007661 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007662
7663 return ret;
7664}
7665EXPORT_SYMBOL_GPL(i915_gpu_lower);
7666
7667/**
7668 * i915_gpu_busy - indicate GPU business to IPS
7669 *
7670 * Tell the IPS driver whether or not the GPU is busy.
7671 */
7672bool i915_gpu_busy(void)
7673{
Daniel Vettereb48eb02012-04-26 23:28:12 +02007674 bool ret = false;
7675
Daniel Vetter92703882012-08-09 16:46:01 +02007676 spin_lock_irq(&mchdev_lock);
Chris Wilsondcff85c2016-08-05 10:14:11 +01007677 if (i915_mch_dev)
7678 ret = i915_mch_dev->gt.awake;
Daniel Vetter92703882012-08-09 16:46:01 +02007679 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007680
7681 return ret;
7682}
7683EXPORT_SYMBOL_GPL(i915_gpu_busy);
7684
7685/**
7686 * i915_gpu_turbo_disable - disable graphics turbo
7687 *
7688 * Disable graphics turbo by resetting the max frequency and setting the
7689 * current frequency to the default.
7690 */
7691bool i915_gpu_turbo_disable(void)
7692{
7693 struct drm_i915_private *dev_priv;
7694 bool ret = true;
7695
Daniel Vetter92703882012-08-09 16:46:01 +02007696 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007697 if (!i915_mch_dev) {
7698 ret = false;
7699 goto out_unlock;
7700 }
7701 dev_priv = i915_mch_dev;
7702
Daniel Vetter20e4d402012-08-08 23:35:39 +02007703 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007704
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01007705 if (!ironlake_set_drps(dev_priv, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02007706 ret = false;
7707
7708out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007709 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007710
7711 return ret;
7712}
7713EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
7714
7715/**
7716 * Tells the intel_ips driver that the i915 driver is now loaded, if
7717 * IPS got loaded first.
7718 *
7719 * This awkward dance is so that neither module has to depend on the
7720 * other in order for IPS to do the appropriate communication of
7721 * GPU turbo limits to i915.
7722 */
7723static void
7724ips_ping_for_i915_load(void)
7725{
7726 void (*link)(void);
7727
7728 link = symbol_get(ips_link_to_i915_driver);
7729 if (link) {
7730 link();
7731 symbol_put(ips_link_to_i915_driver);
7732 }
7733}
7734
7735void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
7736{
Daniel Vetter02d71952012-08-09 16:44:54 +02007737 /* We only register the i915 ips part with intel-ips once everything is
7738 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02007739 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007740 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02007741 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007742
7743 ips_ping_for_i915_load();
7744}
7745
7746void intel_gpu_ips_teardown(void)
7747{
Daniel Vetter92703882012-08-09 16:46:01 +02007748 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007749 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02007750 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007751}
Deepak S76c3552f2014-01-30 23:08:16 +05307752
Chris Wilsondc979972016-05-10 14:10:04 +01007753static void intel_init_emon(struct drm_i915_private *dev_priv)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007754{
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007755 u32 lcfuse;
7756 u8 pxw[16];
7757 int i;
7758
7759 /* Disable to program */
7760 I915_WRITE(ECR, 0);
7761 POSTING_READ(ECR);
7762
7763 /* Program energy weights for various events */
7764 I915_WRITE(SDEW, 0x15040d00);
7765 I915_WRITE(CSIEW0, 0x007f0000);
7766 I915_WRITE(CSIEW1, 0x1e220004);
7767 I915_WRITE(CSIEW2, 0x04000004);
7768
7769 for (i = 0; i < 5; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007770 I915_WRITE(PEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007771 for (i = 0; i < 3; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007772 I915_WRITE(DEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007773
7774 /* Program P-state weights to account for frequency power adjustment */
7775 for (i = 0; i < 16; i++) {
Ville Syrjälä616847e2015-09-18 20:03:19 +03007776 u32 pxvidfreq = I915_READ(PXVFREQ(i));
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007777 unsigned long freq = intel_pxfreq(pxvidfreq);
7778 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
7779 PXVFREQ_PX_SHIFT;
7780 unsigned long val;
7781
7782 val = vid * vid;
7783 val *= (freq / 1000);
7784 val *= 255;
7785 val /= (127*127*900);
7786 if (val > 0xff)
7787 DRM_ERROR("bad pxval: %ld\n", val);
7788 pxw[i] = val;
7789 }
7790 /* Render standby states get 0 weight */
7791 pxw[14] = 0;
7792 pxw[15] = 0;
7793
7794 for (i = 0; i < 4; i++) {
7795 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
7796 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
Ville Syrjälä616847e2015-09-18 20:03:19 +03007797 I915_WRITE(PXW(i), val);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007798 }
7799
7800 /* Adjust magic regs to magic values (more experimental results) */
7801 I915_WRITE(OGW0, 0);
7802 I915_WRITE(OGW1, 0);
7803 I915_WRITE(EG0, 0x00007f00);
7804 I915_WRITE(EG1, 0x0000000e);
7805 I915_WRITE(EG2, 0x000e0000);
7806 I915_WRITE(EG3, 0x68000300);
7807 I915_WRITE(EG4, 0x42000000);
7808 I915_WRITE(EG5, 0x00140031);
7809 I915_WRITE(EG6, 0);
7810 I915_WRITE(EG7, 0);
7811
7812 for (i = 0; i < 8; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007813 I915_WRITE(PXWL(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007814
7815 /* Enable PMON + select events */
7816 I915_WRITE(ECR, 0x80000019);
7817
7818 lcfuse = I915_READ(LCFUSE02);
7819
Daniel Vetter20e4d402012-08-08 23:35:39 +02007820 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007821}
7822
Chris Wilsondc979972016-05-10 14:10:04 +01007823void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007824{
Imre Deakb268c692015-12-15 20:10:31 +02007825 /*
7826 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
7827 * requirement.
7828 */
7829 if (!i915.enable_rc6) {
7830 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
7831 intel_runtime_pm_get(dev_priv);
7832 }
Imre Deake6069ca2014-04-18 16:01:02 +03007833
Chris Wilsonb5163db2016-08-10 13:58:24 +01007834 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson773ea9a2016-07-13 09:10:33 +01007835 mutex_lock(&dev_priv->rps.hw_lock);
7836
7837 /* Initialize RPS limits (for userspace) */
Chris Wilsondc979972016-05-10 14:10:04 +01007838 if (IS_CHERRYVIEW(dev_priv))
7839 cherryview_init_gt_powersave(dev_priv);
7840 else if (IS_VALLEYVIEW(dev_priv))
7841 valleyview_init_gt_powersave(dev_priv);
Chris Wilson2a13ae72016-08-02 11:15:27 +01007842 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson773ea9a2016-07-13 09:10:33 +01007843 gen6_init_rps_frequencies(dev_priv);
7844
7845 /* Derive initial user preferences/limits from the hardware limits */
7846 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
7847 dev_priv->rps.cur_freq = dev_priv->rps.idle_freq;
7848
7849 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
7850 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
7851
7852 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
7853 dev_priv->rps.min_freq_softlimit =
7854 max_t(int,
7855 dev_priv->rps.efficient_freq,
7856 intel_freq_opcode(dev_priv, 450));
7857
Chris Wilson99ac9612016-07-13 09:10:34 +01007858 /* After setting max-softlimit, find the overclock max freq */
7859 if (IS_GEN6(dev_priv) ||
7860 IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
7861 u32 params = 0;
7862
7863 sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &params);
7864 if (params & BIT(31)) { /* OC supported */
7865 DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
7866 (dev_priv->rps.max_freq & 0xff) * 50,
7867 (params & 0xff) * 50);
7868 dev_priv->rps.max_freq = params & 0xff;
7869 }
7870 }
7871
Chris Wilson29ecd78d2016-07-13 09:10:35 +01007872 /* Finally allow us to boost to max by default */
7873 dev_priv->rps.boost_freq = dev_priv->rps.max_freq;
7874
Chris Wilson773ea9a2016-07-13 09:10:33 +01007875 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb5163db2016-08-10 13:58:24 +01007876 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson54b4f682016-07-21 21:16:19 +01007877
7878 intel_autoenable_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03007879}
7880
Chris Wilsondc979972016-05-10 14:10:04 +01007881void intel_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007882{
Ville Syrjälä8dac1e12016-08-02 14:07:33 +03007883 if (IS_VALLEYVIEW(dev_priv))
Chris Wilsondc979972016-05-10 14:10:04 +01007884 valleyview_cleanup_gt_powersave(dev_priv);
Imre Deakb268c692015-12-15 20:10:31 +02007885
7886 if (!i915.enable_rc6)
7887 intel_runtime_pm_put(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03007888}
7889
Chris Wilson54b4f682016-07-21 21:16:19 +01007890/**
7891 * intel_suspend_gt_powersave - suspend PM work and helper threads
7892 * @dev_priv: i915 device
7893 *
7894 * We don't want to disable RC6 or other features here, we just want
7895 * to make sure any work we've queued has finished and won't bother
7896 * us while we're suspended.
7897 */
7898void intel_suspend_gt_powersave(struct drm_i915_private *dev_priv)
7899{
7900 if (INTEL_GEN(dev_priv) < 6)
7901 return;
7902
7903 if (cancel_delayed_work_sync(&dev_priv->rps.autoenable_work))
7904 intel_runtime_pm_put(dev_priv);
7905
7906 /* gen6_rps_idle() will be called later to disable interrupts */
7907}
7908
Chris Wilsonb7137e02016-07-13 09:10:37 +01007909void intel_sanitize_gt_powersave(struct drm_i915_private *dev_priv)
7910{
7911 dev_priv->rps.enabled = true; /* force disabling */
7912 intel_disable_gt_powersave(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007913
7914 gen6_reset_rps_interrupts(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07007915}
7916
Chris Wilsondc979972016-05-10 14:10:04 +01007917void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
Daniel Vetter8090c6b2012-06-24 16:42:32 +02007918{
Chris Wilsonb7137e02016-07-13 09:10:37 +01007919 if (!READ_ONCE(dev_priv->rps.enabled))
7920 return;
Jesse Barnes1a01ab32012-11-02 11:14:00 -07007921
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007922 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007923
Chris Wilsonb7137e02016-07-13 09:10:37 +01007924 if (INTEL_GEN(dev_priv) >= 9) {
7925 gen9_disable_rc6(dev_priv);
7926 gen9_disable_rps(dev_priv);
7927 } else if (IS_CHERRYVIEW(dev_priv)) {
7928 cherryview_disable_rps(dev_priv);
7929 } else if (IS_VALLEYVIEW(dev_priv)) {
7930 valleyview_disable_rps(dev_priv);
7931 } else if (INTEL_GEN(dev_priv) >= 6) {
7932 gen6_disable_rps(dev_priv);
7933 } else if (IS_IRONLAKE_M(dev_priv)) {
7934 ironlake_disable_drps(dev_priv);
7935 }
7936
7937 dev_priv->rps.enabled = false;
7938 mutex_unlock(&dev_priv->rps.hw_lock);
7939}
7940
7941void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
7942{
Chris Wilson54b4f682016-07-21 21:16:19 +01007943 /* We shouldn't be disabling as we submit, so this should be less
7944 * racy than it appears!
7945 */
Chris Wilsonb7137e02016-07-13 09:10:37 +01007946 if (READ_ONCE(dev_priv->rps.enabled))
7947 return;
7948
7949 /* Powersaving is controlled by the host when inside a VM */
7950 if (intel_vgpu_active(dev_priv))
7951 return;
7952
7953 mutex_lock(&dev_priv->rps.hw_lock);
Imre Deak3cc134e2014-11-19 15:30:03 +02007954
Chris Wilsondc979972016-05-10 14:10:04 +01007955 if (IS_CHERRYVIEW(dev_priv)) {
7956 cherryview_enable_rps(dev_priv);
7957 } else if (IS_VALLEYVIEW(dev_priv)) {
7958 valleyview_enable_rps(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007959 } else if (INTEL_GEN(dev_priv) >= 9) {
Chris Wilsondc979972016-05-10 14:10:04 +01007960 gen9_enable_rc6(dev_priv);
7961 gen9_enable_rps(dev_priv);
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07007962 if (IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv))
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007963 gen6_update_ring_freq(dev_priv);
Chris Wilsondc979972016-05-10 14:10:04 +01007964 } else if (IS_BROADWELL(dev_priv)) {
7965 gen8_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007966 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007967 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilsondc979972016-05-10 14:10:04 +01007968 gen6_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007969 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007970 } else if (IS_IRONLAKE_M(dev_priv)) {
7971 ironlake_enable_drps(dev_priv);
7972 intel_init_emon(dev_priv);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007973 }
Chris Wilsonaed242f2015-03-18 09:48:21 +00007974
7975 WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
7976 WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
7977
7978 WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
7979 WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
7980
Chris Wilson54b4f682016-07-21 21:16:19 +01007981 dev_priv->rps.enabled = true;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007982 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007983}
Imre Deakc6df39b2014-04-14 20:24:29 +03007984
Chris Wilson54b4f682016-07-21 21:16:19 +01007985static void __intel_autoenable_gt_powersave(struct work_struct *work)
7986{
7987 struct drm_i915_private *dev_priv =
7988 container_of(work, typeof(*dev_priv), rps.autoenable_work.work);
7989 struct intel_engine_cs *rcs;
7990 struct drm_i915_gem_request *req;
7991
7992 if (READ_ONCE(dev_priv->rps.enabled))
7993 goto out;
7994
Akash Goel3b3f1652016-10-13 22:44:48 +05307995 rcs = dev_priv->engine[RCS];
Chris Wilsone8a9c582016-12-18 15:37:20 +00007996 if (rcs->last_retired_context)
Chris Wilson54b4f682016-07-21 21:16:19 +01007997 goto out;
7998
7999 if (!rcs->init_context)
8000 goto out;
8001
8002 mutex_lock(&dev_priv->drm.struct_mutex);
8003
8004 req = i915_gem_request_alloc(rcs, dev_priv->kernel_context);
8005 if (IS_ERR(req))
8006 goto unlock;
8007
8008 if (!i915.enable_execlists && i915_switch_context(req) == 0)
8009 rcs->init_context(req);
8010
8011 /* Mark the device busy, calling intel_enable_gt_powersave() */
Chris Wilsone642c852017-03-17 11:47:09 +00008012 i915_add_request(req);
Chris Wilson54b4f682016-07-21 21:16:19 +01008013
8014unlock:
8015 mutex_unlock(&dev_priv->drm.struct_mutex);
8016out:
8017 intel_runtime_pm_put(dev_priv);
8018}
8019
8020void intel_autoenable_gt_powersave(struct drm_i915_private *dev_priv)
8021{
8022 if (READ_ONCE(dev_priv->rps.enabled))
8023 return;
8024
8025 if (IS_IRONLAKE_M(dev_priv)) {
8026 ironlake_enable_drps(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01008027 intel_init_emon(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01008028 } else if (INTEL_INFO(dev_priv)->gen >= 6) {
8029 /*
8030 * PCU communication is slow and this doesn't need to be
8031 * done at any specific time, so do this out of our fast path
8032 * to make resume and init faster.
8033 *
8034 * We depend on the HW RC6 power context save/restore
8035 * mechanism when entering D3 through runtime PM suspend. So
8036 * disable RPM until RPS/RC6 is properly setup. We can only
8037 * get here via the driver load/system resume/runtime resume
8038 * paths, so the _noresume version is enough (and in case of
8039 * runtime resume it's necessary).
8040 */
8041 if (queue_delayed_work(dev_priv->wq,
8042 &dev_priv->rps.autoenable_work,
8043 round_jiffies_up_relative(HZ)))
8044 intel_runtime_pm_get_noresume(dev_priv);
8045 }
8046}
8047
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008048static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01008049{
Daniel Vetter3107bd42012-10-31 22:52:31 +01008050 /*
8051 * On Ibex Peak and Cougar Point, we need to disable clock
8052 * gating for the panel power sequencer or it will fail to
8053 * start up when no ports are active.
8054 */
8055 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
8056}
8057
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008058static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008059{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03008060 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008061
Damien Lespiau055e3932014-08-18 13:49:10 +01008062 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008063 I915_WRITE(DSPCNTR(pipe),
8064 I915_READ(DSPCNTR(pipe)) |
8065 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03008066
8067 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
8068 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008069 }
8070}
8071
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008072static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä017636c2013-12-05 15:51:37 +02008073{
Ville Syrjälä017636c2013-12-05 15:51:37 +02008074 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
8075 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
8076 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
8077
8078 /*
8079 * Don't touch WM1S_LP_EN here.
8080 * Doing so could cause underruns.
8081 */
8082}
8083
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008084static void ilk_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008085{
Damien Lespiau231e54f2012-10-19 17:55:41 +01008086 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008087
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01008088 /*
8089 * Required for FBC
8090 * WaFbcDisableDpfcClockGating:ilk
8091 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008092 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
8093 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
8094 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008095
8096 I915_WRITE(PCH_3DCGDIS0,
8097 MARIUNIT_CLOCK_GATE_DISABLE |
8098 SVSMUNIT_CLOCK_GATE_DISABLE);
8099 I915_WRITE(PCH_3DCGDIS1,
8100 VFMUNIT_CLOCK_GATE_DISABLE);
8101
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008102 /*
8103 * According to the spec the following bits should be set in
8104 * order to enable memory self-refresh
8105 * The bit 22/21 of 0x42004
8106 * The bit 5 of 0x42020
8107 * The bit 15 of 0x45000
8108 */
8109 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8110 (I915_READ(ILK_DISPLAY_CHICKEN2) |
8111 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008112 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008113 I915_WRITE(DISP_ARB_CTL,
8114 (I915_READ(DISP_ARB_CTL) |
8115 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02008116
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008117 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008118
8119 /*
8120 * Based on the document from hardware guys the following bits
8121 * should be set unconditionally in order to enable FBC.
8122 * The bit 22 of 0x42000
8123 * The bit 22 of 0x42004
8124 * The bit 7,8,9 of 0x42020.
8125 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008126 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01008127 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008128 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8129 I915_READ(ILK_DISPLAY_CHICKEN1) |
8130 ILK_FBCQ_DIS);
8131 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8132 I915_READ(ILK_DISPLAY_CHICKEN2) |
8133 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008134 }
8135
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008136 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
8137
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008138 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8139 I915_READ(ILK_DISPLAY_CHICKEN2) |
8140 ILK_ELPIN_409_SELECT);
8141 I915_WRITE(_3D_CHICKEN2,
8142 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
8143 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02008144
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008145 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02008146 I915_WRITE(CACHE_MODE_0,
8147 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01008148
Akash Goel4e046322014-04-04 17:14:38 +05308149 /* WaDisable_RenderCache_OperationalFlush:ilk */
8150 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8151
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008152 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03008153
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008154 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008155}
8156
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008157static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01008158{
Daniel Vetter3107bd42012-10-31 22:52:31 +01008159 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008160 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01008161
8162 /*
8163 * On Ibex Peak and Cougar Point, we need to disable clock
8164 * gating for the panel power sequencer or it will fail to
8165 * start up when no ports are active.
8166 */
Jesse Barnescd664072013-10-02 10:34:19 -07008167 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
8168 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
8169 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008170 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
8171 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01008172 /* The below fixes the weird display corruption, a few pixels shifted
8173 * downward, on (only) LVDS of some HP laptops with IVY.
8174 */
Damien Lespiau055e3932014-08-18 13:49:10 +01008175 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03008176 val = I915_READ(TRANS_CHICKEN2(pipe));
8177 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
8178 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008179 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008180 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03008181 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
8182 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
8183 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008184 I915_WRITE(TRANS_CHICKEN2(pipe), val);
8185 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01008186 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01008187 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01008188 I915_WRITE(TRANS_CHICKEN1(pipe),
8189 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
8190 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008191}
8192
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008193static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008194{
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008195 uint32_t tmp;
8196
8197 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02008198 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
8199 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
8200 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008201}
8202
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008203static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008204{
Damien Lespiau231e54f2012-10-19 17:55:41 +01008205 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008206
Damien Lespiau231e54f2012-10-19 17:55:41 +01008207 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008208
8209 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8210 I915_READ(ILK_DISPLAY_CHICKEN2) |
8211 ILK_ELPIN_409_SELECT);
8212
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008213 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01008214 I915_WRITE(_3D_CHICKEN,
8215 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
8216
Akash Goel4e046322014-04-04 17:14:38 +05308217 /* WaDisable_RenderCache_OperationalFlush:snb */
8218 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8219
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008220 /*
8221 * BSpec recoomends 8x4 when MSAA is used,
8222 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008223 *
8224 * Note that PS/WM thread counts depend on the WIZ hashing
8225 * disable bit, which we don't touch here, but it's good
8226 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008227 */
8228 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008229 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008230
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008231 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008232
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008233 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02008234 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008235
8236 I915_WRITE(GEN6_UCGCTL1,
8237 I915_READ(GEN6_UCGCTL1) |
8238 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
8239 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
8240
8241 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
8242 * gating disable must be set. Failure to set it results in
8243 * flickering pixels due to Z write ordering failures after
8244 * some amount of runtime in the Mesa "fire" demo, and Unigine
8245 * Sanctuary and Tropics, and apparently anything else with
8246 * alpha test or pixel discard.
8247 *
8248 * According to the spec, bit 11 (RCCUNIT) must also be set,
8249 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008250 *
Ville Syrjäläef593182014-01-22 21:32:47 +02008251 * WaDisableRCCUnitClockGating:snb
8252 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008253 */
8254 I915_WRITE(GEN6_UCGCTL2,
8255 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
8256 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
8257
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02008258 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02008259 I915_WRITE(_3D_CHICKEN3,
8260 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008261
8262 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02008263 * Bspec says:
8264 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
8265 * 3DSTATE_SF number of SF output attributes is more than 16."
8266 */
8267 I915_WRITE(_3D_CHICKEN3,
8268 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
8269
8270 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008271 * According to the spec the following bits should be
8272 * set in order to enable memory self-refresh and fbc:
8273 * The bit21 and bit22 of 0x42000
8274 * The bit21 and bit22 of 0x42004
8275 * The bit5 and bit7 of 0x42020
8276 * The bit14 of 0x70180
8277 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01008278 *
8279 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008280 */
8281 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8282 I915_READ(ILK_DISPLAY_CHICKEN1) |
8283 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
8284 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8285 I915_READ(ILK_DISPLAY_CHICKEN2) |
8286 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01008287 I915_WRITE(ILK_DSPCLK_GATE_D,
8288 I915_READ(ILK_DSPCLK_GATE_D) |
8289 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
8290 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008291
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008292 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07008293
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008294 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008295
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008296 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008297}
8298
8299static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
8300{
8301 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
8302
Ville Syrjälä3aad9052014-01-22 21:32:59 +02008303 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02008304 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02008305 *
8306 * This actually overrides the dispatch
8307 * mode for all thread types.
8308 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008309 reg &= ~GEN7_FF_SCHED_MASK;
8310 reg |= GEN7_FF_TS_SCHED_HW;
8311 reg |= GEN7_FF_VS_SCHED_HW;
8312 reg |= GEN7_FF_DS_SCHED_HW;
8313
8314 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
8315}
8316
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008317static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008318{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008319 /*
8320 * TODO: this bit should only be enabled when really needed, then
8321 * disabled when not needed anymore in order to save power.
8322 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008323 if (HAS_PCH_LPT_LP(dev_priv))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008324 I915_WRITE(SOUTH_DSPCLK_GATE_D,
8325 I915_READ(SOUTH_DSPCLK_GATE_D) |
8326 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03008327
8328 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03008329 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
8330 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03008331 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008332}
8333
Ville Syrjälä712bf362016-10-31 22:37:23 +02008334static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03008335{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008336 if (HAS_PCH_LPT_LP(dev_priv)) {
Imre Deak7d708ee2013-04-17 14:04:50 +03008337 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
8338
8339 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8340 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8341 }
8342}
8343
Imre Deak450174f2016-05-03 15:54:21 +03008344static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
8345 int general_prio_credits,
8346 int high_prio_credits)
8347{
8348 u32 misccpctl;
8349
8350 /* WaTempDisableDOPClkGating:bdw */
8351 misccpctl = I915_READ(GEN7_MISCCPCTL);
8352 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
8353
8354 I915_WRITE(GEN8_L3SQCREG1,
8355 L3_GENERAL_PRIO_CREDITS(general_prio_credits) |
8356 L3_HIGH_PRIO_CREDITS(high_prio_credits));
8357
8358 /*
8359 * Wait at least 100 clocks before re-enabling clock gating.
8360 * See the definition of L3SQCREG1 in BSpec.
8361 */
8362 POSTING_READ(GEN8_L3SQCREG1);
8363 udelay(1);
8364 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
8365}
8366
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008367static void cnp_init_clock_gating(struct drm_i915_private *dev_priv)
8368{
8369 if (!HAS_PCH_CNP(dev_priv))
8370 return;
8371
8372 /* Wa #1181 */
8373 I915_WRITE(SOUTH_DSPCLK_GATE_D, CNP_PWM_CGE_GATING_DISABLE);
8374}
8375
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008376static void cnl_init_clock_gating(struct drm_i915_private *dev_priv)
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008377{
Rodrigo Vivi8f067832017-09-05 12:30:13 -07008378 u32 val;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008379 cnp_init_clock_gating(dev_priv);
8380
Rodrigo Vivi1a25db62017-08-15 16:16:51 -07008381 /* This is not an Wa. Enable for better image quality */
8382 I915_WRITE(_3D_CHICKEN3,
8383 _MASKED_BIT_ENABLE(_3D_CHICKEN3_AA_LINE_QUALITY_FIX_ENABLE));
8384
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008385 /* WaEnableChickenDCPR:cnl */
8386 I915_WRITE(GEN8_CHICKEN_DCPR_1,
8387 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
8388
8389 /* WaFbcWakeMemOn:cnl */
8390 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
8391 DISP_FBC_MEMORY_WAKE);
8392
8393 /* WaSarbUnitClockGatingDisable:cnl (pre-prod) */
8394 if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_B0))
8395 I915_WRITE(SLICE_UNIT_LEVEL_CLKGATE,
8396 I915_READ(SLICE_UNIT_LEVEL_CLKGATE) |
8397 SARBUNIT_CLKGATE_DIS);
Rodrigo Vivi8f067832017-09-05 12:30:13 -07008398
8399 /* Display WA #1133: WaFbcSkipSegments:cnl */
8400 val = I915_READ(ILK_DPFC_CHICKEN);
8401 val &= ~GLK_SKIP_SEG_COUNT_MASK;
8402 val |= GLK_SKIP_SEG_EN | GLK_SKIP_SEG_COUNT(1);
8403 I915_WRITE(ILK_DPFC_CHICKEN, val);
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008404}
8405
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008406static void cfl_init_clock_gating(struct drm_i915_private *dev_priv)
8407{
8408 cnp_init_clock_gating(dev_priv);
8409 gen9_init_clock_gating(dev_priv);
8410
8411 /* WaFbcNukeOnHostModify:cfl */
8412 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8413 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
8414}
8415
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008416static void kbl_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008417{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008418 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008419
8420 /* WaDisableSDEUnitClockGating:kbl */
8421 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
8422 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8423 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03008424
8425 /* WaDisableGamClockGating:kbl */
8426 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
8427 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
8428 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008429
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008430 /* WaFbcNukeOnHostModify:kbl */
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008431 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8432 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008433}
8434
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008435static void skl_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008436{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008437 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03008438
8439 /* WAC6entrylatency:skl */
8440 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
8441 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008442
8443 /* WaFbcNukeOnHostModify:skl */
8444 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8445 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008446}
8447
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008448static void bdw_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008449{
Damien Lespiau07d27e22014-03-03 17:31:46 +00008450 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008451
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008452 ilk_init_lp_watermarks(dev_priv);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07008453
Ben Widawskyab57fff2013-12-12 15:28:04 -08008454 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07008455 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008456
Ben Widawskyab57fff2013-12-12 15:28:04 -08008457 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008458 I915_WRITE(CHICKEN_PAR1_1,
8459 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
8460
Ben Widawskyab57fff2013-12-12 15:28:04 -08008461 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01008462 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00008463 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02008464 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02008465 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008466 }
Ben Widawsky63801f22013-12-12 17:26:03 -08008467
Ben Widawskyab57fff2013-12-12 15:28:04 -08008468 /* WaVSRefCountFullforceMissDisable:bdw */
8469 /* WaDSRefCountFullforceMissDisable:bdw */
8470 I915_WRITE(GEN7_FF_THREAD_MODE,
8471 I915_READ(GEN7_FF_THREAD_MODE) &
8472 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02008473
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02008474 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
8475 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02008476
8477 /* WaDisableSDEUnitClockGating:bdw */
8478 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8479 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00008480
Imre Deak450174f2016-05-03 15:54:21 +03008481 /* WaProgramL3SqcReg1Default:bdw */
8482 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03008483
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008484 /*
8485 * WaGttCachingOffByDefault:bdw
8486 * GTT cache may not work with big pages, so if those
8487 * are ever enabled GTT cache may need to be disabled.
8488 */
8489 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
8490
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03008491 /* WaKVMNotificationOnConfigChange:bdw */
8492 I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
8493 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
8494
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008495 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00008496
8497 /* WaDisableDopClockGating:bdw
8498 *
8499 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
8500 * clock gating.
8501 */
8502 I915_WRITE(GEN6_UCGCTL1,
8503 I915_READ(GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008504}
8505
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008506static void hsw_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008507{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008508 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008509
Francisco Jerezf3fc4882013-10-02 15:53:16 -07008510 /* L3 caching of data atomics doesn't work -- disable it. */
8511 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
8512 I915_WRITE(HSW_ROW_CHICKEN3,
8513 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
8514
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008515 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008516 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8517 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8518 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8519
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02008520 /* WaVSRefCountFullforceMissDisable:hsw */
8521 I915_WRITE(GEN7_FF_THREAD_MODE,
8522 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008523
Akash Goel4e046322014-04-04 17:14:38 +05308524 /* WaDisable_RenderCache_OperationalFlush:hsw */
8525 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8526
Chia-I Wufe27c602014-01-28 13:29:33 +08008527 /* enable HiZ Raw Stall Optimization */
8528 I915_WRITE(CACHE_MODE_0_GEN7,
8529 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
8530
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008531 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008532 I915_WRITE(CACHE_MODE_1,
8533 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03008534
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008535 /*
8536 * BSpec recommends 8x4 when MSAA is used,
8537 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008538 *
8539 * Note that PS/WM thread counts depend on the WIZ hashing
8540 * disable bit, which we don't touch here, but it's good
8541 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008542 */
8543 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008544 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008545
Kenneth Graunke94411592014-12-31 16:23:00 -08008546 /* WaSampleCChickenBitEnable:hsw */
8547 I915_WRITE(HALF_SLICE_CHICKEN3,
8548 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
8549
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008550 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07008551 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
8552
Paulo Zanoni90a88642013-05-03 17:23:45 -03008553 /* WaRsPkgCStateDisplayPMReq:hsw */
8554 I915_WRITE(CHICKEN_PAR1_1,
8555 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03008556
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008557 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008558}
8559
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008560static void ivb_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008561{
Ben Widawsky20848222012-05-04 18:58:59 -07008562 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008563
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008564 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008565
Damien Lespiau231e54f2012-10-19 17:55:41 +01008566 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008567
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008568 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05008569 I915_WRITE(_3D_CHICKEN3,
8570 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
8571
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008572 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008573 I915_WRITE(IVB_CHICKEN3,
8574 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8575 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8576
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008577 /* WaDisablePSDDualDispatchEnable:ivb */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008578 if (IS_IVB_GT1(dev_priv))
Jesse Barnes12f33822012-10-25 12:15:45 -07008579 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
8580 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07008581
Akash Goel4e046322014-04-04 17:14:38 +05308582 /* WaDisable_RenderCache_OperationalFlush:ivb */
8583 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8584
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008585 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008586 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
8587 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
8588
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008589 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008590 I915_WRITE(GEN7_L3CNTLREG1,
8591 GEN7_WA_FOR_GEN7_L3_CONTROL);
8592 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07008593 GEN7_WA_L3_CHICKEN_MODE);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008594 if (IS_IVB_GT1(dev_priv))
Jesse Barnes8ab43972012-10-25 12:15:42 -07008595 I915_WRITE(GEN7_ROW_CHICKEN2,
8596 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02008597 else {
8598 /* must write both registers */
8599 I915_WRITE(GEN7_ROW_CHICKEN2,
8600 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07008601 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
8602 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02008603 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008604
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008605 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05008606 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
8607 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
8608
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02008609 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07008610 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008611 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008612 */
8613 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02008614 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07008615
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008616 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008617 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8618 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8619 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8620
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008621 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008622
8623 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02008624
Chris Wilson22721342014-03-04 09:41:43 +00008625 if (0) { /* causes HiZ corruption on ivb:gt1 */
8626 /* enable HiZ Raw Stall Optimization */
8627 I915_WRITE(CACHE_MODE_0_GEN7,
8628 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
8629 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08008630
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008631 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02008632 I915_WRITE(CACHE_MODE_1,
8633 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07008634
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008635 /*
8636 * BSpec recommends 8x4 when MSAA is used,
8637 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008638 *
8639 * Note that PS/WM thread counts depend on the WIZ hashing
8640 * disable bit, which we don't touch here, but it's good
8641 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008642 */
8643 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008644 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008645
Ben Widawsky20848222012-05-04 18:58:59 -07008646 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
8647 snpcr &= ~GEN6_MBC_SNPCR_MASK;
8648 snpcr |= GEN6_MBC_SNPCR_MED;
8649 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008650
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008651 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008652 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008653
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008654 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008655}
8656
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008657static void vlv_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008658{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008659 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05008660 I915_WRITE(_3D_CHICKEN3,
8661 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
8662
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008663 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008664 I915_WRITE(IVB_CHICKEN3,
8665 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8666 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8667
Ville Syrjäläfad7d362014-01-22 21:32:39 +02008668 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008669 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07008670 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08008671 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
8672 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07008673
Akash Goel4e046322014-04-04 17:14:38 +05308674 /* WaDisable_RenderCache_OperationalFlush:vlv */
8675 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8676
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008677 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05008678 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
8679 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
8680
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008681 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07008682 I915_WRITE(GEN7_ROW_CHICKEN2,
8683 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
8684
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008685 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008686 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8687 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8688 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8689
Ville Syrjälä46680e02014-01-22 21:33:01 +02008690 gen7_setup_fixed_func_scheduler(dev_priv);
8691
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02008692 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07008693 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008694 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008695 */
8696 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02008697 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07008698
Akash Goelc98f5062014-03-24 23:00:07 +05308699 /* WaDisableL3Bank2xClockGate:vlv
8700 * Disabling L3 clock gating- MMIO 940c[25] = 1
8701 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
8702 I915_WRITE(GEN7_UCGCTL4,
8703 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07008704
Ville Syrjäläafd58e72014-01-22 21:33:03 +02008705 /*
8706 * BSpec says this must be set, even though
8707 * WaDisable4x2SubspanOptimization isn't listed for VLV.
8708 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02008709 I915_WRITE(CACHE_MODE_1,
8710 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07008711
8712 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02008713 * BSpec recommends 8x4 when MSAA is used,
8714 * however in practice 16x4 seems fastest.
8715 *
8716 * Note that PS/WM thread counts depend on the WIZ hashing
8717 * disable bit, which we don't touch here, but it's good
8718 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
8719 */
8720 I915_WRITE(GEN7_GT_MODE,
8721 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
8722
8723 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02008724 * WaIncreaseL3CreditsForVLVB0:vlv
8725 * This is the hardware default actually.
8726 */
8727 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
8728
8729 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008730 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07008731 * Disable clock gating on th GCFG unit to prevent a delay
8732 * in the reporting of vblank events.
8733 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02008734 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008735}
8736
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008737static void chv_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03008738{
Ville Syrjälä232ce332014-04-09 13:28:35 +03008739 /* WaVSRefCountFullforceMissDisable:chv */
8740 /* WaDSRefCountFullforceMissDisable:chv */
8741 I915_WRITE(GEN7_FF_THREAD_MODE,
8742 I915_READ(GEN7_FF_THREAD_MODE) &
8743 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03008744
8745 /* WaDisableSemaphoreAndSyncFlipWait:chv */
8746 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
8747 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03008748
8749 /* WaDisableCSUnitClockGating:chv */
8750 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
8751 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03008752
8753 /* WaDisableSDEUnitClockGating:chv */
8754 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8755 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008756
8757 /*
Imre Deak450174f2016-05-03 15:54:21 +03008758 * WaProgramL3SqcReg1Default:chv
8759 * See gfxspecs/Related Documents/Performance Guide/
8760 * LSQC Setting Recommendations.
8761 */
8762 gen8_set_l3sqc_credits(dev_priv, 38, 2);
8763
8764 /*
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008765 * GTT cache may not work with big pages, so if those
8766 * are ever enabled GTT cache may need to be disabled.
8767 */
8768 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03008769}
8770
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008771static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008772{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008773 uint32_t dspclk_gate;
8774
8775 I915_WRITE(RENCLK_GATE_D1, 0);
8776 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
8777 GS_UNIT_CLOCK_GATE_DISABLE |
8778 CL_UNIT_CLOCK_GATE_DISABLE);
8779 I915_WRITE(RAMCLK_GATE_D, 0);
8780 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
8781 OVRUNIT_CLOCK_GATE_DISABLE |
8782 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008783 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008784 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
8785 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02008786
8787 /* WaDisableRenderCachePipelinedFlush */
8788 I915_WRITE(CACHE_MODE_0,
8789 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03008790
Akash Goel4e046322014-04-04 17:14:38 +05308791 /* WaDisable_RenderCache_OperationalFlush:g4x */
8792 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8793
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008794 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008795}
8796
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008797static void i965gm_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008798{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008799 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
8800 I915_WRITE(RENCLK_GATE_D2, 0);
8801 I915_WRITE(DSPCLK_GATE_D, 0);
8802 I915_WRITE(RAMCLK_GATE_D, 0);
8803 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03008804 I915_WRITE(MI_ARB_STATE,
8805 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05308806
8807 /* WaDisable_RenderCache_OperationalFlush:gen4 */
8808 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008809}
8810
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008811static void i965g_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008812{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008813 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
8814 I965_RCC_CLOCK_GATE_DISABLE |
8815 I965_RCPB_CLOCK_GATE_DISABLE |
8816 I965_ISC_CLOCK_GATE_DISABLE |
8817 I965_FBC_CLOCK_GATE_DISABLE);
8818 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03008819 I915_WRITE(MI_ARB_STATE,
8820 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05308821
8822 /* WaDisable_RenderCache_OperationalFlush:gen4 */
8823 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008824}
8825
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008826static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008827{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008828 u32 dstate = I915_READ(D_STATE);
8829
8830 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
8831 DSTATE_DOT_CLOCK_GATING;
8832 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01008833
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008834 if (IS_PINEVIEW(dev_priv))
Chris Wilson13a86b82012-04-24 14:51:43 +01008835 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02008836
8837 /* IIR "flip pending" means done if this bit is set */
8838 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02008839
8840 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02008841 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02008842
8843 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
8844 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03008845
8846 I915_WRITE(MI_ARB_STATE,
8847 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008848}
8849
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008850static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008851{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008852 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02008853
8854 /* interrupts should cause a wake up from C3 */
8855 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
8856 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03008857
8858 I915_WRITE(MEM_MODE,
8859 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008860}
8861
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008862static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008863{
Ville Syrjälä10383922014-08-15 01:21:54 +03008864 I915_WRITE(MEM_MODE,
8865 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
8866 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008867}
8868
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008869void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008870{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008871 dev_priv->display.init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008872}
8873
Ville Syrjälä712bf362016-10-31 22:37:23 +02008874void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03008875{
Ville Syrjälä712bf362016-10-31 22:37:23 +02008876 if (HAS_PCH_LPT(dev_priv))
8877 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03008878}
8879
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008880static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02008881{
8882 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
8883}
8884
8885/**
8886 * intel_init_clock_gating_hooks - setup the clock gating hooks
8887 * @dev_priv: device private
8888 *
8889 * Setup the hooks that configure which clocks of a given platform can be
8890 * gated and also apply various GT and display specific workarounds for these
8891 * platforms. Note that some GT specific workarounds are applied separately
8892 * when GPU contexts or batchbuffers start their execution.
8893 */
8894void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
8895{
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008896 if (IS_CANNONLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008897 dev_priv->display.init_clock_gating = cnl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008898 else if (IS_COFFEELAKE(dev_priv))
8899 dev_priv->display.init_clock_gating = cfl_init_clock_gating;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008900 else if (IS_SKYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008901 dev_priv->display.init_clock_gating = skl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008902 else if (IS_KABYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008903 dev_priv->display.init_clock_gating = kbl_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02008904 else if (IS_BROXTON(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02008905 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02008906 else if (IS_GEMINILAKE(dev_priv))
8907 dev_priv->display.init_clock_gating = glk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008908 else if (IS_BROADWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008909 dev_priv->display.init_clock_gating = bdw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008910 else if (IS_CHERRYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008911 dev_priv->display.init_clock_gating = chv_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008912 else if (IS_HASWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008913 dev_priv->display.init_clock_gating = hsw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008914 else if (IS_IVYBRIDGE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008915 dev_priv->display.init_clock_gating = ivb_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008916 else if (IS_VALLEYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008917 dev_priv->display.init_clock_gating = vlv_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008918 else if (IS_GEN6(dev_priv))
8919 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
8920 else if (IS_GEN5(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008921 dev_priv->display.init_clock_gating = ilk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008922 else if (IS_G4X(dev_priv))
8923 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02008924 else if (IS_I965GM(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008925 dev_priv->display.init_clock_gating = i965gm_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02008926 else if (IS_I965G(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008927 dev_priv->display.init_clock_gating = i965g_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008928 else if (IS_GEN3(dev_priv))
8929 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
8930 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
8931 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
8932 else if (IS_GEN2(dev_priv))
8933 dev_priv->display.init_clock_gating = i830_init_clock_gating;
8934 else {
8935 MISSING_CASE(INTEL_DEVID(dev_priv));
8936 dev_priv->display.init_clock_gating = nop_init_clock_gating;
8937 }
8938}
8939
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008940/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008941void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008942{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02008943 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008944
Daniel Vetterc921aba2012-04-26 23:28:17 +02008945 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008946 if (IS_PINEVIEW(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02008947 i915_pineview_get_mem_freq(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008948 else if (IS_GEN5(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02008949 i915_ironlake_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02008950
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008951 /* For FIFO watermark updates */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008952 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008953 skl_setup_wm_latency(dev_priv);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01008954 dev_priv->display.initial_watermarks = skl_initial_wm;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01008955 dev_priv->display.atomic_update_watermarks = skl_atomic_update_crtc_wm;
Matt Roper98d39492016-05-12 07:06:03 -07008956 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008957 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008958 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03008959
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008960 if ((IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008961 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008962 (!IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008963 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07008964 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08008965 dev_priv->display.compute_intermediate_wm =
8966 ilk_compute_intermediate_wm;
8967 dev_priv->display.initial_watermarks =
8968 ilk_initial_watermarks;
8969 dev_priv->display.optimize_watermarks =
8970 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02008971 } else {
8972 DRM_DEBUG_KMS("Failed to read display plane latency. "
8973 "Disable CxSR\n");
8974 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02008975 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008976 vlv_setup_wm_latency(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02008977 dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
Ville Syrjälä4841da52017-03-02 19:14:59 +02008978 dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02008979 dev_priv->display.initial_watermarks = vlv_initial_watermarks;
Ville Syrjälä4841da52017-03-02 19:14:59 +02008980 dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
Ville Syrjäläff32c542017-03-02 19:14:57 +02008981 dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03008982 } else if (IS_G4X(dev_priv)) {
8983 g4x_setup_wm_latency(dev_priv);
8984 dev_priv->display.compute_pipe_wm = g4x_compute_pipe_wm;
8985 dev_priv->display.compute_intermediate_wm = g4x_compute_intermediate_wm;
8986 dev_priv->display.initial_watermarks = g4x_initial_watermarks;
8987 dev_priv->display.optimize_watermarks = g4x_optimize_watermarks;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008988 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008989 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008990 dev_priv->is_ddr3,
8991 dev_priv->fsb_freq,
8992 dev_priv->mem_freq)) {
8993 DRM_INFO("failed to find known CxSR latency "
8994 "(found ddr%s fsb freq %d, mem freq %d), "
8995 "disabling CxSR\n",
8996 (dev_priv->is_ddr3 == 1) ? "3" : "2",
8997 dev_priv->fsb_freq, dev_priv->mem_freq);
8998 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03008999 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009000 dev_priv->display.update_wm = NULL;
9001 } else
9002 dev_priv->display.update_wm = pineview_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01009003 } else if (IS_GEN4(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009004 dev_priv->display.update_wm = i965_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01009005 } else if (IS_GEN3(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009006 dev_priv->display.update_wm = i9xx_update_wm;
9007 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01009008 } else if (IS_GEN2(dev_priv)) {
Ville Syrjälä62d75df2016-10-31 22:37:25 +02009009 if (INTEL_INFO(dev_priv)->num_pipes == 1) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009010 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009011 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009012 } else {
9013 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009014 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009015 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02009016 } else {
9017 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009018 }
9019}
9020
Lyude87660502016-08-17 15:55:53 -04009021static inline int gen6_check_mailbox_status(struct drm_i915_private *dev_priv)
9022{
9023 uint32_t flags =
9024 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
9025
9026 switch (flags) {
9027 case GEN6_PCODE_SUCCESS:
9028 return 0;
9029 case GEN6_PCODE_UNIMPLEMENTED_CMD:
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009030 return -ENODEV;
Lyude87660502016-08-17 15:55:53 -04009031 case GEN6_PCODE_ILLEGAL_CMD:
9032 return -ENXIO;
9033 case GEN6_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Chris Wilson7850d1c2016-08-26 11:59:26 +01009034 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Lyude87660502016-08-17 15:55:53 -04009035 return -EOVERFLOW;
9036 case GEN6_PCODE_TIMEOUT:
9037 return -ETIMEDOUT;
9038 default:
Michal Wajdeczkof0d66152017-03-28 08:45:12 +00009039 MISSING_CASE(flags);
Lyude87660502016-08-17 15:55:53 -04009040 return 0;
9041 }
9042}
9043
9044static inline int gen7_check_mailbox_status(struct drm_i915_private *dev_priv)
9045{
9046 uint32_t flags =
9047 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
9048
9049 switch (flags) {
9050 case GEN6_PCODE_SUCCESS:
9051 return 0;
9052 case GEN6_PCODE_ILLEGAL_CMD:
9053 return -ENXIO;
9054 case GEN7_PCODE_TIMEOUT:
9055 return -ETIMEDOUT;
9056 case GEN7_PCODE_ILLEGAL_DATA:
9057 return -EINVAL;
9058 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
9059 return -EOVERFLOW;
9060 default:
9061 MISSING_CASE(flags);
9062 return 0;
9063 }
9064}
9065
Tom O'Rourke151a49d2014-11-13 18:50:10 -08009066int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07009067{
Lyude87660502016-08-17 15:55:53 -04009068 int status;
9069
Jesse Barnes4fc688c2012-11-02 11:14:01 -07009070 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07009071
Chris Wilson3f5582d2016-06-30 15:32:45 +01009072 /* GEN6_PCODE_* are outside of the forcewake domain, we can
9073 * use te fw I915_READ variants to reduce the amount of work
9074 * required when reading/writing.
9075 */
9076
9077 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009078 DRM_DEBUG_DRIVER("warning: pcode (read from mbox %x) mailbox access failed for %ps\n",
9079 mbox, __builtin_return_address(0));
Ben Widawsky42c05262012-09-26 10:34:00 -07009080 return -EAGAIN;
9081 }
9082
Chris Wilson3f5582d2016-06-30 15:32:45 +01009083 I915_WRITE_FW(GEN6_PCODE_DATA, *val);
9084 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
9085 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07009086
Chris Wilsone09a3032017-04-11 11:13:39 +01009087 if (__intel_wait_for_register_fw(dev_priv,
9088 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
9089 500, 0, NULL)) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009090 DRM_ERROR("timeout waiting for pcode read (from mbox %x) to finish for %ps\n",
9091 mbox, __builtin_return_address(0));
Ben Widawsky42c05262012-09-26 10:34:00 -07009092 return -ETIMEDOUT;
9093 }
9094
Chris Wilson3f5582d2016-06-30 15:32:45 +01009095 *val = I915_READ_FW(GEN6_PCODE_DATA);
9096 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07009097
Lyude87660502016-08-17 15:55:53 -04009098 if (INTEL_GEN(dev_priv) > 6)
9099 status = gen7_check_mailbox_status(dev_priv);
9100 else
9101 status = gen6_check_mailbox_status(dev_priv);
9102
9103 if (status) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009104 DRM_DEBUG_DRIVER("warning: pcode (read from mbox %x) mailbox access failed for %ps: %d\n",
9105 mbox, __builtin_return_address(0), status);
Lyude87660502016-08-17 15:55:53 -04009106 return status;
9107 }
9108
Ben Widawsky42c05262012-09-26 10:34:00 -07009109 return 0;
9110}
9111
Chris Wilson3f5582d2016-06-30 15:32:45 +01009112int sandybridge_pcode_write(struct drm_i915_private *dev_priv,
Lyude87660502016-08-17 15:55:53 -04009113 u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07009114{
Lyude87660502016-08-17 15:55:53 -04009115 int status;
9116
Jesse Barnes4fc688c2012-11-02 11:14:01 -07009117 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07009118
Chris Wilson3f5582d2016-06-30 15:32:45 +01009119 /* GEN6_PCODE_* are outside of the forcewake domain, we can
9120 * use te fw I915_READ variants to reduce the amount of work
9121 * required when reading/writing.
9122 */
9123
9124 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009125 DRM_DEBUG_DRIVER("warning: pcode (write of 0x%08x to mbox %x) mailbox access failed for %ps\n",
9126 val, mbox, __builtin_return_address(0));
Ben Widawsky42c05262012-09-26 10:34:00 -07009127 return -EAGAIN;
9128 }
9129
Chris Wilson3f5582d2016-06-30 15:32:45 +01009130 I915_WRITE_FW(GEN6_PCODE_DATA, val);
Imre Deak8bf41b72016-11-28 17:29:27 +02009131 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
Chris Wilson3f5582d2016-06-30 15:32:45 +01009132 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07009133
Chris Wilsone09a3032017-04-11 11:13:39 +01009134 if (__intel_wait_for_register_fw(dev_priv,
9135 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
9136 500, 0, NULL)) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009137 DRM_ERROR("timeout waiting for pcode write of 0x%08x to mbox %x to finish for %ps\n",
9138 val, mbox, __builtin_return_address(0));
Ben Widawsky42c05262012-09-26 10:34:00 -07009139 return -ETIMEDOUT;
9140 }
9141
Chris Wilson3f5582d2016-06-30 15:32:45 +01009142 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07009143
Lyude87660502016-08-17 15:55:53 -04009144 if (INTEL_GEN(dev_priv) > 6)
9145 status = gen7_check_mailbox_status(dev_priv);
9146 else
9147 status = gen6_check_mailbox_status(dev_priv);
9148
9149 if (status) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009150 DRM_DEBUG_DRIVER("warning: pcode (write of 0x%08x to mbox %x) mailbox access failed for %ps: %d\n",
9151 val, mbox, __builtin_return_address(0), status);
Lyude87660502016-08-17 15:55:53 -04009152 return status;
9153 }
9154
Ben Widawsky42c05262012-09-26 10:34:00 -07009155 return 0;
9156}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07009157
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009158static bool skl_pcode_try_request(struct drm_i915_private *dev_priv, u32 mbox,
9159 u32 request, u32 reply_mask, u32 reply,
9160 u32 *status)
9161{
9162 u32 val = request;
9163
9164 *status = sandybridge_pcode_read(dev_priv, mbox, &val);
9165
9166 return *status || ((val & reply_mask) == reply);
9167}
9168
9169/**
9170 * skl_pcode_request - send PCODE request until acknowledgment
9171 * @dev_priv: device private
9172 * @mbox: PCODE mailbox ID the request is targeted for
9173 * @request: request ID
9174 * @reply_mask: mask used to check for request acknowledgment
9175 * @reply: value used to check for request acknowledgment
9176 * @timeout_base_ms: timeout for polling with preemption enabled
9177 *
9178 * Keep resending the @request to @mbox until PCODE acknowledges it, PCODE
Imre Deak01299362017-02-24 16:32:10 +02009179 * reports an error or an overall timeout of @timeout_base_ms+50 ms expires.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009180 * The request is acknowledged once the PCODE reply dword equals @reply after
9181 * applying @reply_mask. Polling is first attempted with preemption enabled
Imre Deak01299362017-02-24 16:32:10 +02009182 * for @timeout_base_ms and if this times out for another 50 ms with
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009183 * preemption disabled.
9184 *
9185 * Returns 0 on success, %-ETIMEDOUT in case of a timeout, <0 in case of some
9186 * other error as reported by PCODE.
9187 */
9188int skl_pcode_request(struct drm_i915_private *dev_priv, u32 mbox, u32 request,
9189 u32 reply_mask, u32 reply, int timeout_base_ms)
9190{
9191 u32 status;
9192 int ret;
9193
9194 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
9195
9196#define COND skl_pcode_try_request(dev_priv, mbox, request, reply_mask, reply, \
9197 &status)
9198
9199 /*
9200 * Prime the PCODE by doing a request first. Normally it guarantees
9201 * that a subsequent request, at most @timeout_base_ms later, succeeds.
9202 * _wait_for() doesn't guarantee when its passed condition is evaluated
9203 * first, so send the first request explicitly.
9204 */
9205 if (COND) {
9206 ret = 0;
9207 goto out;
9208 }
9209 ret = _wait_for(COND, timeout_base_ms * 1000, 10);
9210 if (!ret)
9211 goto out;
9212
9213 /*
9214 * The above can time out if the number of requests was low (2 in the
9215 * worst case) _and_ PCODE was busy for some reason even after a
9216 * (queued) request and @timeout_base_ms delay. As a workaround retry
9217 * the poll with preemption disabled to maximize the number of
Imre Deak01299362017-02-24 16:32:10 +02009218 * requests. Increase the timeout from @timeout_base_ms to 50ms to
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009219 * account for interrupts that could reduce the number of these
Imre Deak01299362017-02-24 16:32:10 +02009220 * requests, and for any quirks of the PCODE firmware that delays
9221 * the request completion.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009222 */
9223 DRM_DEBUG_KMS("PCODE timeout, retrying with preemption disabled\n");
9224 WARN_ON_ONCE(timeout_base_ms > 3);
9225 preempt_disable();
Imre Deak01299362017-02-24 16:32:10 +02009226 ret = wait_for_atomic(COND, 50);
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009227 preempt_enable();
9228
9229out:
9230 return ret ? ret : status;
9231#undef COND
9232}
9233
Ville Syrjälädd06f882014-11-10 22:55:12 +02009234static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
9235{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009236 /*
9237 * N = val - 0xb7
9238 * Slow = Fast = GPLL ref * N
9239 */
9240 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * (val - 0xb7), 1000);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009241}
9242
Fengguang Wub55dd642014-07-12 11:21:39 +02009243static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009244{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009245 return DIV_ROUND_CLOSEST(1000 * val, dev_priv->rps.gpll_ref_freq) + 0xb7;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009246}
9247
Fengguang Wub55dd642014-07-12 11:21:39 +02009248static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05309249{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009250 /*
9251 * N = val / 2
9252 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
9253 */
9254 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * val, 2 * 2 * 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05309255}
9256
Fengguang Wub55dd642014-07-12 11:21:39 +02009257static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05309258{
Ville Syrjälä1c147622014-08-18 14:42:43 +03009259 /* CHV needs even values */
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009260 return DIV_ROUND_CLOSEST(2 * 1000 * val, dev_priv->rps.gpll_ref_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05309261}
9262
Ville Syrjälä616bc822015-01-23 21:04:25 +02009263int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
9264{
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07009265 if (INTEL_GEN(dev_priv) >= 9)
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009266 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
9267 GEN9_FREQ_SCALER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009268 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009269 return chv_gpu_freq(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009270 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009271 return byt_gpu_freq(dev_priv, val);
9272 else
9273 return val * GT_FREQUENCY_MULTIPLIER;
9274}
9275
Ville Syrjälä616bc822015-01-23 21:04:25 +02009276int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
9277{
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07009278 if (INTEL_GEN(dev_priv) >= 9)
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009279 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
9280 GT_FREQUENCY_MULTIPLIER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009281 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009282 return chv_freq_opcode(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009283 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009284 return byt_freq_opcode(dev_priv, val);
9285 else
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009286 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
Deepak S22b1b2f2014-07-12 14:54:33 +05309287}
9288
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00009289void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01009290{
Daniel Vetterf742a552013-12-06 10:17:53 +01009291 mutex_init(&dev_priv->rps.hw_lock);
9292
Chris Wilson54b4f682016-07-21 21:16:19 +01009293 INIT_DELAYED_WORK(&dev_priv->rps.autoenable_work,
9294 __intel_autoenable_gt_powersave);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01009295 atomic_set(&dev_priv->rps.num_waiters, 0);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03009296
Paulo Zanoni33688d92014-03-07 20:08:19 -03009297 dev_priv->pm.suspended = false;
Imre Deak1f814da2015-12-16 02:52:19 +02009298 atomic_set(&dev_priv->pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01009299}
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009300
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009301static u64 vlv_residency_raw(struct drm_i915_private *dev_priv,
9302 const i915_reg_t reg)
9303{
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009304 u32 lower, upper, tmp;
Chris Wilson71cc2b12017-03-24 16:54:18 +00009305 int loop = 2;
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009306
9307 /* The register accessed do not need forcewake. We borrow
9308 * uncore lock to prevent concurrent access to range reg.
9309 */
9310 spin_lock_irq(&dev_priv->uncore.lock);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009311
9312 /* vlv and chv residency counters are 40 bits in width.
9313 * With a control bit, we can choose between upper or lower
9314 * 32bit window into this counter.
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009315 *
9316 * Although we always use the counter in high-range mode elsewhere,
9317 * userspace may attempt to read the value before rc6 is initialised,
9318 * before we have set the default VLV_COUNTER_CONTROL value. So always
9319 * set the high bit to be safe.
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009320 */
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009321 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9322 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009323 upper = I915_READ_FW(reg);
9324 do {
9325 tmp = upper;
9326
9327 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9328 _MASKED_BIT_DISABLE(VLV_COUNT_RANGE_HIGH));
9329 lower = I915_READ_FW(reg);
9330
9331 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9332 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
9333 upper = I915_READ_FW(reg);
Chris Wilson71cc2b12017-03-24 16:54:18 +00009334 } while (upper != tmp && --loop);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009335
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009336 /* Everywhere else we always use VLV_COUNTER_CONTROL with the
9337 * VLV_COUNT_RANGE_HIGH bit set - so it is safe to leave it set
9338 * now.
9339 */
9340
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009341 spin_unlock_irq(&dev_priv->uncore.lock);
9342
9343 return lower | (u64)upper << 8;
9344}
9345
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009346u64 intel_rc6_residency_us(struct drm_i915_private *dev_priv,
9347 const i915_reg_t reg)
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009348{
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009349 u64 time_hw, units, div;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009350
9351 if (!intel_enable_rc6())
9352 return 0;
9353
9354 intel_runtime_pm_get(dev_priv);
9355
9356 /* On VLV and CHV, residency time is in CZ units rather than 1.28us */
9357 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009358 units = 1000;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009359 div = dev_priv->czclk_freq;
9360
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009361 time_hw = vlv_residency_raw(dev_priv, reg);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009362 } else if (IS_GEN9_LP(dev_priv)) {
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009363 units = 1000;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009364 div = 1200; /* 833.33ns */
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009365
9366 time_hw = I915_READ(reg);
9367 } else {
9368 units = 128000; /* 1.28us */
9369 div = 100000;
9370
9371 time_hw = I915_READ(reg);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009372 }
9373
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009374 intel_runtime_pm_put(dev_priv);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009375 return DIV_ROUND_UP_ULL(time_hw * units, div);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009376}