blob: 0201816a422978d0e79086a7c916f5634953136b [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 Deakd965e7a2015-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ä24304d82017-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ä7b510452017-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ä7b510452017-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ä7b510452017-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ä7b510452017-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ä7b510452017-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ä7b510452017-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ä7b510452017-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ä24304d82017-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ä24304d82017-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ä24304d82017-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ä24304d82017-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äfacd6192013-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äfacd6192013-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äfacd6192013-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
Matt Roper55994c22016-05-12 07:06:08 -07004379static int skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
4380 struct intel_crtc_state *cstate,
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304381 const struct intel_plane_state *intel_pstate,
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004382 uint16_t ddb_allocation,
Matt Roper55994c22016-05-12 07:06:08 -07004383 int level,
4384 uint16_t *out_blocks, /* out */
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004385 uint8_t *out_lines, /* out */
4386 bool *enabled /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004387{
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004388 struct intel_plane *plane = to_intel_plane(intel_pstate->base.plane);
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304389 const struct drm_plane_state *pstate = &intel_pstate->base;
4390 const struct drm_framebuffer *fb = pstate->fb;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004391 uint32_t latency = dev_priv->wm.skl_latency[level];
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304392 uint_fixed_16_16_t method1, method2;
4393 uint_fixed_16_16_t plane_blocks_per_line;
4394 uint_fixed_16_16_t selected_result;
4395 uint32_t interm_pbpl;
4396 uint32_t plane_bytes_per_line;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004397 uint32_t res_blocks, res_lines;
Ville Syrjäläac484962016-01-20 21:05:26 +02004398 uint8_t cpp;
Kumar, Mahesh129eaa92017-07-05 20:01:48 +05304399 uint32_t width = 0;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004400 uint32_t plane_pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304401 uint_fixed_16_16_t y_tile_minimum;
4402 uint32_t y_min_scanlines;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03004403 struct intel_atomic_state *state =
4404 to_intel_atomic_state(cstate->base.state);
4405 bool apply_memory_bw_wa = skl_needs_memory_bw_wa(state);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304406 bool y_tiled, x_tiled;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004407
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004408 if (latency == 0 ||
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004409 !intel_wm_plane_visible(cstate, intel_pstate)) {
4410 *enabled = false;
Matt Roper55994c22016-05-12 07:06:08 -07004411 return 0;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004412 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004413
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304414 y_tiled = fb->modifier == I915_FORMAT_MOD_Y_TILED ||
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07004415 fb->modifier == I915_FORMAT_MOD_Yf_TILED ||
4416 fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
4417 fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304418 x_tiled = fb->modifier == I915_FORMAT_MOD_X_TILED;
4419
Rodrigo Vivi82525c12017-06-08 08:50:00 -07004420 /* Display WA #1141: kbl,cfl */
4421 if ((IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv)) &&
4422 dev_priv->ipc_enabled)
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05304423 latency += 4;
4424
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304425 if (apply_memory_bw_wa && x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03004426 latency += 15;
4427
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004428 if (plane->id == PLANE_CURSOR) {
4429 width = intel_pstate->base.crtc_w;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004430 } else {
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004431 /*
4432 * Src coordinates are already rotated by 270 degrees for
4433 * the 90/270 degree plane rotation cases (to match the
4434 * GTT mapping), hence no need to account for rotation here.
4435 */
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004436 width = drm_rect_width(&intel_pstate->base.src) >> 16;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004437 }
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004438
Kumar, Maheshb064be02017-07-05 20:01:49 +05304439 cpp = (fb->format->format == DRM_FORMAT_NV12) ? fb->format->cpp[1] :
4440 fb->format->cpp[0];
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004441 plane_pixel_rate = skl_adjusted_plane_pixel_rate(cstate, intel_pstate);
4442
Dave Airlie61d0a042016-10-25 16:35:20 +10004443 if (drm_rotation_90_or_270(pstate->rotation)) {
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004444
4445 switch (cpp) {
4446 case 1:
4447 y_min_scanlines = 16;
4448 break;
4449 case 2:
4450 y_min_scanlines = 8;
4451 break;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004452 case 4:
4453 y_min_scanlines = 4;
4454 break;
Paulo Zanoni86a462b2016-09-22 18:00:35 -03004455 default:
4456 MISSING_CASE(cpp);
4457 return -EINVAL;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004458 }
4459 } else {
4460 y_min_scanlines = 4;
4461 }
4462
Paulo Zanoni2ef32de2016-11-08 18:22:11 -02004463 if (apply_memory_bw_wa)
4464 y_min_scanlines *= 2;
4465
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03004466 plane_bytes_per_line = width * cpp;
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304467 if (y_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304468 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line *
4469 y_min_scanlines, 512);
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07004470
4471 if (INTEL_GEN(dev_priv) >= 10)
4472 interm_pbpl++;
4473
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304474 plane_blocks_per_line = div_fixed16(interm_pbpl,
Kumar, Maheshafbc95c2017-05-17 17:28:20 +05304475 y_min_scanlines);
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07004476 } else if (x_tiled && INTEL_GEN(dev_priv) == 9) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304477 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line, 512);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304478 plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304479 } else {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304480 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line, 512) + 1;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304481 plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03004482 }
4483
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07004484 method1 = skl_wm_method1(dev_priv, plane_pixel_rate, cpp, latency);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004485 method2 = skl_wm_method2(plane_pixel_rate,
Matt Roper024c9042015-09-24 15:53:11 -07004486 cstate->base.adjusted_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004487 latency,
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03004488 plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004489
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304490 y_tile_minimum = mul_u32_fixed16(y_min_scanlines,
4491 plane_blocks_per_line);
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004492
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304493 if (y_tiled) {
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304494 selected_result = max_fixed16(method2, y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004495 } else {
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304496 uint32_t linetime_us;
4497
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304498 linetime_us = fixed16_to_u32_round_up(
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304499 intel_get_linetime_us(cstate));
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03004500 if ((cpp * cstate->base.adjusted_mode.crtc_htotal / 512 < 1) &&
4501 (plane_bytes_per_line / 512 < 1))
4502 selected_result = method2;
Maarten Lankhorst54d20ed2017-07-17 14:02:30 +02004503 else if (ddb_allocation >=
4504 fixed16_to_u32_round_up(plane_blocks_per_line))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304505 selected_result = min_fixed16(method1, method2);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304506 else if (latency >= linetime_us)
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304507 selected_result = min_fixed16(method1, method2);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004508 else
4509 selected_result = method1;
4510 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004511
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304512 res_blocks = fixed16_to_u32_round_up(selected_result) + 1;
Kumar, Maheshd273ecc2017-05-17 17:28:22 +05304513 res_lines = div_round_up_fixed16(selected_result,
4514 plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00004515
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07004516 /* Display WA #1125: skl,bxt,kbl,glk */
4517 if (level == 0 &&
4518 (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
4519 fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS))
4520 res_blocks += fixed16_to_u32_round_up(y_tile_minimum);
4521
4522 /* Display WA #1126: skl,bxt,kbl,glk */
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004523 if (level >= 1 && level <= 7) {
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05304524 if (y_tiled) {
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304525 res_blocks += fixed16_to_u32_round_up(y_tile_minimum);
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004526 res_lines += y_min_scanlines;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004527 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004528 res_blocks++;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004529 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004530 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004531
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004532 if (res_blocks >= ddb_allocation || res_lines > 31) {
4533 *enabled = false;
Matt Roper6b6bada2016-05-12 07:06:10 -07004534
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004535 /*
4536 * If there are no valid level 0 watermarks, then we can't
4537 * support this display configuration.
4538 */
4539 if (level) {
4540 return 0;
4541 } else {
4542 struct drm_plane *plane = pstate->plane;
4543
4544 DRM_DEBUG_KMS("Requested display configuration exceeds system watermark limitations\n");
4545 DRM_DEBUG_KMS("[PLANE:%d:%s] blocks required = %u/%u, lines required = %u/31\n",
4546 plane->base.id, plane->name,
4547 res_blocks, ddb_allocation, res_lines);
4548 return -EINVAL;
4549 }
Matt Roper55994c22016-05-12 07:06:08 -07004550 }
Damien Lespiaue6d66172014-11-04 17:06:55 +00004551
4552 *out_blocks = res_blocks;
4553 *out_lines = res_lines;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004554 *enabled = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004555
Matt Roper55994c22016-05-12 07:06:08 -07004556 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004557}
4558
Matt Roperf4a96752016-05-12 07:06:06 -07004559static int
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304560skl_compute_wm_levels(const struct drm_i915_private *dev_priv,
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004561 struct skl_ddb_allocation *ddb,
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304562 struct intel_crtc_state *cstate,
4563 const struct intel_plane_state *intel_pstate,
4564 struct skl_plane_wm *wm)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004565{
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004566 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
4567 struct drm_plane *plane = intel_pstate->base.plane;
4568 struct intel_plane *intel_plane = to_intel_plane(plane);
4569 uint16_t ddb_blocks;
4570 enum pipe pipe = intel_crtc->pipe;
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304571 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Roper55994c22016-05-12 07:06:08 -07004572 int ret;
Lyudea62163e2016-10-04 14:28:20 -04004573
Kumar, Mahesh7b751192017-05-17 17:28:24 +05304574 if (WARN_ON(!intel_pstate->base.fb))
4575 return -EINVAL;
Matt Roper024c9042015-09-24 15:53:11 -07004576
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004577 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][intel_plane->id]);
4578
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304579 for (level = 0; level <= max_level; level++) {
4580 struct skl_wm_level *result = &wm->wm[level];
4581
4582 ret = skl_compute_plane_wm(dev_priv,
4583 cstate,
4584 intel_pstate,
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004585 ddb_blocks,
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304586 level,
4587 &result->plane_res_b,
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004588 &result->plane_res_l,
4589 &result->plane_en);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304590 if (ret)
4591 return ret;
4592 }
Matt Roperf4a96752016-05-12 07:06:06 -07004593
4594 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004595}
4596
Damien Lespiau407b50f2014-11-04 17:06:57 +00004597static uint32_t
Matt Roper024c9042015-09-24 15:53:11 -07004598skl_compute_linetime_wm(struct intel_crtc_state *cstate)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004599{
Mahesh Kumara3a89862016-12-01 21:19:34 +05304600 struct drm_atomic_state *state = cstate->base.state;
4601 struct drm_i915_private *dev_priv = to_i915(state->dev);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304602 uint_fixed_16_16_t linetime_us;
Mahesh Kumara3a89862016-12-01 21:19:34 +05304603 uint32_t linetime_wm;
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03004604
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304605 linetime_us = intel_get_linetime_us(cstate);
4606
4607 if (is_fixed16_zero(linetime_us))
Damien Lespiau407b50f2014-11-04 17:06:57 +00004608 return 0;
4609
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304610 linetime_wm = fixed16_to_u32_round_up(mul_u32_fixed16(8, linetime_us));
Mahesh Kumara3a89862016-12-01 21:19:34 +05304611
4612 /* Display WA #1135: bxt. */
4613 if (IS_BROXTON(dev_priv) && dev_priv->ipc_enabled)
4614 linetime_wm = DIV_ROUND_UP(linetime_wm, 2);
4615
4616 return linetime_wm;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004617}
4618
Matt Roper024c9042015-09-24 15:53:11 -07004619static void skl_compute_transition_wm(struct intel_crtc_state *cstate,
Damien Lespiau9414f562014-11-04 17:06:58 +00004620 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004621{
Matt Roper024c9042015-09-24 15:53:11 -07004622 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004623 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00004624
4625 /* Until we know more, just disable transition WMs */
Lyudea62163e2016-10-04 14:28:20 -04004626 trans_wm->plane_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004627}
4628
Matt Roper55994c22016-05-12 07:06:08 -07004629static int skl_build_pipe_wm(struct intel_crtc_state *cstate,
4630 struct skl_ddb_allocation *ddb,
4631 struct skl_pipe_wm *pipe_wm)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004632{
Matt Roper024c9042015-09-24 15:53:11 -07004633 struct drm_device *dev = cstate->base.crtc->dev;
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304634 struct drm_crtc_state *crtc_state = &cstate->base;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004635 const struct drm_i915_private *dev_priv = to_i915(dev);
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304636 struct drm_plane *plane;
4637 const struct drm_plane_state *pstate;
Lyudea62163e2016-10-04 14:28:20 -04004638 struct skl_plane_wm *wm;
Matt Roper55994c22016-05-12 07:06:08 -07004639 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004640
Lyudea62163e2016-10-04 14:28:20 -04004641 /*
4642 * We'll only calculate watermarks for planes that are actually
4643 * enabled, so make sure all other planes are set as disabled.
4644 */
4645 memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
4646
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304647 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, crtc_state) {
4648 const struct intel_plane_state *intel_pstate =
4649 to_intel_plane_state(pstate);
4650 enum plane_id plane_id = to_intel_plane(plane)->id;
4651
4652 wm = &pipe_wm->planes[plane_id];
Lyudea62163e2016-10-04 14:28:20 -04004653
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004654 ret = skl_compute_wm_levels(dev_priv, ddb, cstate,
4655 intel_pstate, wm);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304656 if (ret)
4657 return ret;
Lyudea62163e2016-10-04 14:28:20 -04004658 skl_compute_transition_wm(cstate, &wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004659 }
Matt Roper024c9042015-09-24 15:53:11 -07004660 pipe_wm->linetime = skl_compute_linetime_wm(cstate);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004661
Matt Roper55994c22016-05-12 07:06:08 -07004662 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004663}
4664
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004665static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
4666 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00004667 const struct skl_ddb_entry *entry)
4668{
4669 if (entry->end)
4670 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
4671 else
4672 I915_WRITE(reg, 0);
4673}
4674
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004675static void skl_write_wm_level(struct drm_i915_private *dev_priv,
4676 i915_reg_t reg,
4677 const struct skl_wm_level *level)
4678{
4679 uint32_t val = 0;
4680
4681 if (level->plane_en) {
4682 val |= PLANE_WM_EN;
4683 val |= level->plane_res_b;
4684 val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
4685 }
4686
4687 I915_WRITE(reg, val);
4688}
4689
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004690static void skl_write_plane_wm(struct intel_crtc *intel_crtc,
4691 const struct skl_plane_wm *wm,
4692 const struct skl_ddb_allocation *ddb,
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004693 enum plane_id plane_id)
Lyude62e0fb82016-08-22 12:50:08 -04004694{
4695 struct drm_crtc *crtc = &intel_crtc->base;
4696 struct drm_device *dev = crtc->dev;
4697 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004698 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004699 enum pipe pipe = intel_crtc->pipe;
4700
4701 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004702 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004703 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004704 }
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004705 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004706 &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004707
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004708 skl_ddb_entry_write(dev_priv, PLANE_BUF_CFG(pipe, plane_id),
4709 &ddb->plane[pipe][plane_id]);
4710 skl_ddb_entry_write(dev_priv, PLANE_NV12_BUF_CFG(pipe, plane_id),
4711 &ddb->y_plane[pipe][plane_id]);
Lyude62e0fb82016-08-22 12:50:08 -04004712}
4713
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004714static void skl_write_cursor_wm(struct intel_crtc *intel_crtc,
4715 const struct skl_plane_wm *wm,
4716 const struct skl_ddb_allocation *ddb)
Lyude62e0fb82016-08-22 12:50:08 -04004717{
4718 struct drm_crtc *crtc = &intel_crtc->base;
4719 struct drm_device *dev = crtc->dev;
4720 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004721 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004722 enum pipe pipe = intel_crtc->pipe;
4723
4724 for (level = 0; level <= max_level; level++) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004725 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
4726 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004727 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004728 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004729
4730 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004731 &ddb->plane[pipe][PLANE_CURSOR]);
Lyude62e0fb82016-08-22 12:50:08 -04004732}
4733
cpaul@redhat.com45ece232016-10-14 17:31:56 -04004734bool skl_wm_level_equals(const struct skl_wm_level *l1,
4735 const struct skl_wm_level *l2)
4736{
4737 if (l1->plane_en != l2->plane_en)
4738 return false;
4739
4740 /* If both planes aren't enabled, the rest shouldn't matter */
4741 if (!l1->plane_en)
4742 return true;
4743
4744 return (l1->plane_res_l == l2->plane_res_l &&
4745 l1->plane_res_b == l2->plane_res_b);
4746}
4747
Lyude27082492016-08-24 07:48:10 +02004748static inline bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
4749 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004750{
Lyude27082492016-08-24 07:48:10 +02004751 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004752}
4753
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01004754bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry **entries,
4755 const struct skl_ddb_entry *ddb,
4756 int ignore)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004757{
Lyudece0ba282016-09-15 10:46:35 -04004758 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004759
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01004760 for (i = 0; i < I915_MAX_PIPES; i++)
4761 if (i != ignore && entries[i] &&
4762 skl_ddb_entries_overlap(ddb, entries[i]))
Lyude27082492016-08-24 07:48:10 +02004763 return true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004764
Lyude27082492016-08-24 07:48:10 +02004765 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004766}
4767
Matt Roper55994c22016-05-12 07:06:08 -07004768static int skl_update_pipe_wm(struct drm_crtc_state *cstate,
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004769 const struct skl_pipe_wm *old_pipe_wm,
Matt Roper55994c22016-05-12 07:06:08 -07004770 struct skl_pipe_wm *pipe_wm, /* out */
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004771 struct skl_ddb_allocation *ddb, /* out */
Matt Roper55994c22016-05-12 07:06:08 -07004772 bool *changed /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004773{
Matt Roperf4a96752016-05-12 07:06:06 -07004774 struct intel_crtc_state *intel_cstate = to_intel_crtc_state(cstate);
Matt Roper55994c22016-05-12 07:06:08 -07004775 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004776
Matt Roper55994c22016-05-12 07:06:08 -07004777 ret = skl_build_pipe_wm(intel_cstate, ddb, pipe_wm);
4778 if (ret)
4779 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004780
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004781 if (!memcmp(old_pipe_wm, pipe_wm, sizeof(*pipe_wm)))
Matt Roper55994c22016-05-12 07:06:08 -07004782 *changed = false;
4783 else
4784 *changed = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004785
Matt Roper55994c22016-05-12 07:06:08 -07004786 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004787}
4788
Matt Roper9b613022016-06-27 16:42:44 -07004789static uint32_t
4790pipes_modified(struct drm_atomic_state *state)
4791{
4792 struct drm_crtc *crtc;
4793 struct drm_crtc_state *cstate;
4794 uint32_t i, ret = 0;
4795
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004796 for_each_new_crtc_in_state(state, crtc, cstate, i)
Matt Roper9b613022016-06-27 16:42:44 -07004797 ret |= drm_crtc_mask(crtc);
4798
4799 return ret;
4800}
4801
Jani Nikulabb7791b2016-10-04 12:29:17 +03004802static int
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004803skl_ddb_add_affected_planes(struct intel_crtc_state *cstate)
4804{
4805 struct drm_atomic_state *state = cstate->base.state;
4806 struct drm_device *dev = state->dev;
4807 struct drm_crtc *crtc = cstate->base.crtc;
4808 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4809 struct drm_i915_private *dev_priv = to_i915(dev);
4810 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4811 struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
4812 struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
4813 struct drm_plane_state *plane_state;
4814 struct drm_plane *plane;
4815 enum pipe pipe = intel_crtc->pipe;
4816
4817 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
4818
4819 drm_for_each_plane_mask(plane, dev, cstate->base.plane_mask) {
4820 enum plane_id plane_id = to_intel_plane(plane)->id;
4821
4822 if (skl_ddb_entry_equal(&cur_ddb->plane[pipe][plane_id],
4823 &new_ddb->plane[pipe][plane_id]) &&
4824 skl_ddb_entry_equal(&cur_ddb->y_plane[pipe][plane_id],
4825 &new_ddb->y_plane[pipe][plane_id]))
4826 continue;
4827
4828 plane_state = drm_atomic_get_plane_state(state, plane);
4829 if (IS_ERR(plane_state))
4830 return PTR_ERR(plane_state);
4831 }
4832
4833 return 0;
4834}
4835
4836static int
4837skl_compute_ddb(struct drm_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07004838{
4839 struct drm_device *dev = state->dev;
4840 struct drm_i915_private *dev_priv = to_i915(dev);
4841 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4842 struct intel_crtc *intel_crtc;
Matt Roper734fa012016-05-12 15:11:40 -07004843 struct skl_ddb_allocation *ddb = &intel_state->wm_results.ddb;
Matt Roper9b613022016-06-27 16:42:44 -07004844 uint32_t realloc_pipes = pipes_modified(state);
Matt Roper98d39492016-05-12 07:06:03 -07004845 int ret;
4846
4847 /*
4848 * If this is our first atomic update following hardware readout,
4849 * we can't trust the DDB that the BIOS programmed for us. Let's
4850 * pretend that all pipes switched active status so that we'll
4851 * ensure a full DDB recompute.
4852 */
Matt Roper1b54a882016-06-17 13:42:18 -07004853 if (dev_priv->wm.distrust_bios_wm) {
4854 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
4855 state->acquire_ctx);
4856 if (ret)
4857 return ret;
4858
Matt Roper98d39492016-05-12 07:06:03 -07004859 intel_state->active_pipe_changes = ~0;
4860
Matt Roper1b54a882016-06-17 13:42:18 -07004861 /*
4862 * We usually only initialize intel_state->active_crtcs if we
4863 * we're doing a modeset; make sure this field is always
4864 * initialized during the sanitization process that happens
4865 * on the first commit too.
4866 */
4867 if (!intel_state->modeset)
4868 intel_state->active_crtcs = dev_priv->active_crtcs;
4869 }
4870
Matt Roper98d39492016-05-12 07:06:03 -07004871 /*
4872 * If the modeset changes which CRTC's are active, we need to
4873 * recompute the DDB allocation for *all* active pipes, even
4874 * those that weren't otherwise being modified in any way by this
4875 * atomic commit. Due to the shrinking of the per-pipe allocations
4876 * when new active CRTC's are added, it's possible for a pipe that
4877 * we were already using and aren't changing at all here to suddenly
4878 * become invalid if its DDB needs exceeds its new allocation.
4879 *
4880 * Note that if we wind up doing a full DDB recompute, we can't let
4881 * any other display updates race with this transaction, so we need
4882 * to grab the lock on *all* CRTC's.
4883 */
Matt Roper734fa012016-05-12 15:11:40 -07004884 if (intel_state->active_pipe_changes) {
Matt Roper98d39492016-05-12 07:06:03 -07004885 realloc_pipes = ~0;
Matt Roper734fa012016-05-12 15:11:40 -07004886 intel_state->wm_results.dirty_pipes = ~0;
4887 }
Matt Roper98d39492016-05-12 07:06:03 -07004888
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004889 /*
4890 * We're not recomputing for the pipes not included in the commit, so
4891 * make sure we start with the current state.
4892 */
4893 memcpy(ddb, &dev_priv->wm.skl_hw.ddb, sizeof(*ddb));
4894
Matt Roper98d39492016-05-12 07:06:03 -07004895 for_each_intel_crtc_mask(dev, intel_crtc, realloc_pipes) {
4896 struct intel_crtc_state *cstate;
4897
4898 cstate = intel_atomic_get_crtc_state(state, intel_crtc);
4899 if (IS_ERR(cstate))
4900 return PTR_ERR(cstate);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004901
4902 ret = skl_allocate_pipe_ddb(cstate, ddb);
4903 if (ret)
4904 return ret;
4905
4906 ret = skl_ddb_add_affected_planes(cstate);
4907 if (ret)
4908 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07004909 }
4910
4911 return 0;
4912}
4913
Matt Roper2722efb2016-08-17 15:55:55 -04004914static void
4915skl_copy_wm_for_pipe(struct skl_wm_values *dst,
4916 struct skl_wm_values *src,
4917 enum pipe pipe)
4918{
Matt Roper2722efb2016-08-17 15:55:55 -04004919 memcpy(dst->ddb.y_plane[pipe], src->ddb.y_plane[pipe],
4920 sizeof(dst->ddb.y_plane[pipe]));
4921 memcpy(dst->ddb.plane[pipe], src->ddb.plane[pipe],
4922 sizeof(dst->ddb.plane[pipe]));
4923}
4924
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004925static void
4926skl_print_wm_changes(const struct drm_atomic_state *state)
4927{
4928 const struct drm_device *dev = state->dev;
4929 const struct drm_i915_private *dev_priv = to_i915(dev);
4930 const struct intel_atomic_state *intel_state =
4931 to_intel_atomic_state(state);
4932 const struct drm_crtc *crtc;
4933 const struct drm_crtc_state *cstate;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004934 const struct intel_plane *intel_plane;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004935 const struct skl_ddb_allocation *old_ddb = &dev_priv->wm.skl_hw.ddb;
4936 const struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
Maarten Lankhorst75704982016-11-01 12:04:10 +01004937 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004938
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004939 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst75704982016-11-01 12:04:10 +01004940 const struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4941 enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004942
Maarten Lankhorst75704982016-11-01 12:04:10 +01004943 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004944 enum plane_id plane_id = intel_plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004945 const struct skl_ddb_entry *old, *new;
4946
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004947 old = &old_ddb->plane[pipe][plane_id];
4948 new = &new_ddb->plane[pipe][plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004949
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004950 if (skl_ddb_entry_equal(old, new))
4951 continue;
4952
Maarten Lankhorst75704982016-11-01 12:04:10 +01004953 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] ddb (%d - %d) -> (%d - %d)\n",
4954 intel_plane->base.base.id,
4955 intel_plane->base.name,
4956 old->start, old->end,
4957 new->start, new->end);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004958 }
4959 }
4960}
4961
Matt Roper98d39492016-05-12 07:06:03 -07004962static int
4963skl_compute_wm(struct drm_atomic_state *state)
4964{
4965 struct drm_crtc *crtc;
4966 struct drm_crtc_state *cstate;
Matt Roper734fa012016-05-12 15:11:40 -07004967 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4968 struct skl_wm_values *results = &intel_state->wm_results;
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02004969 struct drm_device *dev = state->dev;
Matt Roper734fa012016-05-12 15:11:40 -07004970 struct skl_pipe_wm *pipe_wm;
Matt Roper98d39492016-05-12 07:06:03 -07004971 bool changed = false;
Matt Roper734fa012016-05-12 15:11:40 -07004972 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07004973
4974 /*
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02004975 * When we distrust bios wm we always need to recompute to set the
4976 * expected DDB allocations for each CRTC.
4977 */
4978 if (to_i915(dev)->wm.distrust_bios_wm)
4979 changed = true;
4980
4981 /*
Matt Roper98d39492016-05-12 07:06:03 -07004982 * If this transaction isn't actually touching any CRTC's, don't
4983 * bother with watermark calculation. Note that if we pass this
4984 * test, we're guaranteed to hold at least one CRTC state mutex,
4985 * which means we can safely use values like dev_priv->active_crtcs
4986 * since any racing commits that want to update them would need to
4987 * hold _all_ CRTC state mutexes.
4988 */
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004989 for_each_new_crtc_in_state(state, crtc, cstate, i)
Matt Roper98d39492016-05-12 07:06:03 -07004990 changed = true;
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02004991
Matt Roper98d39492016-05-12 07:06:03 -07004992 if (!changed)
4993 return 0;
4994
Matt Roper734fa012016-05-12 15:11:40 -07004995 /* Clear all dirty flags */
4996 results->dirty_pipes = 0;
4997
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004998 ret = skl_compute_ddb(state);
Matt Roper98d39492016-05-12 07:06:03 -07004999 if (ret)
5000 return ret;
5001
Matt Roper734fa012016-05-12 15:11:40 -07005002 /*
5003 * Calculate WM's for all pipes that are part of this transaction.
5004 * Note that the DDB allocation above may have added more CRTC's that
5005 * weren't otherwise being modified (and set bits in dirty_pipes) if
5006 * pipe allocations had to change.
5007 *
5008 * FIXME: Now that we're doing this in the atomic check phase, we
5009 * should allow skl_update_pipe_wm() to return failure in cases where
5010 * no suitable watermark values can be found.
5011 */
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01005012 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Matt Roper734fa012016-05-12 15:11:40 -07005013 struct intel_crtc_state *intel_cstate =
5014 to_intel_crtc_state(cstate);
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02005015 const struct skl_pipe_wm *old_pipe_wm =
5016 &to_intel_crtc_state(crtc->state)->wm.skl.optimal;
Matt Roper734fa012016-05-12 15:11:40 -07005017
5018 pipe_wm = &intel_cstate->wm.skl.optimal;
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02005019 ret = skl_update_pipe_wm(cstate, old_pipe_wm, pipe_wm,
5020 &results->ddb, &changed);
Matt Roper734fa012016-05-12 15:11:40 -07005021 if (ret)
5022 return ret;
5023
5024 if (changed)
5025 results->dirty_pipes |= drm_crtc_mask(crtc);
5026
5027 if ((results->dirty_pipes & drm_crtc_mask(crtc)) == 0)
5028 /* This pipe's WM's did not change */
5029 continue;
5030
5031 intel_cstate->update_wm_pre = true;
Matt Roper734fa012016-05-12 15:11:40 -07005032 }
5033
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005034 skl_print_wm_changes(state);
5035
Matt Roper98d39492016-05-12 07:06:03 -07005036 return 0;
5037}
5038
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005039static void skl_atomic_update_crtc_wm(struct intel_atomic_state *state,
5040 struct intel_crtc_state *cstate)
5041{
5042 struct intel_crtc *crtc = to_intel_crtc(cstate->base.crtc);
5043 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5044 struct skl_pipe_wm *pipe_wm = &cstate->wm.skl.optimal;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005045 const struct skl_ddb_allocation *ddb = &state->wm_results.ddb;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005046 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005047 enum plane_id plane_id;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005048
5049 if (!(state->wm_results.dirty_pipes & drm_crtc_mask(&crtc->base)))
5050 return;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005051
5052 I915_WRITE(PIPE_WM_LINETIME(pipe), pipe_wm->linetime);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005053
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005054 for_each_plane_id_on_crtc(crtc, plane_id) {
5055 if (plane_id != PLANE_CURSOR)
5056 skl_write_plane_wm(crtc, &pipe_wm->planes[plane_id],
5057 ddb, plane_id);
5058 else
5059 skl_write_cursor_wm(crtc, &pipe_wm->planes[plane_id],
5060 ddb);
5061 }
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005062}
5063
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005064static void skl_initial_wm(struct intel_atomic_state *state,
5065 struct intel_crtc_state *cstate)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005066{
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005067 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02005068 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005069 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005070 struct skl_wm_values *results = &state->wm_results;
Matt Roper2722efb2016-08-17 15:55:55 -04005071 struct skl_wm_values *hw_vals = &dev_priv->wm.skl_hw;
Lyude27082492016-08-24 07:48:10 +02005072 enum pipe pipe = intel_crtc->pipe;
Bob Paauweadda50b2015-07-21 10:42:53 -07005073
Ville Syrjälä432081b2016-10-31 22:37:03 +02005074 if ((results->dirty_pipes & drm_crtc_mask(&intel_crtc->base)) == 0)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005075 return;
5076
Matt Roper734fa012016-05-12 15:11:40 -07005077 mutex_lock(&dev_priv->wm.wm_mutex);
5078
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005079 if (cstate->base.active_changed)
5080 skl_atomic_update_crtc_wm(state, cstate);
Lyude27082492016-08-24 07:48:10 +02005081
5082 skl_copy_wm_for_pipe(hw_vals, results, pipe);
Matt Roper734fa012016-05-12 15:11:40 -07005083
5084 mutex_unlock(&dev_priv->wm.wm_mutex);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005085}
5086
Ville Syrjäläd8905652016-01-14 14:53:35 +02005087static void ilk_compute_wm_config(struct drm_device *dev,
5088 struct intel_wm_config *config)
5089{
5090 struct intel_crtc *crtc;
5091
5092 /* Compute the currently _active_ config */
5093 for_each_intel_crtc(dev, crtc) {
5094 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
5095
5096 if (!wm->pipe_enabled)
5097 continue;
5098
5099 config->sprites_enabled |= wm->sprites_enabled;
5100 config->sprites_scaled |= wm->sprites_scaled;
5101 config->num_pipes_active++;
5102 }
5103}
5104
Matt Ropered4a6a72016-02-23 17:20:13 -08005105static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03005106{
Chris Wilson91c8a322016-07-05 10:40:23 +01005107 struct drm_device *dev = &dev_priv->drm;
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005108 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02005109 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02005110 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02005111 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005112 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07005113
Ville Syrjäläd8905652016-01-14 14:53:35 +02005114 ilk_compute_wm_config(dev, &config);
5115
5116 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
5117 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03005118
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005119 /* 5/6 split only in single pipe config on IVB+ */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00005120 if (INTEL_GEN(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02005121 config.num_pipes_active == 1 && config.sprites_enabled) {
5122 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
5123 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005124
Imre Deak820c1982013-12-17 14:46:36 +02005125 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03005126 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005127 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005128 }
5129
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005130 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005131 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005132
Imre Deak820c1982013-12-17 14:46:36 +02005133 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03005134
Imre Deak820c1982013-12-17 14:46:36 +02005135 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03005136}
5137
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005138static void ilk_initial_watermarks(struct intel_atomic_state *state,
5139 struct intel_crtc_state *cstate)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005140{
Matt Ropered4a6a72016-02-23 17:20:13 -08005141 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
5142 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005143
Matt Ropered4a6a72016-02-23 17:20:13 -08005144 mutex_lock(&dev_priv->wm.wm_mutex);
Matt Ropere8f1f022016-05-12 07:05:55 -07005145 intel_crtc->wm.active.ilk = cstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08005146 ilk_program_watermarks(dev_priv);
5147 mutex_unlock(&dev_priv->wm.wm_mutex);
5148}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005149
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005150static void ilk_optimize_watermarks(struct intel_atomic_state *state,
5151 struct intel_crtc_state *cstate)
Matt Ropered4a6a72016-02-23 17:20:13 -08005152{
5153 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
5154 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
5155
5156 mutex_lock(&dev_priv->wm.wm_mutex);
5157 if (cstate->wm.need_postvbl_update) {
Matt Ropere8f1f022016-05-12 07:05:55 -07005158 intel_crtc->wm.active.ilk = cstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08005159 ilk_program_watermarks(dev_priv);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005160 }
Matt Ropered4a6a72016-02-23 17:20:13 -08005161 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005162}
5163
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005164static inline void skl_wm_level_from_reg_val(uint32_t val,
5165 struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00005166{
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005167 level->plane_en = val & PLANE_WM_EN;
5168 level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
5169 level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
5170 PLANE_WM_LINES_MASK;
Pradeep Bhat30789992014-11-04 17:06:45 +00005171}
5172
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005173void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc,
5174 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00005175{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005176 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Pradeep Bhat30789992014-11-04 17:06:45 +00005177 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Pradeep Bhat30789992014-11-04 17:06:45 +00005178 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005179 int level, max_level;
5180 enum plane_id plane_id;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005181 uint32_t val;
Pradeep Bhat30789992014-11-04 17:06:45 +00005182
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005183 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00005184
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005185 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
5186 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00005187
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005188 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005189 if (plane_id != PLANE_CURSOR)
5190 val = I915_READ(PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005191 else
5192 val = I915_READ(CUR_WM(pipe, level));
5193
5194 skl_wm_level_from_reg_val(val, &wm->wm[level]);
5195 }
5196
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005197 if (plane_id != PLANE_CURSOR)
5198 val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005199 else
5200 val = I915_READ(CUR_WM_TRANS(pipe));
5201
5202 skl_wm_level_from_reg_val(val, &wm->trans_wm);
5203 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005204
Matt Roper3ef00282015-03-09 10:19:24 -07005205 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00005206 return;
5207
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005208 out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00005209}
5210
5211void skl_wm_get_hw_state(struct drm_device *dev)
5212{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005213 struct drm_i915_private *dev_priv = to_i915(dev);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005214 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
Damien Lespiaua269c582014-11-04 17:06:49 +00005215 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00005216 struct drm_crtc *crtc;
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005217 struct intel_crtc *intel_crtc;
5218 struct intel_crtc_state *cstate;
Pradeep Bhat30789992014-11-04 17:06:45 +00005219
Damien Lespiaua269c582014-11-04 17:06:49 +00005220 skl_ddb_get_hw_state(dev_priv, ddb);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005221 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5222 intel_crtc = to_intel_crtc(crtc);
5223 cstate = to_intel_crtc_state(crtc->state);
5224
5225 skl_pipe_wm_get_hw_state(crtc, &cstate->wm.skl.optimal);
5226
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02005227 if (intel_crtc->active)
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005228 hw->dirty_pipes |= drm_crtc_mask(crtc);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005229 }
Matt Ropera1de91e2016-05-12 07:05:57 -07005230
Matt Roper279e99d2016-05-12 07:06:02 -07005231 if (dev_priv->active_crtcs) {
5232 /* Fully recompute DDB on first atomic commit */
5233 dev_priv->wm.distrust_bios_wm = true;
5234 } else {
5235 /* Easy/common case; just sanitize DDB now if everything off */
5236 memset(ddb, 0, sizeof(*ddb));
5237 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005238}
5239
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005240static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
5241{
5242 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005243 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02005244 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005245 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper4e0963c2015-09-24 15:53:15 -07005246 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Matt Ropere8f1f022016-05-12 07:05:55 -07005247 struct intel_pipe_wm *active = &cstate->wm.ilk.optimal;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005248 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005249 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005250 [PIPE_A] = WM0_PIPEA_ILK,
5251 [PIPE_B] = WM0_PIPEB_ILK,
5252 [PIPE_C] = WM0_PIPEC_IVB,
5253 };
5254
5255 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005256 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02005257 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005258
Ville Syrjälä15606532016-05-13 17:55:17 +03005259 memset(active, 0, sizeof(*active));
5260
Matt Roper3ef00282015-03-09 10:19:24 -07005261 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02005262
5263 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005264 u32 tmp = hw->wm_pipe[pipe];
5265
5266 /*
5267 * For active pipes LP0 watermark is marked as
5268 * enabled, and LP1+ watermaks as disabled since
5269 * we can't really reverse compute them in case
5270 * multiple pipes are active.
5271 */
5272 active->wm[0].enable = true;
5273 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
5274 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
5275 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
5276 active->linetime = hw->wm_linetime[pipe];
5277 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005278 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005279
5280 /*
5281 * For inactive pipes, all watermark levels
5282 * should be marked as enabled but zeroed,
5283 * which is what we'd compute them to.
5284 */
5285 for (level = 0; level <= max_level; level++)
5286 active->wm[level].enable = true;
5287 }
Matt Roper4e0963c2015-09-24 15:53:15 -07005288
5289 intel_crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005290}
5291
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005292#define _FW_WM(value, plane) \
5293 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
5294#define _FW_WM_VLV(value, plane) \
5295 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
5296
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005297static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
5298 struct g4x_wm_values *wm)
5299{
5300 uint32_t tmp;
5301
5302 tmp = I915_READ(DSPFW1);
5303 wm->sr.plane = _FW_WM(tmp, SR);
5304 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5305 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
5306 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
5307
5308 tmp = I915_READ(DSPFW2);
5309 wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
5310 wm->sr.fbc = _FW_WM(tmp, FBC_SR);
5311 wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
5312 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
5313 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5314 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
5315
5316 tmp = I915_READ(DSPFW3);
5317 wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
5318 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5319 wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
5320 wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
5321}
5322
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005323static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
5324 struct vlv_wm_values *wm)
5325{
5326 enum pipe pipe;
5327 uint32_t tmp;
5328
5329 for_each_pipe(dev_priv, pipe) {
5330 tmp = I915_READ(VLV_DDL(pipe));
5331
Ville Syrjälä1b313892016-11-28 19:37:08 +02005332 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005333 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005334 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005335 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005336 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005337 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005338 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005339 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
5340 }
5341
5342 tmp = I915_READ(DSPFW1);
5343 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005344 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5345 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
5346 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005347
5348 tmp = I915_READ(DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005349 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
5350 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5351 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005352
5353 tmp = I915_READ(DSPFW3);
5354 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5355
5356 if (IS_CHERRYVIEW(dev_priv)) {
5357 tmp = I915_READ(DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005358 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5359 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005360
5361 tmp = I915_READ(DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005362 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
5363 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005364
5365 tmp = I915_READ(DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005366 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
5367 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005368
5369 tmp = I915_READ(DSPHOWM);
5370 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005371 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
5372 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
5373 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
5374 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5375 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5376 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5377 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5378 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5379 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005380 } else {
5381 tmp = I915_READ(DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005382 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5383 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005384
5385 tmp = I915_READ(DSPHOWM);
5386 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005387 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5388 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5389 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5390 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5391 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5392 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005393 }
5394}
5395
5396#undef _FW_WM
5397#undef _FW_WM_VLV
5398
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005399void g4x_wm_get_hw_state(struct drm_device *dev)
5400{
5401 struct drm_i915_private *dev_priv = to_i915(dev);
5402 struct g4x_wm_values *wm = &dev_priv->wm.g4x;
5403 struct intel_crtc *crtc;
5404
5405 g4x_read_wm_values(dev_priv, wm);
5406
5407 wm->cxsr = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
5408
5409 for_each_intel_crtc(dev, crtc) {
5410 struct intel_crtc_state *crtc_state =
5411 to_intel_crtc_state(crtc->base.state);
5412 struct g4x_wm_state *active = &crtc->wm.active.g4x;
5413 struct g4x_pipe_wm *raw;
5414 enum pipe pipe = crtc->pipe;
5415 enum plane_id plane_id;
5416 int level, max_level;
5417
5418 active->cxsr = wm->cxsr;
5419 active->hpll_en = wm->hpll_en;
5420 active->fbc_en = wm->fbc_en;
5421
5422 active->sr = wm->sr;
5423 active->hpll = wm->hpll;
5424
5425 for_each_plane_id_on_crtc(crtc, plane_id) {
5426 active->wm.plane[plane_id] =
5427 wm->pipe[pipe].plane[plane_id];
5428 }
5429
5430 if (wm->cxsr && wm->hpll_en)
5431 max_level = G4X_WM_LEVEL_HPLL;
5432 else if (wm->cxsr)
5433 max_level = G4X_WM_LEVEL_SR;
5434 else
5435 max_level = G4X_WM_LEVEL_NORMAL;
5436
5437 level = G4X_WM_LEVEL_NORMAL;
5438 raw = &crtc_state->wm.g4x.raw[level];
5439 for_each_plane_id_on_crtc(crtc, plane_id)
5440 raw->plane[plane_id] = active->wm.plane[plane_id];
5441
5442 if (++level > max_level)
5443 goto out;
5444
5445 raw = &crtc_state->wm.g4x.raw[level];
5446 raw->plane[PLANE_PRIMARY] = active->sr.plane;
5447 raw->plane[PLANE_CURSOR] = active->sr.cursor;
5448 raw->plane[PLANE_SPRITE0] = 0;
5449 raw->fbc = active->sr.fbc;
5450
5451 if (++level > max_level)
5452 goto out;
5453
5454 raw = &crtc_state->wm.g4x.raw[level];
5455 raw->plane[PLANE_PRIMARY] = active->hpll.plane;
5456 raw->plane[PLANE_CURSOR] = active->hpll.cursor;
5457 raw->plane[PLANE_SPRITE0] = 0;
5458 raw->fbc = active->hpll.fbc;
5459
5460 out:
5461 for_each_plane_id_on_crtc(crtc, plane_id)
5462 g4x_raw_plane_wm_set(crtc_state, level,
5463 plane_id, USHRT_MAX);
5464 g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
5465
5466 crtc_state->wm.g4x.optimal = *active;
5467 crtc_state->wm.g4x.intermediate = *active;
5468
5469 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
5470 pipe_name(pipe),
5471 wm->pipe[pipe].plane[PLANE_PRIMARY],
5472 wm->pipe[pipe].plane[PLANE_CURSOR],
5473 wm->pipe[pipe].plane[PLANE_SPRITE0]);
5474 }
5475
5476 DRM_DEBUG_KMS("Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
5477 wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
5478 DRM_DEBUG_KMS("Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
5479 wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
5480 DRM_DEBUG_KMS("Initial SR=%s HPLL=%s FBC=%s\n",
5481 yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
5482}
5483
5484void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
5485{
5486 struct intel_plane *plane;
5487 struct intel_crtc *crtc;
5488
5489 mutex_lock(&dev_priv->wm.wm_mutex);
5490
5491 for_each_intel_plane(&dev_priv->drm, plane) {
5492 struct intel_crtc *crtc =
5493 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
5494 struct intel_crtc_state *crtc_state =
5495 to_intel_crtc_state(crtc->base.state);
5496 struct intel_plane_state *plane_state =
5497 to_intel_plane_state(plane->base.state);
5498 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
5499 enum plane_id plane_id = plane->id;
5500 int level;
5501
5502 if (plane_state->base.visible)
5503 continue;
5504
5505 for (level = 0; level < 3; level++) {
5506 struct g4x_pipe_wm *raw =
5507 &crtc_state->wm.g4x.raw[level];
5508
5509 raw->plane[plane_id] = 0;
5510 wm_state->wm.plane[plane_id] = 0;
5511 }
5512
5513 if (plane_id == PLANE_PRIMARY) {
5514 for (level = 0; level < 3; level++) {
5515 struct g4x_pipe_wm *raw =
5516 &crtc_state->wm.g4x.raw[level];
5517 raw->fbc = 0;
5518 }
5519
5520 wm_state->sr.fbc = 0;
5521 wm_state->hpll.fbc = 0;
5522 wm_state->fbc_en = false;
5523 }
5524 }
5525
5526 for_each_intel_crtc(&dev_priv->drm, crtc) {
5527 struct intel_crtc_state *crtc_state =
5528 to_intel_crtc_state(crtc->base.state);
5529
5530 crtc_state->wm.g4x.intermediate =
5531 crtc_state->wm.g4x.optimal;
5532 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
5533 }
5534
5535 g4x_program_watermarks(dev_priv);
5536
5537 mutex_unlock(&dev_priv->wm.wm_mutex);
5538}
5539
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005540void vlv_wm_get_hw_state(struct drm_device *dev)
5541{
5542 struct drm_i915_private *dev_priv = to_i915(dev);
5543 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02005544 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005545 u32 val;
5546
5547 vlv_read_wm_values(dev_priv, wm);
5548
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005549 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
5550 wm->level = VLV_WM_LEVEL_PM2;
5551
5552 if (IS_CHERRYVIEW(dev_priv)) {
5553 mutex_lock(&dev_priv->rps.hw_lock);
5554
5555 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5556 if (val & DSP_MAXFIFO_PM5_ENABLE)
5557 wm->level = VLV_WM_LEVEL_PM5;
5558
Ville Syrjälä58590c12015-09-08 21:05:12 +03005559 /*
5560 * If DDR DVFS is disabled in the BIOS, Punit
5561 * will never ack the request. So if that happens
5562 * assume we don't have to enable/disable DDR DVFS
5563 * dynamically. To test that just set the REQ_ACK
5564 * bit to poke the Punit, but don't change the
5565 * HIGH/LOW bits so that we don't actually change
5566 * the current state.
5567 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005568 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03005569 val |= FORCE_DDR_FREQ_REQ_ACK;
5570 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
5571
5572 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
5573 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
5574 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
5575 "assuming DDR DVFS is disabled\n");
5576 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
5577 } else {
5578 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
5579 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
5580 wm->level = VLV_WM_LEVEL_DDR_DVFS;
5581 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005582
5583 mutex_unlock(&dev_priv->rps.hw_lock);
5584 }
5585
Ville Syrjäläff32c542017-03-02 19:14:57 +02005586 for_each_intel_crtc(dev, crtc) {
5587 struct intel_crtc_state *crtc_state =
5588 to_intel_crtc_state(crtc->base.state);
5589 struct vlv_wm_state *active = &crtc->wm.active.vlv;
5590 const struct vlv_fifo_state *fifo_state =
5591 &crtc_state->wm.vlv.fifo_state;
5592 enum pipe pipe = crtc->pipe;
5593 enum plane_id plane_id;
5594 int level;
5595
5596 vlv_get_fifo_size(crtc_state);
5597
5598 active->num_levels = wm->level + 1;
5599 active->cxsr = wm->cxsr;
5600
Ville Syrjäläff32c542017-03-02 19:14:57 +02005601 for (level = 0; level < active->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03005602 struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02005603 &crtc_state->wm.vlv.raw[level];
5604
5605 active->sr[level].plane = wm->sr.plane;
5606 active->sr[level].cursor = wm->sr.cursor;
5607
5608 for_each_plane_id_on_crtc(crtc, plane_id) {
5609 active->wm[level].plane[plane_id] =
5610 wm->pipe[pipe].plane[plane_id];
5611
5612 raw->plane[plane_id] =
5613 vlv_invert_wm_value(active->wm[level].plane[plane_id],
5614 fifo_state->plane[plane_id]);
5615 }
5616 }
5617
5618 for_each_plane_id_on_crtc(crtc, plane_id)
5619 vlv_raw_plane_wm_set(crtc_state, level,
5620 plane_id, USHRT_MAX);
5621 vlv_invalidate_wms(crtc, active, level);
5622
5623 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02005624 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02005625
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005626 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 +02005627 pipe_name(pipe),
5628 wm->pipe[pipe].plane[PLANE_PRIMARY],
5629 wm->pipe[pipe].plane[PLANE_CURSOR],
5630 wm->pipe[pipe].plane[PLANE_SPRITE0],
5631 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02005632 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005633
5634 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
5635 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
5636}
5637
Ville Syrjälä602ae832017-03-02 19:15:02 +02005638void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
5639{
5640 struct intel_plane *plane;
5641 struct intel_crtc *crtc;
5642
5643 mutex_lock(&dev_priv->wm.wm_mutex);
5644
5645 for_each_intel_plane(&dev_priv->drm, plane) {
5646 struct intel_crtc *crtc =
5647 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
5648 struct intel_crtc_state *crtc_state =
5649 to_intel_crtc_state(crtc->base.state);
5650 struct intel_plane_state *plane_state =
5651 to_intel_plane_state(plane->base.state);
5652 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
5653 const struct vlv_fifo_state *fifo_state =
5654 &crtc_state->wm.vlv.fifo_state;
5655 enum plane_id plane_id = plane->id;
5656 int level;
5657
5658 if (plane_state->base.visible)
5659 continue;
5660
5661 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03005662 struct g4x_pipe_wm *raw =
Ville Syrjälä602ae832017-03-02 19:15:02 +02005663 &crtc_state->wm.vlv.raw[level];
5664
5665 raw->plane[plane_id] = 0;
5666
5667 wm_state->wm[level].plane[plane_id] =
5668 vlv_invert_wm_value(raw->plane[plane_id],
5669 fifo_state->plane[plane_id]);
5670 }
5671 }
5672
5673 for_each_intel_crtc(&dev_priv->drm, crtc) {
5674 struct intel_crtc_state *crtc_state =
5675 to_intel_crtc_state(crtc->base.state);
5676
5677 crtc_state->wm.vlv.intermediate =
5678 crtc_state->wm.vlv.optimal;
5679 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
5680 }
5681
5682 vlv_program_watermarks(dev_priv);
5683
5684 mutex_unlock(&dev_priv->wm.wm_mutex);
5685}
5686
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005687void ilk_wm_get_hw_state(struct drm_device *dev)
5688{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005689 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02005690 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005691 struct drm_crtc *crtc;
5692
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01005693 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005694 ilk_pipe_wm_get_hw_state(crtc);
5695
5696 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
5697 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
5698 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
5699
5700 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00005701 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjäläcfa76982014-03-07 18:32:08 +02005702 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
5703 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
5704 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005705
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005706 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02005707 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
5708 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01005709 else if (IS_IVYBRIDGE(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02005710 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
5711 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005712
5713 hw->enable_fbc_wm =
5714 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
5715}
5716
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005717/**
5718 * intel_update_watermarks - update FIFO watermark values based on current modes
5719 *
5720 * Calculate watermark values for the various WM regs based on current mode
5721 * and plane configuration.
5722 *
5723 * There are several cases to deal with here:
5724 * - normal (i.e. non-self-refresh)
5725 * - self-refresh (SR) mode
5726 * - lines are large relative to FIFO size (buffer can hold up to 2)
5727 * - lines are small relative to FIFO size (buffer can hold more than 2
5728 * lines), so need to account for TLB latency
5729 *
5730 * The normal calculation is:
5731 * watermark = dotclock * bytes per pixel * latency
5732 * where latency is platform & configuration dependent (we assume pessimal
5733 * values here).
5734 *
5735 * The SR calculation is:
5736 * watermark = (trunc(latency/line time)+1) * surface width *
5737 * bytes per pixel
5738 * where
5739 * line time = htotal / dotclock
5740 * surface width = hdisplay for normal plane and 64 for cursor
5741 * and latency is assumed to be high, as above.
5742 *
5743 * The final value programmed to the register should always be rounded up,
5744 * and include an extra 2 entries to account for clock crossings.
5745 *
5746 * We don't use the sprite, so we can ignore that. And on Crestline we have
5747 * to set the non-SR watermarks to 8.
5748 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02005749void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005750{
Ville Syrjälä432081b2016-10-31 22:37:03 +02005751 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005752
5753 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005754 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005755}
5756
Jani Nikulae2828912016-01-18 09:19:47 +02005757/*
Daniel Vetter92703882012-08-09 16:46:01 +02005758 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02005759 */
5760DEFINE_SPINLOCK(mchdev_lock);
5761
5762/* Global for IPS driver to get at the current i915 device. Protected by
5763 * mchdev_lock. */
5764static struct drm_i915_private *i915_mch_dev;
5765
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005766bool ironlake_set_drps(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005767{
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005768 u16 rgvswctl;
5769
Chris Wilson67520412017-03-02 13:28:01 +00005770 lockdep_assert_held(&mchdev_lock);
Daniel Vetter92703882012-08-09 16:46:01 +02005771
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005772 rgvswctl = I915_READ16(MEMSWCTL);
5773 if (rgvswctl & MEMCTL_CMD_STS) {
5774 DRM_DEBUG("gpu busy, RCS change rejected\n");
5775 return false; /* still busy with another command */
5776 }
5777
5778 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
5779 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
5780 I915_WRITE16(MEMSWCTL, rgvswctl);
5781 POSTING_READ16(MEMSWCTL);
5782
5783 rgvswctl |= MEMCTL_CMD_STS;
5784 I915_WRITE16(MEMSWCTL, rgvswctl);
5785
5786 return true;
5787}
5788
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005789static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005790{
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00005791 u32 rgvmodectl;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005792 u8 fmax, fmin, fstart, vstart;
5793
Daniel Vetter92703882012-08-09 16:46:01 +02005794 spin_lock_irq(&mchdev_lock);
5795
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00005796 rgvmodectl = I915_READ(MEMMODECTL);
5797
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005798 /* Enable temp reporting */
5799 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
5800 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
5801
5802 /* 100ms RC evaluation intervals */
5803 I915_WRITE(RCUPEI, 100000);
5804 I915_WRITE(RCDNEI, 100000);
5805
5806 /* Set max/min thresholds to 90ms and 80ms respectively */
5807 I915_WRITE(RCBMAXAVG, 90000);
5808 I915_WRITE(RCBMINAVG, 80000);
5809
5810 I915_WRITE(MEMIHYST, 1);
5811
5812 /* Set up min, max, and cur for interrupt handling */
5813 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
5814 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
5815 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
5816 MEMMODE_FSTART_SHIFT;
5817
Ville Syrjälä616847e2015-09-18 20:03:19 +03005818 vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005819 PXVFREQ_PX_SHIFT;
5820
Daniel Vetter20e4d402012-08-08 23:35:39 +02005821 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
5822 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005823
Daniel Vetter20e4d402012-08-08 23:35:39 +02005824 dev_priv->ips.max_delay = fstart;
5825 dev_priv->ips.min_delay = fmin;
5826 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005827
5828 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
5829 fmax, fmin, fstart);
5830
5831 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
5832
5833 /*
5834 * Interrupts will be enabled in ironlake_irq_postinstall
5835 */
5836
5837 I915_WRITE(VIDSTART, vstart);
5838 POSTING_READ(VIDSTART);
5839
5840 rgvmodectl |= MEMMODE_SWMODE_EN;
5841 I915_WRITE(MEMMODECTL, rgvmodectl);
5842
Daniel Vetter92703882012-08-09 16:46:01 +02005843 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005844 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005845 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005846
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005847 ironlake_set_drps(dev_priv, fstart);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005848
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03005849 dev_priv->ips.last_count1 = I915_READ(DMIEC) +
5850 I915_READ(DDREC) + I915_READ(CSIEC);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005851 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03005852 dev_priv->ips.last_count2 = I915_READ(GFXEC);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005853 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02005854
5855 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005856}
5857
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005858static void ironlake_disable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005859{
Daniel Vetter92703882012-08-09 16:46:01 +02005860 u16 rgvswctl;
5861
5862 spin_lock_irq(&mchdev_lock);
5863
5864 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005865
5866 /* Ack interrupts, disable EFC interrupt */
5867 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
5868 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
5869 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
5870 I915_WRITE(DEIIR, DE_PCU_EVENT);
5871 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
5872
5873 /* Go back to the starting frequency */
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005874 ironlake_set_drps(dev_priv, dev_priv->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005875 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005876 rgvswctl |= MEMCTL_CMD_STS;
5877 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005878 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005879
Daniel Vetter92703882012-08-09 16:46:01 +02005880 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005881}
5882
Daniel Vetteracbe9472012-07-26 11:50:05 +02005883/* There's a funny hw issue where the hw returns all 0 when reading from
5884 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
5885 * ourselves, instead of doing a rmw cycle (which might result in us clearing
5886 * all limits and the gpu stuck at whatever frequency it is at atm).
5887 */
Akash Goel74ef1172015-03-06 11:07:19 +05305888static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005889{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005890 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005891
Daniel Vetter20b46e52012-07-26 11:16:14 +02005892 /* Only set the down limit when we've reached the lowest level to avoid
5893 * getting more interrupts, otherwise leave this clear. This prevents a
5894 * race in the hw when coming out of rc6: There's a tiny window where
5895 * the hw runs at the minimal clock before selecting the desired
5896 * frequency, if the down threshold expires in that window we will not
5897 * receive a down interrupt. */
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07005898 if (INTEL_GEN(dev_priv) >= 9) {
Akash Goel74ef1172015-03-06 11:07:19 +05305899 limits = (dev_priv->rps.max_freq_softlimit) << 23;
5900 if (val <= dev_priv->rps.min_freq_softlimit)
5901 limits |= (dev_priv->rps.min_freq_softlimit) << 14;
5902 } else {
5903 limits = dev_priv->rps.max_freq_softlimit << 24;
5904 if (val <= dev_priv->rps.min_freq_softlimit)
5905 limits |= dev_priv->rps.min_freq_softlimit << 16;
5906 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02005907
5908 return limits;
5909}
5910
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005911static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
5912{
5913 int new_power;
Akash Goel8a586432015-03-06 11:07:18 +05305914 u32 threshold_up = 0, threshold_down = 0; /* in % */
5915 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005916
5917 new_power = dev_priv->rps.power;
5918 switch (dev_priv->rps.power) {
5919 case LOW_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01005920 if (val > dev_priv->rps.efficient_freq + 1 &&
5921 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005922 new_power = BETWEEN;
5923 break;
5924
5925 case BETWEEN:
Chris Wilsona72b5622016-07-02 15:35:59 +01005926 if (val <= dev_priv->rps.efficient_freq &&
5927 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005928 new_power = LOW_POWER;
Chris Wilsona72b5622016-07-02 15:35:59 +01005929 else if (val >= dev_priv->rps.rp0_freq &&
5930 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005931 new_power = HIGH_POWER;
5932 break;
5933
5934 case HIGH_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01005935 if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 &&
5936 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005937 new_power = BETWEEN;
5938 break;
5939 }
5940 /* Max/min bins are special */
Chris Wilsonaed242f2015-03-18 09:48:21 +00005941 if (val <= dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005942 new_power = LOW_POWER;
Chris Wilsonaed242f2015-03-18 09:48:21 +00005943 if (val >= dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005944 new_power = HIGH_POWER;
5945 if (new_power == dev_priv->rps.power)
5946 return;
5947
5948 /* Note the units here are not exactly 1us, but 1280ns. */
5949 switch (new_power) {
5950 case LOW_POWER:
5951 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05305952 ei_up = 16000;
5953 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005954
5955 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305956 ei_down = 32000;
5957 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005958 break;
5959
5960 case BETWEEN:
5961 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05305962 ei_up = 13000;
5963 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005964
5965 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305966 ei_down = 32000;
5967 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005968 break;
5969
5970 case HIGH_POWER:
5971 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05305972 ei_up = 10000;
5973 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005974
5975 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305976 ei_down = 32000;
5977 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005978 break;
5979 }
5980
Mika Kuoppala6067a272017-02-15 15:52:59 +02005981 /* When byt can survive without system hang with dynamic
5982 * sw freq adjustments, this restriction can be lifted.
5983 */
5984 if (IS_VALLEYVIEW(dev_priv))
5985 goto skip_hw_write;
5986
Akash Goel8a586432015-03-06 11:07:18 +05305987 I915_WRITE(GEN6_RP_UP_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01005988 GT_INTERVAL_FROM_US(dev_priv, ei_up));
Akash Goel8a586432015-03-06 11:07:18 +05305989 I915_WRITE(GEN6_RP_UP_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01005990 GT_INTERVAL_FROM_US(dev_priv,
5991 ei_up * threshold_up / 100));
Akash Goel8a586432015-03-06 11:07:18 +05305992
5993 I915_WRITE(GEN6_RP_DOWN_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01005994 GT_INTERVAL_FROM_US(dev_priv, ei_down));
Akash Goel8a586432015-03-06 11:07:18 +05305995 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01005996 GT_INTERVAL_FROM_US(dev_priv,
5997 ei_down * threshold_down / 100));
Akash Goel8a586432015-03-06 11:07:18 +05305998
Chris Wilsona72b5622016-07-02 15:35:59 +01005999 I915_WRITE(GEN6_RP_CONTROL,
6000 GEN6_RP_MEDIA_TURBO |
6001 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6002 GEN6_RP_MEDIA_IS_GFX |
6003 GEN6_RP_ENABLE |
6004 GEN6_RP_UP_BUSY_AVG |
6005 GEN6_RP_DOWN_IDLE_AVG);
Akash Goel8a586432015-03-06 11:07:18 +05306006
Mika Kuoppala6067a272017-02-15 15:52:59 +02006007skip_hw_write:
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006008 dev_priv->rps.power = new_power;
Chris Wilson8fb55192015-04-07 16:20:28 +01006009 dev_priv->rps.up_threshold = threshold_up;
6010 dev_priv->rps.down_threshold = threshold_down;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006011 dev_priv->rps.last_adj = 0;
6012}
6013
Chris Wilson2876ce72014-03-28 08:03:34 +00006014static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
6015{
6016 u32 mask = 0;
6017
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006018 /* We use UP_EI_EXPIRED interupts for both up/down in manual mode */
Chris Wilson2876ce72014-03-28 08:03:34 +00006019 if (val > dev_priv->rps.min_freq_softlimit)
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006020 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
Chris Wilson2876ce72014-03-28 08:03:34 +00006021 if (val < dev_priv->rps.max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00006022 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00006023
Chris Wilson7b3c29f2014-07-10 20:31:19 +01006024 mask &= dev_priv->pm_rps_events;
6025
Imre Deak59d02a12014-12-19 19:33:26 +02006026 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00006027}
6028
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006029/* gen6_set_rps is called to update the frequency request, but should also be
6030 * called when the range (min_delay and max_delay) is modified so that we can
6031 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006032static int gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02006033{
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006034 /* min/max delay may still have been modified so be sure to
6035 * write the limits value.
6036 */
6037 if (val != dev_priv->rps.cur_freq) {
6038 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006039
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006040 if (INTEL_GEN(dev_priv) >= 9)
Akash Goel57041952015-03-06 11:07:17 +05306041 I915_WRITE(GEN6_RPNSWREQ,
6042 GEN9_FREQUENCY(val));
Chris Wilsondc979972016-05-10 14:10:04 +01006043 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006044 I915_WRITE(GEN6_RPNSWREQ,
6045 HSW_FREQUENCY(val));
6046 else
6047 I915_WRITE(GEN6_RPNSWREQ,
6048 GEN6_FREQUENCY(val) |
6049 GEN6_OFFSET(0) |
6050 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006051 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006052
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006053 /* Make sure we continue to get interrupts
6054 * until we hit the minimum or maximum frequencies.
6055 */
Akash Goel74ef1172015-03-06 11:07:19 +05306056 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00006057 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006058
Ben Widawskyb39fb292014-03-19 18:31:11 -07006059 dev_priv->rps.cur_freq = val;
Mika Kuoppala0f945922015-11-17 18:14:26 +02006060 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006061
6062 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006063}
6064
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006065static int valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006066{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006067 int err;
6068
Chris Wilsondc979972016-05-10 14:10:04 +01006069 if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006070 "Odd GPU freq value\n"))
6071 val &= ~1;
6072
Deepak Scd25dd52015-07-10 18:31:40 +05306073 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
6074
Chris Wilson8fb55192015-04-07 16:20:28 +01006075 if (val != dev_priv->rps.cur_freq) {
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006076 err = vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
6077 if (err)
6078 return err;
6079
Chris Wilsondb4c5e02017-02-10 15:03:46 +00006080 gen6_set_rps_thresholds(dev_priv, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01006081 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006082
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006083 dev_priv->rps.cur_freq = val;
6084 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006085
6086 return 0;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006087}
6088
Deepak Sa7f6e232015-05-09 18:04:44 +05306089/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05306090 *
6091 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05306092 * 1. Forcewake Media well.
6093 * 2. Request idle freq.
6094 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05306095*/
6096static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
6097{
Chris Wilsonaed242f2015-03-18 09:48:21 +00006098 u32 val = dev_priv->rps.idle_freq;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006099 int err;
Deepak S5549d252014-06-28 11:26:11 +05306100
Chris Wilsonaed242f2015-03-18 09:48:21 +00006101 if (dev_priv->rps.cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05306102 return;
6103
Chris Wilsonc9efef72017-01-02 15:28:45 +00006104 /* The punit delays the write of the frequency and voltage until it
6105 * determines the GPU is awake. During normal usage we don't want to
6106 * waste power changing the frequency if the GPU is sleeping (rc6).
6107 * However, the GPU and driver is now idle and we do not want to delay
6108 * switching to minimum voltage (reducing power whilst idle) as we do
6109 * not expect to be woken in the near future and so must flush the
6110 * change by waking the device.
6111 *
6112 * We choose to take the media powerwell (either would do to trick the
6113 * punit into committing the voltage change) as that takes a lot less
6114 * power than the render powerwell.
6115 */
Deepak Sa7f6e232015-05-09 18:04:44 +05306116 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006117 err = valleyview_set_rps(dev_priv, val);
Deepak Sa7f6e232015-05-09 18:04:44 +05306118 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006119
6120 if (err)
6121 DRM_ERROR("Failed to set RPS for idle\n");
Deepak S76c3552f2014-01-30 23:08:16 +05306122}
6123
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006124void gen6_rps_busy(struct drm_i915_private *dev_priv)
6125{
6126 mutex_lock(&dev_priv->rps.hw_lock);
6127 if (dev_priv->rps.enabled) {
Chris Wilsonbd648182017-02-10 15:03:48 +00006128 u8 freq;
6129
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006130 if (dev_priv->pm_rps_events & GEN6_PM_RP_UP_EI_EXPIRED)
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006131 gen6_rps_reset_ei(dev_priv);
6132 I915_WRITE(GEN6_PMINTRMSK,
6133 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Michał Winiarski2b83c4c2016-06-20 11:58:27 +02006134
Chris Wilsonc33d2472016-07-04 08:08:36 +01006135 gen6_enable_rps_interrupts(dev_priv);
6136
Chris Wilsonbd648182017-02-10 15:03:48 +00006137 /* Use the user's desired frequency as a guide, but for better
6138 * performance, jump directly to RPe as our starting frequency.
6139 */
6140 freq = max(dev_priv->rps.cur_freq,
6141 dev_priv->rps.efficient_freq);
6142
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006143 if (intel_set_rps(dev_priv,
Chris Wilsonbd648182017-02-10 15:03:48 +00006144 clamp(freq,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006145 dev_priv->rps.min_freq_softlimit,
6146 dev_priv->rps.max_freq_softlimit)))
6147 DRM_DEBUG_DRIVER("Failed to set idle frequency\n");
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006148 }
6149 mutex_unlock(&dev_priv->rps.hw_lock);
6150}
6151
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006152void gen6_rps_idle(struct drm_i915_private *dev_priv)
6153{
Chris Wilsonc33d2472016-07-04 08:08:36 +01006154 /* Flush our bottom-half so that it does not race with us
6155 * setting the idle frequency and so that it is bounded by
6156 * our rpm wakeref. And then disable the interrupts to stop any
6157 * futher RPS reclocking whilst we are asleep.
6158 */
6159 gen6_disable_rps_interrupts(dev_priv);
6160
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006161 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01006162 if (dev_priv->rps.enabled) {
Chris Wilsondc979972016-05-10 14:10:04 +01006163 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Deepak S76c3552f2014-01-30 23:08:16 +05306164 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02006165 else
Chris Wilsondc979972016-05-10 14:10:04 +01006166 gen6_set_rps(dev_priv, dev_priv->rps.idle_freq);
Chris Wilsonc0951f02013-10-10 21:58:50 +01006167 dev_priv->rps.last_adj = 0;
Ville Syrjälä12c100b2016-05-23 17:42:48 +03006168 I915_WRITE(GEN6_PMINTRMSK,
6169 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Chris Wilsonc0951f02013-10-10 21:58:50 +01006170 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01006171 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006172}
6173
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006174void gen6_rps_boost(struct drm_i915_gem_request *rq,
6175 struct intel_rps_client *rps)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006176{
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006177 struct drm_i915_private *i915 = rq->i915;
Chris Wilson74d290f2017-08-17 13:37:06 +01006178 unsigned long flags;
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006179 bool boost;
6180
Chris Wilson8d3afd72015-05-21 21:01:47 +01006181 /* This is intentionally racy! We peek at the state here, then
6182 * validate inside the RPS worker.
6183 */
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006184 if (!i915->rps.enabled)
Chris Wilson8d3afd72015-05-21 21:01:47 +01006185 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006186
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006187 boost = false;
Chris Wilson74d290f2017-08-17 13:37:06 +01006188 spin_lock_irqsave(&rq->lock, flags);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006189 if (!rq->waitboost && !i915_gem_request_completed(rq)) {
6190 atomic_inc(&i915->rps.num_waiters);
6191 rq->waitboost = true;
6192 boost = true;
Chris Wilsonc0951f02013-10-10 21:58:50 +01006193 }
Chris Wilson74d290f2017-08-17 13:37:06 +01006194 spin_unlock_irqrestore(&rq->lock, flags);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006195 if (!boost)
6196 return;
6197
6198 if (READ_ONCE(i915->rps.cur_freq) < i915->rps.boost_freq)
6199 schedule_work(&i915->rps.work);
6200
6201 atomic_inc(rps ? &rps->boosts : &i915->rps.boosts);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006202}
6203
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006204int intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006205{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006206 int err;
6207
Chris Wilsoncfd1c482017-02-20 09:47:07 +00006208 lockdep_assert_held(&dev_priv->rps.hw_lock);
6209 GEM_BUG_ON(val > dev_priv->rps.max_freq);
6210 GEM_BUG_ON(val < dev_priv->rps.min_freq);
6211
Chris Wilson76e4e4b2017-02-20 09:47:08 +00006212 if (!dev_priv->rps.enabled) {
6213 dev_priv->rps.cur_freq = val;
6214 return 0;
6215 }
6216
Chris Wilsondc979972016-05-10 14:10:04 +01006217 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006218 err = valleyview_set_rps(dev_priv, val);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006219 else
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006220 err = gen6_set_rps(dev_priv, val);
6221
6222 return err;
Jesse Barnes0a073b82013-04-17 15:54:58 -07006223}
6224
Chris Wilsondc979972016-05-10 14:10:04 +01006225static void gen9_disable_rc6(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00006226{
Zhe Wang20e49362014-11-04 17:07:05 +00006227 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00006228 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00006229}
6230
Chris Wilsondc979972016-05-10 14:10:04 +01006231static void gen9_disable_rps(struct drm_i915_private *dev_priv)
Akash Goel2030d682016-04-23 00:05:45 +05306232{
Akash Goel2030d682016-04-23 00:05:45 +05306233 I915_WRITE(GEN6_RP_CONTROL, 0);
6234}
6235
Chris Wilsondc979972016-05-10 14:10:04 +01006236static void gen6_disable_rps(struct drm_i915_private *dev_priv)
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006237{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006238 I915_WRITE(GEN6_RC_CONTROL, 0);
6239 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Akash Goel2030d682016-04-23 00:05:45 +05306240 I915_WRITE(GEN6_RP_CONTROL, 0);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006241}
6242
Chris Wilsondc979972016-05-10 14:10:04 +01006243static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306244{
Deepak S38807742014-05-23 21:00:15 +05306245 I915_WRITE(GEN6_RC_CONTROL, 0);
6246}
6247
Chris Wilsondc979972016-05-10 14:10:04 +01006248static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006249{
Deepak S98a2e5f2014-08-18 10:35:27 -07006250 /* we're doing forcewake before Disabling RC6,
6251 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02006252 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07006253
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006254 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006255
Mika Kuoppala59bad942015-01-16 11:34:40 +02006256 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006257}
6258
Chris Wilsondc979972016-05-10 14:10:04 +01006259static void intel_print_rc6_info(struct drm_i915_private *dev_priv, u32 mode)
Ben Widawskydc39fff2013-10-18 12:32:07 -07006260{
Chris Wilsondc979972016-05-10 14:10:04 +01006261 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Imre Deak91ca6892014-04-14 20:24:25 +03006262 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
6263 mode = GEN6_RC_CTL_RC6_ENABLE;
6264 else
6265 mode = 0;
6266 }
Chris Wilsondc979972016-05-10 14:10:04 +01006267 if (HAS_RC6p(dev_priv))
Imre Deakb99d49c2016-06-29 19:13:54 +03006268 DRM_DEBUG_DRIVER("Enabling RC6 states: "
6269 "RC6 %s RC6p %s RC6pp %s\n",
6270 onoff(mode & GEN6_RC_CTL_RC6_ENABLE),
6271 onoff(mode & GEN6_RC_CTL_RC6p_ENABLE),
6272 onoff(mode & GEN6_RC_CTL_RC6pp_ENABLE));
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07006273
6274 else
Imre Deakb99d49c2016-06-29 19:13:54 +03006275 DRM_DEBUG_DRIVER("Enabling RC6 states: RC6 %s\n",
6276 onoff(mode & GEN6_RC_CTL_RC6_ENABLE));
Ben Widawskydc39fff2013-10-18 12:32:07 -07006277}
6278
Chris Wilsondc979972016-05-10 14:10:04 +01006279static bool bxt_check_bios_rc6_setup(struct drm_i915_private *dev_priv)
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306280{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006281 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306282 bool enable_rc6 = true;
6283 unsigned long rc6_ctx_base;
Imre Deakfc619842016-06-29 19:13:55 +03006284 u32 rc_ctl;
6285 int rc_sw_target;
6286
6287 rc_ctl = I915_READ(GEN6_RC_CONTROL);
6288 rc_sw_target = (I915_READ(GEN6_RC_STATE) & RC_SW_TARGET_STATE_MASK) >>
6289 RC_SW_TARGET_STATE_SHIFT;
6290 DRM_DEBUG_DRIVER("BIOS enabled RC states: "
6291 "HW_CTRL %s HW_RC6 %s SW_TARGET_STATE %x\n",
6292 onoff(rc_ctl & GEN6_RC_CTL_HW_ENABLE),
6293 onoff(rc_ctl & GEN6_RC_CTL_RC6_ENABLE),
6294 rc_sw_target);
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306295
6296 if (!(I915_READ(RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006297 DRM_DEBUG_DRIVER("RC6 Base location not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306298 enable_rc6 = false;
6299 }
6300
6301 /*
6302 * The exact context size is not known for BXT, so assume a page size
6303 * for this check.
6304 */
6305 rc6_ctx_base = I915_READ(RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006306 if (!((rc6_ctx_base >= ggtt->stolen_reserved_base) &&
6307 (rc6_ctx_base + PAGE_SIZE <= ggtt->stolen_reserved_base +
6308 ggtt->stolen_reserved_size))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006309 DRM_DEBUG_DRIVER("RC6 Base address not as expected.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306310 enable_rc6 = false;
6311 }
6312
6313 if (!(((I915_READ(PWRCTX_MAXCNT_RCSUNIT) & IDLE_TIME_MASK) > 1) &&
6314 ((I915_READ(PWRCTX_MAXCNT_VCSUNIT0) & IDLE_TIME_MASK) > 1) &&
6315 ((I915_READ(PWRCTX_MAXCNT_BCSUNIT) & IDLE_TIME_MASK) > 1) &&
6316 ((I915_READ(PWRCTX_MAXCNT_VECSUNIT) & IDLE_TIME_MASK) > 1))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006317 DRM_DEBUG_DRIVER("Engine Idle wait time not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306318 enable_rc6 = false;
6319 }
6320
Imre Deakfc619842016-06-29 19:13:55 +03006321 if (!I915_READ(GEN8_PUSHBUS_CONTROL) ||
6322 !I915_READ(GEN8_PUSHBUS_ENABLE) ||
6323 !I915_READ(GEN8_PUSHBUS_SHIFT)) {
6324 DRM_DEBUG_DRIVER("Pushbus not setup properly.\n");
6325 enable_rc6 = false;
6326 }
6327
6328 if (!I915_READ(GEN6_GFXPAUSE)) {
6329 DRM_DEBUG_DRIVER("GFX pause not setup properly.\n");
6330 enable_rc6 = false;
6331 }
6332
6333 if (!I915_READ(GEN8_MISC_CTRL0)) {
6334 DRM_DEBUG_DRIVER("GPM control not setup properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306335 enable_rc6 = false;
6336 }
6337
6338 return enable_rc6;
6339}
6340
Chris Wilsondc979972016-05-10 14:10:04 +01006341int sanitize_rc6_option(struct drm_i915_private *dev_priv, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006342{
Daniel Vettere7d66d82015-06-15 23:23:54 +02006343 /* No RC6 before Ironlake and code is gone for ilk. */
Chris Wilsondc979972016-05-10 14:10:04 +01006344 if (INTEL_INFO(dev_priv)->gen < 6)
Imre Deake6069ca2014-04-18 16:01:02 +03006345 return 0;
6346
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306347 if (!enable_rc6)
6348 return 0;
6349
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02006350 if (IS_GEN9_LP(dev_priv) && !bxt_check_bios_rc6_setup(dev_priv)) {
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306351 DRM_INFO("RC6 disabled by BIOS\n");
6352 return 0;
6353 }
6354
Daniel Vetter456470e2012-08-08 23:35:40 +02006355 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03006356 if (enable_rc6 >= 0) {
6357 int mask;
6358
Chris Wilsondc979972016-05-10 14:10:04 +01006359 if (HAS_RC6p(dev_priv))
Imre Deake6069ca2014-04-18 16:01:02 +03006360 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
6361 INTEL_RC6pp_ENABLE;
6362 else
6363 mask = INTEL_RC6_ENABLE;
6364
6365 if ((enable_rc6 & mask) != enable_rc6)
Imre Deakb99d49c2016-06-29 19:13:54 +03006366 DRM_DEBUG_DRIVER("Adjusting RC6 mask to %d "
6367 "(requested %d, valid %d)\n",
6368 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03006369
6370 return enable_rc6 & mask;
6371 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006372
Chris Wilsondc979972016-05-10 14:10:04 +01006373 if (IS_IVYBRIDGE(dev_priv))
Ben Widawskycca84a12014-01-28 20:25:38 -08006374 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08006375
6376 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006377}
6378
Chris Wilsondc979972016-05-10 14:10:04 +01006379static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
Imre Deake6069ca2014-04-18 16:01:02 +03006380{
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006381 /* All of these values are in units of 50MHz */
Chris Wilson773ea9a2016-07-13 09:10:33 +01006382
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006383 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02006384 if (IS_GEN9_LP(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006385 u32 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07006386 dev_priv->rps.rp0_freq = (rp_state_cap >> 16) & 0xff;
6387 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
6388 dev_priv->rps.min_freq = (rp_state_cap >> 0) & 0xff;
6389 } else {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006390 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07006391 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
6392 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
6393 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
6394 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006395 /* hw_max = RP0 until we check for overclocking */
Chris Wilson773ea9a2016-07-13 09:10:33 +01006396 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006397
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006398 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01006399 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006400 IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006401 u32 ddcc_status = 0;
6402
6403 if (sandybridge_pcode_read(dev_priv,
6404 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
6405 &ddcc_status) == 0)
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006406 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08006407 clamp_t(u8,
6408 ((ddcc_status >> 8) & 0xff),
6409 dev_priv->rps.min_freq,
6410 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006411 }
6412
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006413 if (IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Akash Goelc5e06882015-06-29 14:50:19 +05306414 /* Store the frequency values in 16.66 MHZ units, which is
Chris Wilson773ea9a2016-07-13 09:10:33 +01006415 * the natural hardware unit for SKL
6416 */
Akash Goelc5e06882015-06-29 14:50:19 +05306417 dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
6418 dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
6419 dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
6420 dev_priv->rps.max_freq *= GEN9_FREQ_SCALER;
6421 dev_priv->rps.efficient_freq *= GEN9_FREQ_SCALER;
6422 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006423}
6424
Chris Wilson3a45b052016-07-13 09:10:32 +01006425static void reset_rps(struct drm_i915_private *dev_priv,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006426 int (*set)(struct drm_i915_private *, u8))
Chris Wilson3a45b052016-07-13 09:10:32 +01006427{
6428 u8 freq = dev_priv->rps.cur_freq;
6429
6430 /* force a reset */
6431 dev_priv->rps.power = -1;
6432 dev_priv->rps.cur_freq = -1;
6433
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006434 if (set(dev_priv, freq))
6435 DRM_ERROR("Failed to reset RPS to initial values\n");
Chris Wilson3a45b052016-07-13 09:10:32 +01006436}
6437
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006438/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Chris Wilsondc979972016-05-10 14:10:04 +01006439static void gen9_enable_rps(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00006440{
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006441 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
6442
Akash Goel0beb0592015-03-06 11:07:20 +05306443 /* Program defaults and thresholds for RPS*/
6444 I915_WRITE(GEN6_RC_VIDEO_FREQ,
6445 GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006446
Akash Goel0beb0592015-03-06 11:07:20 +05306447 /* 1 second timeout*/
6448 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
6449 GT_INTERVAL_FROM_US(dev_priv, 1000000));
6450
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006451 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006452
Akash Goel0beb0592015-03-06 11:07:20 +05306453 /* Leaning on the below call to gen6_set_rps to program/setup the
6454 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
6455 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
Chris Wilson3a45b052016-07-13 09:10:32 +01006456 reset_rps(dev_priv, gen6_set_rps);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006457
6458 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
6459}
6460
Chris Wilsondc979972016-05-10 14:10:04 +01006461static void gen9_enable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006462{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006463 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306464 enum intel_engine_id id;
Zhe Wang20e49362014-11-04 17:07:05 +00006465 uint32_t rc6_mask = 0;
Zhe Wang20e49362014-11-04 17:07:05 +00006466
6467 /* 1a: Software RC state - RC0 */
6468 I915_WRITE(GEN6_RC_STATE, 0);
6469
6470 /* 1b: Get forcewake during program sequence. Although the driver
6471 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006472 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00006473
6474 /* 2a: Disable RC states. */
6475 I915_WRITE(GEN6_RC_CONTROL, 0);
6476
6477 /* 2b: Program RC6 thresholds.*/
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05306478
6479 /* WaRsDoubleRc6WrlWithCoarsePowerGating: Doubling WRL only when CPG is enabled */
Chris Wilsondc979972016-05-10 14:10:04 +01006480 if (IS_SKYLAKE(dev_priv))
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05306481 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
6482 else
6483 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
Zhe Wang20e49362014-11-04 17:07:05 +00006484 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6485 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05306486 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006487 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05306488
Dave Gordon1a3d1892016-05-13 15:36:30 +01006489 if (HAS_GUC(dev_priv))
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05306490 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
6491
Zhe Wang20e49362014-11-04 17:07:05 +00006492 I915_WRITE(GEN6_RC_SLEEP, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00006493
Zhe Wang38c23522015-01-20 12:23:04 +00006494 /* 2c: Program Coarse Power Gating Policies. */
6495 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
6496 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
6497
Zhe Wang20e49362014-11-04 17:07:05 +00006498 /* 3a: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006499 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Zhe Wang20e49362014-11-04 17:07:05 +00006500 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Jani Nikula87ad3212016-01-14 12:53:34 +02006501 DRM_INFO("RC6 %s\n", onoff(rc6_mask & GEN6_RC_CTL_RC6_ENABLE));
Chris Wilson1c044f92017-01-25 17:26:01 +00006502 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
6503 I915_WRITE(GEN6_RC_CONTROL,
6504 GEN6_RC_CTL_HW_ENABLE | GEN6_RC_CTL_EI_MODE(1) | rc6_mask);
Zhe Wang20e49362014-11-04 17:07:05 +00006505
Sagar Kamblecb07bae2015-04-12 11:28:14 +05306506 /*
6507 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05306508 * WaRsDisableCoarsePowerGating:skl,bxt - Render/Media PG need to be disabled with RC6.
Sagar Kamblecb07bae2015-04-12 11:28:14 +05306509 */
Chris Wilsondc979972016-05-10 14:10:04 +01006510 if (NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05306511 I915_WRITE(GEN9_PG_ENABLE, 0);
6512 else
6513 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
6514 (GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE) : 0);
Zhe Wang38c23522015-01-20 12:23:04 +00006515
Mika Kuoppala59bad942015-01-16 11:34:40 +02006516 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00006517}
6518
Chris Wilsondc979972016-05-10 14:10:04 +01006519static void gen8_enable_rps(struct drm_i915_private *dev_priv)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006520{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006521 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306522 enum intel_engine_id id;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006523 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006524
6525 /* 1a: Software RC state - RC0 */
6526 I915_WRITE(GEN6_RC_STATE, 0);
6527
6528 /* 1c & 1d: Get forcewake during program sequence. Although the driver
6529 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006530 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006531
6532 /* 2a: Disable RC states. */
6533 I915_WRITE(GEN6_RC_CONTROL, 0);
6534
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006535 /* 2b: Program RC6 thresholds.*/
6536 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
6537 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6538 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05306539 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006540 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006541 I915_WRITE(GEN6_RC_SLEEP, 0);
Chris Wilsondc979972016-05-10 14:10:04 +01006542 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07006543 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
6544 else
6545 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006546
6547 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006548 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006549 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Chris Wilsondc979972016-05-10 14:10:04 +01006550 intel_print_rc6_info(dev_priv, rc6_mask);
6551 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07006552 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
6553 GEN7_RC_CTL_TO_MODE |
6554 rc6_mask);
6555 else
6556 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
6557 GEN6_RC_CTL_EI_MODE(1) |
6558 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006559
6560 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07006561 I915_WRITE(GEN6_RPNSWREQ,
6562 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
6563 I915_WRITE(GEN6_RC_VIDEO_FREQ,
6564 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02006565 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
6566 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006567
Daniel Vetter7526ed72014-09-29 15:07:19 +02006568 /* Docs recommend 900MHz, and 300 MHz respectively */
6569 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
6570 dev_priv->rps.max_freq_softlimit << 24 |
6571 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006572
Daniel Vetter7526ed72014-09-29 15:07:19 +02006573 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
6574 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
6575 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
6576 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006577
Daniel Vetter7526ed72014-09-29 15:07:19 +02006578 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006579
6580 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02006581 I915_WRITE(GEN6_RP_CONTROL,
6582 GEN6_RP_MEDIA_TURBO |
6583 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6584 GEN6_RP_MEDIA_IS_GFX |
6585 GEN6_RP_ENABLE |
6586 GEN6_RP_UP_BUSY_AVG |
6587 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006588
Daniel Vetter7526ed72014-09-29 15:07:19 +02006589 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006590
Chris Wilson3a45b052016-07-13 09:10:32 +01006591 reset_rps(dev_priv, gen6_set_rps);
Daniel Vetter7526ed72014-09-29 15:07:19 +02006592
Mika Kuoppala59bad942015-01-16 11:34:40 +02006593 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006594}
6595
Chris Wilsondc979972016-05-10 14:10:04 +01006596static void gen6_enable_rps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006597{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006598 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306599 enum intel_engine_id id;
Chris Wilson99ac9612016-07-13 09:10:34 +01006600 u32 rc6vids, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006601 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006602 int rc6_mode;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00006603 int ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006604
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006605 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02006606
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006607 /* Here begins a magic sequence of register writes to enable
6608 * auto-downclocking.
6609 *
6610 * Perhaps there might be some value in exposing these to
6611 * userspace...
6612 */
6613 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006614
6615 /* Clear the DBG now so we don't confuse earlier errors */
Ville Syrjälä297b32e2016-04-13 21:09:30 +03006616 gtfifodbg = I915_READ(GTFIFODBG);
6617 if (gtfifodbg) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006618 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
6619 I915_WRITE(GTFIFODBG, gtfifodbg);
6620 }
6621
Mika Kuoppala59bad942015-01-16 11:34:40 +02006622 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006623
6624 /* disable the counters and set deterministic thresholds */
6625 I915_WRITE(GEN6_RC_CONTROL, 0);
6626
6627 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
6628 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
6629 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
6630 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
6631 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
6632
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);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006635
6636 I915_WRITE(GEN6_RC_SLEEP, 0);
6637 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Chris Wilsondc979972016-05-10 14:10:04 +01006638 if (IS_IVYBRIDGE(dev_priv))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07006639 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
6640 else
6641 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08006642 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006643 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
6644
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006645 /* Check if we are enabling RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006646 rc6_mode = intel_enable_rc6();
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006647 if (rc6_mode & INTEL_RC6_ENABLE)
6648 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
6649
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006650 /* We don't use those on Haswell */
Chris Wilsondc979972016-05-10 14:10:04 +01006651 if (!IS_HASWELL(dev_priv)) {
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006652 if (rc6_mode & INTEL_RC6p_ENABLE)
6653 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006654
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006655 if (rc6_mode & INTEL_RC6pp_ENABLE)
6656 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
6657 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006658
Chris Wilsondc979972016-05-10 14:10:04 +01006659 intel_print_rc6_info(dev_priv, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006660
6661 I915_WRITE(GEN6_RC_CONTROL,
6662 rc6_mask |
6663 GEN6_RC_CTL_EI_MODE(1) |
6664 GEN6_RC_CTL_HW_ENABLE);
6665
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006666 /* Power down if completely idle for over 50ms */
6667 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006668 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006669
Chris Wilson3a45b052016-07-13 09:10:32 +01006670 reset_rps(dev_priv, gen6_set_rps);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006671
Ben Widawsky31643d52012-09-26 10:34:01 -07006672 rc6vids = 0;
6673 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
Chris Wilsondc979972016-05-10 14:10:04 +01006674 if (IS_GEN6(dev_priv) && ret) {
Ben Widawsky31643d52012-09-26 10:34:01 -07006675 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
Chris Wilsondc979972016-05-10 14:10:04 +01006676 } else if (IS_GEN6(dev_priv) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
Ben Widawsky31643d52012-09-26 10:34:01 -07006677 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
6678 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
6679 rc6vids &= 0xffff00;
6680 rc6vids |= GEN6_ENCODE_RC6_VID(450);
6681 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
6682 if (ret)
6683 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
6684 }
6685
Mika Kuoppala59bad942015-01-16 11:34:40 +02006686 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006687}
6688
Chris Wilsonfb7404e2016-07-13 09:10:38 +01006689static void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006690{
6691 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01006692 unsigned int gpu_freq;
6693 unsigned int max_ia_freq, min_ring_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05306694 unsigned int max_gpu_freq, min_gpu_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006695 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03006696 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006697
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006698 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02006699
Ben Widawskyeda79642013-10-07 17:15:48 -03006700 policy = cpufreq_cpu_get(0);
6701 if (policy) {
6702 max_ia_freq = policy->cpuinfo.max_freq;
6703 cpufreq_cpu_put(policy);
6704 } else {
6705 /*
6706 * Default to measured freq if none found, PCU will ensure we
6707 * don't go over
6708 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006709 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03006710 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006711
6712 /* Convert from kHz to MHz */
6713 max_ia_freq /= 1000;
6714
Ben Widawsky153b4b952013-10-22 22:05:09 -07006715 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07006716 /* convert DDR frequency from units of 266.6MHz to bandwidth */
6717 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01006718
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006719 if (IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05306720 /* Convert GT frequency to 50 HZ units */
6721 min_gpu_freq = dev_priv->rps.min_freq / GEN9_FREQ_SCALER;
6722 max_gpu_freq = dev_priv->rps.max_freq / GEN9_FREQ_SCALER;
6723 } else {
6724 min_gpu_freq = dev_priv->rps.min_freq;
6725 max_gpu_freq = dev_priv->rps.max_freq;
6726 }
6727
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006728 /*
6729 * For each potential GPU frequency, load a ring frequency we'd like
6730 * to use for memory access. We do this by specifying the IA frequency
6731 * the PCU should use as a reference to determine the ring frequency.
6732 */
Akash Goel4c8c7742015-06-29 14:50:20 +05306733 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
6734 int diff = max_gpu_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01006735 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006736
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006737 if (IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05306738 /*
6739 * ring_freq = 2 * GT. ring_freq is in 100MHz units
6740 * No floor required for ring frequency on SKL.
6741 */
6742 ring_freq = gpu_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01006743 } else if (INTEL_INFO(dev_priv)->gen >= 8) {
Ben Widawsky46c764d2013-11-02 21:07:49 -07006744 /* max(2 * GT, DDR). NB: GT is 50MHz units */
6745 ring_freq = max(min_ring_freq, gpu_freq);
Chris Wilsondc979972016-05-10 14:10:04 +01006746 } else if (IS_HASWELL(dev_priv)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07006747 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01006748 ring_freq = max(min_ring_freq, ring_freq);
6749 /* leave ia_freq as the default, chosen by cpufreq */
6750 } else {
6751 /* On older processors, there is no separate ring
6752 * clock domain, so in order to boost the bandwidth
6753 * of the ring, we need to upclock the CPU (ia_freq).
6754 *
6755 * For GPU frequencies less than 750MHz,
6756 * just use the lowest ring freq.
6757 */
6758 if (gpu_freq < min_freq)
6759 ia_freq = 800;
6760 else
6761 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
6762 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
6763 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006764
Ben Widawsky42c05262012-09-26 10:34:00 -07006765 sandybridge_pcode_write(dev_priv,
6766 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01006767 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
6768 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
6769 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006770 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006771}
6772
Ville Syrjälä03af2042014-06-28 02:03:53 +03006773static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05306774{
6775 u32 val, rp0;
6776
Jani Nikula5b5929c2015-10-07 11:17:46 +03006777 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05306778
Imre Deak43b67992016-08-31 19:13:02 +03006779 switch (INTEL_INFO(dev_priv)->sseu.eu_total) {
Jani Nikula5b5929c2015-10-07 11:17:46 +03006780 case 8:
6781 /* (2 * 4) config */
6782 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
6783 break;
6784 case 12:
6785 /* (2 * 6) config */
6786 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
6787 break;
6788 case 16:
6789 /* (2 * 8) config */
6790 default:
6791 /* Setting (2 * 8) Min RP0 for any other combination */
6792 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
6793 break;
Deepak S095acd52015-01-17 11:05:59 +05306794 }
Jani Nikula5b5929c2015-10-07 11:17:46 +03006795
6796 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
6797
Deepak S2b6b3a02014-05-27 15:59:30 +05306798 return rp0;
6799}
6800
6801static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
6802{
6803 u32 val, rpe;
6804
6805 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
6806 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
6807
6808 return rpe;
6809}
6810
Deepak S7707df42014-07-12 18:46:14 +05306811static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
6812{
6813 u32 val, rp1;
6814
Jani Nikula5b5929c2015-10-07 11:17:46 +03006815 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
6816 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
6817
Deepak S7707df42014-07-12 18:46:14 +05306818 return rp1;
6819}
6820
Deepak S96676fe2016-08-12 18:46:41 +05306821static u32 cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
6822{
6823 u32 val, rpn;
6824
6825 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
6826 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
6827 FB_GFX_FREQ_FUSE_MASK);
6828
6829 return rpn;
6830}
6831
Deepak Sf8f2b002014-07-10 13:16:21 +05306832static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
6833{
6834 u32 val, rp1;
6835
6836 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
6837
6838 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
6839
6840 return rp1;
6841}
6842
Ville Syrjälä03af2042014-06-28 02:03:53 +03006843static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006844{
6845 u32 val, rp0;
6846
Jani Nikula64936252013-05-22 15:36:20 +03006847 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006848
6849 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
6850 /* Clamp to max */
6851 rp0 = min_t(u32, rp0, 0xea);
6852
6853 return rp0;
6854}
6855
6856static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
6857{
6858 u32 val, rpe;
6859
Jani Nikula64936252013-05-22 15:36:20 +03006860 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006861 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03006862 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006863 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
6864
6865 return rpe;
6866}
6867
Ville Syrjälä03af2042014-06-28 02:03:53 +03006868static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006869{
Imre Deak36146032014-12-04 18:39:35 +02006870 u32 val;
6871
6872 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
6873 /*
6874 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
6875 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
6876 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
6877 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
6878 * to make sure it matches what Punit accepts.
6879 */
6880 return max_t(u32, val, 0xc0);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006881}
6882
Imre Deakae484342014-03-31 15:10:44 +03006883/* Check that the pctx buffer wasn't move under us. */
6884static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
6885{
6886 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
6887
6888 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
6889 dev_priv->vlv_pctx->stolen->start);
6890}
6891
Deepak S38807742014-05-23 21:00:15 +05306892
6893/* Check that the pcbr address is not empty. */
6894static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
6895{
6896 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
6897
6898 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
6899}
6900
Chris Wilsondc979972016-05-10 14:10:04 +01006901static void cherryview_setup_pctx(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306902{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02006903 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006904 unsigned long pctx_paddr, paddr;
Deepak S38807742014-05-23 21:00:15 +05306905 u32 pcbr;
6906 int pctx_size = 32*1024;
6907
Deepak S38807742014-05-23 21:00:15 +05306908 pcbr = I915_READ(VLV_PCBR);
6909 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006910 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05306911 paddr = (dev_priv->mm.stolen_base +
Joonas Lahtinen62106b42016-03-18 10:42:57 +02006912 (ggtt->stolen_size - pctx_size));
Deepak S38807742014-05-23 21:00:15 +05306913
6914 pctx_paddr = (paddr & (~4095));
6915 I915_WRITE(VLV_PCBR, pctx_paddr);
6916 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006917
6918 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05306919}
6920
Chris Wilsondc979972016-05-10 14:10:04 +01006921static void valleyview_setup_pctx(struct drm_i915_private *dev_priv)
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006922{
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006923 struct drm_i915_gem_object *pctx;
6924 unsigned long pctx_paddr;
6925 u32 pcbr;
6926 int pctx_size = 24*1024;
6927
6928 pcbr = I915_READ(VLV_PCBR);
6929 if (pcbr) {
6930 /* BIOS set it up already, grab the pre-alloc'd space */
6931 int pcbr_offset;
6932
6933 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00006934 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006935 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02006936 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006937 pctx_size);
6938 goto out;
6939 }
6940
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006941 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
6942
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006943 /*
6944 * From the Gunit register HAS:
6945 * The Gfx driver is expected to program this register and ensure
6946 * proper allocation within Gfx stolen memory. For example, this
6947 * register should be programmed such than the PCBR range does not
6948 * overlap with other ranges, such as the frame buffer, protected
6949 * memory, or any other relevant ranges.
6950 */
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00006951 pctx = i915_gem_object_create_stolen(dev_priv, pctx_size);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006952 if (!pctx) {
6953 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
Tvrtko Ursulinee504892016-02-11 10:27:30 +00006954 goto out;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006955 }
6956
6957 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
6958 I915_WRITE(VLV_PCBR, pctx_paddr);
6959
6960out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006961 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006962 dev_priv->vlv_pctx = pctx;
6963}
6964
Chris Wilsondc979972016-05-10 14:10:04 +01006965static void valleyview_cleanup_pctx(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006966{
Imre Deakae484342014-03-31 15:10:44 +03006967 if (WARN_ON(!dev_priv->vlv_pctx))
6968 return;
6969
Chris Wilsonf0cd5182016-10-28 13:58:43 +01006970 i915_gem_object_put(dev_priv->vlv_pctx);
Imre Deakae484342014-03-31 15:10:44 +03006971 dev_priv->vlv_pctx = NULL;
6972}
6973
Ville Syrjäläc30fec62016-03-04 21:43:02 +02006974static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
6975{
6976 dev_priv->rps.gpll_ref_freq =
6977 vlv_get_cck_clock(dev_priv, "GPLL ref",
6978 CCK_GPLL_CLOCK_CONTROL,
6979 dev_priv->czclk_freq);
6980
6981 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
6982 dev_priv->rps.gpll_ref_freq);
6983}
6984
Chris Wilsondc979972016-05-10 14:10:04 +01006985static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03006986{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006987 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03006988
Chris Wilsondc979972016-05-10 14:10:04 +01006989 valleyview_setup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03006990
Ville Syrjäläc30fec62016-03-04 21:43:02 +02006991 vlv_init_gpll_ref_freq(dev_priv);
6992
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006993 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
6994 switch ((val >> 6) & 3) {
6995 case 0:
6996 case 1:
6997 dev_priv->mem_freq = 800;
6998 break;
6999 case 2:
7000 dev_priv->mem_freq = 1066;
7001 break;
7002 case 3:
7003 dev_priv->mem_freq = 1333;
7004 break;
7005 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02007006 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007007
Imre Deak4e805192014-04-14 20:24:41 +03007008 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
7009 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
7010 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007011 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03007012 dev_priv->rps.max_freq);
7013
7014 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
7015 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007016 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03007017 dev_priv->rps.efficient_freq);
7018
Deepak Sf8f2b002014-07-10 13:16:21 +05307019 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
7020 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007021 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05307022 dev_priv->rps.rp1_freq);
7023
Imre Deak4e805192014-04-14 20:24:41 +03007024 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
7025 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007026 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03007027 dev_priv->rps.min_freq);
Imre Deak4e805192014-04-14 20:24:41 +03007028}
7029
Chris Wilsondc979972016-05-10 14:10:04 +01007030static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05307031{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007032 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05307033
Chris Wilsondc979972016-05-10 14:10:04 +01007034 cherryview_setup_pctx(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307035
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007036 vlv_init_gpll_ref_freq(dev_priv);
7037
Ville Syrjäläa5805162015-05-26 20:42:30 +03007038 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02007039 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007040 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02007041
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007042 switch ((val >> 2) & 0x7) {
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007043 case 3:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007044 dev_priv->mem_freq = 2000;
7045 break;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03007046 default:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007047 dev_priv->mem_freq = 1600;
7048 break;
7049 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02007050 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007051
Deepak S2b6b3a02014-05-27 15:59:30 +05307052 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
7053 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
7054 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007055 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05307056 dev_priv->rps.max_freq);
7057
7058 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
7059 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007060 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05307061 dev_priv->rps.efficient_freq);
7062
Deepak S7707df42014-07-12 18:46:14 +05307063 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
7064 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007065 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05307066 dev_priv->rps.rp1_freq);
7067
Deepak S96676fe2016-08-12 18:46:41 +05307068 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307069 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007070 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05307071 dev_priv->rps.min_freq);
7072
Ville Syrjälä1c147622014-08-18 14:42:43 +03007073 WARN_ONCE((dev_priv->rps.max_freq |
7074 dev_priv->rps.efficient_freq |
7075 dev_priv->rps.rp1_freq |
7076 dev_priv->rps.min_freq) & 1,
7077 "Odd GPU freq values\n");
Deepak S38807742014-05-23 21:00:15 +05307078}
7079
Chris Wilsondc979972016-05-10 14:10:04 +01007080static void valleyview_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03007081{
Chris Wilsondc979972016-05-10 14:10:04 +01007082 valleyview_cleanup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007083}
7084
Chris Wilsondc979972016-05-10 14:10:04 +01007085static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05307086{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007087 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05307088 enum intel_engine_id id;
Deepak S2b6b3a02014-05-27 15:59:30 +05307089 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05307090
7091 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
7092
Ville Syrjälä297b32e2016-04-13 21:09:30 +03007093 gtfifodbg = I915_READ(GTFIFODBG) & ~(GT_FIFO_SBDEDICATE_FREE_ENTRY_CHV |
7094 GT_FIFO_FREE_ENTRIES_CHV);
Deepak S38807742014-05-23 21:00:15 +05307095 if (gtfifodbg) {
7096 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
7097 gtfifodbg);
7098 I915_WRITE(GTFIFODBG, gtfifodbg);
7099 }
7100
7101 cherryview_check_pctx(dev_priv);
7102
7103 /* 1a & 1b: Get forcewake during program sequence. Although the driver
7104 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02007105 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05307106
Ville Syrjälä160614a2015-01-19 13:50:47 +02007107 /* Disable RC states. */
7108 I915_WRITE(GEN6_RC_CONTROL, 0);
7109
Deepak S38807742014-05-23 21:00:15 +05307110 /* 2a: Program RC6 thresholds.*/
7111 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
7112 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
7113 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
7114
Akash Goel3b3f1652016-10-13 22:44:48 +05307115 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007116 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Deepak S38807742014-05-23 21:00:15 +05307117 I915_WRITE(GEN6_RC_SLEEP, 0);
7118
Deepak Sf4f71c72015-03-28 15:23:35 +05307119 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
7120 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
Deepak S38807742014-05-23 21:00:15 +05307121
7122 /* allows RC6 residency counter to work */
7123 I915_WRITE(VLV_COUNTER_CONTROL,
7124 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
7125 VLV_MEDIA_RC6_COUNT_EN |
7126 VLV_RENDER_RC6_COUNT_EN));
7127
7128 /* For now we assume BIOS is allocating and populating the PCBR */
7129 pcbr = I915_READ(VLV_PCBR);
7130
Deepak S38807742014-05-23 21:00:15 +05307131 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01007132 if ((intel_enable_rc6() & INTEL_RC6_ENABLE) &&
7133 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02007134 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05307135
7136 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
7137
Deepak S2b6b3a02014-05-27 15:59:30 +05307138 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02007139 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05307140 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
7141 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
7142 I915_WRITE(GEN6_RP_UP_EI, 66000);
7143 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
7144
7145 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7146
7147 /* 5: Enable RPS */
7148 I915_WRITE(GEN6_RP_CONTROL,
7149 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02007150 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05307151 GEN6_RP_ENABLE |
7152 GEN6_RP_UP_BUSY_AVG |
7153 GEN6_RP_DOWN_IDLE_AVG);
7154
Deepak S3ef62342015-04-29 08:36:24 +05307155 /* Setting Fixed Bias */
7156 val = VLV_OVERRIDE_EN |
7157 VLV_SOC_TDP_EN |
7158 CHV_BIAS_CPU_50_SOC_50;
7159 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
7160
Deepak S2b6b3a02014-05-27 15:59:30 +05307161 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
7162
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02007163 /* RPS code assumes GPLL is used */
7164 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
7165
Jani Nikula742f4912015-09-03 11:16:09 +03007166 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05307167 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7168
Chris Wilson3a45b052016-07-13 09:10:32 +01007169 reset_rps(dev_priv, valleyview_set_rps);
Deepak S2b6b3a02014-05-27 15:59:30 +05307170
Mika Kuoppala59bad942015-01-16 11:34:40 +02007171 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05307172}
7173
Chris Wilsondc979972016-05-10 14:10:04 +01007174static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07007175{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007176 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05307177 enum intel_engine_id id;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07007178 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07007179
7180 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
7181
Imre Deakae484342014-03-31 15:10:44 +03007182 valleyview_check_pctx(dev_priv);
7183
Ville Syrjälä297b32e2016-04-13 21:09:30 +03007184 gtfifodbg = I915_READ(GTFIFODBG);
7185 if (gtfifodbg) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07007186 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
7187 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007188 I915_WRITE(GTFIFODBG, gtfifodbg);
7189 }
7190
Deepak Sc8d9a592013-11-23 14:55:42 +05307191 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02007192 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007193
Ville Syrjälä160614a2015-01-19 13:50:47 +02007194 /* Disable RC states. */
7195 I915_WRITE(GEN6_RC_CONTROL, 0);
7196
Ville Syrjäläcad725f2015-01-19 13:50:48 +02007197 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007198 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
7199 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
7200 I915_WRITE(GEN6_RP_UP_EI, 66000);
7201 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
7202
7203 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7204
7205 I915_WRITE(GEN6_RP_CONTROL,
7206 GEN6_RP_MEDIA_TURBO |
7207 GEN6_RP_MEDIA_HW_NORMAL_MODE |
7208 GEN6_RP_MEDIA_IS_GFX |
7209 GEN6_RP_ENABLE |
7210 GEN6_RP_UP_BUSY_AVG |
7211 GEN6_RP_DOWN_IDLE_CONT);
7212
7213 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
7214 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
7215 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
7216
Akash Goel3b3f1652016-10-13 22:44:48 +05307217 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007218 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007219
Jesse Barnes2f0aa302013-11-15 09:32:11 -08007220 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007221
7222 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07007223 I915_WRITE(VLV_COUNTER_CONTROL,
Mika Kuoppala6b7f6aa2017-03-15 18:12:59 +02007224 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
7225 VLV_MEDIA_RC0_COUNT_EN |
Deepak S31685c22014-07-03 17:33:01 -04007226 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07007227 VLV_MEDIA_RC6_COUNT_EN |
7228 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04007229
Chris Wilsondc979972016-05-10 14:10:04 +01007230 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08007231 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07007232
Chris Wilsondc979972016-05-10 14:10:04 +01007233 intel_print_rc6_info(dev_priv, rc6_mode);
Ben Widawskydc39fff2013-10-18 12:32:07 -07007234
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07007235 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007236
Deepak S3ef62342015-04-29 08:36:24 +05307237 /* Setting Fixed Bias */
7238 val = VLV_OVERRIDE_EN |
7239 VLV_SOC_TDP_EN |
7240 VLV_BIAS_CPU_125_SOC_875;
7241 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
7242
Jani Nikula64936252013-05-22 15:36:20 +03007243 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007244
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02007245 /* RPS code assumes GPLL is used */
7246 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
7247
Jani Nikula742f4912015-09-03 11:16:09 +03007248 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07007249 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7250
Chris Wilson3a45b052016-07-13 09:10:32 +01007251 reset_rps(dev_priv, valleyview_set_rps);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007252
Mika Kuoppala59bad942015-01-16 11:34:40 +02007253 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007254}
7255
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007256static unsigned long intel_pxfreq(u32 vidfreq)
7257{
7258 unsigned long freq;
7259 int div = (vidfreq & 0x3f0000) >> 16;
7260 int post = (vidfreq & 0x3000) >> 12;
7261 int pre = (vidfreq & 0x7);
7262
7263 if (!pre)
7264 return 0;
7265
7266 freq = ((div * 133333) / ((1<<post) * pre));
7267
7268 return freq;
7269}
7270
Daniel Vettereb48eb02012-04-26 23:28:12 +02007271static const struct cparams {
7272 u16 i;
7273 u16 t;
7274 u16 m;
7275 u16 c;
7276} cparams[] = {
7277 { 1, 1333, 301, 28664 },
7278 { 1, 1066, 294, 24460 },
7279 { 1, 800, 294, 25192 },
7280 { 0, 1333, 276, 27605 },
7281 { 0, 1066, 276, 27605 },
7282 { 0, 800, 231, 23784 },
7283};
7284
Chris Wilsonf531dcb2012-09-25 10:16:12 +01007285static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007286{
7287 u64 total_count, diff, ret;
7288 u32 count1, count2, count3, m = 0, c = 0;
7289 unsigned long now = jiffies_to_msecs(jiffies), diff1;
7290 int i;
7291
Chris Wilson67520412017-03-02 13:28:01 +00007292 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007293
Daniel Vetter20e4d402012-08-08 23:35:39 +02007294 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007295
7296 /* Prevent division-by-zero if we are asking too fast.
7297 * Also, we don't get interesting results if we are polling
7298 * faster than once in 10ms, so just return the saved value
7299 * in such cases.
7300 */
7301 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02007302 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007303
7304 count1 = I915_READ(DMIEC);
7305 count2 = I915_READ(DDREC);
7306 count3 = I915_READ(CSIEC);
7307
7308 total_count = count1 + count2 + count3;
7309
7310 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02007311 if (total_count < dev_priv->ips.last_count1) {
7312 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007313 diff += total_count;
7314 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007315 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007316 }
7317
7318 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007319 if (cparams[i].i == dev_priv->ips.c_m &&
7320 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02007321 m = cparams[i].m;
7322 c = cparams[i].c;
7323 break;
7324 }
7325 }
7326
7327 diff = div_u64(diff, diff1);
7328 ret = ((m * diff) + c);
7329 ret = div_u64(ret, 10);
7330
Daniel Vetter20e4d402012-08-08 23:35:39 +02007331 dev_priv->ips.last_count1 = total_count;
7332 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007333
Daniel Vetter20e4d402012-08-08 23:35:39 +02007334 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007335
7336 return ret;
7337}
7338
Chris Wilsonf531dcb2012-09-25 10:16:12 +01007339unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
7340{
7341 unsigned long val;
7342
Chris Wilsondc979972016-05-10 14:10:04 +01007343 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01007344 return 0;
7345
7346 spin_lock_irq(&mchdev_lock);
7347
7348 val = __i915_chipset_val(dev_priv);
7349
7350 spin_unlock_irq(&mchdev_lock);
7351
7352 return val;
7353}
7354
Daniel Vettereb48eb02012-04-26 23:28:12 +02007355unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
7356{
7357 unsigned long m, x, b;
7358 u32 tsfs;
7359
7360 tsfs = I915_READ(TSFS);
7361
7362 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
7363 x = I915_READ8(TR1);
7364
7365 b = tsfs & TSFS_INTR_MASK;
7366
7367 return ((m * x) / 127) - b;
7368}
7369
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007370static int _pxvid_to_vd(u8 pxvid)
7371{
7372 if (pxvid == 0)
7373 return 0;
7374
7375 if (pxvid >= 8 && pxvid < 31)
7376 pxvid = 31;
7377
7378 return (pxvid + 2) * 125;
7379}
7380
7381static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007382{
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007383 const int vd = _pxvid_to_vd(pxvid);
7384 const int vm = vd - 1125;
7385
Chris Wilsondc979972016-05-10 14:10:04 +01007386 if (INTEL_INFO(dev_priv)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007387 return vm > 0 ? vm : 0;
7388
7389 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007390}
7391
Daniel Vetter02d71952012-08-09 16:44:54 +02007392static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007393{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00007394 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007395 u32 count;
7396
Chris Wilson67520412017-03-02 13:28:01 +00007397 lockdep_assert_held(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007398
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00007399 now = ktime_get_raw_ns();
7400 diffms = now - dev_priv->ips.last_time2;
7401 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007402
7403 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02007404 if (!diffms)
7405 return;
7406
7407 count = I915_READ(GFXEC);
7408
Daniel Vetter20e4d402012-08-08 23:35:39 +02007409 if (count < dev_priv->ips.last_count2) {
7410 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007411 diff += count;
7412 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007413 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007414 }
7415
Daniel Vetter20e4d402012-08-08 23:35:39 +02007416 dev_priv->ips.last_count2 = count;
7417 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007418
7419 /* More magic constants... */
7420 diff = diff * 1181;
7421 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02007422 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007423}
7424
Daniel Vetter02d71952012-08-09 16:44:54 +02007425void i915_update_gfx_val(struct drm_i915_private *dev_priv)
7426{
Chris Wilsondc979972016-05-10 14:10:04 +01007427 if (INTEL_INFO(dev_priv)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02007428 return;
7429
Daniel Vetter92703882012-08-09 16:46:01 +02007430 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007431
7432 __i915_update_gfx_val(dev_priv);
7433
Daniel Vetter92703882012-08-09 16:46:01 +02007434 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007435}
7436
Chris Wilsonf531dcb2012-09-25 10:16:12 +01007437static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007438{
7439 unsigned long t, corr, state1, corr2, state2;
7440 u32 pxvid, ext_v;
7441
Chris Wilson67520412017-03-02 13:28:01 +00007442 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007443
Ville Syrjälä616847e2015-09-18 20:03:19 +03007444 pxvid = I915_READ(PXVFREQ(dev_priv->rps.cur_freq));
Daniel Vettereb48eb02012-04-26 23:28:12 +02007445 pxvid = (pxvid >> 24) & 0x7f;
7446 ext_v = pvid_to_extvid(dev_priv, pxvid);
7447
7448 state1 = ext_v;
7449
7450 t = i915_mch_val(dev_priv);
7451
7452 /* Revel in the empirically derived constants */
7453
7454 /* Correction factor in 1/100000 units */
7455 if (t > 80)
7456 corr = ((t * 2349) + 135940);
7457 else if (t >= 50)
7458 corr = ((t * 964) + 29317);
7459 else /* < 50 */
7460 corr = ((t * 301) + 1004);
7461
7462 corr = corr * ((150142 * state1) / 10000 - 78642);
7463 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02007464 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007465
7466 state2 = (corr2 * state1) / 10000;
7467 state2 /= 100; /* convert to mW */
7468
Daniel Vetter02d71952012-08-09 16:44:54 +02007469 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007470
Daniel Vetter20e4d402012-08-08 23:35:39 +02007471 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007472}
7473
Chris Wilsonf531dcb2012-09-25 10:16:12 +01007474unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
7475{
7476 unsigned long val;
7477
Chris Wilsondc979972016-05-10 14:10:04 +01007478 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01007479 return 0;
7480
7481 spin_lock_irq(&mchdev_lock);
7482
7483 val = __i915_gfx_val(dev_priv);
7484
7485 spin_unlock_irq(&mchdev_lock);
7486
7487 return val;
7488}
7489
Daniel Vettereb48eb02012-04-26 23:28:12 +02007490/**
7491 * i915_read_mch_val - return value for IPS use
7492 *
7493 * Calculate and return a value for the IPS driver to use when deciding whether
7494 * we have thermal and power headroom to increase CPU or GPU power budget.
7495 */
7496unsigned long i915_read_mch_val(void)
7497{
7498 struct drm_i915_private *dev_priv;
7499 unsigned long chipset_val, graphics_val, ret = 0;
7500
Daniel Vetter92703882012-08-09 16:46:01 +02007501 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007502 if (!i915_mch_dev)
7503 goto out_unlock;
7504 dev_priv = i915_mch_dev;
7505
Chris Wilsonf531dcb2012-09-25 10:16:12 +01007506 chipset_val = __i915_chipset_val(dev_priv);
7507 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007508
7509 ret = chipset_val + graphics_val;
7510
7511out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007512 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007513
7514 return ret;
7515}
7516EXPORT_SYMBOL_GPL(i915_read_mch_val);
7517
7518/**
7519 * i915_gpu_raise - raise GPU frequency limit
7520 *
7521 * Raise the limit; IPS indicates we have thermal headroom.
7522 */
7523bool i915_gpu_raise(void)
7524{
7525 struct drm_i915_private *dev_priv;
7526 bool ret = true;
7527
Daniel Vetter92703882012-08-09 16:46:01 +02007528 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007529 if (!i915_mch_dev) {
7530 ret = false;
7531 goto out_unlock;
7532 }
7533 dev_priv = i915_mch_dev;
7534
Daniel Vetter20e4d402012-08-08 23:35:39 +02007535 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
7536 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007537
7538out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007539 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007540
7541 return ret;
7542}
7543EXPORT_SYMBOL_GPL(i915_gpu_raise);
7544
7545/**
7546 * i915_gpu_lower - lower GPU frequency limit
7547 *
7548 * IPS indicates we're close to a thermal limit, so throttle back the GPU
7549 * frequency maximum.
7550 */
7551bool i915_gpu_lower(void)
7552{
7553 struct drm_i915_private *dev_priv;
7554 bool ret = true;
7555
Daniel Vetter92703882012-08-09 16:46:01 +02007556 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007557 if (!i915_mch_dev) {
7558 ret = false;
7559 goto out_unlock;
7560 }
7561 dev_priv = i915_mch_dev;
7562
Daniel Vetter20e4d402012-08-08 23:35:39 +02007563 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
7564 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007565
7566out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007567 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007568
7569 return ret;
7570}
7571EXPORT_SYMBOL_GPL(i915_gpu_lower);
7572
7573/**
7574 * i915_gpu_busy - indicate GPU business to IPS
7575 *
7576 * Tell the IPS driver whether or not the GPU is busy.
7577 */
7578bool i915_gpu_busy(void)
7579{
Daniel Vettereb48eb02012-04-26 23:28:12 +02007580 bool ret = false;
7581
Daniel Vetter92703882012-08-09 16:46:01 +02007582 spin_lock_irq(&mchdev_lock);
Chris Wilsondcff85c2016-08-05 10:14:11 +01007583 if (i915_mch_dev)
7584 ret = i915_mch_dev->gt.awake;
Daniel Vetter92703882012-08-09 16:46:01 +02007585 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007586
7587 return ret;
7588}
7589EXPORT_SYMBOL_GPL(i915_gpu_busy);
7590
7591/**
7592 * i915_gpu_turbo_disable - disable graphics turbo
7593 *
7594 * Disable graphics turbo by resetting the max frequency and setting the
7595 * current frequency to the default.
7596 */
7597bool i915_gpu_turbo_disable(void)
7598{
7599 struct drm_i915_private *dev_priv;
7600 bool ret = true;
7601
Daniel Vetter92703882012-08-09 16:46:01 +02007602 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007603 if (!i915_mch_dev) {
7604 ret = false;
7605 goto out_unlock;
7606 }
7607 dev_priv = i915_mch_dev;
7608
Daniel Vetter20e4d402012-08-08 23:35:39 +02007609 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007610
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01007611 if (!ironlake_set_drps(dev_priv, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02007612 ret = false;
7613
7614out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007615 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007616
7617 return ret;
7618}
7619EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
7620
7621/**
7622 * Tells the intel_ips driver that the i915 driver is now loaded, if
7623 * IPS got loaded first.
7624 *
7625 * This awkward dance is so that neither module has to depend on the
7626 * other in order for IPS to do the appropriate communication of
7627 * GPU turbo limits to i915.
7628 */
7629static void
7630ips_ping_for_i915_load(void)
7631{
7632 void (*link)(void);
7633
7634 link = symbol_get(ips_link_to_i915_driver);
7635 if (link) {
7636 link();
7637 symbol_put(ips_link_to_i915_driver);
7638 }
7639}
7640
7641void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
7642{
Daniel Vetter02d71952012-08-09 16:44:54 +02007643 /* We only register the i915 ips part with intel-ips once everything is
7644 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02007645 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007646 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02007647 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007648
7649 ips_ping_for_i915_load();
7650}
7651
7652void intel_gpu_ips_teardown(void)
7653{
Daniel Vetter92703882012-08-09 16:46:01 +02007654 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007655 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02007656 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007657}
Deepak S76c3552f2014-01-30 23:08:16 +05307658
Chris Wilsondc979972016-05-10 14:10:04 +01007659static void intel_init_emon(struct drm_i915_private *dev_priv)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007660{
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007661 u32 lcfuse;
7662 u8 pxw[16];
7663 int i;
7664
7665 /* Disable to program */
7666 I915_WRITE(ECR, 0);
7667 POSTING_READ(ECR);
7668
7669 /* Program energy weights for various events */
7670 I915_WRITE(SDEW, 0x15040d00);
7671 I915_WRITE(CSIEW0, 0x007f0000);
7672 I915_WRITE(CSIEW1, 0x1e220004);
7673 I915_WRITE(CSIEW2, 0x04000004);
7674
7675 for (i = 0; i < 5; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007676 I915_WRITE(PEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007677 for (i = 0; i < 3; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007678 I915_WRITE(DEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007679
7680 /* Program P-state weights to account for frequency power adjustment */
7681 for (i = 0; i < 16; i++) {
Ville Syrjälä616847e2015-09-18 20:03:19 +03007682 u32 pxvidfreq = I915_READ(PXVFREQ(i));
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007683 unsigned long freq = intel_pxfreq(pxvidfreq);
7684 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
7685 PXVFREQ_PX_SHIFT;
7686 unsigned long val;
7687
7688 val = vid * vid;
7689 val *= (freq / 1000);
7690 val *= 255;
7691 val /= (127*127*900);
7692 if (val > 0xff)
7693 DRM_ERROR("bad pxval: %ld\n", val);
7694 pxw[i] = val;
7695 }
7696 /* Render standby states get 0 weight */
7697 pxw[14] = 0;
7698 pxw[15] = 0;
7699
7700 for (i = 0; i < 4; i++) {
7701 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
7702 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
Ville Syrjälä616847e2015-09-18 20:03:19 +03007703 I915_WRITE(PXW(i), val);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007704 }
7705
7706 /* Adjust magic regs to magic values (more experimental results) */
7707 I915_WRITE(OGW0, 0);
7708 I915_WRITE(OGW1, 0);
7709 I915_WRITE(EG0, 0x00007f00);
7710 I915_WRITE(EG1, 0x0000000e);
7711 I915_WRITE(EG2, 0x000e0000);
7712 I915_WRITE(EG3, 0x68000300);
7713 I915_WRITE(EG4, 0x42000000);
7714 I915_WRITE(EG5, 0x00140031);
7715 I915_WRITE(EG6, 0);
7716 I915_WRITE(EG7, 0);
7717
7718 for (i = 0; i < 8; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007719 I915_WRITE(PXWL(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007720
7721 /* Enable PMON + select events */
7722 I915_WRITE(ECR, 0x80000019);
7723
7724 lcfuse = I915_READ(LCFUSE02);
7725
Daniel Vetter20e4d402012-08-08 23:35:39 +02007726 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007727}
7728
Chris Wilsondc979972016-05-10 14:10:04 +01007729void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007730{
Imre Deakb268c692015-12-15 20:10:31 +02007731 /*
7732 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
7733 * requirement.
7734 */
7735 if (!i915.enable_rc6) {
7736 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
7737 intel_runtime_pm_get(dev_priv);
7738 }
Imre Deake6069ca2014-04-18 16:01:02 +03007739
Chris Wilsonb5163db2016-08-10 13:58:24 +01007740 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson773ea9a2016-07-13 09:10:33 +01007741 mutex_lock(&dev_priv->rps.hw_lock);
7742
7743 /* Initialize RPS limits (for userspace) */
Chris Wilsondc979972016-05-10 14:10:04 +01007744 if (IS_CHERRYVIEW(dev_priv))
7745 cherryview_init_gt_powersave(dev_priv);
7746 else if (IS_VALLEYVIEW(dev_priv))
7747 valleyview_init_gt_powersave(dev_priv);
Chris Wilson2a13ae72016-08-02 11:15:27 +01007748 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson773ea9a2016-07-13 09:10:33 +01007749 gen6_init_rps_frequencies(dev_priv);
7750
7751 /* Derive initial user preferences/limits from the hardware limits */
7752 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
7753 dev_priv->rps.cur_freq = dev_priv->rps.idle_freq;
7754
7755 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
7756 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
7757
7758 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
7759 dev_priv->rps.min_freq_softlimit =
7760 max_t(int,
7761 dev_priv->rps.efficient_freq,
7762 intel_freq_opcode(dev_priv, 450));
7763
Chris Wilson99ac9612016-07-13 09:10:34 +01007764 /* After setting max-softlimit, find the overclock max freq */
7765 if (IS_GEN6(dev_priv) ||
7766 IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
7767 u32 params = 0;
7768
7769 sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &params);
7770 if (params & BIT(31)) { /* OC supported */
7771 DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
7772 (dev_priv->rps.max_freq & 0xff) * 50,
7773 (params & 0xff) * 50);
7774 dev_priv->rps.max_freq = params & 0xff;
7775 }
7776 }
7777
Chris Wilson29ecd78d2016-07-13 09:10:35 +01007778 /* Finally allow us to boost to max by default */
7779 dev_priv->rps.boost_freq = dev_priv->rps.max_freq;
7780
Chris Wilson773ea9a2016-07-13 09:10:33 +01007781 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb5163db2016-08-10 13:58:24 +01007782 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson54b4f682016-07-21 21:16:19 +01007783
7784 intel_autoenable_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03007785}
7786
Chris Wilsondc979972016-05-10 14:10:04 +01007787void intel_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007788{
Ville Syrjälä8dac1e12016-08-02 14:07:33 +03007789 if (IS_VALLEYVIEW(dev_priv))
Chris Wilsondc979972016-05-10 14:10:04 +01007790 valleyview_cleanup_gt_powersave(dev_priv);
Imre Deakb268c692015-12-15 20:10:31 +02007791
7792 if (!i915.enable_rc6)
7793 intel_runtime_pm_put(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03007794}
7795
Chris Wilson54b4f682016-07-21 21:16:19 +01007796/**
7797 * intel_suspend_gt_powersave - suspend PM work and helper threads
7798 * @dev_priv: i915 device
7799 *
7800 * We don't want to disable RC6 or other features here, we just want
7801 * to make sure any work we've queued has finished and won't bother
7802 * us while we're suspended.
7803 */
7804void intel_suspend_gt_powersave(struct drm_i915_private *dev_priv)
7805{
7806 if (INTEL_GEN(dev_priv) < 6)
7807 return;
7808
7809 if (cancel_delayed_work_sync(&dev_priv->rps.autoenable_work))
7810 intel_runtime_pm_put(dev_priv);
7811
7812 /* gen6_rps_idle() will be called later to disable interrupts */
7813}
7814
Chris Wilsonb7137e02016-07-13 09:10:37 +01007815void intel_sanitize_gt_powersave(struct drm_i915_private *dev_priv)
7816{
7817 dev_priv->rps.enabled = true; /* force disabling */
7818 intel_disable_gt_powersave(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007819
7820 gen6_reset_rps_interrupts(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07007821}
7822
Chris Wilsondc979972016-05-10 14:10:04 +01007823void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
Daniel Vetter8090c6b2012-06-24 16:42:32 +02007824{
Chris Wilsonb7137e02016-07-13 09:10:37 +01007825 if (!READ_ONCE(dev_priv->rps.enabled))
7826 return;
Jesse Barnes1a01ab32012-11-02 11:14:00 -07007827
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007828 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007829
Chris Wilsonb7137e02016-07-13 09:10:37 +01007830 if (INTEL_GEN(dev_priv) >= 9) {
7831 gen9_disable_rc6(dev_priv);
7832 gen9_disable_rps(dev_priv);
7833 } else if (IS_CHERRYVIEW(dev_priv)) {
7834 cherryview_disable_rps(dev_priv);
7835 } else if (IS_VALLEYVIEW(dev_priv)) {
7836 valleyview_disable_rps(dev_priv);
7837 } else if (INTEL_GEN(dev_priv) >= 6) {
7838 gen6_disable_rps(dev_priv);
7839 } else if (IS_IRONLAKE_M(dev_priv)) {
7840 ironlake_disable_drps(dev_priv);
7841 }
7842
7843 dev_priv->rps.enabled = false;
7844 mutex_unlock(&dev_priv->rps.hw_lock);
7845}
7846
7847void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
7848{
Chris Wilson54b4f682016-07-21 21:16:19 +01007849 /* We shouldn't be disabling as we submit, so this should be less
7850 * racy than it appears!
7851 */
Chris Wilsonb7137e02016-07-13 09:10:37 +01007852 if (READ_ONCE(dev_priv->rps.enabled))
7853 return;
7854
7855 /* Powersaving is controlled by the host when inside a VM */
7856 if (intel_vgpu_active(dev_priv))
7857 return;
7858
7859 mutex_lock(&dev_priv->rps.hw_lock);
Imre Deak3cc134e2014-11-19 15:30:03 +02007860
Chris Wilsondc979972016-05-10 14:10:04 +01007861 if (IS_CHERRYVIEW(dev_priv)) {
7862 cherryview_enable_rps(dev_priv);
7863 } else if (IS_VALLEYVIEW(dev_priv)) {
7864 valleyview_enable_rps(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007865 } else if (INTEL_GEN(dev_priv) >= 9) {
Chris Wilsondc979972016-05-10 14:10:04 +01007866 gen9_enable_rc6(dev_priv);
7867 gen9_enable_rps(dev_priv);
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07007868 if (IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv))
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007869 gen6_update_ring_freq(dev_priv);
Chris Wilsondc979972016-05-10 14:10:04 +01007870 } else if (IS_BROADWELL(dev_priv)) {
7871 gen8_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007872 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007873 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilsondc979972016-05-10 14:10:04 +01007874 gen6_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007875 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007876 } else if (IS_IRONLAKE_M(dev_priv)) {
7877 ironlake_enable_drps(dev_priv);
7878 intel_init_emon(dev_priv);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007879 }
Chris Wilsonaed242f2015-03-18 09:48:21 +00007880
7881 WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
7882 WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
7883
7884 WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
7885 WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
7886
Chris Wilson54b4f682016-07-21 21:16:19 +01007887 dev_priv->rps.enabled = true;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007888 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007889}
Imre Deakc6df39b2014-04-14 20:24:29 +03007890
Chris Wilson54b4f682016-07-21 21:16:19 +01007891static void __intel_autoenable_gt_powersave(struct work_struct *work)
7892{
7893 struct drm_i915_private *dev_priv =
7894 container_of(work, typeof(*dev_priv), rps.autoenable_work.work);
7895 struct intel_engine_cs *rcs;
7896 struct drm_i915_gem_request *req;
7897
7898 if (READ_ONCE(dev_priv->rps.enabled))
7899 goto out;
7900
Akash Goel3b3f1652016-10-13 22:44:48 +05307901 rcs = dev_priv->engine[RCS];
Chris Wilsone8a9c582016-12-18 15:37:20 +00007902 if (rcs->last_retired_context)
Chris Wilson54b4f682016-07-21 21:16:19 +01007903 goto out;
7904
7905 if (!rcs->init_context)
7906 goto out;
7907
7908 mutex_lock(&dev_priv->drm.struct_mutex);
7909
7910 req = i915_gem_request_alloc(rcs, dev_priv->kernel_context);
7911 if (IS_ERR(req))
7912 goto unlock;
7913
7914 if (!i915.enable_execlists && i915_switch_context(req) == 0)
7915 rcs->init_context(req);
7916
7917 /* Mark the device busy, calling intel_enable_gt_powersave() */
Chris Wilsone642c852017-03-17 11:47:09 +00007918 i915_add_request(req);
Chris Wilson54b4f682016-07-21 21:16:19 +01007919
7920unlock:
7921 mutex_unlock(&dev_priv->drm.struct_mutex);
7922out:
7923 intel_runtime_pm_put(dev_priv);
7924}
7925
7926void intel_autoenable_gt_powersave(struct drm_i915_private *dev_priv)
7927{
7928 if (READ_ONCE(dev_priv->rps.enabled))
7929 return;
7930
7931 if (IS_IRONLAKE_M(dev_priv)) {
7932 ironlake_enable_drps(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007933 intel_init_emon(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007934 } else if (INTEL_INFO(dev_priv)->gen >= 6) {
7935 /*
7936 * PCU communication is slow and this doesn't need to be
7937 * done at any specific time, so do this out of our fast path
7938 * to make resume and init faster.
7939 *
7940 * We depend on the HW RC6 power context save/restore
7941 * mechanism when entering D3 through runtime PM suspend. So
7942 * disable RPM until RPS/RC6 is properly setup. We can only
7943 * get here via the driver load/system resume/runtime resume
7944 * paths, so the _noresume version is enough (and in case of
7945 * runtime resume it's necessary).
7946 */
7947 if (queue_delayed_work(dev_priv->wq,
7948 &dev_priv->rps.autoenable_work,
7949 round_jiffies_up_relative(HZ)))
7950 intel_runtime_pm_get_noresume(dev_priv);
7951 }
7952}
7953
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007954static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007955{
Daniel Vetter3107bd42012-10-31 22:52:31 +01007956 /*
7957 * On Ibex Peak and Cougar Point, we need to disable clock
7958 * gating for the panel power sequencer or it will fail to
7959 * start up when no ports are active.
7960 */
7961 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
7962}
7963
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007964static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007965{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007966 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007967
Damien Lespiau055e3932014-08-18 13:49:10 +01007968 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007969 I915_WRITE(DSPCNTR(pipe),
7970 I915_READ(DSPCNTR(pipe)) |
7971 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007972
7973 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
7974 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007975 }
7976}
7977
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007978static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä017636c2013-12-05 15:51:37 +02007979{
Ville Syrjälä017636c2013-12-05 15:51:37 +02007980 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
7981 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
7982 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
7983
7984 /*
7985 * Don't touch WM1S_LP_EN here.
7986 * Doing so could cause underruns.
7987 */
7988}
7989
Rodrigo Vivi91200c02017-08-28 22:20:26 -07007990static void ilk_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007991{
Damien Lespiau231e54f2012-10-19 17:55:41 +01007992 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007993
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01007994 /*
7995 * Required for FBC
7996 * WaFbcDisableDpfcClockGating:ilk
7997 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007998 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
7999 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
8000 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008001
8002 I915_WRITE(PCH_3DCGDIS0,
8003 MARIUNIT_CLOCK_GATE_DISABLE |
8004 SVSMUNIT_CLOCK_GATE_DISABLE);
8005 I915_WRITE(PCH_3DCGDIS1,
8006 VFMUNIT_CLOCK_GATE_DISABLE);
8007
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008008 /*
8009 * According to the spec the following bits should be set in
8010 * order to enable memory self-refresh
8011 * The bit 22/21 of 0x42004
8012 * The bit 5 of 0x42020
8013 * The bit 15 of 0x45000
8014 */
8015 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8016 (I915_READ(ILK_DISPLAY_CHICKEN2) |
8017 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008018 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008019 I915_WRITE(DISP_ARB_CTL,
8020 (I915_READ(DISP_ARB_CTL) |
8021 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02008022
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008023 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008024
8025 /*
8026 * Based on the document from hardware guys the following bits
8027 * should be set unconditionally in order to enable FBC.
8028 * The bit 22 of 0x42000
8029 * The bit 22 of 0x42004
8030 * The bit 7,8,9 of 0x42020.
8031 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008032 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01008033 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008034 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8035 I915_READ(ILK_DISPLAY_CHICKEN1) |
8036 ILK_FBCQ_DIS);
8037 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8038 I915_READ(ILK_DISPLAY_CHICKEN2) |
8039 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008040 }
8041
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008042 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
8043
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008044 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8045 I915_READ(ILK_DISPLAY_CHICKEN2) |
8046 ILK_ELPIN_409_SELECT);
8047 I915_WRITE(_3D_CHICKEN2,
8048 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
8049 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02008050
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008051 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02008052 I915_WRITE(CACHE_MODE_0,
8053 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01008054
Akash Goel4e046322014-04-04 17:14:38 +05308055 /* WaDisable_RenderCache_OperationalFlush:ilk */
8056 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8057
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008058 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03008059
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008060 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008061}
8062
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008063static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01008064{
Daniel Vetter3107bd42012-10-31 22:52:31 +01008065 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008066 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01008067
8068 /*
8069 * On Ibex Peak and Cougar Point, we need to disable clock
8070 * gating for the panel power sequencer or it will fail to
8071 * start up when no ports are active.
8072 */
Jesse Barnescd664072013-10-02 10:34:19 -07008073 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
8074 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
8075 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008076 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
8077 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01008078 /* The below fixes the weird display corruption, a few pixels shifted
8079 * downward, on (only) LVDS of some HP laptops with IVY.
8080 */
Damien Lespiau055e3932014-08-18 13:49:10 +01008081 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03008082 val = I915_READ(TRANS_CHICKEN2(pipe));
8083 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
8084 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008085 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008086 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03008087 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
8088 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
8089 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008090 I915_WRITE(TRANS_CHICKEN2(pipe), val);
8091 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01008092 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01008093 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01008094 I915_WRITE(TRANS_CHICKEN1(pipe),
8095 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
8096 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008097}
8098
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008099static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008100{
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008101 uint32_t tmp;
8102
8103 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02008104 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
8105 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
8106 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008107}
8108
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008109static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008110{
Damien Lespiau231e54f2012-10-19 17:55:41 +01008111 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008112
Damien Lespiau231e54f2012-10-19 17:55:41 +01008113 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008114
8115 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8116 I915_READ(ILK_DISPLAY_CHICKEN2) |
8117 ILK_ELPIN_409_SELECT);
8118
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008119 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01008120 I915_WRITE(_3D_CHICKEN,
8121 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
8122
Akash Goel4e046322014-04-04 17:14:38 +05308123 /* WaDisable_RenderCache_OperationalFlush:snb */
8124 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8125
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008126 /*
8127 * BSpec recoomends 8x4 when MSAA is used,
8128 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008129 *
8130 * Note that PS/WM thread counts depend on the WIZ hashing
8131 * disable bit, which we don't touch here, but it's good
8132 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008133 */
8134 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008135 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008136
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008137 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008138
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008139 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02008140 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008141
8142 I915_WRITE(GEN6_UCGCTL1,
8143 I915_READ(GEN6_UCGCTL1) |
8144 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
8145 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
8146
8147 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
8148 * gating disable must be set. Failure to set it results in
8149 * flickering pixels due to Z write ordering failures after
8150 * some amount of runtime in the Mesa "fire" demo, and Unigine
8151 * Sanctuary and Tropics, and apparently anything else with
8152 * alpha test or pixel discard.
8153 *
8154 * According to the spec, bit 11 (RCCUNIT) must also be set,
8155 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008156 *
Ville Syrjäläef593182014-01-22 21:32:47 +02008157 * WaDisableRCCUnitClockGating:snb
8158 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008159 */
8160 I915_WRITE(GEN6_UCGCTL2,
8161 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
8162 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
8163
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02008164 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02008165 I915_WRITE(_3D_CHICKEN3,
8166 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008167
8168 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02008169 * Bspec says:
8170 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
8171 * 3DSTATE_SF number of SF output attributes is more than 16."
8172 */
8173 I915_WRITE(_3D_CHICKEN3,
8174 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
8175
8176 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008177 * According to the spec the following bits should be
8178 * set in order to enable memory self-refresh and fbc:
8179 * The bit21 and bit22 of 0x42000
8180 * The bit21 and bit22 of 0x42004
8181 * The bit5 and bit7 of 0x42020
8182 * The bit14 of 0x70180
8183 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01008184 *
8185 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008186 */
8187 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8188 I915_READ(ILK_DISPLAY_CHICKEN1) |
8189 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
8190 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8191 I915_READ(ILK_DISPLAY_CHICKEN2) |
8192 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01008193 I915_WRITE(ILK_DSPCLK_GATE_D,
8194 I915_READ(ILK_DSPCLK_GATE_D) |
8195 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
8196 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008197
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008198 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07008199
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008200 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008201
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008202 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008203}
8204
8205static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
8206{
8207 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
8208
Ville Syrjälä3aad9052014-01-22 21:32:59 +02008209 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02008210 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02008211 *
8212 * This actually overrides the dispatch
8213 * mode for all thread types.
8214 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008215 reg &= ~GEN7_FF_SCHED_MASK;
8216 reg |= GEN7_FF_TS_SCHED_HW;
8217 reg |= GEN7_FF_VS_SCHED_HW;
8218 reg |= GEN7_FF_DS_SCHED_HW;
8219
8220 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
8221}
8222
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008223static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008224{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008225 /*
8226 * TODO: this bit should only be enabled when really needed, then
8227 * disabled when not needed anymore in order to save power.
8228 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008229 if (HAS_PCH_LPT_LP(dev_priv))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008230 I915_WRITE(SOUTH_DSPCLK_GATE_D,
8231 I915_READ(SOUTH_DSPCLK_GATE_D) |
8232 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03008233
8234 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03008235 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
8236 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03008237 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008238}
8239
Ville Syrjälä712bf362016-10-31 22:37:23 +02008240static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03008241{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008242 if (HAS_PCH_LPT_LP(dev_priv)) {
Imre Deak7d708ee2013-04-17 14:04:50 +03008243 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
8244
8245 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8246 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8247 }
8248}
8249
Imre Deak450174f2016-05-03 15:54:21 +03008250static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
8251 int general_prio_credits,
8252 int high_prio_credits)
8253{
8254 u32 misccpctl;
8255
8256 /* WaTempDisableDOPClkGating:bdw */
8257 misccpctl = I915_READ(GEN7_MISCCPCTL);
8258 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
8259
8260 I915_WRITE(GEN8_L3SQCREG1,
8261 L3_GENERAL_PRIO_CREDITS(general_prio_credits) |
8262 L3_HIGH_PRIO_CREDITS(high_prio_credits));
8263
8264 /*
8265 * Wait at least 100 clocks before re-enabling clock gating.
8266 * See the definition of L3SQCREG1 in BSpec.
8267 */
8268 POSTING_READ(GEN8_L3SQCREG1);
8269 udelay(1);
8270 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
8271}
8272
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008273static void cnp_init_clock_gating(struct drm_i915_private *dev_priv)
8274{
8275 if (!HAS_PCH_CNP(dev_priv))
8276 return;
8277
8278 /* Wa #1181 */
8279 I915_WRITE(SOUTH_DSPCLK_GATE_D, CNP_PWM_CGE_GATING_DISABLE);
8280}
8281
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008282static void cnl_init_clock_gating(struct drm_i915_private *dev_priv)
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008283{
Rodrigo Vivi8f067832017-09-05 12:30:13 -07008284 u32 val;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008285 cnp_init_clock_gating(dev_priv);
8286
Rodrigo Vivi1a25db62017-08-15 16:16:51 -07008287 /* This is not an Wa. Enable for better image quality */
8288 I915_WRITE(_3D_CHICKEN3,
8289 _MASKED_BIT_ENABLE(_3D_CHICKEN3_AA_LINE_QUALITY_FIX_ENABLE));
8290
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008291 /* WaEnableChickenDCPR:cnl */
8292 I915_WRITE(GEN8_CHICKEN_DCPR_1,
8293 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
8294
8295 /* WaFbcWakeMemOn:cnl */
8296 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
8297 DISP_FBC_MEMORY_WAKE);
8298
8299 /* WaSarbUnitClockGatingDisable:cnl (pre-prod) */
8300 if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_B0))
8301 I915_WRITE(SLICE_UNIT_LEVEL_CLKGATE,
8302 I915_READ(SLICE_UNIT_LEVEL_CLKGATE) |
8303 SARBUNIT_CLKGATE_DIS);
Rodrigo Vivi8f067832017-09-05 12:30:13 -07008304
8305 /* Display WA #1133: WaFbcSkipSegments:cnl */
8306 val = I915_READ(ILK_DPFC_CHICKEN);
8307 val &= ~GLK_SKIP_SEG_COUNT_MASK;
8308 val |= GLK_SKIP_SEG_EN | GLK_SKIP_SEG_COUNT(1);
8309 I915_WRITE(ILK_DPFC_CHICKEN, val);
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008310}
8311
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008312static void cfl_init_clock_gating(struct drm_i915_private *dev_priv)
8313{
8314 cnp_init_clock_gating(dev_priv);
8315 gen9_init_clock_gating(dev_priv);
8316
8317 /* WaFbcNukeOnHostModify:cfl */
8318 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8319 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
8320}
8321
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008322static void kbl_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008323{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008324 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008325
8326 /* WaDisableSDEUnitClockGating:kbl */
8327 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
8328 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8329 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03008330
8331 /* WaDisableGamClockGating:kbl */
8332 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
8333 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
8334 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008335
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008336 /* WaFbcNukeOnHostModify:kbl */
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008337 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8338 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008339}
8340
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008341static void skl_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008342{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008343 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03008344
8345 /* WAC6entrylatency:skl */
8346 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
8347 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008348
8349 /* WaFbcNukeOnHostModify:skl */
8350 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8351 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008352}
8353
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008354static void bdw_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008355{
Damien Lespiau07d27e22014-03-03 17:31:46 +00008356 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008357
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008358 ilk_init_lp_watermarks(dev_priv);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07008359
Ben Widawskyab57fff2013-12-12 15:28:04 -08008360 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07008361 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008362
Ben Widawskyab57fff2013-12-12 15:28:04 -08008363 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008364 I915_WRITE(CHICKEN_PAR1_1,
8365 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
8366
Ben Widawskyab57fff2013-12-12 15:28:04 -08008367 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01008368 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00008369 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02008370 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02008371 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008372 }
Ben Widawsky63801f22013-12-12 17:26:03 -08008373
Ben Widawskyab57fff2013-12-12 15:28:04 -08008374 /* WaVSRefCountFullforceMissDisable:bdw */
8375 /* WaDSRefCountFullforceMissDisable:bdw */
8376 I915_WRITE(GEN7_FF_THREAD_MODE,
8377 I915_READ(GEN7_FF_THREAD_MODE) &
8378 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02008379
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02008380 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
8381 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02008382
8383 /* WaDisableSDEUnitClockGating:bdw */
8384 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8385 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00008386
Imre Deak450174f2016-05-03 15:54:21 +03008387 /* WaProgramL3SqcReg1Default:bdw */
8388 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03008389
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008390 /*
8391 * WaGttCachingOffByDefault:bdw
8392 * GTT cache may not work with big pages, so if those
8393 * are ever enabled GTT cache may need to be disabled.
8394 */
8395 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
8396
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03008397 /* WaKVMNotificationOnConfigChange:bdw */
8398 I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
8399 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
8400
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008401 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00008402
8403 /* WaDisableDopClockGating:bdw
8404 *
8405 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
8406 * clock gating.
8407 */
8408 I915_WRITE(GEN6_UCGCTL1,
8409 I915_READ(GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008410}
8411
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008412static void hsw_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008413{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008414 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008415
Francisco Jerezf3fc4882013-10-02 15:53:16 -07008416 /* L3 caching of data atomics doesn't work -- disable it. */
8417 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
8418 I915_WRITE(HSW_ROW_CHICKEN3,
8419 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
8420
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008421 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008422 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8423 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8424 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8425
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02008426 /* WaVSRefCountFullforceMissDisable:hsw */
8427 I915_WRITE(GEN7_FF_THREAD_MODE,
8428 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008429
Akash Goel4e046322014-04-04 17:14:38 +05308430 /* WaDisable_RenderCache_OperationalFlush:hsw */
8431 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8432
Chia-I Wufe27c602014-01-28 13:29:33 +08008433 /* enable HiZ Raw Stall Optimization */
8434 I915_WRITE(CACHE_MODE_0_GEN7,
8435 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
8436
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008437 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008438 I915_WRITE(CACHE_MODE_1,
8439 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03008440
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008441 /*
8442 * BSpec recommends 8x4 when MSAA is used,
8443 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008444 *
8445 * Note that PS/WM thread counts depend on the WIZ hashing
8446 * disable bit, which we don't touch here, but it's good
8447 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008448 */
8449 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008450 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008451
Kenneth Graunke94411592014-12-31 16:23:00 -08008452 /* WaSampleCChickenBitEnable:hsw */
8453 I915_WRITE(HALF_SLICE_CHICKEN3,
8454 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
8455
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008456 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07008457 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
8458
Paulo Zanoni90a88642013-05-03 17:23:45 -03008459 /* WaRsPkgCStateDisplayPMReq:hsw */
8460 I915_WRITE(CHICKEN_PAR1_1,
8461 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03008462
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008463 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008464}
8465
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008466static void ivb_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008467{
Ben Widawsky20848222012-05-04 18:58:59 -07008468 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008469
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008470 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008471
Damien Lespiau231e54f2012-10-19 17:55:41 +01008472 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008473
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008474 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05008475 I915_WRITE(_3D_CHICKEN3,
8476 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
8477
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008478 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008479 I915_WRITE(IVB_CHICKEN3,
8480 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8481 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8482
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008483 /* WaDisablePSDDualDispatchEnable:ivb */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008484 if (IS_IVB_GT1(dev_priv))
Jesse Barnes12f33822012-10-25 12:15:45 -07008485 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
8486 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07008487
Akash Goel4e046322014-04-04 17:14:38 +05308488 /* WaDisable_RenderCache_OperationalFlush:ivb */
8489 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8490
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008491 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008492 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
8493 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
8494
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008495 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008496 I915_WRITE(GEN7_L3CNTLREG1,
8497 GEN7_WA_FOR_GEN7_L3_CONTROL);
8498 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07008499 GEN7_WA_L3_CHICKEN_MODE);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008500 if (IS_IVB_GT1(dev_priv))
Jesse Barnes8ab43972012-10-25 12:15:42 -07008501 I915_WRITE(GEN7_ROW_CHICKEN2,
8502 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02008503 else {
8504 /* must write both registers */
8505 I915_WRITE(GEN7_ROW_CHICKEN2,
8506 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07008507 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
8508 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02008509 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008510
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008511 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05008512 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
8513 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
8514
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02008515 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07008516 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008517 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008518 */
8519 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02008520 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07008521
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008522 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008523 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8524 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8525 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8526
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008527 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008528
8529 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02008530
Chris Wilson22721342014-03-04 09:41:43 +00008531 if (0) { /* causes HiZ corruption on ivb:gt1 */
8532 /* enable HiZ Raw Stall Optimization */
8533 I915_WRITE(CACHE_MODE_0_GEN7,
8534 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
8535 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08008536
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008537 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02008538 I915_WRITE(CACHE_MODE_1,
8539 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07008540
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008541 /*
8542 * BSpec recommends 8x4 when MSAA is used,
8543 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008544 *
8545 * Note that PS/WM thread counts depend on the WIZ hashing
8546 * disable bit, which we don't touch here, but it's good
8547 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008548 */
8549 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008550 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008551
Ben Widawsky20848222012-05-04 18:58:59 -07008552 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
8553 snpcr &= ~GEN6_MBC_SNPCR_MASK;
8554 snpcr |= GEN6_MBC_SNPCR_MED;
8555 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008556
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008557 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008558 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008559
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008560 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008561}
8562
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008563static void vlv_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008564{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008565 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05008566 I915_WRITE(_3D_CHICKEN3,
8567 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
8568
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008569 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008570 I915_WRITE(IVB_CHICKEN3,
8571 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8572 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8573
Ville Syrjäläfad7d362014-01-22 21:32:39 +02008574 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008575 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07008576 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08008577 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
8578 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07008579
Akash Goel4e046322014-04-04 17:14:38 +05308580 /* WaDisable_RenderCache_OperationalFlush:vlv */
8581 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8582
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008583 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05008584 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
8585 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
8586
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008587 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07008588 I915_WRITE(GEN7_ROW_CHICKEN2,
8589 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
8590
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008591 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008592 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8593 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8594 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8595
Ville Syrjälä46680e02014-01-22 21:33:01 +02008596 gen7_setup_fixed_func_scheduler(dev_priv);
8597
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02008598 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07008599 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008600 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008601 */
8602 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02008603 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07008604
Akash Goelc98f5062014-03-24 23:00:07 +05308605 /* WaDisableL3Bank2xClockGate:vlv
8606 * Disabling L3 clock gating- MMIO 940c[25] = 1
8607 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
8608 I915_WRITE(GEN7_UCGCTL4,
8609 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07008610
Ville Syrjäläafd58e72014-01-22 21:33:03 +02008611 /*
8612 * BSpec says this must be set, even though
8613 * WaDisable4x2SubspanOptimization isn't listed for VLV.
8614 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02008615 I915_WRITE(CACHE_MODE_1,
8616 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07008617
8618 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02008619 * BSpec recommends 8x4 when MSAA is used,
8620 * however in practice 16x4 seems fastest.
8621 *
8622 * Note that PS/WM thread counts depend on the WIZ hashing
8623 * disable bit, which we don't touch here, but it's good
8624 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
8625 */
8626 I915_WRITE(GEN7_GT_MODE,
8627 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
8628
8629 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02008630 * WaIncreaseL3CreditsForVLVB0:vlv
8631 * This is the hardware default actually.
8632 */
8633 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
8634
8635 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008636 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07008637 * Disable clock gating on th GCFG unit to prevent a delay
8638 * in the reporting of vblank events.
8639 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02008640 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008641}
8642
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008643static void chv_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03008644{
Ville Syrjälä232ce332014-04-09 13:28:35 +03008645 /* WaVSRefCountFullforceMissDisable:chv */
8646 /* WaDSRefCountFullforceMissDisable:chv */
8647 I915_WRITE(GEN7_FF_THREAD_MODE,
8648 I915_READ(GEN7_FF_THREAD_MODE) &
8649 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03008650
8651 /* WaDisableSemaphoreAndSyncFlipWait:chv */
8652 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
8653 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03008654
8655 /* WaDisableCSUnitClockGating:chv */
8656 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
8657 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03008658
8659 /* WaDisableSDEUnitClockGating:chv */
8660 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8661 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008662
8663 /*
Imre Deak450174f2016-05-03 15:54:21 +03008664 * WaProgramL3SqcReg1Default:chv
8665 * See gfxspecs/Related Documents/Performance Guide/
8666 * LSQC Setting Recommendations.
8667 */
8668 gen8_set_l3sqc_credits(dev_priv, 38, 2);
8669
8670 /*
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008671 * GTT cache may not work with big pages, so if those
8672 * are ever enabled GTT cache may need to be disabled.
8673 */
8674 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03008675}
8676
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008677static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008678{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008679 uint32_t dspclk_gate;
8680
8681 I915_WRITE(RENCLK_GATE_D1, 0);
8682 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
8683 GS_UNIT_CLOCK_GATE_DISABLE |
8684 CL_UNIT_CLOCK_GATE_DISABLE);
8685 I915_WRITE(RAMCLK_GATE_D, 0);
8686 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
8687 OVRUNIT_CLOCK_GATE_DISABLE |
8688 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008689 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008690 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
8691 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02008692
8693 /* WaDisableRenderCachePipelinedFlush */
8694 I915_WRITE(CACHE_MODE_0,
8695 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03008696
Akash Goel4e046322014-04-04 17:14:38 +05308697 /* WaDisable_RenderCache_OperationalFlush:g4x */
8698 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8699
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008700 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008701}
8702
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008703static void i965gm_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008704{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008705 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
8706 I915_WRITE(RENCLK_GATE_D2, 0);
8707 I915_WRITE(DSPCLK_GATE_D, 0);
8708 I915_WRITE(RAMCLK_GATE_D, 0);
8709 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03008710 I915_WRITE(MI_ARB_STATE,
8711 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05308712
8713 /* WaDisable_RenderCache_OperationalFlush:gen4 */
8714 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008715}
8716
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008717static void i965g_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008718{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008719 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
8720 I965_RCC_CLOCK_GATE_DISABLE |
8721 I965_RCPB_CLOCK_GATE_DISABLE |
8722 I965_ISC_CLOCK_GATE_DISABLE |
8723 I965_FBC_CLOCK_GATE_DISABLE);
8724 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03008725 I915_WRITE(MI_ARB_STATE,
8726 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05308727
8728 /* WaDisable_RenderCache_OperationalFlush:gen4 */
8729 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008730}
8731
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008732static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008733{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008734 u32 dstate = I915_READ(D_STATE);
8735
8736 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
8737 DSTATE_DOT_CLOCK_GATING;
8738 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01008739
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008740 if (IS_PINEVIEW(dev_priv))
Chris Wilson13a86b82012-04-24 14:51:43 +01008741 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02008742
8743 /* IIR "flip pending" means done if this bit is set */
8744 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02008745
8746 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02008747 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02008748
8749 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
8750 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03008751
8752 I915_WRITE(MI_ARB_STATE,
8753 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008754}
8755
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008756static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008757{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008758 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02008759
8760 /* interrupts should cause a wake up from C3 */
8761 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
8762 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03008763
8764 I915_WRITE(MEM_MODE,
8765 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008766}
8767
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008768static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008769{
Ville Syrjälä10383922014-08-15 01:21:54 +03008770 I915_WRITE(MEM_MODE,
8771 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
8772 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008773}
8774
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008775void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008776{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008777 dev_priv->display.init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008778}
8779
Ville Syrjälä712bf362016-10-31 22:37:23 +02008780void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03008781{
Ville Syrjälä712bf362016-10-31 22:37:23 +02008782 if (HAS_PCH_LPT(dev_priv))
8783 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03008784}
8785
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008786static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02008787{
8788 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
8789}
8790
8791/**
8792 * intel_init_clock_gating_hooks - setup the clock gating hooks
8793 * @dev_priv: device private
8794 *
8795 * Setup the hooks that configure which clocks of a given platform can be
8796 * gated and also apply various GT and display specific workarounds for these
8797 * platforms. Note that some GT specific workarounds are applied separately
8798 * when GPU contexts or batchbuffers start their execution.
8799 */
8800void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
8801{
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008802 if (IS_CANNONLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008803 dev_priv->display.init_clock_gating = cnl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008804 else if (IS_COFFEELAKE(dev_priv))
8805 dev_priv->display.init_clock_gating = cfl_init_clock_gating;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008806 else if (IS_SKYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008807 dev_priv->display.init_clock_gating = skl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008808 else if (IS_KABYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008809 dev_priv->display.init_clock_gating = kbl_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02008810 else if (IS_BROXTON(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02008811 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02008812 else if (IS_GEMINILAKE(dev_priv))
8813 dev_priv->display.init_clock_gating = glk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008814 else if (IS_BROADWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008815 dev_priv->display.init_clock_gating = bdw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008816 else if (IS_CHERRYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008817 dev_priv->display.init_clock_gating = chv_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008818 else if (IS_HASWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008819 dev_priv->display.init_clock_gating = hsw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008820 else if (IS_IVYBRIDGE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008821 dev_priv->display.init_clock_gating = ivb_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008822 else if (IS_VALLEYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008823 dev_priv->display.init_clock_gating = vlv_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008824 else if (IS_GEN6(dev_priv))
8825 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
8826 else if (IS_GEN5(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008827 dev_priv->display.init_clock_gating = ilk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008828 else if (IS_G4X(dev_priv))
8829 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02008830 else if (IS_I965GM(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008831 dev_priv->display.init_clock_gating = i965gm_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02008832 else if (IS_I965G(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008833 dev_priv->display.init_clock_gating = i965g_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008834 else if (IS_GEN3(dev_priv))
8835 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
8836 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
8837 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
8838 else if (IS_GEN2(dev_priv))
8839 dev_priv->display.init_clock_gating = i830_init_clock_gating;
8840 else {
8841 MISSING_CASE(INTEL_DEVID(dev_priv));
8842 dev_priv->display.init_clock_gating = nop_init_clock_gating;
8843 }
8844}
8845
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008846/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008847void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008848{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02008849 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008850
Daniel Vetterc921aba2012-04-26 23:28:17 +02008851 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008852 if (IS_PINEVIEW(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02008853 i915_pineview_get_mem_freq(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008854 else if (IS_GEN5(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02008855 i915_ironlake_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02008856
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008857 /* For FIFO watermark updates */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008858 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008859 skl_setup_wm_latency(dev_priv);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01008860 dev_priv->display.initial_watermarks = skl_initial_wm;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01008861 dev_priv->display.atomic_update_watermarks = skl_atomic_update_crtc_wm;
Matt Roper98d39492016-05-12 07:06:03 -07008862 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008863 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008864 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03008865
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008866 if ((IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008867 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008868 (!IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008869 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07008870 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08008871 dev_priv->display.compute_intermediate_wm =
8872 ilk_compute_intermediate_wm;
8873 dev_priv->display.initial_watermarks =
8874 ilk_initial_watermarks;
8875 dev_priv->display.optimize_watermarks =
8876 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02008877 } else {
8878 DRM_DEBUG_KMS("Failed to read display plane latency. "
8879 "Disable CxSR\n");
8880 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02008881 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008882 vlv_setup_wm_latency(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02008883 dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
Ville Syrjälä4841da52017-03-02 19:14:59 +02008884 dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02008885 dev_priv->display.initial_watermarks = vlv_initial_watermarks;
Ville Syrjälä4841da52017-03-02 19:14:59 +02008886 dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
Ville Syrjäläff32c542017-03-02 19:14:57 +02008887 dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03008888 } else if (IS_G4X(dev_priv)) {
8889 g4x_setup_wm_latency(dev_priv);
8890 dev_priv->display.compute_pipe_wm = g4x_compute_pipe_wm;
8891 dev_priv->display.compute_intermediate_wm = g4x_compute_intermediate_wm;
8892 dev_priv->display.initial_watermarks = g4x_initial_watermarks;
8893 dev_priv->display.optimize_watermarks = g4x_optimize_watermarks;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008894 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008895 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008896 dev_priv->is_ddr3,
8897 dev_priv->fsb_freq,
8898 dev_priv->mem_freq)) {
8899 DRM_INFO("failed to find known CxSR latency "
8900 "(found ddr%s fsb freq %d, mem freq %d), "
8901 "disabling CxSR\n",
8902 (dev_priv->is_ddr3 == 1) ? "3" : "2",
8903 dev_priv->fsb_freq, dev_priv->mem_freq);
8904 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03008905 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008906 dev_priv->display.update_wm = NULL;
8907 } else
8908 dev_priv->display.update_wm = pineview_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008909 } else if (IS_GEN4(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008910 dev_priv->display.update_wm = i965_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008911 } else if (IS_GEN3(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008912 dev_priv->display.update_wm = i9xx_update_wm;
8913 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008914 } else if (IS_GEN2(dev_priv)) {
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008915 if (INTEL_INFO(dev_priv)->num_pipes == 1) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008916 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008917 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008918 } else {
8919 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008920 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008921 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008922 } else {
8923 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008924 }
8925}
8926
Lyude87660502016-08-17 15:55:53 -04008927static inline int gen6_check_mailbox_status(struct drm_i915_private *dev_priv)
8928{
8929 uint32_t flags =
8930 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
8931
8932 switch (flags) {
8933 case GEN6_PCODE_SUCCESS:
8934 return 0;
8935 case GEN6_PCODE_UNIMPLEMENTED_CMD:
Chris Wilson5a9cfff2017-07-28 09:50:22 +01008936 return -ENODEV;
Lyude87660502016-08-17 15:55:53 -04008937 case GEN6_PCODE_ILLEGAL_CMD:
8938 return -ENXIO;
8939 case GEN6_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Chris Wilson7850d1c2016-08-26 11:59:26 +01008940 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Lyude87660502016-08-17 15:55:53 -04008941 return -EOVERFLOW;
8942 case GEN6_PCODE_TIMEOUT:
8943 return -ETIMEDOUT;
8944 default:
Michal Wajdeczkof0d66152017-03-28 08:45:12 +00008945 MISSING_CASE(flags);
Lyude87660502016-08-17 15:55:53 -04008946 return 0;
8947 }
8948}
8949
8950static inline int gen7_check_mailbox_status(struct drm_i915_private *dev_priv)
8951{
8952 uint32_t flags =
8953 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
8954
8955 switch (flags) {
8956 case GEN6_PCODE_SUCCESS:
8957 return 0;
8958 case GEN6_PCODE_ILLEGAL_CMD:
8959 return -ENXIO;
8960 case GEN7_PCODE_TIMEOUT:
8961 return -ETIMEDOUT;
8962 case GEN7_PCODE_ILLEGAL_DATA:
8963 return -EINVAL;
8964 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
8965 return -EOVERFLOW;
8966 default:
8967 MISSING_CASE(flags);
8968 return 0;
8969 }
8970}
8971
Tom O'Rourke151a49d2014-11-13 18:50:10 -08008972int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07008973{
Lyude87660502016-08-17 15:55:53 -04008974 int status;
8975
Jesse Barnes4fc688c2012-11-02 11:14:01 -07008976 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07008977
Chris Wilson3f5582d2016-06-30 15:32:45 +01008978 /* GEN6_PCODE_* are outside of the forcewake domain, we can
8979 * use te fw I915_READ variants to reduce the amount of work
8980 * required when reading/writing.
8981 */
8982
8983 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01008984 DRM_DEBUG_DRIVER("warning: pcode (read from mbox %x) mailbox access failed for %ps\n",
8985 mbox, __builtin_return_address(0));
Ben Widawsky42c05262012-09-26 10:34:00 -07008986 return -EAGAIN;
8987 }
8988
Chris Wilson3f5582d2016-06-30 15:32:45 +01008989 I915_WRITE_FW(GEN6_PCODE_DATA, *val);
8990 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
8991 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07008992
Chris Wilsone09a3032017-04-11 11:13:39 +01008993 if (__intel_wait_for_register_fw(dev_priv,
8994 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
8995 500, 0, NULL)) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01008996 DRM_ERROR("timeout waiting for pcode read (from mbox %x) to finish for %ps\n",
8997 mbox, __builtin_return_address(0));
Ben Widawsky42c05262012-09-26 10:34:00 -07008998 return -ETIMEDOUT;
8999 }
9000
Chris Wilson3f5582d2016-06-30 15:32:45 +01009001 *val = I915_READ_FW(GEN6_PCODE_DATA);
9002 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07009003
Lyude87660502016-08-17 15:55:53 -04009004 if (INTEL_GEN(dev_priv) > 6)
9005 status = gen7_check_mailbox_status(dev_priv);
9006 else
9007 status = gen6_check_mailbox_status(dev_priv);
9008
9009 if (status) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009010 DRM_DEBUG_DRIVER("warning: pcode (read from mbox %x) mailbox access failed for %ps: %d\n",
9011 mbox, __builtin_return_address(0), status);
Lyude87660502016-08-17 15:55:53 -04009012 return status;
9013 }
9014
Ben Widawsky42c05262012-09-26 10:34:00 -07009015 return 0;
9016}
9017
Chris Wilson3f5582d2016-06-30 15:32:45 +01009018int sandybridge_pcode_write(struct drm_i915_private *dev_priv,
Lyude87660502016-08-17 15:55:53 -04009019 u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07009020{
Lyude87660502016-08-17 15:55:53 -04009021 int status;
9022
Jesse Barnes4fc688c2012-11-02 11:14:01 -07009023 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07009024
Chris Wilson3f5582d2016-06-30 15:32:45 +01009025 /* GEN6_PCODE_* are outside of the forcewake domain, we can
9026 * use te fw I915_READ variants to reduce the amount of work
9027 * required when reading/writing.
9028 */
9029
9030 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009031 DRM_DEBUG_DRIVER("warning: pcode (write of 0x%08x to mbox %x) mailbox access failed for %ps\n",
9032 val, mbox, __builtin_return_address(0));
Ben Widawsky42c05262012-09-26 10:34:00 -07009033 return -EAGAIN;
9034 }
9035
Chris Wilson3f5582d2016-06-30 15:32:45 +01009036 I915_WRITE_FW(GEN6_PCODE_DATA, val);
Imre Deak8bf41b72016-11-28 17:29:27 +02009037 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
Chris Wilson3f5582d2016-06-30 15:32:45 +01009038 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07009039
Chris Wilsone09a3032017-04-11 11:13:39 +01009040 if (__intel_wait_for_register_fw(dev_priv,
9041 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
9042 500, 0, NULL)) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009043 DRM_ERROR("timeout waiting for pcode write of 0x%08x to mbox %x to finish for %ps\n",
9044 val, mbox, __builtin_return_address(0));
Ben Widawsky42c05262012-09-26 10:34:00 -07009045 return -ETIMEDOUT;
9046 }
9047
Chris Wilson3f5582d2016-06-30 15:32:45 +01009048 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07009049
Lyude87660502016-08-17 15:55:53 -04009050 if (INTEL_GEN(dev_priv) > 6)
9051 status = gen7_check_mailbox_status(dev_priv);
9052 else
9053 status = gen6_check_mailbox_status(dev_priv);
9054
9055 if (status) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009056 DRM_DEBUG_DRIVER("warning: pcode (write of 0x%08x to mbox %x) mailbox access failed for %ps: %d\n",
9057 val, mbox, __builtin_return_address(0), status);
Lyude87660502016-08-17 15:55:53 -04009058 return status;
9059 }
9060
Ben Widawsky42c05262012-09-26 10:34:00 -07009061 return 0;
9062}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07009063
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009064static bool skl_pcode_try_request(struct drm_i915_private *dev_priv, u32 mbox,
9065 u32 request, u32 reply_mask, u32 reply,
9066 u32 *status)
9067{
9068 u32 val = request;
9069
9070 *status = sandybridge_pcode_read(dev_priv, mbox, &val);
9071
9072 return *status || ((val & reply_mask) == reply);
9073}
9074
9075/**
9076 * skl_pcode_request - send PCODE request until acknowledgment
9077 * @dev_priv: device private
9078 * @mbox: PCODE mailbox ID the request is targeted for
9079 * @request: request ID
9080 * @reply_mask: mask used to check for request acknowledgment
9081 * @reply: value used to check for request acknowledgment
9082 * @timeout_base_ms: timeout for polling with preemption enabled
9083 *
9084 * Keep resending the @request to @mbox until PCODE acknowledges it, PCODE
Imre Deak01299362017-02-24 16:32:10 +02009085 * reports an error or an overall timeout of @timeout_base_ms+50 ms expires.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009086 * The request is acknowledged once the PCODE reply dword equals @reply after
9087 * applying @reply_mask. Polling is first attempted with preemption enabled
Imre Deak01299362017-02-24 16:32:10 +02009088 * for @timeout_base_ms and if this times out for another 50 ms with
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009089 * preemption disabled.
9090 *
9091 * Returns 0 on success, %-ETIMEDOUT in case of a timeout, <0 in case of some
9092 * other error as reported by PCODE.
9093 */
9094int skl_pcode_request(struct drm_i915_private *dev_priv, u32 mbox, u32 request,
9095 u32 reply_mask, u32 reply, int timeout_base_ms)
9096{
9097 u32 status;
9098 int ret;
9099
9100 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
9101
9102#define COND skl_pcode_try_request(dev_priv, mbox, request, reply_mask, reply, \
9103 &status)
9104
9105 /*
9106 * Prime the PCODE by doing a request first. Normally it guarantees
9107 * that a subsequent request, at most @timeout_base_ms later, succeeds.
9108 * _wait_for() doesn't guarantee when its passed condition is evaluated
9109 * first, so send the first request explicitly.
9110 */
9111 if (COND) {
9112 ret = 0;
9113 goto out;
9114 }
9115 ret = _wait_for(COND, timeout_base_ms * 1000, 10);
9116 if (!ret)
9117 goto out;
9118
9119 /*
9120 * The above can time out if the number of requests was low (2 in the
9121 * worst case) _and_ PCODE was busy for some reason even after a
9122 * (queued) request and @timeout_base_ms delay. As a workaround retry
9123 * the poll with preemption disabled to maximize the number of
Imre Deak01299362017-02-24 16:32:10 +02009124 * requests. Increase the timeout from @timeout_base_ms to 50ms to
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009125 * account for interrupts that could reduce the number of these
Imre Deak01299362017-02-24 16:32:10 +02009126 * requests, and for any quirks of the PCODE firmware that delays
9127 * the request completion.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009128 */
9129 DRM_DEBUG_KMS("PCODE timeout, retrying with preemption disabled\n");
9130 WARN_ON_ONCE(timeout_base_ms > 3);
9131 preempt_disable();
Imre Deak01299362017-02-24 16:32:10 +02009132 ret = wait_for_atomic(COND, 50);
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009133 preempt_enable();
9134
9135out:
9136 return ret ? ret : status;
9137#undef COND
9138}
9139
Ville Syrjälädd06f882014-11-10 22:55:12 +02009140static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
9141{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009142 /*
9143 * N = val - 0xb7
9144 * Slow = Fast = GPLL ref * N
9145 */
9146 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * (val - 0xb7), 1000);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009147}
9148
Fengguang Wub55dd642014-07-12 11:21:39 +02009149static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009150{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009151 return DIV_ROUND_CLOSEST(1000 * val, dev_priv->rps.gpll_ref_freq) + 0xb7;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009152}
9153
Fengguang Wub55dd642014-07-12 11:21:39 +02009154static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05309155{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009156 /*
9157 * N = val / 2
9158 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
9159 */
9160 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * val, 2 * 2 * 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05309161}
9162
Fengguang Wub55dd642014-07-12 11:21:39 +02009163static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05309164{
Ville Syrjälä1c147622014-08-18 14:42:43 +03009165 /* CHV needs even values */
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009166 return DIV_ROUND_CLOSEST(2 * 1000 * val, dev_priv->rps.gpll_ref_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05309167}
9168
Ville Syrjälä616bc822015-01-23 21:04:25 +02009169int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
9170{
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07009171 if (INTEL_GEN(dev_priv) >= 9)
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009172 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
9173 GEN9_FREQ_SCALER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009174 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009175 return chv_gpu_freq(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009176 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009177 return byt_gpu_freq(dev_priv, val);
9178 else
9179 return val * GT_FREQUENCY_MULTIPLIER;
9180}
9181
Ville Syrjälä616bc822015-01-23 21:04:25 +02009182int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
9183{
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07009184 if (INTEL_GEN(dev_priv) >= 9)
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009185 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
9186 GT_FREQUENCY_MULTIPLIER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009187 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009188 return chv_freq_opcode(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009189 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009190 return byt_freq_opcode(dev_priv, val);
9191 else
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009192 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
Deepak S22b1b2f2014-07-12 14:54:33 +05309193}
9194
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00009195void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01009196{
Daniel Vetterf742a552013-12-06 10:17:53 +01009197 mutex_init(&dev_priv->rps.hw_lock);
9198
Chris Wilson54b4f682016-07-21 21:16:19 +01009199 INIT_DELAYED_WORK(&dev_priv->rps.autoenable_work,
9200 __intel_autoenable_gt_powersave);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01009201 atomic_set(&dev_priv->rps.num_waiters, 0);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03009202
Paulo Zanoni33688d92014-03-07 20:08:19 -03009203 dev_priv->pm.suspended = false;
Imre Deak1f814da2015-12-16 02:52:19 +02009204 atomic_set(&dev_priv->pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01009205}
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009206
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009207static u64 vlv_residency_raw(struct drm_i915_private *dev_priv,
9208 const i915_reg_t reg)
9209{
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009210 u32 lower, upper, tmp;
Chris Wilson71cc2b12017-03-24 16:54:18 +00009211 int loop = 2;
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009212
9213 /* The register accessed do not need forcewake. We borrow
9214 * uncore lock to prevent concurrent access to range reg.
9215 */
9216 spin_lock_irq(&dev_priv->uncore.lock);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009217
9218 /* vlv and chv residency counters are 40 bits in width.
9219 * With a control bit, we can choose between upper or lower
9220 * 32bit window into this counter.
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009221 *
9222 * Although we always use the counter in high-range mode elsewhere,
9223 * userspace may attempt to read the value before rc6 is initialised,
9224 * before we have set the default VLV_COUNTER_CONTROL value. So always
9225 * set the high bit to be safe.
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009226 */
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009227 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9228 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009229 upper = I915_READ_FW(reg);
9230 do {
9231 tmp = upper;
9232
9233 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9234 _MASKED_BIT_DISABLE(VLV_COUNT_RANGE_HIGH));
9235 lower = I915_READ_FW(reg);
9236
9237 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9238 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
9239 upper = I915_READ_FW(reg);
Chris Wilson71cc2b12017-03-24 16:54:18 +00009240 } while (upper != tmp && --loop);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009241
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009242 /* Everywhere else we always use VLV_COUNTER_CONTROL with the
9243 * VLV_COUNT_RANGE_HIGH bit set - so it is safe to leave it set
9244 * now.
9245 */
9246
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009247 spin_unlock_irq(&dev_priv->uncore.lock);
9248
9249 return lower | (u64)upper << 8;
9250}
9251
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009252u64 intel_rc6_residency_us(struct drm_i915_private *dev_priv,
9253 const i915_reg_t reg)
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009254{
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009255 u64 time_hw, units, div;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009256
9257 if (!intel_enable_rc6())
9258 return 0;
9259
9260 intel_runtime_pm_get(dev_priv);
9261
9262 /* On VLV and CHV, residency time is in CZ units rather than 1.28us */
9263 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009264 units = 1000;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009265 div = dev_priv->czclk_freq;
9266
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009267 time_hw = vlv_residency_raw(dev_priv, reg);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009268 } else if (IS_GEN9_LP(dev_priv)) {
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009269 units = 1000;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009270 div = 1200; /* 833.33ns */
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009271
9272 time_hw = I915_READ(reg);
9273 } else {
9274 units = 128000; /* 1.28us */
9275 div = 100000;
9276
9277 time_hw = I915_READ(reg);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009278 }
9279
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009280 intel_runtime_pm_put(dev_priv);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009281 return DIV_ROUND_UP_ULL(time_hw * units, div);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009282}