blob: 2dd3af3debe97b25e6c83fe6e76dd1bad9525aba [file] [log] [blame]
Eugeni Dodonov85208be2012-04-16 22:20:34 -03001/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eugeni Dodonov <eugeni.dodonov@intel.com>
25 *
26 */
27
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -030028#include <linux/cpufreq.h>
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -070029#include <drm/drm_plane_helper.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030030#include "i915_drv.h"
31#include "intel_drv.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020032#include "../../../platform/x86/intel_ips.h"
33#include <linux/module.h>
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +020034#include <drm/drm_atomic_helper.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030035
Ben Widawskydc39fff2013-10-18 12:32:07 -070036/**
Jani Nikula18afd442016-01-18 09:19:48 +020037 * DOC: RC6
38 *
Ben Widawskydc39fff2013-10-18 12:32:07 -070039 * RC6 is a special power stage which allows the GPU to enter an very
40 * low-voltage mode when idle, using down to 0V while at this stage. This
41 * stage is entered automatically when the GPU is idle when RC6 support is
42 * enabled, and as soon as new workload arises GPU wakes up automatically as well.
43 *
44 * There are different RC6 modes available in Intel GPU, which differentiate
45 * among each other with the latency required to enter and leave RC6 and
46 * voltage consumed by the GPU in different states.
47 *
48 * The combination of the following flags define which states GPU is allowed
49 * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
50 * RC6pp is deepest RC6. Their support by hardware varies according to the
51 * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
52 * which brings the most power savings; deeper states save more power, but
53 * require higher latency to switch to and wake up.
54 */
55#define INTEL_RC6_ENABLE (1<<0)
56#define INTEL_RC6p_ENABLE (1<<1)
57#define INTEL_RC6pp_ENABLE (1<<2)
58
Ville Syrjälä46f16e62016-10-31 22:37:22 +020059static void gen9_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppalab033bb62016-06-07 17:19:04 +030060{
Rodrigo Vivi82525c12017-06-08 08:50:00 -070061 /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl,cfl */
Mika Kuoppalab033bb62016-06-07 17:19:04 +030062 I915_WRITE(CHICKEN_PAR1_1,
63 I915_READ(CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
64
Ville Syrjälä2e2adb02017-08-01 09:58:13 -070065 /*
66 * Display WA#0390: skl,bxt,kbl,glk
67 *
68 * Must match Sampler, Pixel Back End, and Media
69 * (0xE194 bit 8, 0x7014 bit 13, 0x4DDC bits 27 and 31).
70 *
71 * Including bits outside the page in the hash would
72 * require 2 (or 4?) MiB alignment of resources. Just
73 * assume the defaul hashing mode which only uses bits
74 * within the page.
75 */
76 I915_WRITE(CHICKEN_PAR1_1,
77 I915_READ(CHICKEN_PAR1_1) & ~SKL_RC_HASH_OUTSIDE);
78
Mika Kuoppalab033bb62016-06-07 17:19:04 +030079 I915_WRITE(GEN8_CONFIG0,
80 I915_READ(GEN8_CONFIG0) | GEN9_DEFAULT_FIXES);
Mika Kuoppala590e8ff2016-06-07 17:19:13 +030081
Rodrigo Vivi82525c12017-06-08 08:50:00 -070082 /* WaEnableChickenDCPR:skl,bxt,kbl,glk,cfl */
Mika Kuoppala590e8ff2016-06-07 17:19:13 +030083 I915_WRITE(GEN8_CHICKEN_DCPR_1,
84 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
Mika Kuoppala0f78dee2016-06-07 17:19:16 +030085
Rodrigo Vivi82525c12017-06-08 08:50:00 -070086 /* WaFbcTurnOffFbcWatermark:skl,bxt,kbl,cfl */
87 /* WaFbcWakeMemOn:skl,bxt,kbl,glk,cfl */
Mika Kuoppala303d4ea2016-06-07 17:19:17 +030088 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
89 DISP_FBC_WM_DIS |
90 DISP_FBC_MEMORY_WAKE);
Mika Kuoppalad1b4eef2016-06-07 17:19:19 +030091
Rodrigo Vivi82525c12017-06-08 08:50:00 -070092 /* WaFbcHighMemBwCorruptionAvoidance:skl,bxt,kbl,cfl */
Mika Kuoppalad1b4eef2016-06-07 17:19:19 +030093 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
94 ILK_DPFC_DISABLE_DUMMY0);
Praveen Paneri32087d12017-08-03 23:02:10 +053095
96 if (IS_SKYLAKE(dev_priv)) {
97 /* WaDisableDopClockGating */
98 I915_WRITE(GEN7_MISCCPCTL, I915_READ(GEN7_MISCCPCTL)
99 & ~GEN7_DOP_CLOCK_GATE_ENABLE);
100 }
Mika Kuoppalab033bb62016-06-07 17:19:04 +0300101}
102
Ville Syrjälä46f16e62016-10-31 22:37:22 +0200103static void bxt_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deaka82abe42015-03-27 14:00:04 +0200104{
Ville Syrjälä46f16e62016-10-31 22:37:22 +0200105 gen9_init_clock_gating(dev_priv);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +0200106
Nick Hoatha7546152015-06-29 14:07:32 +0100107 /* WaDisableSDEUnitClockGating:bxt */
108 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
109 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
110
Imre Deak32608ca2015-03-11 11:10:27 +0200111 /*
112 * FIXME:
Ben Widawsky868434c2015-03-11 10:49:32 +0200113 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
Imre Deak32608ca2015-03-11 11:10:27 +0200114 */
Imre Deak32608ca2015-03-11 11:10:27 +0200115 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
Ben Widawsky868434c2015-03-11 10:49:32 +0200116 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
Imre Deakd965e7ac2015-12-01 10:23:52 +0200117
118 /*
119 * Wa: Backlight PWM may stop in the asserted state, causing backlight
120 * to stay fully on.
121 */
Jani Nikula8aeaf642017-02-15 17:21:37 +0200122 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
123 PWM1_GATING_DIS | PWM2_GATING_DIS);
Imre Deaka82abe42015-03-27 14:00:04 +0200124}
125
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200126static void glk_init_clock_gating(struct drm_i915_private *dev_priv)
127{
Rodrigo Vivi8f067832017-09-05 12:30:13 -0700128 u32 val;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200129 gen9_init_clock_gating(dev_priv);
130
131 /*
132 * WaDisablePWMClockGating:glk
133 * Backlight PWM may stop in the asserted state, causing backlight
134 * to stay fully on.
135 */
136 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
137 PWM1_GATING_DIS | PWM2_GATING_DIS);
Ander Conselvan de Oliveiraf4f4b592017-02-22 08:34:29 +0200138
139 /* WaDDIIOTimeout:glk */
140 if (IS_GLK_REVID(dev_priv, 0, GLK_REVID_A1)) {
141 u32 val = I915_READ(CHICKEN_MISC_2);
142 val &= ~(GLK_CL0_PWR_DOWN |
143 GLK_CL1_PWR_DOWN |
144 GLK_CL2_PWR_DOWN);
145 I915_WRITE(CHICKEN_MISC_2, val);
146 }
147
Rodrigo Vivi8f067832017-09-05 12:30:13 -0700148 /* Display WA #1133: WaFbcSkipSegments:glk */
149 val = I915_READ(ILK_DPFC_CHICKEN);
150 val &= ~GLK_SKIP_SEG_COUNT_MASK;
151 val |= GLK_SKIP_SEG_EN | GLK_SKIP_SEG_COUNT(1);
152 I915_WRITE(ILK_DPFC_CHICKEN, val);
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200153}
154
Ville Syrjälä148ac1f2016-10-31 22:37:16 +0200155static void i915_pineview_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200156{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200157 u32 tmp;
158
159 tmp = I915_READ(CLKCFG);
160
161 switch (tmp & CLKCFG_FSB_MASK) {
162 case CLKCFG_FSB_533:
163 dev_priv->fsb_freq = 533; /* 133*4 */
164 break;
165 case CLKCFG_FSB_800:
166 dev_priv->fsb_freq = 800; /* 200*4 */
167 break;
168 case CLKCFG_FSB_667:
169 dev_priv->fsb_freq = 667; /* 167*4 */
170 break;
171 case CLKCFG_FSB_400:
172 dev_priv->fsb_freq = 400; /* 100*4 */
173 break;
174 }
175
176 switch (tmp & CLKCFG_MEM_MASK) {
177 case CLKCFG_MEM_533:
178 dev_priv->mem_freq = 533;
179 break;
180 case CLKCFG_MEM_667:
181 dev_priv->mem_freq = 667;
182 break;
183 case CLKCFG_MEM_800:
184 dev_priv->mem_freq = 800;
185 break;
186 }
187
188 /* detect pineview DDR3 setting */
189 tmp = I915_READ(CSHRDDR3CTL);
190 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
191}
192
Ville Syrjälä148ac1f2016-10-31 22:37:16 +0200193static void i915_ironlake_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200194{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200195 u16 ddrpll, csipll;
196
197 ddrpll = I915_READ16(DDRMPLL1);
198 csipll = I915_READ16(CSIPLL0);
199
200 switch (ddrpll & 0xff) {
201 case 0xc:
202 dev_priv->mem_freq = 800;
203 break;
204 case 0x10:
205 dev_priv->mem_freq = 1066;
206 break;
207 case 0x14:
208 dev_priv->mem_freq = 1333;
209 break;
210 case 0x18:
211 dev_priv->mem_freq = 1600;
212 break;
213 default:
214 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
215 ddrpll & 0xff);
216 dev_priv->mem_freq = 0;
217 break;
218 }
219
Daniel Vetter20e4d402012-08-08 23:35:39 +0200220 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200221
222 switch (csipll & 0x3ff) {
223 case 0x00c:
224 dev_priv->fsb_freq = 3200;
225 break;
226 case 0x00e:
227 dev_priv->fsb_freq = 3733;
228 break;
229 case 0x010:
230 dev_priv->fsb_freq = 4266;
231 break;
232 case 0x012:
233 dev_priv->fsb_freq = 4800;
234 break;
235 case 0x014:
236 dev_priv->fsb_freq = 5333;
237 break;
238 case 0x016:
239 dev_priv->fsb_freq = 5866;
240 break;
241 case 0x018:
242 dev_priv->fsb_freq = 6400;
243 break;
244 default:
245 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
246 csipll & 0x3ff);
247 dev_priv->fsb_freq = 0;
248 break;
249 }
250
251 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200252 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200253 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200254 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200255 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200256 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200257 }
258}
259
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300260static const struct cxsr_latency cxsr_latency_table[] = {
261 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
262 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
263 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
264 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
265 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
266
267 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
268 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
269 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
270 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
271 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
272
273 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
274 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
275 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
276 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
277 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
278
279 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
280 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
281 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
282 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
283 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
284
285 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
286 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
287 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
288 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
289 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
290
291 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
292 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
293 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
294 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
295 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
296};
297
Tvrtko Ursulin44a655c2016-10-13 11:09:23 +0100298static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
299 bool is_ddr3,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300300 int fsb,
301 int mem)
302{
303 const struct cxsr_latency *latency;
304 int i;
305
306 if (fsb == 0 || mem == 0)
307 return NULL;
308
309 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
310 latency = &cxsr_latency_table[i];
311 if (is_desktop == latency->is_desktop &&
312 is_ddr3 == latency->is_ddr3 &&
313 fsb == latency->fsb_freq && mem == latency->mem_freq)
314 return latency;
315 }
316
317 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
318
319 return NULL;
320}
321
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200322static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
323{
324 u32 val;
325
326 mutex_lock(&dev_priv->rps.hw_lock);
327
328 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
329 if (enable)
330 val &= ~FORCE_DDR_HIGH_FREQ;
331 else
332 val |= FORCE_DDR_HIGH_FREQ;
333 val &= ~FORCE_DDR_LOW_FREQ;
334 val |= FORCE_DDR_FREQ_REQ_ACK;
335 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
336
337 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
338 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
339 DRM_ERROR("timed out waiting for Punit DDR DVFS request\n");
340
341 mutex_unlock(&dev_priv->rps.hw_lock);
342}
343
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200344static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
345{
346 u32 val;
347
348 mutex_lock(&dev_priv->rps.hw_lock);
349
350 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
351 if (enable)
352 val |= DSP_MAXFIFO_PM5_ENABLE;
353 else
354 val &= ~DSP_MAXFIFO_PM5_ENABLE;
355 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
356
357 mutex_unlock(&dev_priv->rps.hw_lock);
358}
359
Ville Syrjäläf4998962015-03-10 17:02:21 +0200360#define FW_WM(value, plane) \
361 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
362
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200363static bool _intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300364{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200365 bool was_enabled;
Imre Deak5209b1f2014-07-01 12:36:17 +0300366 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300367
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100368 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200369 was_enabled = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300370 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300371 POSTING_READ(FW_BLC_SELF_VLV);
Jani Nikulac0f86832016-12-07 12:13:04 +0200372 } else if (IS_G4X(dev_priv) || IS_I965GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200373 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300374 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300375 POSTING_READ(FW_BLC_SELF);
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +0200376 } else if (IS_PINEVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200377 val = I915_READ(DSPFW3);
378 was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
379 if (enable)
380 val |= PINEVIEW_SELF_REFRESH_EN;
381 else
382 val &= ~PINEVIEW_SELF_REFRESH_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300383 I915_WRITE(DSPFW3, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300384 POSTING_READ(DSPFW3);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100385 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200386 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300387 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
388 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
389 I915_WRITE(FW_BLC_SELF, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300390 POSTING_READ(FW_BLC_SELF);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100391 } else if (IS_I915GM(dev_priv)) {
Ville Syrjäläacb91352016-07-29 17:57:02 +0300392 /*
393 * FIXME can't find a bit like this for 915G, and
394 * and yet it does have the related watermark in
395 * FW_BLC_SELF. What's going on?
396 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200397 was_enabled = I915_READ(INSTPM) & INSTPM_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300398 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
399 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
400 I915_WRITE(INSTPM, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300401 POSTING_READ(INSTPM);
Imre Deak5209b1f2014-07-01 12:36:17 +0300402 } else {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200403 return false;
Imre Deak5209b1f2014-07-01 12:36:17 +0300404 }
405
Ville Syrjälä1489bba2017-03-02 19:15:07 +0200406 trace_intel_memory_cxsr(dev_priv, was_enabled, enable);
407
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200408 DRM_DEBUG_KMS("memory self-refresh is %s (was %s)\n",
409 enableddisabled(enable),
410 enableddisabled(was_enabled));
411
412 return was_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300413}
414
Ville Syrjälä62571fc2017-04-21 21:14:23 +0300415/**
416 * intel_set_memory_cxsr - Configure CxSR state
417 * @dev_priv: i915 device
418 * @enable: Allow vs. disallow CxSR
419 *
420 * Allow or disallow the system to enter a special CxSR
421 * (C-state self refresh) state. What typically happens in CxSR mode
422 * is that several display FIFOs may get combined into a single larger
423 * FIFO for a particular plane (so called max FIFO mode) to allow the
424 * system to defer memory fetches longer, and the memory will enter
425 * self refresh.
426 *
427 * Note that enabling CxSR does not guarantee that the system enter
428 * this special mode, nor does it guarantee that the system stays
429 * in that mode once entered. So this just allows/disallows the system
430 * to autonomously utilize the CxSR mode. Other factors such as core
431 * C-states will affect when/if the system actually enters/exits the
432 * CxSR mode.
433 *
434 * Note that on VLV/CHV this actually only controls the max FIFO mode,
435 * and the system is free to enter/exit memory self refresh at any time
436 * even when the use of CxSR has been disallowed.
437 *
438 * While the system is actually in the CxSR/max FIFO mode, some plane
439 * control registers will not get latched on vblank. Thus in order to
440 * guarantee the system will respond to changes in the plane registers
441 * we must always disallow CxSR prior to making changes to those registers.
442 * Unfortunately the system will re-evaluate the CxSR conditions at
443 * frame start which happens after vblank start (which is when the plane
444 * registers would get latched), so we can't proceed with the plane update
445 * during the same frame where we disallowed CxSR.
446 *
447 * Certain platforms also have a deeper HPLL SR mode. Fortunately the
448 * HPLL SR mode depends on CxSR itself, so we don't have to hand hold
449 * the hardware w.r.t. HPLL SR when writing to plane registers.
450 * Disallowing just CxSR is sufficient.
451 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200452bool intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200453{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200454 bool ret;
455
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200456 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200457 ret = _intel_set_memory_cxsr(dev_priv, enable);
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300458 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
459 dev_priv->wm.vlv.cxsr = enable;
460 else if (IS_G4X(dev_priv))
461 dev_priv->wm.g4x.cxsr = enable;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200462 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200463
464 return ret;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200465}
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200466
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300467/*
468 * Latency for FIFO fetches is dependent on several factors:
469 * - memory configuration (speed, channels)
470 * - chipset
471 * - current MCH state
472 * It can be fairly high in some situations, so here we assume a fairly
473 * pessimal value. It's a tradeoff between extra memory fetches (if we
474 * set this value too high, the FIFO will fetch frequently to stay full)
475 * and power consumption (set it too low to save power and we might see
476 * FIFO underruns and display "flicker").
477 *
478 * A value of 5us seems to be a good balance; safe for very low end
479 * platforms but not overly aggressive on lower latency configs.
480 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100481static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300482
Ville Syrjäläb5004722015-03-05 21:19:47 +0200483#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
484 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
485
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200486static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
Ville Syrjäläb5004722015-03-05 21:19:47 +0200487{
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200488 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200489 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200490 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200491 enum pipe pipe = crtc->pipe;
492 int sprite0_start, sprite1_start;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200493
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200494 switch (pipe) {
Ville Syrjäläb5004722015-03-05 21:19:47 +0200495 uint32_t dsparb, dsparb2, dsparb3;
496 case PIPE_A:
497 dsparb = I915_READ(DSPARB);
498 dsparb2 = I915_READ(DSPARB2);
499 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
500 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
501 break;
502 case PIPE_B:
503 dsparb = I915_READ(DSPARB);
504 dsparb2 = I915_READ(DSPARB2);
505 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
506 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
507 break;
508 case PIPE_C:
509 dsparb2 = I915_READ(DSPARB2);
510 dsparb3 = I915_READ(DSPARB3);
511 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
512 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
513 break;
514 default:
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200515 MISSING_CASE(pipe);
516 return;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200517 }
518
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200519 fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
520 fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
521 fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
522 fifo_state->plane[PLANE_CURSOR] = 63;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200523}
524
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200525static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300526{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300527 uint32_t dsparb = I915_READ(DSPARB);
528 int size;
529
530 size = dsparb & 0x7f;
531 if (plane)
532 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
533
534 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
535 plane ? "B" : "A", size);
536
537 return size;
538}
539
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200540static int i830_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300541{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300542 uint32_t dsparb = I915_READ(DSPARB);
543 int size;
544
545 size = dsparb & 0x1ff;
546 if (plane)
547 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
548 size >>= 1; /* Convert to cachelines */
549
550 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
551 plane ? "B" : "A", size);
552
553 return size;
554}
555
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200556static int i845_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300557{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300558 uint32_t dsparb = I915_READ(DSPARB);
559 int size;
560
561 size = dsparb & 0x7f;
562 size >>= 2; /* Convert to cachelines */
563
564 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
565 plane ? "B" : "A",
566 size);
567
568 return size;
569}
570
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300571/* Pineview has different values for various configs */
572static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300573 .fifo_size = PINEVIEW_DISPLAY_FIFO,
574 .max_wm = PINEVIEW_MAX_WM,
575 .default_wm = PINEVIEW_DFT_WM,
576 .guard_size = PINEVIEW_GUARD_WM,
577 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300578};
579static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300580 .fifo_size = PINEVIEW_DISPLAY_FIFO,
581 .max_wm = PINEVIEW_MAX_WM,
582 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
583 .guard_size = PINEVIEW_GUARD_WM,
584 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300585};
586static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300587 .fifo_size = PINEVIEW_CURSOR_FIFO,
588 .max_wm = PINEVIEW_CURSOR_MAX_WM,
589 .default_wm = PINEVIEW_CURSOR_DFT_WM,
590 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
591 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300592};
593static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300594 .fifo_size = PINEVIEW_CURSOR_FIFO,
595 .max_wm = PINEVIEW_CURSOR_MAX_WM,
596 .default_wm = PINEVIEW_CURSOR_DFT_WM,
597 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
598 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300599};
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300600static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300601 .fifo_size = I965_CURSOR_FIFO,
602 .max_wm = I965_CURSOR_MAX_WM,
603 .default_wm = I965_CURSOR_DFT_WM,
604 .guard_size = 2,
605 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300606};
607static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300608 .fifo_size = I945_FIFO_SIZE,
609 .max_wm = I915_MAX_WM,
610 .default_wm = 1,
611 .guard_size = 2,
612 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300613};
614static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300615 .fifo_size = I915_FIFO_SIZE,
616 .max_wm = I915_MAX_WM,
617 .default_wm = 1,
618 .guard_size = 2,
619 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300620};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300621static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300622 .fifo_size = I855GM_FIFO_SIZE,
623 .max_wm = I915_MAX_WM,
624 .default_wm = 1,
625 .guard_size = 2,
626 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300627};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300628static const struct intel_watermark_params i830_bc_wm_info = {
629 .fifo_size = I855GM_FIFO_SIZE,
630 .max_wm = I915_MAX_WM/2,
631 .default_wm = 1,
632 .guard_size = 2,
633 .cacheline_size = I830_FIFO_LINE_SIZE,
634};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200635static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300636 .fifo_size = I830_FIFO_SIZE,
637 .max_wm = I915_MAX_WM,
638 .default_wm = 1,
639 .guard_size = 2,
640 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300641};
642
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300643/**
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300644 * intel_wm_method1 - Method 1 / "small buffer" watermark formula
645 * @pixel_rate: Pipe pixel rate in kHz
646 * @cpp: Plane bytes per pixel
647 * @latency: Memory wakeup latency in 0.1us units
648 *
649 * Compute the watermark using the method 1 or "small buffer"
650 * formula. The caller may additonally add extra cachelines
651 * to account for TLB misses and clock crossings.
652 *
653 * This method is concerned with the short term drain rate
654 * of the FIFO, ie. it does not account for blanking periods
655 * which would effectively reduce the average drain rate across
656 * a longer period. The name "small" refers to the fact the
657 * FIFO is relatively small compared to the amount of data
658 * fetched.
659 *
660 * The FIFO level vs. time graph might look something like:
661 *
662 * |\ |\
663 * | \ | \
664 * __---__---__ (- plane active, _ blanking)
665 * -> time
666 *
667 * or perhaps like this:
668 *
669 * |\|\ |\|\
670 * __----__----__ (- plane active, _ blanking)
671 * -> time
672 *
673 * Returns:
674 * The watermark in bytes
675 */
676static unsigned int intel_wm_method1(unsigned int pixel_rate,
677 unsigned int cpp,
678 unsigned int latency)
679{
680 uint64_t ret;
681
682 ret = (uint64_t) pixel_rate * cpp * latency;
683 ret = DIV_ROUND_UP_ULL(ret, 10000);
684
685 return ret;
686}
687
688/**
689 * intel_wm_method2 - Method 2 / "large buffer" watermark formula
690 * @pixel_rate: Pipe pixel rate in kHz
691 * @htotal: Pipe horizontal total
692 * @width: Plane width in pixels
693 * @cpp: Plane bytes per pixel
694 * @latency: Memory wakeup latency in 0.1us units
695 *
696 * Compute the watermark using the method 2 or "large buffer"
697 * formula. The caller may additonally add extra cachelines
698 * to account for TLB misses and clock crossings.
699 *
700 * This method is concerned with the long term drain rate
701 * of the FIFO, ie. it does account for blanking periods
702 * which effectively reduce the average drain rate across
703 * a longer period. The name "large" refers to the fact the
704 * FIFO is relatively large compared to the amount of data
705 * fetched.
706 *
707 * The FIFO level vs. time graph might look something like:
708 *
709 * |\___ |\___
710 * | \___ | \___
711 * | \ | \
712 * __ --__--__--__--__--__--__ (- plane active, _ blanking)
713 * -> time
714 *
715 * Returns:
716 * The watermark in bytes
717 */
718static unsigned int intel_wm_method2(unsigned int pixel_rate,
719 unsigned int htotal,
720 unsigned int width,
721 unsigned int cpp,
722 unsigned int latency)
723{
724 unsigned int ret;
725
726 /*
727 * FIXME remove once all users are computing
728 * watermarks in the correct place.
729 */
730 if (WARN_ON_ONCE(htotal == 0))
731 htotal = 1;
732
733 ret = (latency * pixel_rate) / (htotal * 10000);
734 ret = (ret + 1) * width * cpp;
735
736 return ret;
737}
738
739/**
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300740 * intel_calculate_wm - calculate watermark level
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300741 * @pixel_rate: pixel clock
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300742 * @wm: chip FIFO params
Ville Syrjäläac484962016-01-20 21:05:26 +0200743 * @cpp: bytes per pixel
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300744 * @latency_ns: memory latency for the platform
745 *
746 * Calculate the watermark level (the level at which the display plane will
747 * start fetching from memory again). Each chip has a different display
748 * FIFO size and allocation, so the caller needs to figure that out and pass
749 * in the correct intel_watermark_params structure.
750 *
751 * As the pixel clock runs, the FIFO will be drained at a rate that depends
752 * on the pixel size. When it reaches the watermark level, it'll start
753 * fetching FIFO line sized based chunks from memory until the FIFO fills
754 * past the watermark point. If the FIFO drains completely, a FIFO underrun
755 * will occur, and a display engine hang could result.
756 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300757static unsigned int intel_calculate_wm(int pixel_rate,
758 const struct intel_watermark_params *wm,
759 int fifo_size, int cpp,
760 unsigned int latency_ns)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300761{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300762 int entries, wm_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300763
764 /*
765 * Note: we need to make sure we don't overflow for various clock &
766 * latency values.
767 * clocks go from a few thousand to several hundred thousand.
768 * latency is usually a few thousand
769 */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300770 entries = intel_wm_method1(pixel_rate, cpp,
771 latency_ns / 100);
772 entries = DIV_ROUND_UP(entries, wm->cacheline_size) +
773 wm->guard_size;
774 DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300775
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300776 wm_size = fifo_size - entries;
777 DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300778
779 /* Don't promote wm_size to unsigned... */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300780 if (wm_size > wm->max_wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300781 wm_size = wm->max_wm;
782 if (wm_size <= 0)
783 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300784
785 /*
786 * Bspec seems to indicate that the value shouldn't be lower than
787 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
788 * Lets go for 8 which is the burst size since certain platforms
789 * already use a hardcoded 8 (which is what the spec says should be
790 * done).
791 */
792 if (wm_size <= 8)
793 wm_size = 8;
794
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300795 return wm_size;
796}
797
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300798static bool is_disabling(int old, int new, int threshold)
799{
800 return old >= threshold && new < threshold;
801}
802
803static bool is_enabling(int old, int new, int threshold)
804{
805 return old < threshold && new >= threshold;
806}
807
Ville Syrjälä6d5019b2017-04-21 21:14:20 +0300808static int intel_wm_num_levels(struct drm_i915_private *dev_priv)
809{
810 return dev_priv->wm.max_level + 1;
811}
812
Ville Syrjälä24304d812017-03-14 17:10:49 +0200813static bool intel_wm_plane_visible(const struct intel_crtc_state *crtc_state,
814 const struct intel_plane_state *plane_state)
815{
816 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
817
818 /* FIXME check the 'enable' instead */
819 if (!crtc_state->base.active)
820 return false;
821
822 /*
823 * Treat cursor with fb as always visible since cursor updates
824 * can happen faster than the vrefresh rate, and the current
825 * watermark code doesn't handle that correctly. Cursor updates
826 * which set/clear the fb or change the cursor size are going
827 * to get throttled by intel_legacy_cursor_update() to work
828 * around this problem with the watermark code.
829 */
830 if (plane->id == PLANE_CURSOR)
831 return plane_state->base.fb != NULL;
832 else
833 return plane_state->base.visible;
834}
835
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200836static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300837{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200838 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300839
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200840 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200841 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300842 if (enabled)
843 return NULL;
844 enabled = crtc;
845 }
846 }
847
848 return enabled;
849}
850
Ville Syrjälä432081b2016-10-31 22:37:03 +0200851static void pineview_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300852{
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200853 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200854 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300855 const struct cxsr_latency *latency;
856 u32 reg;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +0300857 unsigned int wm;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300858
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100859 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
860 dev_priv->is_ddr3,
861 dev_priv->fsb_freq,
862 dev_priv->mem_freq);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300863 if (!latency) {
864 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300865 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300866 return;
867 }
868
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200869 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300870 if (crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200871 const struct drm_display_mode *adjusted_mode =
872 &crtc->config->base.adjusted_mode;
873 const struct drm_framebuffer *fb =
874 crtc->base.primary->state->fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200875 int cpp = fb->format->cpp[0];
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300876 int clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300877
878 /* Display SR */
879 wm = intel_calculate_wm(clock, &pineview_display_wm,
880 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200881 cpp, latency->display_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300882 reg = I915_READ(DSPFW1);
883 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200884 reg |= FW_WM(wm, SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300885 I915_WRITE(DSPFW1, reg);
886 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
887
888 /* cursor SR */
889 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
890 pineview_display_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300891 4, latency->cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300892 reg = I915_READ(DSPFW3);
893 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200894 reg |= FW_WM(wm, CURSOR_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300895 I915_WRITE(DSPFW3, reg);
896
897 /* Display HPLL off SR */
898 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
899 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200900 cpp, latency->display_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300901 reg = I915_READ(DSPFW3);
902 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200903 reg |= FW_WM(wm, HPLL_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300904 I915_WRITE(DSPFW3, reg);
905
906 /* cursor HPLL off SR */
907 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
908 pineview_display_hplloff_wm.fifo_size,
Ville Syrjälä99834b12017-04-21 21:14:24 +0300909 4, latency->cursor_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300910 reg = I915_READ(DSPFW3);
911 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200912 reg |= FW_WM(wm, HPLL_CURSOR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300913 I915_WRITE(DSPFW3, reg);
914 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
915
Imre Deak5209b1f2014-07-01 12:36:17 +0300916 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300917 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300918 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300919 }
920}
921
Ville Syrjälä0f95ff82017-04-21 21:14:26 +0300922/*
923 * Documentation says:
924 * "If the line size is small, the TLB fetches can get in the way of the
925 * data fetches, causing some lag in the pixel data return which is not
926 * accounted for in the above formulas. The following adjustment only
927 * needs to be applied if eight whole lines fit in the buffer at once.
928 * The WM is adjusted upwards by the difference between the FIFO size
929 * and the size of 8 whole lines. This adjustment is always performed
930 * in the actual pixel depth regardless of whether FBC is enabled or not."
931 */
932static int g4x_tlb_miss_wa(int fifo_size, int width, int cpp)
933{
934 int tlb_miss = fifo_size * 64 - width * cpp * 8;
935
936 return max(0, tlb_miss);
937}
938
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300939static void g4x_write_wm_values(struct drm_i915_private *dev_priv,
940 const struct g4x_wm_values *wm)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300941{
Ville Syrjäläe93329a2017-04-21 21:14:31 +0300942 enum pipe pipe;
943
944 for_each_pipe(dev_priv, pipe)
945 trace_g4x_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
946
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300947 I915_WRITE(DSPFW1,
948 FW_WM(wm->sr.plane, SR) |
949 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
950 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
951 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
952 I915_WRITE(DSPFW2,
953 (wm->fbc_en ? DSPFW_FBC_SR_EN : 0) |
954 FW_WM(wm->sr.fbc, FBC_SR) |
955 FW_WM(wm->hpll.fbc, FBC_HPLL_SR) |
956 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEB) |
957 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
958 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
959 I915_WRITE(DSPFW3,
960 (wm->hpll_en ? DSPFW_HPLL_SR_EN : 0) |
961 FW_WM(wm->sr.cursor, CURSOR_SR) |
962 FW_WM(wm->hpll.cursor, HPLL_CURSOR) |
963 FW_WM(wm->hpll.plane, HPLL_SR));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300964
Ville Syrjälä04548cb2017-04-21 21:14:29 +0300965 POSTING_READ(DSPFW1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300966}
967
Ville Syrjälä15665972015-03-10 16:16:28 +0200968#define FW_WM_VLV(value, plane) \
969 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
970
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200971static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200972 const struct vlv_wm_values *wm)
973{
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200974 enum pipe pipe;
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200975
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200976 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläc137d662017-03-02 19:15:06 +0200977 trace_vlv_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
978
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200979 I915_WRITE(VLV_DDL(pipe),
980 (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
981 (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
982 (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
983 (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
984 }
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200985
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +0200986 /*
987 * Zero the (unused) WM1 watermarks, and also clear all the
988 * high order bits so that there are no out of bounds values
989 * present in the registers during the reprogramming.
990 */
991 I915_WRITE(DSPHOWM, 0);
992 I915_WRITE(DSPHOWM1, 0);
993 I915_WRITE(DSPFW4, 0);
994 I915_WRITE(DSPFW5, 0);
995 I915_WRITE(DSPFW6, 0);
996
Ville Syrjäläae801522015-03-05 21:19:49 +0200997 I915_WRITE(DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +0200998 FW_WM(wm->sr.plane, SR) |
Ville Syrjälä1b313892016-11-28 19:37:08 +0200999 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
1000 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
1001 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Ville Syrjäläae801522015-03-05 21:19:49 +02001002 I915_WRITE(DSPFW2,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001003 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
1004 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
1005 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Ville Syrjäläae801522015-03-05 21:19:49 +02001006 I915_WRITE(DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +02001007 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +02001008
1009 if (IS_CHERRYVIEW(dev_priv)) {
1010 I915_WRITE(DSPFW7_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001011 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1012 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001013 I915_WRITE(DSPFW8_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001014 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
1015 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
Ville Syrjäläae801522015-03-05 21:19:49 +02001016 I915_WRITE(DSPFW9_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001017 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
1018 FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001019 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001020 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001021 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
1022 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
1023 FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
1024 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1025 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1026 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1027 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1028 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1029 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001030 } else {
1031 I915_WRITE(DSPFW7,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001032 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1033 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +02001034 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +02001035 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +02001036 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1037 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1038 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1039 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1040 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1041 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +02001042 }
1043
1044 POSTING_READ(DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001045}
1046
Ville Syrjälä15665972015-03-10 16:16:28 +02001047#undef FW_WM_VLV
1048
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001049static void g4x_setup_wm_latency(struct drm_i915_private *dev_priv)
1050{
1051 /* all latencies in usec */
1052 dev_priv->wm.pri_latency[G4X_WM_LEVEL_NORMAL] = 5;
1053 dev_priv->wm.pri_latency[G4X_WM_LEVEL_SR] = 12;
Ville Syrjälä79d94302017-04-21 21:14:30 +03001054 dev_priv->wm.pri_latency[G4X_WM_LEVEL_HPLL] = 35;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001055
Ville Syrjälä79d94302017-04-21 21:14:30 +03001056 dev_priv->wm.max_level = G4X_WM_LEVEL_HPLL;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001057}
1058
1059static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
1060{
1061 /*
1062 * DSPCNTR[13] supposedly controls whether the
1063 * primary plane can use the FIFO space otherwise
1064 * reserved for the sprite plane. It's not 100% clear
1065 * what the actual FIFO size is, but it looks like we
1066 * can happily set both primary and sprite watermarks
1067 * up to 127 cachelines. So that would seem to mean
1068 * that either DSPCNTR[13] doesn't do anything, or that
1069 * the total FIFO is >= 256 cachelines in size. Either
1070 * way, we don't seem to have to worry about this
1071 * repartitioning as the maximum watermark value the
1072 * register can hold for each plane is lower than the
1073 * minimum FIFO size.
1074 */
1075 switch (plane_id) {
1076 case PLANE_CURSOR:
1077 return 63;
1078 case PLANE_PRIMARY:
1079 return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
1080 case PLANE_SPRITE0:
1081 return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
1082 default:
1083 MISSING_CASE(plane_id);
1084 return 0;
1085 }
1086}
1087
1088static int g4x_fbc_fifo_size(int level)
1089{
1090 switch (level) {
1091 case G4X_WM_LEVEL_SR:
1092 return 7;
1093 case G4X_WM_LEVEL_HPLL:
1094 return 15;
1095 default:
1096 MISSING_CASE(level);
1097 return 0;
1098 }
1099}
1100
1101static uint16_t g4x_compute_wm(const struct intel_crtc_state *crtc_state,
1102 const struct intel_plane_state *plane_state,
1103 int level)
1104{
1105 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1106 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1107 const struct drm_display_mode *adjusted_mode =
1108 &crtc_state->base.adjusted_mode;
1109 int clock, htotal, cpp, width, wm;
1110 int latency = dev_priv->wm.pri_latency[level] * 10;
1111
1112 if (latency == 0)
1113 return USHRT_MAX;
1114
1115 if (!intel_wm_plane_visible(crtc_state, plane_state))
1116 return 0;
1117
1118 /*
1119 * Not 100% sure which way ELK should go here as the
1120 * spec only says CL/CTG should assume 32bpp and BW
1121 * doesn't need to. But as these things followed the
1122 * mobile vs. desktop lines on gen3 as well, let's
1123 * assume ELK doesn't need this.
1124 *
1125 * The spec also fails to list such a restriction for
1126 * the HPLL watermark, which seems a little strange.
1127 * Let's use 32bpp for the HPLL watermark as well.
1128 */
1129 if (IS_GM45(dev_priv) && plane->id == PLANE_PRIMARY &&
1130 level != G4X_WM_LEVEL_NORMAL)
1131 cpp = 4;
1132 else
1133 cpp = plane_state->base.fb->format->cpp[0];
1134
1135 clock = adjusted_mode->crtc_clock;
1136 htotal = adjusted_mode->crtc_htotal;
1137
1138 if (plane->id == PLANE_CURSOR)
1139 width = plane_state->base.crtc_w;
1140 else
1141 width = drm_rect_width(&plane_state->base.dst);
1142
1143 if (plane->id == PLANE_CURSOR) {
1144 wm = intel_wm_method2(clock, htotal, width, cpp, latency);
1145 } else if (plane->id == PLANE_PRIMARY &&
1146 level == G4X_WM_LEVEL_NORMAL) {
1147 wm = intel_wm_method1(clock, cpp, latency);
1148 } else {
1149 int small, large;
1150
1151 small = intel_wm_method1(clock, cpp, latency);
1152 large = intel_wm_method2(clock, htotal, width, cpp, latency);
1153
1154 wm = min(small, large);
1155 }
1156
1157 wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
1158 width, cpp);
1159
1160 wm = DIV_ROUND_UP(wm, 64) + 2;
1161
1162 return min_t(int, wm, USHRT_MAX);
1163}
1164
1165static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1166 int level, enum plane_id plane_id, u16 value)
1167{
1168 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1169 bool dirty = false;
1170
1171 for (; level < intel_wm_num_levels(dev_priv); level++) {
1172 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1173
1174 dirty |= raw->plane[plane_id] != value;
1175 raw->plane[plane_id] = value;
1176 }
1177
1178 return dirty;
1179}
1180
1181static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
1182 int level, u16 value)
1183{
1184 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1185 bool dirty = false;
1186
1187 /* NORMAL level doesn't have an FBC watermark */
1188 level = max(level, G4X_WM_LEVEL_SR);
1189
1190 for (; level < intel_wm_num_levels(dev_priv); level++) {
1191 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1192
1193 dirty |= raw->fbc != value;
1194 raw->fbc = value;
1195 }
1196
1197 return dirty;
1198}
1199
1200static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
1201 const struct intel_plane_state *pstate,
1202 uint32_t pri_val);
1203
1204static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1205 const struct intel_plane_state *plane_state)
1206{
1207 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1208 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
1209 enum plane_id plane_id = plane->id;
1210 bool dirty = false;
1211 int level;
1212
1213 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1214 dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1215 if (plane_id == PLANE_PRIMARY)
1216 dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
1217 goto out;
1218 }
1219
1220 for (level = 0; level < num_levels; level++) {
1221 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1222 int wm, max_wm;
1223
1224 wm = g4x_compute_wm(crtc_state, plane_state, level);
1225 max_wm = g4x_plane_fifo_size(plane_id, level);
1226
1227 if (wm > max_wm)
1228 break;
1229
1230 dirty |= raw->plane[plane_id] != wm;
1231 raw->plane[plane_id] = wm;
1232
1233 if (plane_id != PLANE_PRIMARY ||
1234 level == G4X_WM_LEVEL_NORMAL)
1235 continue;
1236
1237 wm = ilk_compute_fbc_wm(crtc_state, plane_state,
1238 raw->plane[plane_id]);
1239 max_wm = g4x_fbc_fifo_size(level);
1240
1241 /*
1242 * FBC wm is not mandatory as we
1243 * can always just disable its use.
1244 */
1245 if (wm > max_wm)
1246 wm = USHRT_MAX;
1247
1248 dirty |= raw->fbc != wm;
1249 raw->fbc = wm;
1250 }
1251
1252 /* mark watermarks as invalid */
1253 dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1254
1255 if (plane_id == PLANE_PRIMARY)
1256 dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
1257
1258 out:
1259 if (dirty) {
1260 DRM_DEBUG_KMS("%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
1261 plane->base.name,
1262 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
1263 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
1264 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
1265
1266 if (plane_id == PLANE_PRIMARY)
1267 DRM_DEBUG_KMS("FBC watermarks: SR=%d, HPLL=%d\n",
1268 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
1269 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
1270 }
1271
1272 return dirty;
1273}
1274
1275static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1276 enum plane_id plane_id, int level)
1277{
1278 const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1279
1280 return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
1281}
1282
1283static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
1284 int level)
1285{
1286 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1287
1288 if (level > dev_priv->wm.max_level)
1289 return false;
1290
1291 return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1292 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1293 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1294}
1295
1296/* mark all levels starting from 'level' as invalid */
1297static void g4x_invalidate_wms(struct intel_crtc *crtc,
1298 struct g4x_wm_state *wm_state, int level)
1299{
1300 if (level <= G4X_WM_LEVEL_NORMAL) {
1301 enum plane_id plane_id;
1302
1303 for_each_plane_id_on_crtc(crtc, plane_id)
1304 wm_state->wm.plane[plane_id] = USHRT_MAX;
1305 }
1306
1307 if (level <= G4X_WM_LEVEL_SR) {
1308 wm_state->cxsr = false;
1309 wm_state->sr.cursor = USHRT_MAX;
1310 wm_state->sr.plane = USHRT_MAX;
1311 wm_state->sr.fbc = USHRT_MAX;
1312 }
1313
1314 if (level <= G4X_WM_LEVEL_HPLL) {
1315 wm_state->hpll_en = false;
1316 wm_state->hpll.cursor = USHRT_MAX;
1317 wm_state->hpll.plane = USHRT_MAX;
1318 wm_state->hpll.fbc = USHRT_MAX;
1319 }
1320}
1321
1322static int g4x_compute_pipe_wm(struct intel_crtc_state *crtc_state)
1323{
1324 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1325 struct intel_atomic_state *state =
1326 to_intel_atomic_state(crtc_state->base.state);
1327 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
1328 int num_active_planes = hweight32(crtc_state->active_planes &
1329 ~BIT(PLANE_CURSOR));
1330 const struct g4x_pipe_wm *raw;
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001331 const struct intel_plane_state *old_plane_state;
1332 const struct intel_plane_state *new_plane_state;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001333 struct intel_plane *plane;
1334 enum plane_id plane_id;
1335 int i, level;
1336 unsigned int dirty = 0;
1337
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001338 for_each_oldnew_intel_plane_in_state(state, plane,
1339 old_plane_state,
1340 new_plane_state, i) {
1341 if (new_plane_state->base.crtc != &crtc->base &&
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001342 old_plane_state->base.crtc != &crtc->base)
1343 continue;
1344
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001345 if (g4x_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä04548cb2017-04-21 21:14:29 +03001346 dirty |= BIT(plane->id);
1347 }
1348
1349 if (!dirty)
1350 return 0;
1351
1352 level = G4X_WM_LEVEL_NORMAL;
1353 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1354 goto out;
1355
1356 raw = &crtc_state->wm.g4x.raw[level];
1357 for_each_plane_id_on_crtc(crtc, plane_id)
1358 wm_state->wm.plane[plane_id] = raw->plane[plane_id];
1359
1360 level = G4X_WM_LEVEL_SR;
1361
1362 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1363 goto out;
1364
1365 raw = &crtc_state->wm.g4x.raw[level];
1366 wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
1367 wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
1368 wm_state->sr.fbc = raw->fbc;
1369
1370 wm_state->cxsr = num_active_planes == BIT(PLANE_PRIMARY);
1371
1372 level = G4X_WM_LEVEL_HPLL;
1373
1374 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1375 goto out;
1376
1377 raw = &crtc_state->wm.g4x.raw[level];
1378 wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
1379 wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
1380 wm_state->hpll.fbc = raw->fbc;
1381
1382 wm_state->hpll_en = wm_state->cxsr;
1383
1384 level++;
1385
1386 out:
1387 if (level == G4X_WM_LEVEL_NORMAL)
1388 return -EINVAL;
1389
1390 /* invalidate the higher levels */
1391 g4x_invalidate_wms(crtc, wm_state, level);
1392
1393 /*
1394 * Determine if the FBC watermark(s) can be used. IF
1395 * this isn't the case we prefer to disable the FBC
1396 ( watermark(s) rather than disable the SR/HPLL
1397 * level(s) entirely.
1398 */
1399 wm_state->fbc_en = level > G4X_WM_LEVEL_NORMAL;
1400
1401 if (level >= G4X_WM_LEVEL_SR &&
1402 wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
1403 wm_state->fbc_en = false;
1404 else if (level >= G4X_WM_LEVEL_HPLL &&
1405 wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
1406 wm_state->fbc_en = false;
1407
1408 return 0;
1409}
1410
1411static int g4x_compute_intermediate_wm(struct drm_device *dev,
1412 struct intel_crtc *crtc,
1413 struct intel_crtc_state *crtc_state)
1414{
1415 struct g4x_wm_state *intermediate = &crtc_state->wm.g4x.intermediate;
1416 const struct g4x_wm_state *optimal = &crtc_state->wm.g4x.optimal;
1417 const struct g4x_wm_state *active = &crtc->wm.active.g4x;
1418 enum plane_id plane_id;
1419
1420 intermediate->cxsr = optimal->cxsr && active->cxsr &&
1421 !crtc_state->disable_cxsr;
1422 intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
1423 !crtc_state->disable_cxsr;
1424 intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
1425
1426 for_each_plane_id_on_crtc(crtc, plane_id) {
1427 intermediate->wm.plane[plane_id] =
1428 max(optimal->wm.plane[plane_id],
1429 active->wm.plane[plane_id]);
1430
1431 WARN_ON(intermediate->wm.plane[plane_id] >
1432 g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
1433 }
1434
1435 intermediate->sr.plane = max(optimal->sr.plane,
1436 active->sr.plane);
1437 intermediate->sr.cursor = max(optimal->sr.cursor,
1438 active->sr.cursor);
1439 intermediate->sr.fbc = max(optimal->sr.fbc,
1440 active->sr.fbc);
1441
1442 intermediate->hpll.plane = max(optimal->hpll.plane,
1443 active->hpll.plane);
1444 intermediate->hpll.cursor = max(optimal->hpll.cursor,
1445 active->hpll.cursor);
1446 intermediate->hpll.fbc = max(optimal->hpll.fbc,
1447 active->hpll.fbc);
1448
1449 WARN_ON((intermediate->sr.plane >
1450 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
1451 intermediate->sr.cursor >
1452 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
1453 intermediate->cxsr);
1454 WARN_ON((intermediate->sr.plane >
1455 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
1456 intermediate->sr.cursor >
1457 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
1458 intermediate->hpll_en);
1459
1460 WARN_ON(intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
1461 intermediate->fbc_en && intermediate->cxsr);
1462 WARN_ON(intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
1463 intermediate->fbc_en && intermediate->hpll_en);
1464
1465 /*
1466 * If our intermediate WM are identical to the final WM, then we can
1467 * omit the post-vblank programming; only update if it's different.
1468 */
1469 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
1470 crtc_state->wm.need_postvbl_update = true;
1471
1472 return 0;
1473}
1474
1475static void g4x_merge_wm(struct drm_i915_private *dev_priv,
1476 struct g4x_wm_values *wm)
1477{
1478 struct intel_crtc *crtc;
1479 int num_active_crtcs = 0;
1480
1481 wm->cxsr = true;
1482 wm->hpll_en = true;
1483 wm->fbc_en = true;
1484
1485 for_each_intel_crtc(&dev_priv->drm, crtc) {
1486 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1487
1488 if (!crtc->active)
1489 continue;
1490
1491 if (!wm_state->cxsr)
1492 wm->cxsr = false;
1493 if (!wm_state->hpll_en)
1494 wm->hpll_en = false;
1495 if (!wm_state->fbc_en)
1496 wm->fbc_en = false;
1497
1498 num_active_crtcs++;
1499 }
1500
1501 if (num_active_crtcs != 1) {
1502 wm->cxsr = false;
1503 wm->hpll_en = false;
1504 wm->fbc_en = false;
1505 }
1506
1507 for_each_intel_crtc(&dev_priv->drm, crtc) {
1508 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1509 enum pipe pipe = crtc->pipe;
1510
1511 wm->pipe[pipe] = wm_state->wm;
1512 if (crtc->active && wm->cxsr)
1513 wm->sr = wm_state->sr;
1514 if (crtc->active && wm->hpll_en)
1515 wm->hpll = wm_state->hpll;
1516 }
1517}
1518
1519static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
1520{
1521 struct g4x_wm_values *old_wm = &dev_priv->wm.g4x;
1522 struct g4x_wm_values new_wm = {};
1523
1524 g4x_merge_wm(dev_priv, &new_wm);
1525
1526 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
1527 return;
1528
1529 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
1530 _intel_set_memory_cxsr(dev_priv, false);
1531
1532 g4x_write_wm_values(dev_priv, &new_wm);
1533
1534 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
1535 _intel_set_memory_cxsr(dev_priv, true);
1536
1537 *old_wm = new_wm;
1538}
1539
1540static void g4x_initial_watermarks(struct intel_atomic_state *state,
1541 struct intel_crtc_state *crtc_state)
1542{
1543 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1544 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1545
1546 mutex_lock(&dev_priv->wm.wm_mutex);
1547 crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
1548 g4x_program_watermarks(dev_priv);
1549 mutex_unlock(&dev_priv->wm.wm_mutex);
1550}
1551
1552static void g4x_optimize_watermarks(struct intel_atomic_state *state,
1553 struct intel_crtc_state *crtc_state)
1554{
1555 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1556 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
1557
1558 if (!crtc_state->wm.need_postvbl_update)
1559 return;
1560
1561 mutex_lock(&dev_priv->wm.wm_mutex);
1562 intel_crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
1563 g4x_program_watermarks(dev_priv);
1564 mutex_unlock(&dev_priv->wm.wm_mutex);
1565}
1566
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001567/* latency must be in 0.1us units. */
1568static unsigned int vlv_wm_method2(unsigned int pixel_rate,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001569 unsigned int htotal,
1570 unsigned int width,
Ville Syrjäläac484962016-01-20 21:05:26 +02001571 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001572 unsigned int latency)
1573{
1574 unsigned int ret;
1575
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03001576 ret = intel_wm_method2(pixel_rate, htotal,
1577 width, cpp, latency);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001578 ret = DIV_ROUND_UP(ret, 64);
1579
1580 return ret;
1581}
1582
Ville Syrjäläbb726512016-10-31 22:37:24 +02001583static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001584{
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001585 /* all latencies in usec */
1586 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1587
Ville Syrjälä58590c12015-09-08 21:05:12 +03001588 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
1589
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001590 if (IS_CHERRYVIEW(dev_priv)) {
1591 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1592 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001593
1594 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001595 }
1596}
1597
Ville Syrjäläe339d672016-11-28 19:37:17 +02001598static uint16_t vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1599 const struct intel_plane_state *plane_state,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001600 int level)
1601{
Ville Syrjäläe339d672016-11-28 19:37:17 +02001602 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001603 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläe339d672016-11-28 19:37:17 +02001604 const struct drm_display_mode *adjusted_mode =
1605 &crtc_state->base.adjusted_mode;
Ville Syrjäläac484962016-01-20 21:05:26 +02001606 int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001607
1608 if (dev_priv->wm.pri_latency[level] == 0)
1609 return USHRT_MAX;
1610
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001611 if (!intel_wm_plane_visible(crtc_state, plane_state))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001612 return 0;
1613
Daniel Vetteref426c12017-01-04 11:41:10 +01001614 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjäläe339d672016-11-28 19:37:17 +02001615 clock = adjusted_mode->crtc_clock;
1616 htotal = adjusted_mode->crtc_htotal;
1617 width = crtc_state->pipe_src_w;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001618
Ville Syrjälä709f3fc2017-03-03 17:19:26 +02001619 if (plane->id == PLANE_CURSOR) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001620 /*
1621 * FIXME the formula gives values that are
1622 * too big for the cursor FIFO, and hence we
1623 * would never be able to use cursors. For
1624 * now just hardcode the watermark.
1625 */
1626 wm = 63;
1627 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +02001628 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001629 dev_priv->wm.pri_latency[level] * 10);
1630 }
1631
1632 return min_t(int, wm, USHRT_MAX);
1633}
1634
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001635static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1636{
1637 return (active_planes & (BIT(PLANE_SPRITE0) |
1638 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1639}
1640
Ville Syrjälä5012e602017-03-02 19:14:56 +02001641static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001642{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001643 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001644 const struct g4x_pipe_wm *raw =
Ville Syrjälä5012e602017-03-02 19:14:56 +02001645 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001646 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001647 unsigned int active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1648 int num_active_planes = hweight32(active_planes);
1649 const int fifo_size = 511;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001650 int fifo_extra, fifo_left = fifo_size;
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001651 int sprite0_fifo_extra = 0;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001652 unsigned int total_rate;
1653 enum plane_id plane_id;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001654
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001655 /*
1656 * When enabling sprite0 after sprite1 has already been enabled
1657 * we tend to get an underrun unless sprite0 already has some
1658 * FIFO space allcoated. Hence we always allocate at least one
1659 * cacheline for sprite0 whenever sprite1 is enabled.
1660 *
1661 * All other plane enable sequences appear immune to this problem.
1662 */
1663 if (vlv_need_sprite0_fifo_workaround(active_planes))
1664 sprite0_fifo_extra = 1;
1665
Ville Syrjälä5012e602017-03-02 19:14:56 +02001666 total_rate = raw->plane[PLANE_PRIMARY] +
1667 raw->plane[PLANE_SPRITE0] +
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001668 raw->plane[PLANE_SPRITE1] +
1669 sprite0_fifo_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001670
Ville Syrjälä5012e602017-03-02 19:14:56 +02001671 if (total_rate > fifo_size)
1672 return -EINVAL;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001673
Ville Syrjälä5012e602017-03-02 19:14:56 +02001674 if (total_rate == 0)
1675 total_rate = 1;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001676
Ville Syrjälä5012e602017-03-02 19:14:56 +02001677 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001678 unsigned int rate;
1679
Ville Syrjälä5012e602017-03-02 19:14:56 +02001680 if ((active_planes & BIT(plane_id)) == 0) {
1681 fifo_state->plane[plane_id] = 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001682 continue;
1683 }
1684
Ville Syrjälä5012e602017-03-02 19:14:56 +02001685 rate = raw->plane[plane_id];
1686 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1687 fifo_left -= fifo_state->plane[plane_id];
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001688 }
1689
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001690 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1691 fifo_left -= sprite0_fifo_extra;
1692
Ville Syrjälä5012e602017-03-02 19:14:56 +02001693 fifo_state->plane[PLANE_CURSOR] = 63;
1694
1695 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001696
1697 /* spread the remainder evenly */
Ville Syrjälä5012e602017-03-02 19:14:56 +02001698 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001699 int plane_extra;
1700
1701 if (fifo_left == 0)
1702 break;
1703
Ville Syrjälä5012e602017-03-02 19:14:56 +02001704 if ((active_planes & BIT(plane_id)) == 0)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001705 continue;
1706
1707 plane_extra = min(fifo_extra, fifo_left);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001708 fifo_state->plane[plane_id] += plane_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001709 fifo_left -= plane_extra;
1710 }
1711
Ville Syrjälä5012e602017-03-02 19:14:56 +02001712 WARN_ON(active_planes != 0 && fifo_left != 0);
1713
1714 /* give it all to the first plane if none are active */
1715 if (active_planes == 0) {
1716 WARN_ON(fifo_left != fifo_size);
1717 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1718 }
1719
1720 return 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001721}
1722
Ville Syrjäläff32c542017-03-02 19:14:57 +02001723/* mark all levels starting from 'level' as invalid */
1724static void vlv_invalidate_wms(struct intel_crtc *crtc,
1725 struct vlv_wm_state *wm_state, int level)
1726{
1727 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1728
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001729 for (; level < intel_wm_num_levels(dev_priv); level++) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02001730 enum plane_id plane_id;
1731
1732 for_each_plane_id_on_crtc(crtc, plane_id)
1733 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1734
1735 wm_state->sr[level].cursor = USHRT_MAX;
1736 wm_state->sr[level].plane = USHRT_MAX;
1737 }
1738}
1739
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001740static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1741{
1742 if (wm > fifo_size)
1743 return USHRT_MAX;
1744 else
1745 return fifo_size - wm;
1746}
1747
Ville Syrjäläff32c542017-03-02 19:14:57 +02001748/*
1749 * Starting from 'level' set all higher
1750 * levels to 'value' in the "raw" watermarks.
1751 */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001752static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001753 int level, enum plane_id plane_id, u16 value)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001754{
Ville Syrjäläff32c542017-03-02 19:14:57 +02001755 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001756 int num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001757 bool dirty = false;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001758
Ville Syrjäläff32c542017-03-02 19:14:57 +02001759 for (; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001760 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001761
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001762 dirty |= raw->plane[plane_id] != value;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001763 raw->plane[plane_id] = value;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001764 }
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001765
1766 return dirty;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001767}
1768
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001769static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1770 const struct intel_plane_state *plane_state)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001771{
1772 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1773 enum plane_id plane_id = plane->id;
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001774 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
Ville Syrjäläff32c542017-03-02 19:14:57 +02001775 int level;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001776 bool dirty = false;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001777
Ville Syrjäläa07102f2017-03-03 17:19:27 +02001778 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001779 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1780 goto out;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001781 }
1782
1783 for (level = 0; level < num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001784 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001785 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1786 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1787
Ville Syrjäläff32c542017-03-02 19:14:57 +02001788 if (wm > max_wm)
1789 break;
1790
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001791 dirty |= raw->plane[plane_id] != wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001792 raw->plane[plane_id] = wm;
1793 }
1794
1795 /* mark all higher levels as invalid */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001796 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001797
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001798out:
1799 if (dirty)
Ville Syrjälä57a65282017-04-21 21:14:22 +03001800 DRM_DEBUG_KMS("%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001801 plane->base.name,
1802 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1803 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1804 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
1805
1806 return dirty;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001807}
1808
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001809static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1810 enum plane_id plane_id, int level)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001811{
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001812 const struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02001813 &crtc_state->wm.vlv.raw[level];
1814 const struct vlv_fifo_state *fifo_state =
1815 &crtc_state->wm.vlv.fifo_state;
1816
1817 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1818}
1819
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001820static bool vlv_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state, int level)
Ville Syrjäläff32c542017-03-02 19:14:57 +02001821{
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001822 return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1823 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1824 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1825 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001826}
1827
1828static int vlv_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001829{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001830 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001831 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001832 struct intel_atomic_state *state =
1833 to_intel_atomic_state(crtc_state->base.state);
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001834 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001835 const struct vlv_fifo_state *fifo_state =
1836 &crtc_state->wm.vlv.fifo_state;
1837 int num_active_planes = hweight32(crtc_state->active_planes &
1838 ~BIT(PLANE_CURSOR));
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001839 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->base);
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001840 const struct intel_plane_state *old_plane_state;
1841 const struct intel_plane_state *new_plane_state;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001842 struct intel_plane *plane;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001843 enum plane_id plane_id;
1844 int level, ret, i;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001845 unsigned int dirty = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001846
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001847 for_each_oldnew_intel_plane_in_state(state, plane,
1848 old_plane_state,
1849 new_plane_state, i) {
1850 if (new_plane_state->base.crtc != &crtc->base &&
Ville Syrjäläff32c542017-03-02 19:14:57 +02001851 old_plane_state->base.crtc != &crtc->base)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001852 continue;
1853
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001854 if (vlv_raw_plane_wm_compute(crtc_state, new_plane_state))
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001855 dirty |= BIT(plane->id);
1856 }
1857
1858 /*
1859 * DSPARB registers may have been reset due to the
1860 * power well being turned off. Make sure we restore
1861 * them to a consistent state even if no primary/sprite
1862 * planes are initially active.
1863 */
1864 if (needs_modeset)
1865 crtc_state->fifo_changed = true;
1866
1867 if (!dirty)
1868 return 0;
1869
1870 /* cursor changes don't warrant a FIFO recompute */
1871 if (dirty & ~BIT(PLANE_CURSOR)) {
1872 const struct intel_crtc_state *old_crtc_state =
Ville Syrjälä7b5104512017-08-23 18:22:22 +03001873 intel_atomic_get_old_crtc_state(state, crtc);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001874 const struct vlv_fifo_state *old_fifo_state =
1875 &old_crtc_state->wm.vlv.fifo_state;
1876
1877 ret = vlv_compute_fifo(crtc_state);
1878 if (ret)
1879 return ret;
1880
1881 if (needs_modeset ||
1882 memcmp(old_fifo_state, fifo_state,
1883 sizeof(*fifo_state)) != 0)
1884 crtc_state->fifo_changed = true;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001885 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001886
Ville Syrjäläff32c542017-03-02 19:14:57 +02001887 /* initially allow all levels */
Ville Syrjälä6d5019b2017-04-21 21:14:20 +03001888 wm_state->num_levels = intel_wm_num_levels(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001889 /*
1890 * Note that enabling cxsr with no primary/sprite planes
1891 * enabled can wedge the pipe. Hence we only allow cxsr
1892 * with exactly one enabled primary/sprite plane.
1893 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001894 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001895
Ville Syrjälä5012e602017-03-02 19:14:56 +02001896 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03001897 const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001898 const int sr_fifo_size = INTEL_INFO(dev_priv)->num_pipes * 512 - 1;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001899
Ville Syrjälä77d14ee2017-04-21 21:14:18 +03001900 if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
Ville Syrjäläff32c542017-03-02 19:14:57 +02001901 break;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001902
Ville Syrjäläff32c542017-03-02 19:14:57 +02001903 for_each_plane_id_on_crtc(crtc, plane_id) {
1904 wm_state->wm[level].plane[plane_id] =
1905 vlv_invert_wm_value(raw->plane[plane_id],
1906 fifo_state->plane[plane_id]);
1907 }
1908
1909 wm_state->sr[level].plane =
1910 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
Ville Syrjälä5012e602017-03-02 19:14:56 +02001911 raw->plane[PLANE_SPRITE0],
Ville Syrjäläff32c542017-03-02 19:14:57 +02001912 raw->plane[PLANE_SPRITE1]),
1913 sr_fifo_size);
1914
1915 wm_state->sr[level].cursor =
1916 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1917 63);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001918 }
1919
Ville Syrjäläff32c542017-03-02 19:14:57 +02001920 if (level == 0)
1921 return -EINVAL;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001922
Ville Syrjäläff32c542017-03-02 19:14:57 +02001923 /* limit to only levels we can actually handle */
1924 wm_state->num_levels = level;
1925
1926 /* invalidate the higher levels */
1927 vlv_invalidate_wms(crtc, wm_state, level);
1928
1929 return 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001930}
1931
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001932#define VLV_FIFO(plane, value) \
1933 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1934
Ville Syrjäläff32c542017-03-02 19:14:57 +02001935static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
1936 struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001937{
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001938 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001939 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001940 const struct vlv_fifo_state *fifo_state =
1941 &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001942 int sprite0_start, sprite1_start, fifo_size;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001943
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001944 if (!crtc_state->fifo_changed)
1945 return;
1946
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001947 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
1948 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
1949 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001950
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001951 WARN_ON(fifo_state->plane[PLANE_CURSOR] != 63);
1952 WARN_ON(fifo_size != 511);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001953
Ville Syrjäläc137d662017-03-02 19:15:06 +02001954 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
1955
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001956 /*
1957 * uncore.lock serves a double purpose here. It allows us to
1958 * use the less expensive I915_{READ,WRITE}_FW() functions, and
1959 * it protects the DSPARB registers from getting clobbered by
1960 * parallel updates from multiple pipes.
1961 *
1962 * intel_pipe_update_start() has already disabled interrupts
1963 * for us, so a plain spin_lock() is sufficient here.
1964 */
1965 spin_lock(&dev_priv->uncore.lock);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001966
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001967 switch (crtc->pipe) {
1968 uint32_t dsparb, dsparb2, dsparb3;
1969 case PIPE_A:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001970 dsparb = I915_READ_FW(DSPARB);
1971 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001972
1973 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1974 VLV_FIFO(SPRITEB, 0xff));
1975 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1976 VLV_FIFO(SPRITEB, sprite1_start));
1977
1978 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1979 VLV_FIFO(SPRITEB_HI, 0x1));
1980 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1981 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1982
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001983 I915_WRITE_FW(DSPARB, dsparb);
1984 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001985 break;
1986 case PIPE_B:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001987 dsparb = I915_READ_FW(DSPARB);
1988 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001989
1990 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1991 VLV_FIFO(SPRITED, 0xff));
1992 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1993 VLV_FIFO(SPRITED, sprite1_start));
1994
1995 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
1996 VLV_FIFO(SPRITED_HI, 0xff));
1997 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
1998 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
1999
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002000 I915_WRITE_FW(DSPARB, dsparb);
2001 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002002 break;
2003 case PIPE_C:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002004 dsparb3 = I915_READ_FW(DSPARB3);
2005 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002006
2007 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
2008 VLV_FIFO(SPRITEF, 0xff));
2009 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
2010 VLV_FIFO(SPRITEF, sprite1_start));
2011
2012 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
2013 VLV_FIFO(SPRITEF_HI, 0xff));
2014 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
2015 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
2016
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002017 I915_WRITE_FW(DSPARB3, dsparb3);
2018 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002019 break;
2020 default:
2021 break;
2022 }
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002023
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002024 POSTING_READ_FW(DSPARB);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02002025
Ville Syrjälä44e921d2017-03-09 17:44:34 +02002026 spin_unlock(&dev_priv->uncore.lock);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03002027}
2028
2029#undef VLV_FIFO
2030
Ville Syrjälä4841da52017-03-02 19:14:59 +02002031static int vlv_compute_intermediate_wm(struct drm_device *dev,
2032 struct intel_crtc *crtc,
2033 struct intel_crtc_state *crtc_state)
2034{
2035 struct vlv_wm_state *intermediate = &crtc_state->wm.vlv.intermediate;
2036 const struct vlv_wm_state *optimal = &crtc_state->wm.vlv.optimal;
2037 const struct vlv_wm_state *active = &crtc->wm.active.vlv;
2038 int level;
2039
2040 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002041 intermediate->cxsr = optimal->cxsr && active->cxsr &&
2042 !crtc_state->disable_cxsr;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002043
2044 for (level = 0; level < intermediate->num_levels; level++) {
2045 enum plane_id plane_id;
2046
2047 for_each_plane_id_on_crtc(crtc, plane_id) {
2048 intermediate->wm[level].plane[plane_id] =
2049 min(optimal->wm[level].plane[plane_id],
2050 active->wm[level].plane[plane_id]);
2051 }
2052
2053 intermediate->sr[level].plane = min(optimal->sr[level].plane,
2054 active->sr[level].plane);
2055 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
2056 active->sr[level].cursor);
2057 }
2058
2059 vlv_invalidate_wms(crtc, intermediate, level);
2060
2061 /*
2062 * If our intermediate WM are identical to the final WM, then we can
2063 * omit the post-vblank programming; only update if it's different.
2064 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002065 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
2066 crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä4841da52017-03-02 19:14:59 +02002067
2068 return 0;
2069}
2070
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002071static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002072 struct vlv_wm_values *wm)
2073{
2074 struct intel_crtc *crtc;
2075 int num_active_crtcs = 0;
2076
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002077 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002078 wm->cxsr = true;
2079
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002080 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002081 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002082
2083 if (!crtc->active)
2084 continue;
2085
2086 if (!wm_state->cxsr)
2087 wm->cxsr = false;
2088
2089 num_active_crtcs++;
2090 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
2091 }
2092
2093 if (num_active_crtcs != 1)
2094 wm->cxsr = false;
2095
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03002096 if (num_active_crtcs > 1)
2097 wm->level = VLV_WM_LEVEL_PM2;
2098
Ville Syrjälä7c951c02016-11-28 19:37:10 +02002099 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02002100 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002101 enum pipe pipe = crtc->pipe;
2102
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002103 wm->pipe[pipe] = wm_state->wm[wm->level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02002104 if (crtc->active && wm->cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002105 wm->sr = wm_state->sr[wm->level];
2106
Ville Syrjälä1b313892016-11-28 19:37:08 +02002107 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
2108 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
2109 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
2110 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002111 }
2112}
2113
Ville Syrjäläff32c542017-03-02 19:14:57 +02002114static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002115{
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002116 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
2117 struct vlv_wm_values new_wm = {};
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002118
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002119 vlv_merge_wm(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002120
Ville Syrjäläff32c542017-03-02 19:14:57 +02002121 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002122 return;
2123
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002124 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002125 chv_set_memory_dvfs(dev_priv, false);
2126
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002127 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002128 chv_set_memory_pm5(dev_priv, false);
2129
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002130 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002131 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002132
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002133 vlv_write_wm_values(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002134
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002135 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02002136 _intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002137
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002138 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002139 chv_set_memory_pm5(dev_priv, true);
2140
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002141 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03002142 chv_set_memory_dvfs(dev_priv, true);
2143
Ville Syrjäläfa292a42016-11-28 19:37:16 +02002144 *old_wm = new_wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03002145}
2146
Ville Syrjäläff32c542017-03-02 19:14:57 +02002147static void vlv_initial_watermarks(struct intel_atomic_state *state,
2148 struct intel_crtc_state *crtc_state)
2149{
2150 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2151 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
2152
2153 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä4841da52017-03-02 19:14:59 +02002154 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
2155 vlv_program_watermarks(dev_priv);
2156 mutex_unlock(&dev_priv->wm.wm_mutex);
2157}
2158
2159static void vlv_optimize_watermarks(struct intel_atomic_state *state,
2160 struct intel_crtc_state *crtc_state)
2161{
2162 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2163 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2164
2165 if (!crtc_state->wm.need_postvbl_update)
2166 return;
2167
2168 mutex_lock(&dev_priv->wm.wm_mutex);
2169 intel_crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02002170 vlv_program_watermarks(dev_priv);
2171 mutex_unlock(&dev_priv->wm.wm_mutex);
2172}
2173
Ville Syrjälä432081b2016-10-31 22:37:03 +02002174static void i965_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002175{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002176 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002177 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002178 int srwm = 1;
2179 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03002180 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002181
2182 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002183 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002184 if (crtc) {
2185 /* self-refresh has much higher latency */
2186 static const int sr_latency_ns = 12000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002187 const struct drm_display_mode *adjusted_mode =
2188 &crtc->config->base.adjusted_mode;
2189 const struct drm_framebuffer *fb =
2190 crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002191 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002192 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002193 int hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +02002194 int cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002195 int entries;
2196
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002197 entries = intel_wm_method2(clock, htotal,
2198 hdisplay, cpp, sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002199 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
2200 srwm = I965_FIFO_SIZE - entries;
2201 if (srwm < 0)
2202 srwm = 1;
2203 srwm &= 0x1ff;
2204 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
2205 entries, srwm);
2206
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002207 entries = intel_wm_method2(clock, htotal,
2208 crtc->base.cursor->state->crtc_w, 4,
2209 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002210 entries = DIV_ROUND_UP(entries,
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002211 i965_cursor_wm_info.cacheline_size) +
2212 i965_cursor_wm_info.guard_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002213
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002214 cursor_sr = i965_cursor_wm_info.fifo_size - entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002215 if (cursor_sr > i965_cursor_wm_info.max_wm)
2216 cursor_sr = i965_cursor_wm_info.max_wm;
2217
2218 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
2219 "cursor %d\n", srwm, cursor_sr);
2220
Imre Deak98584252014-06-13 14:54:20 +03002221 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002222 } else {
Imre Deak98584252014-06-13 14:54:20 +03002223 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002224 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03002225 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002226 }
2227
2228 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2229 srwm);
2230
2231 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002232 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
2233 FW_WM(8, CURSORB) |
2234 FW_WM(8, PLANEB) |
2235 FW_WM(8, PLANEA));
2236 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
2237 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002238 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02002239 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03002240
2241 if (cxsr_enabled)
2242 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002243}
2244
Ville Syrjäläf4998962015-03-10 17:02:21 +02002245#undef FW_WM
2246
Ville Syrjälä432081b2016-10-31 22:37:03 +02002247static void i9xx_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002248{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002249 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002250 const struct intel_watermark_params *wm_info;
2251 uint32_t fwater_lo;
2252 uint32_t fwater_hi;
2253 int cwm, srwm = 1;
2254 int fifo_size;
2255 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002256 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002257
Ville Syrjäläa9097be2016-10-31 22:37:20 +02002258 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002259 wm_info = &i945_wm_info;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002260 else if (!IS_GEN2(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002261 wm_info = &i915_wm_info;
2262 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03002263 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002264
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02002265 fifo_size = dev_priv->display.get_fifo_size(dev_priv, 0);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02002266 crtc = intel_get_crtc_for_plane(dev_priv, 0);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002267 if (intel_crtc_active(crtc)) {
2268 const struct drm_display_mode *adjusted_mode =
2269 &crtc->config->base.adjusted_mode;
2270 const struct drm_framebuffer *fb =
2271 crtc->base.primary->state->fb;
2272 int cpp;
2273
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002274 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002275 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002276 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002277 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002278
Damien Lespiau241bfc32013-09-25 16:45:37 +01002279 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002280 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002281 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002282 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002283 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002284 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002285 if (planea_wm > (long)wm_info->max_wm)
2286 planea_wm = wm_info->max_wm;
2287 }
2288
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002289 if (IS_GEN2(dev_priv))
Ville Syrjälä9d539102014-08-15 01:21:53 +03002290 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002291
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02002292 fifo_size = dev_priv->display.get_fifo_size(dev_priv, 1);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02002293 crtc = intel_get_crtc_for_plane(dev_priv, 1);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002294 if (intel_crtc_active(crtc)) {
2295 const struct drm_display_mode *adjusted_mode =
2296 &crtc->config->base.adjusted_mode;
2297 const struct drm_framebuffer *fb =
2298 crtc->base.primary->state->fb;
2299 int cpp;
2300
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002301 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002302 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002303 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002304 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002305
Damien Lespiau241bfc32013-09-25 16:45:37 +01002306 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01002307 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01002308 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002309 if (enabled == NULL)
2310 enabled = crtc;
2311 else
2312 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002313 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002314 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03002315 if (planeb_wm > (long)wm_info->max_wm)
2316 planeb_wm = wm_info->max_wm;
2317 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002318
2319 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
2320
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002321 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07002322 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002323
Ville Syrjäläefc26112016-10-31 22:37:04 +02002324 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002325
2326 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01002327 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02002328 enabled = NULL;
2329 }
2330
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002331 /*
2332 * Overlay gets an aggressive default since video jitter is bad.
2333 */
2334 cwm = 2;
2335
2336 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03002337 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002338
2339 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02002340 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002341 /* self-refresh has much higher latency */
2342 static const int sr_latency_ns = 6000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002343 const struct drm_display_mode *adjusted_mode =
2344 &enabled->config->base.adjusted_mode;
2345 const struct drm_framebuffer *fb =
2346 enabled->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002347 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002348 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002349 int hdisplay = enabled->config->pipe_src_w;
2350 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002351 int entries;
2352
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002353 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002354 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02002355 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02002356 cpp = fb->format->cpp[0];
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03002357
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002358 entries = intel_wm_method2(clock, htotal, hdisplay, cpp,
2359 sr_latency_ns / 100);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002360 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
2361 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
2362 srwm = wm_info->fifo_size - entries;
2363 if (srwm < 0)
2364 srwm = 1;
2365
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01002366 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002367 I915_WRITE(FW_BLC_SELF,
2368 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03002369 else
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002370 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
2371 }
2372
2373 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
2374 planea_wm, planeb_wm, cwm, srwm);
2375
2376 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2377 fwater_hi = (cwm & 0x1f);
2378
2379 /* Set request length to 8 cachelines per fetch */
2380 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2381 fwater_hi = fwater_hi | (1 << 8);
2382
2383 I915_WRITE(FW_BLC, fwater_lo);
2384 I915_WRITE(FW_BLC2, fwater_hi);
2385
Imre Deak5209b1f2014-07-01 12:36:17 +03002386 if (enabled)
2387 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002388}
2389
Ville Syrjälä432081b2016-10-31 22:37:03 +02002390static void i845_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002391{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002392 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02002393 struct intel_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002394 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002395 uint32_t fwater_lo;
2396 int planea_wm;
2397
Ville Syrjäläffc7a762016-10-31 22:37:21 +02002398 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002399 if (crtc == NULL)
2400 return;
2401
Ville Syrjäläefc26112016-10-31 22:37:04 +02002402 adjusted_mode = &crtc->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01002403 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02002404 &i845_wm_info,
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02002405 dev_priv->display.get_fifo_size(dev_priv, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01002406 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002407 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
2408 fwater_lo |= (3<<8) | planea_wm;
2409
2410 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
2411
2412 I915_WRITE(FW_BLC, fwater_lo);
2413}
2414
Ville Syrjälä37126462013-08-01 16:18:55 +03002415/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002416static unsigned int ilk_wm_method1(unsigned int pixel_rate,
2417 unsigned int cpp,
2418 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002419{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002420 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002421
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002422 ret = intel_wm_method1(pixel_rate, cpp, latency);
2423 ret = DIV_ROUND_UP(ret, 64) + 2;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002424
2425 return ret;
2426}
2427
Ville Syrjälä37126462013-08-01 16:18:55 +03002428/* latency must be in 0.1us units. */
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002429static unsigned int ilk_wm_method2(unsigned int pixel_rate,
2430 unsigned int htotal,
2431 unsigned int width,
2432 unsigned int cpp,
2433 unsigned int latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002434{
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002435 unsigned int ret;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002436
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002437 ret = intel_wm_method2(pixel_rate, htotal,
2438 width, cpp, latency);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002439 ret = DIV_ROUND_UP(ret, 64) + 2;
Ville Syrjäläbaf69ca2017-04-21 21:14:27 +03002440
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002441 return ret;
2442}
2443
Ville Syrjälä23297042013-07-05 11:57:17 +03002444static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Ville Syrjäläac484962016-01-20 21:05:26 +02002445 uint8_t cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002446{
Matt Roper15126882015-12-03 11:37:40 -08002447 /*
2448 * Neither of these should be possible since this function shouldn't be
2449 * called if the CRTC is off or the plane is invisible. But let's be
2450 * extra paranoid to avoid a potential divide-by-zero if we screw up
2451 * elsewhere in the driver.
2452 */
Ville Syrjäläac484962016-01-20 21:05:26 +02002453 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08002454 return 0;
2455 if (WARN_ON(!horiz_pixels))
2456 return 0;
2457
Ville Syrjäläac484962016-01-20 21:05:26 +02002458 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002459}
2460
Imre Deak820c1982013-12-17 14:46:36 +02002461struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002462 uint16_t pri;
2463 uint16_t spr;
2464 uint16_t cur;
2465 uint16_t fbc;
2466};
2467
Ville Syrjälä37126462013-08-01 16:18:55 +03002468/*
2469 * For both WM_PIPE and WM_LP.
2470 * mem_value must be in 0.1us units.
2471 */
Matt Roper7221fc32015-09-24 15:53:08 -07002472static uint32_t ilk_compute_pri_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002473 const struct intel_plane_state *pstate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03002474 uint32_t mem_value,
2475 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002476{
Paulo Zanonicca32e92013-05-31 11:45:06 -03002477 uint32_t method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002478 int cpp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002479
Ville Syrjälä24304d812017-03-14 17:10:49 +02002480 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002481 return 0;
2482
Ville Syrjälä353c8592016-12-14 23:30:57 +02002483 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002484
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002485 method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002486
2487 if (!is_lp)
2488 return method1;
2489
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002490 method2 = ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002491 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002492 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002493 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002494
2495 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002496}
2497
Ville Syrjälä37126462013-08-01 16:18:55 +03002498/*
2499 * For both WM_PIPE and WM_LP.
2500 * mem_value must be in 0.1us units.
2501 */
Matt Roper7221fc32015-09-24 15:53:08 -07002502static uint32_t ilk_compute_spr_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002503 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002504 uint32_t mem_value)
2505{
2506 uint32_t method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002507 int cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002508
Ville Syrjälä24304d812017-03-14 17:10:49 +02002509 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002510 return 0;
2511
Ville Syrjälä353c8592016-12-14 23:30:57 +02002512 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002513
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002514 method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
2515 method2 = ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002516 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002517 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002518 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002519 return min(method1, method2);
2520}
2521
Ville Syrjälä37126462013-08-01 16:18:55 +03002522/*
2523 * For both WM_PIPE and WM_LP.
2524 * mem_value must be in 0.1us units.
2525 */
Matt Roper7221fc32015-09-24 15:53:08 -07002526static uint32_t ilk_compute_cur_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002527 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002528 uint32_t mem_value)
2529{
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002530 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002531
Ville Syrjälä24304d812017-03-14 17:10:49 +02002532 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002533 return 0;
2534
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002535 cpp = pstate->base.fb->format->cpp[0];
2536
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002537 return ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002538 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002539 pstate->base.crtc_w, cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002540}
2541
Paulo Zanonicca32e92013-05-31 11:45:06 -03002542/* Only for WM_LP. */
Matt Roper7221fc32015-09-24 15:53:08 -07002543static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002544 const struct intel_plane_state *pstate,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03002545 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002546{
Ville Syrjälä83054942016-11-18 21:53:00 +02002547 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002548
Ville Syrjälä24304d812017-03-14 17:10:49 +02002549 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanonicca32e92013-05-31 11:45:06 -03002550 return 0;
2551
Ville Syrjälä353c8592016-12-14 23:30:57 +02002552 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002553
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002554 return ilk_wm_fbc(pri_val, drm_rect_width(&pstate->base.dst), cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002555}
2556
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002557static unsigned int
2558ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002559{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002560 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002561 return 3072;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002562 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002563 return 768;
2564 else
2565 return 512;
2566}
2567
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002568static unsigned int
2569ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2570 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002571{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002572 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002573 /* BDW primary/sprite plane watermarks */
2574 return level == 0 ? 255 : 2047;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002575 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002576 /* IVB/HSW primary/sprite plane watermarks */
2577 return level == 0 ? 127 : 1023;
2578 else if (!is_sprite)
2579 /* ILK/SNB primary plane watermarks */
2580 return level == 0 ? 127 : 511;
2581 else
2582 /* ILK/SNB sprite plane watermarks */
2583 return level == 0 ? 63 : 255;
2584}
2585
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002586static unsigned int
2587ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002588{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002589 if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002590 return level == 0 ? 63 : 255;
2591 else
2592 return level == 0 ? 31 : 63;
2593}
2594
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002595static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002596{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002597 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002598 return 31;
2599 else
2600 return 15;
2601}
2602
Ville Syrjälä158ae642013-08-07 13:28:19 +03002603/* Calculate the maximum primary/sprite plane watermark */
2604static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
2605 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002606 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002607 enum intel_ddb_partitioning ddb_partitioning,
2608 bool is_sprite)
2609{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002610 struct drm_i915_private *dev_priv = to_i915(dev);
2611 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002612
2613 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002614 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002615 return 0;
2616
2617 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002618 if (level == 0 || config->num_pipes_active > 1) {
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002619 fifo_size /= INTEL_INFO(dev_priv)->num_pipes;
Ville Syrjälä158ae642013-08-07 13:28:19 +03002620
2621 /*
2622 * For some reason the non self refresh
2623 * FIFO size is only half of the self
2624 * refresh FIFO size on ILK/SNB.
2625 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002626 if (INTEL_GEN(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002627 fifo_size /= 2;
2628 }
2629
Ville Syrjälä240264f2013-08-07 13:29:12 +03002630 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002631 /* level 0 is always calculated with 1:1 split */
2632 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2633 if (is_sprite)
2634 fifo_size *= 5;
2635 fifo_size /= 6;
2636 } else {
2637 fifo_size /= 2;
2638 }
2639 }
2640
2641 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002642 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002643}
2644
2645/* Calculate the maximum cursor plane watermark */
2646static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002647 int level,
2648 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002649{
2650 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002651 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002652 return 64;
2653
2654 /* otherwise just report max that registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002655 return ilk_cursor_wm_reg_max(to_i915(dev), level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002656}
2657
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002658static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002659 int level,
2660 const struct intel_wm_config *config,
2661 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002662 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002663{
Ville Syrjälä240264f2013-08-07 13:29:12 +03002664 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
2665 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
2666 max->cur = ilk_cursor_wm_max(dev, level, config);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002667 max->fbc = ilk_fbc_wm_reg_max(to_i915(dev));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002668}
2669
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002670static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002671 int level,
2672 struct ilk_wm_maximums *max)
2673{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002674 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2675 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2676 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2677 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002678}
2679
Ville Syrjäläd9395652013-10-09 19:18:10 +03002680static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002681 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002682 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002683{
2684 bool ret;
2685
2686 /* already determined to be invalid? */
2687 if (!result->enable)
2688 return false;
2689
2690 result->enable = result->pri_val <= max->pri &&
2691 result->spr_val <= max->spr &&
2692 result->cur_val <= max->cur;
2693
2694 ret = result->enable;
2695
2696 /*
2697 * HACK until we can pre-compute everything,
2698 * and thus fail gracefully if LP0 watermarks
2699 * are exceeded...
2700 */
2701 if (level == 0 && !result->enable) {
2702 if (result->pri_val > max->pri)
2703 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2704 level, result->pri_val, max->pri);
2705 if (result->spr_val > max->spr)
2706 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2707 level, result->spr_val, max->spr);
2708 if (result->cur_val > max->cur)
2709 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2710 level, result->cur_val, max->cur);
2711
2712 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
2713 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
2714 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
2715 result->enable = true;
2716 }
2717
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002718 return ret;
2719}
2720
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002721static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Matt Roper43d59ed2015-09-24 15:53:07 -07002722 const struct intel_crtc *intel_crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002723 int level,
Matt Roper7221fc32015-09-24 15:53:08 -07002724 struct intel_crtc_state *cstate,
Matt Roper86c8bbb2015-09-24 15:53:16 -07002725 struct intel_plane_state *pristate,
2726 struct intel_plane_state *sprstate,
2727 struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002728 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002729{
2730 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2731 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2732 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2733
2734 /* WM1+ latency values stored in 0.5us units */
2735 if (level > 0) {
2736 pri_latency *= 5;
2737 spr_latency *= 5;
2738 cur_latency *= 5;
2739 }
2740
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002741 if (pristate) {
2742 result->pri_val = ilk_compute_pri_wm(cstate, pristate,
2743 pri_latency, level);
2744 result->fbc_val = ilk_compute_fbc_wm(cstate, pristate, result->pri_val);
2745 }
2746
2747 if (sprstate)
2748 result->spr_val = ilk_compute_spr_wm(cstate, sprstate, spr_latency);
2749
2750 if (curstate)
2751 result->cur_val = ilk_compute_cur_wm(cstate, curstate, cur_latency);
2752
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002753 result->enable = true;
2754}
2755
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002756static uint32_t
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002757hsw_compute_linetime_wm(const struct intel_crtc_state *cstate)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002758{
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002759 const struct intel_atomic_state *intel_state =
2760 to_intel_atomic_state(cstate->base.state);
Matt Roperee91a152015-12-03 11:37:39 -08002761 const struct drm_display_mode *adjusted_mode =
2762 &cstate->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002763 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002764
Matt Roperee91a152015-12-03 11:37:39 -08002765 if (!cstate->base.active)
2766 return 0;
2767 if (WARN_ON(adjusted_mode->crtc_clock == 0))
2768 return 0;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002769 if (WARN_ON(intel_state->cdclk.logical.cdclk == 0))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002770 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002771
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002772 /* The WM are computed with base on how long it takes to fill a single
2773 * row at the given clock rate, multiplied by 8.
2774 * */
Ville Syrjälä124abe02015-09-08 13:40:45 +03002775 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2776 adjusted_mode->crtc_clock);
2777 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002778 intel_state->cdclk.logical.cdclk);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002779
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002780 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2781 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002782}
2783
Ville Syrjäläbb726512016-10-31 22:37:24 +02002784static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
2785 uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002786{
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002787 if (INTEL_GEN(dev_priv) >= 9) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002788 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002789 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002790 int level, max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002791
2792 /* read the first set of memory latencies[0:3] */
2793 val = 0; /* data0 to be programmed to 0 for first set */
2794 mutex_lock(&dev_priv->rps.hw_lock);
2795 ret = sandybridge_pcode_read(dev_priv,
2796 GEN9_PCODE_READ_MEM_LATENCY,
2797 &val);
2798 mutex_unlock(&dev_priv->rps.hw_lock);
2799
2800 if (ret) {
2801 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2802 return;
2803 }
2804
2805 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2806 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2807 GEN9_MEM_LATENCY_LEVEL_MASK;
2808 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2809 GEN9_MEM_LATENCY_LEVEL_MASK;
2810 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2811 GEN9_MEM_LATENCY_LEVEL_MASK;
2812
2813 /* read the second set of memory latencies[4:7] */
2814 val = 1; /* data0 to be programmed to 1 for second set */
2815 mutex_lock(&dev_priv->rps.hw_lock);
2816 ret = sandybridge_pcode_read(dev_priv,
2817 GEN9_PCODE_READ_MEM_LATENCY,
2818 &val);
2819 mutex_unlock(&dev_priv->rps.hw_lock);
2820 if (ret) {
2821 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2822 return;
2823 }
2824
2825 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2826 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2827 GEN9_MEM_LATENCY_LEVEL_MASK;
2828 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2829 GEN9_MEM_LATENCY_LEVEL_MASK;
2830 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2831 GEN9_MEM_LATENCY_LEVEL_MASK;
2832
Vandana Kannan367294b2014-11-04 17:06:46 +00002833 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002834 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2835 * need to be disabled. We make sure to sanitize the values out
2836 * of the punit to satisfy this requirement.
2837 */
2838 for (level = 1; level <= max_level; level++) {
2839 if (wm[level] == 0) {
2840 for (i = level + 1; i <= max_level; i++)
2841 wm[i] = 0;
2842 break;
2843 }
2844 }
2845
2846 /*
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002847 * WaWmMemoryReadLatency:skl+,glk
Damien Lespiau6f972352015-02-09 19:33:07 +00002848 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002849 * punit doesn't take into account the read latency so we need
Paulo Zanoni0727e402016-09-22 18:00:30 -03002850 * to add 2us to the various latency levels we retrieve from the
2851 * punit when level 0 response data us 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002852 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002853 if (wm[0] == 0) {
2854 wm[0] += 2;
2855 for (level = 1; level <= max_level; level++) {
2856 if (wm[level] == 0)
2857 break;
Vandana Kannan367294b2014-11-04 17:06:46 +00002858 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002859 }
Paulo Zanoni0727e402016-09-22 18:00:30 -03002860 }
2861
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002862 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002863 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2864
2865 wm[0] = (sskpd >> 56) & 0xFF;
2866 if (wm[0] == 0)
2867 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002868 wm[1] = (sskpd >> 4) & 0xFF;
2869 wm[2] = (sskpd >> 12) & 0xFF;
2870 wm[3] = (sskpd >> 20) & 0x1FF;
2871 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002872 } else if (INTEL_GEN(dev_priv) >= 6) {
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002873 uint32_t sskpd = I915_READ(MCH_SSKPD);
2874
2875 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2876 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2877 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2878 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002879 } else if (INTEL_GEN(dev_priv) >= 5) {
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002880 uint32_t mltr = I915_READ(MLTR_ILK);
2881
2882 /* ILK primary LP0 latency is 700 ns */
2883 wm[0] = 7;
2884 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2885 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Paulo Zanoni50682ee2017-08-09 13:52:43 -07002886 } else {
2887 MISSING_CASE(INTEL_DEVID(dev_priv));
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002888 }
2889}
2890
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002891static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
2892 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002893{
2894 /* ILK sprite LP0 latency is 1300 ns */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002895 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002896 wm[0] = 13;
2897}
2898
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002899static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
2900 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002901{
2902 /* ILK cursor LP0 latency is 1300 ns */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002903 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002904 wm[0] = 13;
2905
2906 /* WaDoubleCursorLP3Latency:ivb */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002907 if (IS_IVYBRIDGE(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002908 wm[3] *= 2;
2909}
2910
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002911int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002912{
2913 /* how many WM levels are we expecting */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002914 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002915 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002916 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002917 return 4;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002918 else if (INTEL_GEN(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002919 return 3;
2920 else
2921 return 2;
2922}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002923
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002924static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002925 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002926 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002927{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002928 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002929
2930 for (level = 0; level <= max_level; level++) {
2931 unsigned int latency = wm[level];
2932
2933 if (latency == 0) {
2934 DRM_ERROR("%s WM%d latency not provided\n",
2935 name, level);
2936 continue;
2937 }
2938
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002939 /*
2940 * - latencies are in us on gen9.
2941 * - before then, WM1+ latency values are in 0.5us units
2942 */
Paulo Zanonidfc267a2017-08-09 13:52:46 -07002943 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002944 latency *= 10;
2945 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002946 latency *= 5;
2947
2948 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2949 name, level, wm[level],
2950 latency / 10, latency % 10);
2951 }
2952}
2953
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002954static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2955 uint16_t wm[5], uint16_t min)
2956{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002957 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002958
2959 if (wm[0] >= min)
2960 return false;
2961
2962 wm[0] = max(wm[0], min);
2963 for (level = 1; level <= max_level; level++)
2964 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2965
2966 return true;
2967}
2968
Ville Syrjäläbb726512016-10-31 22:37:24 +02002969static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002970{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002971 bool changed;
2972
2973 /*
2974 * The BIOS provided WM memory latency values are often
2975 * inadequate for high resolution displays. Adjust them.
2976 */
2977 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2978 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2979 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2980
2981 if (!changed)
2982 return;
2983
2984 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002985 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
2986 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
2987 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002988}
2989
Ville Syrjäläbb726512016-10-31 22:37:24 +02002990static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002991{
Ville Syrjäläbb726512016-10-31 22:37:24 +02002992 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002993
2994 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2995 sizeof(dev_priv->wm.pri_latency));
2996 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2997 sizeof(dev_priv->wm.pri_latency));
2998
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002999 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003000 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03003001
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003002 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3003 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3004 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03003005
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003006 if (IS_GEN6(dev_priv))
Ville Syrjäläbb726512016-10-31 22:37:24 +02003007 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03003008}
3009
Ville Syrjäläbb726512016-10-31 22:37:24 +02003010static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003011{
Ville Syrjäläbb726512016-10-31 22:37:24 +02003012 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003013 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00003014}
3015
Matt Ropered4a6a72016-02-23 17:20:13 -08003016static bool ilk_validate_pipe_wm(struct drm_device *dev,
3017 struct intel_pipe_wm *pipe_wm)
3018{
3019 /* LP0 watermark maximums depend on this pipe alone */
3020 const struct intel_wm_config config = {
3021 .num_pipes_active = 1,
3022 .sprites_enabled = pipe_wm->sprites_enabled,
3023 .sprites_scaled = pipe_wm->sprites_scaled,
3024 };
3025 struct ilk_wm_maximums max;
3026
3027 /* LP0 watermarks always use 1/2 DDB partitioning */
3028 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
3029
3030 /* At least LP0 must be valid */
3031 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
3032 DRM_DEBUG_KMS("LP0 watermark invalid\n");
3033 return false;
3034 }
3035
3036 return true;
3037}
3038
Matt Roper261a27d2015-10-08 15:28:25 -07003039/* Compute new watermarks for the pipe */
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003040static int ilk_compute_pipe_wm(struct intel_crtc_state *cstate)
Matt Roper261a27d2015-10-08 15:28:25 -07003041{
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003042 struct drm_atomic_state *state = cstate->base.state;
3043 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07003044 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003045 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003046 const struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper43d59ed2015-09-24 15:53:07 -07003047 struct intel_plane *intel_plane;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003048 struct intel_plane_state *pristate = NULL;
Matt Roper43d59ed2015-09-24 15:53:07 -07003049 struct intel_plane_state *sprstate = NULL;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003050 struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003051 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02003052 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003053
Matt Ropere8f1f022016-05-12 07:05:55 -07003054 pipe_wm = &cstate->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003055
Matt Roper43d59ed2015-09-24 15:53:07 -07003056 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003057 struct intel_plane_state *ps;
3058
3059 ps = intel_atomic_get_existing_plane_state(state,
3060 intel_plane);
3061 if (!ps)
3062 continue;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003063
3064 if (intel_plane->base.type == DRM_PLANE_TYPE_PRIMARY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003065 pristate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003066 else if (intel_plane->base.type == DRM_PLANE_TYPE_OVERLAY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003067 sprstate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07003068 else if (intel_plane->base.type == DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003069 curstate = ps;
Matt Roper43d59ed2015-09-24 15:53:07 -07003070 }
3071
Matt Ropered4a6a72016-02-23 17:20:13 -08003072 pipe_wm->pipe_enabled = cstate->base.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003073 if (sprstate) {
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003074 pipe_wm->sprites_enabled = sprstate->base.visible;
3075 pipe_wm->sprites_scaled = sprstate->base.visible &&
3076 (drm_rect_width(&sprstate->base.dst) != drm_rect_width(&sprstate->base.src) >> 16 ||
3077 drm_rect_height(&sprstate->base.dst) != drm_rect_height(&sprstate->base.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01003078 }
3079
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003080 usable_level = max_level;
3081
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003082 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003083 if (INTEL_GEN(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003084 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003085
3086 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08003087 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003088 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02003089
Matt Roper86c8bbb2015-09-24 15:53:16 -07003090 ilk_compute_wm_level(dev_priv, intel_crtc, 0, cstate,
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003091 pristate, sprstate, curstate, &pipe_wm->raw_wm[0]);
3092
3093 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
3094 pipe_wm->wm[0] = pipe_wm->raw_wm[0];
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003095
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003096 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjälä532f7a72016-04-29 17:31:17 +03003097 pipe_wm->linetime = hsw_compute_linetime_wm(cstate);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003098
Matt Ropered4a6a72016-02-23 17:20:13 -08003099 if (!ilk_validate_pipe_wm(dev, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01003100 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003101
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003102 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003103
3104 for (level = 1; level <= max_level; level++) {
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003105 struct intel_wm_level *wm = &pipe_wm->raw_wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003106
Matt Roper86c8bbb2015-09-24 15:53:16 -07003107 ilk_compute_wm_level(dev_priv, intel_crtc, level, cstate,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003108 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003109
3110 /*
3111 * Disable any watermark level that exceeds the
3112 * register maximums since such watermarks are
3113 * always invalid.
3114 */
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01003115 if (level > usable_level)
3116 continue;
3117
3118 if (ilk_validate_wm_level(level, &max, wm))
3119 pipe_wm->wm[level] = *wm;
3120 else
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01003121 usable_level = level;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03003122 }
3123
Matt Roper86c8bbb2015-09-24 15:53:16 -07003124 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003125}
3126
3127/*
Matt Ropered4a6a72016-02-23 17:20:13 -08003128 * Build a set of 'intermediate' watermark values that satisfy both the old
3129 * state and the new state. These can be programmed to the hardware
3130 * immediately.
3131 */
3132static int ilk_compute_intermediate_wm(struct drm_device *dev,
3133 struct intel_crtc *intel_crtc,
3134 struct intel_crtc_state *newstate)
3135{
Matt Ropere8f1f022016-05-12 07:05:55 -07003136 struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08003137 struct intel_pipe_wm *b = &intel_crtc->wm.active.ilk;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003138 int level, max_level = ilk_wm_max_level(to_i915(dev));
Matt Ropered4a6a72016-02-23 17:20:13 -08003139
3140 /*
3141 * Start with the final, target watermarks, then combine with the
3142 * currently active watermarks to get values that are safe both before
3143 * and after the vblank.
3144 */
Matt Ropere8f1f022016-05-12 07:05:55 -07003145 *a = newstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08003146 a->pipe_enabled |= b->pipe_enabled;
3147 a->sprites_enabled |= b->sprites_enabled;
3148 a->sprites_scaled |= b->sprites_scaled;
3149
3150 for (level = 0; level <= max_level; level++) {
3151 struct intel_wm_level *a_wm = &a->wm[level];
3152 const struct intel_wm_level *b_wm = &b->wm[level];
3153
3154 a_wm->enable &= b_wm->enable;
3155 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
3156 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
3157 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
3158 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
3159 }
3160
3161 /*
3162 * We need to make sure that these merged watermark values are
3163 * actually a valid configuration themselves. If they're not,
3164 * there's no safe way to transition from the old state to
3165 * the new state, so we need to fail the atomic transaction.
3166 */
3167 if (!ilk_validate_pipe_wm(dev, a))
3168 return -EINVAL;
3169
3170 /*
3171 * If our intermediate WM are identical to the final WM, then we can
3172 * omit the post-vblank programming; only update if it's different.
3173 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02003174 if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) != 0)
3175 newstate->wm.need_postvbl_update = true;
Matt Ropered4a6a72016-02-23 17:20:13 -08003176
3177 return 0;
3178}
3179
3180/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003181 * Merge the watermarks from all active pipes for a specific level.
3182 */
3183static void ilk_merge_wm_level(struct drm_device *dev,
3184 int level,
3185 struct intel_wm_level *ret_wm)
3186{
3187 const struct intel_crtc *intel_crtc;
3188
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003189 ret_wm->enable = true;
3190
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003191 for_each_intel_crtc(dev, intel_crtc) {
Matt Ropered4a6a72016-02-23 17:20:13 -08003192 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02003193 const struct intel_wm_level *wm = &active->wm[level];
3194
3195 if (!active->pipe_enabled)
3196 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003197
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003198 /*
3199 * The watermark values may have been used in the past,
3200 * so we must maintain them in the registers for some
3201 * time even if the level is now disabled.
3202 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003203 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003204 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003205
3206 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
3207 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
3208 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
3209 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
3210 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003211}
3212
3213/*
3214 * Merge all low power watermarks for all active pipes.
3215 */
3216static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003217 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02003218 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003219 struct intel_pipe_wm *merged)
3220{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003221 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003222 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003223 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003224
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003225 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003226 if ((INTEL_GEN(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003227 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03003228 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003229
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003230 /* ILK: FBC WM must be disabled always */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003231 merged->fbc_wm_enabled = INTEL_GEN(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003232
3233 /* merge each WM1+ level */
3234 for (level = 1; level <= max_level; level++) {
3235 struct intel_wm_level *wm = &merged->wm[level];
3236
3237 ilk_merge_wm_level(dev, level, wm);
3238
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003239 if (level > last_enabled_level)
3240 wm->enable = false;
3241 else if (!ilk_validate_wm_level(level, max, wm))
3242 /* make sure all following levels get disabled */
3243 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003244
3245 /*
3246 * The spec says it is preferred to disable
3247 * FBC WMs instead of disabling a WM level.
3248 */
3249 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003250 if (wm->enable)
3251 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003252 wm->fbc_val = 0;
3253 }
3254 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003255
3256 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
3257 /*
3258 * FIXME this is racy. FBC might get enabled later.
3259 * What we should check here is whether FBC can be
3260 * enabled sometime later.
3261 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003262 if (IS_GEN5(dev_priv) && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03003263 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02003264 for (level = 2; level <= max_level; level++) {
3265 struct intel_wm_level *wm = &merged->wm[level];
3266
3267 wm->enable = false;
3268 }
3269 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003270}
3271
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003272static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
3273{
3274 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
3275 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
3276}
3277
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003278/* The value we need to program into the WM_LPx latency field */
3279static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
3280{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003281 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003282
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003283 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003284 return 2 * level;
3285 else
3286 return dev_priv->wm.pri_latency[level];
3287}
3288
Imre Deak820c1982013-12-17 14:46:36 +02003289static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03003290 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03003291 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02003292 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003293{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003294 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003295 struct intel_crtc *intel_crtc;
3296 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003297
Ville Syrjälä0362c782013-10-09 19:17:57 +03003298 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03003299 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003300
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003301 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03003302 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03003303 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003304
Ville Syrjäläb380ca32013-10-09 19:18:01 +03003305 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003306
Ville Syrjälä0362c782013-10-09 19:17:57 +03003307 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03003308
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003309 /*
3310 * Maintain the watermark values even if the level is
3311 * disabled. Doing otherwise could cause underruns.
3312 */
3313 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02003314 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07003315 (r->pri_val << WM1_LP_SR_SHIFT) |
3316 r->cur_val;
3317
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003318 if (r->enable)
3319 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
3320
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003321 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07003322 results->wm_lp[wm_lp - 1] |=
3323 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
3324 else
3325 results->wm_lp[wm_lp - 1] |=
3326 r->fbc_val << WM1_LP_FBC_SHIFT;
3327
Ville Syrjäläd52fea52014-04-28 15:44:57 +03003328 /*
3329 * Always set WM1S_LP_EN when spr_val != 0, even if the
3330 * level is disabled. Doing otherwise could cause underruns.
3331 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003332 if (INTEL_GEN(dev_priv) <= 6 && r->spr_val) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003333 WARN_ON(wm_lp != 1);
3334 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
3335 } else
3336 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03003337 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003338
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003339 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003340 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003341 enum pipe pipe = intel_crtc->pipe;
Matt Ropered4a6a72016-02-23 17:20:13 -08003342 const struct intel_wm_level *r =
3343 &intel_crtc->wm.active.ilk.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003344
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003345 if (WARN_ON(!r->enable))
3346 continue;
3347
Matt Ropered4a6a72016-02-23 17:20:13 -08003348 results->wm_linetime[pipe] = intel_crtc->wm.active.ilk.linetime;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03003349
3350 results->wm_pipe[pipe] =
3351 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
3352 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
3353 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003354 }
3355}
3356
Paulo Zanoni861f3382013-05-31 10:19:21 -03003357/* Find the result with the highest level enabled. Check for enable_fbc_wm in
3358 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02003359static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003360 struct intel_pipe_wm *r1,
3361 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003362{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003363 int level, max_level = ilk_wm_max_level(to_i915(dev));
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003364 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003365
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003366 for (level = 1; level <= max_level; level++) {
3367 if (r1->wm[level].enable)
3368 level1 = level;
3369 if (r2->wm[level].enable)
3370 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003371 }
3372
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003373 if (level1 == level2) {
3374 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03003375 return r2;
3376 else
3377 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003378 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03003379 return r1;
3380 } else {
3381 return r2;
3382 }
3383}
3384
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003385/* dirty bits used to track which watermarks need changes */
3386#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
3387#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
3388#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
3389#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
3390#define WM_DIRTY_FBC (1 << 24)
3391#define WM_DIRTY_DDB (1 << 25)
3392
Damien Lespiau055e3932014-08-18 13:49:10 +01003393static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02003394 const struct ilk_wm_values *old,
3395 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003396{
3397 unsigned int dirty = 0;
3398 enum pipe pipe;
3399 int wm_lp;
3400
Damien Lespiau055e3932014-08-18 13:49:10 +01003401 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003402 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
3403 dirty |= WM_DIRTY_LINETIME(pipe);
3404 /* Must disable LP1+ watermarks too */
3405 dirty |= WM_DIRTY_LP_ALL;
3406 }
3407
3408 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
3409 dirty |= WM_DIRTY_PIPE(pipe);
3410 /* Must disable LP1+ watermarks too */
3411 dirty |= WM_DIRTY_LP_ALL;
3412 }
3413 }
3414
3415 if (old->enable_fbc_wm != new->enable_fbc_wm) {
3416 dirty |= WM_DIRTY_FBC;
3417 /* Must disable LP1+ watermarks too */
3418 dirty |= WM_DIRTY_LP_ALL;
3419 }
3420
3421 if (old->partitioning != new->partitioning) {
3422 dirty |= WM_DIRTY_DDB;
3423 /* Must disable LP1+ watermarks too */
3424 dirty |= WM_DIRTY_LP_ALL;
3425 }
3426
3427 /* LP1+ watermarks already deemed dirty, no need to continue */
3428 if (dirty & WM_DIRTY_LP_ALL)
3429 return dirty;
3430
3431 /* Find the lowest numbered LP1+ watermark in need of an update... */
3432 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3433 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
3434 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
3435 break;
3436 }
3437
3438 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
3439 for (; wm_lp <= 3; wm_lp++)
3440 dirty |= WM_DIRTY_LP(wm_lp);
3441
3442 return dirty;
3443}
3444
Ville Syrjälä8553c182013-12-05 15:51:39 +02003445static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
3446 unsigned int dirty)
3447{
Imre Deak820c1982013-12-17 14:46:36 +02003448 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02003449 bool changed = false;
3450
3451 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
3452 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
3453 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
3454 changed = true;
3455 }
3456 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
3457 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
3458 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
3459 changed = true;
3460 }
3461 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
3462 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
3463 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
3464 changed = true;
3465 }
3466
3467 /*
3468 * Don't touch WM1S_LP_EN here.
3469 * Doing so could cause underruns.
3470 */
3471
3472 return changed;
3473}
3474
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003475/*
3476 * The spec says we shouldn't write when we don't need, because every write
3477 * causes WMs to be re-evaluated, expending some power.
3478 */
Imre Deak820c1982013-12-17 14:46:36 +02003479static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
3480 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003481{
Imre Deak820c1982013-12-17 14:46:36 +02003482 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003483 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003484 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003485
Damien Lespiau055e3932014-08-18 13:49:10 +01003486 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003487 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003488 return;
3489
Ville Syrjälä8553c182013-12-05 15:51:39 +02003490 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003491
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003492 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003493 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003494 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003495 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003496 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003497 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
3498
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003499 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003500 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003501 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003502 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003503 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003504 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
3505
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003506 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003507 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003508 val = I915_READ(WM_MISC);
3509 if (results->partitioning == INTEL_DDB_PART_1_2)
3510 val &= ~WM_MISC_DATA_PARTITION_5_6;
3511 else
3512 val |= WM_MISC_DATA_PARTITION_5_6;
3513 I915_WRITE(WM_MISC, val);
3514 } else {
3515 val = I915_READ(DISP_ARB_CTL2);
3516 if (results->partitioning == INTEL_DDB_PART_1_2)
3517 val &= ~DISP_DATA_PARTITION_5_6;
3518 else
3519 val |= DISP_DATA_PARTITION_5_6;
3520 I915_WRITE(DISP_ARB_CTL2, val);
3521 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003522 }
3523
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003524 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03003525 val = I915_READ(DISP_ARB_CTL);
3526 if (results->enable_fbc_wm)
3527 val &= ~DISP_FBC_WM_DIS;
3528 else
3529 val |= DISP_FBC_WM_DIS;
3530 I915_WRITE(DISP_ARB_CTL, val);
3531 }
3532
Imre Deak954911e2013-12-17 14:46:34 +02003533 if (dirty & WM_DIRTY_LP(1) &&
3534 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
3535 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
3536
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003537 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003538 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
3539 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
3540 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
3541 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
3542 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003543
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003544 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003545 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003546 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003547 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003548 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003549 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003550
3551 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003552}
3553
Matt Ropered4a6a72016-02-23 17:20:13 -08003554bool ilk_disable_lp_wm(struct drm_device *dev)
Ville Syrjälä8553c182013-12-05 15:51:39 +02003555{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003556 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003557
3558 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3559}
3560
Matt Roper024c9042015-09-24 15:53:11 -07003561/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003562 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3563 * so assume we'll always need it in order to avoid underruns.
3564 */
3565static bool skl_needs_memory_bw_wa(struct intel_atomic_state *state)
3566{
3567 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3568
Rodrigo Vivib976dc52017-01-23 10:32:37 -08003569 if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv))
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003570 return true;
3571
3572 return false;
3573}
3574
Paulo Zanoni56feca92016-09-22 18:00:28 -03003575static bool
3576intel_has_sagv(struct drm_i915_private *dev_priv)
3577{
Rodrigo Vivi01971812017-08-09 13:52:44 -07003578 if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv) ||
3579 IS_CANNONLAKE(dev_priv))
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003580 return true;
3581
3582 if (IS_SKYLAKE(dev_priv) &&
3583 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED)
3584 return true;
3585
3586 return false;
Paulo Zanoni56feca92016-09-22 18:00:28 -03003587}
3588
Lyude656d1b82016-08-17 15:55:54 -04003589/*
3590 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3591 * depending on power and performance requirements. The display engine access
3592 * to system memory is blocked during the adjustment time. Because of the
3593 * blocking time, having this enabled can cause full system hangs and/or pipe
3594 * underruns if we don't meet all of the following requirements:
3595 *
3596 * - <= 1 pipe enabled
3597 * - All planes can enable watermarks for latencies >= SAGV engine block time
3598 * - We're not using an interlaced display configuration
3599 */
3600int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003601intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003602{
3603 int ret;
3604
Paulo Zanoni56feca92016-09-22 18:00:28 -03003605 if (!intel_has_sagv(dev_priv))
3606 return 0;
3607
3608 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003609 return 0;
3610
3611 DRM_DEBUG_KMS("Enabling the SAGV\n");
3612 mutex_lock(&dev_priv->rps.hw_lock);
3613
3614 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3615 GEN9_SAGV_ENABLE);
3616
3617 /* We don't need to wait for the SAGV when enabling */
3618 mutex_unlock(&dev_priv->rps.hw_lock);
3619
3620 /*
3621 * Some skl systems, pre-release machines in particular,
3622 * don't actually have an SAGV.
3623 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003624 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003625 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003626 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003627 return 0;
3628 } else if (ret < 0) {
3629 DRM_ERROR("Failed to enable the SAGV\n");
3630 return ret;
3631 }
3632
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003633 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003634 return 0;
3635}
3636
Lyude656d1b82016-08-17 15:55:54 -04003637int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003638intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003639{
Imre Deakb3b8e992016-12-05 18:27:38 +02003640 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003641
Paulo Zanoni56feca92016-09-22 18:00:28 -03003642 if (!intel_has_sagv(dev_priv))
3643 return 0;
3644
3645 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003646 return 0;
3647
3648 DRM_DEBUG_KMS("Disabling the SAGV\n");
3649 mutex_lock(&dev_priv->rps.hw_lock);
3650
3651 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003652 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3653 GEN9_SAGV_DISABLE,
3654 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3655 1);
Lyude656d1b82016-08-17 15:55:54 -04003656 mutex_unlock(&dev_priv->rps.hw_lock);
3657
Lyude656d1b82016-08-17 15:55:54 -04003658 /*
3659 * Some skl systems, pre-release machines in particular,
3660 * don't actually have an SAGV.
3661 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003662 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003663 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003664 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003665 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003666 } else if (ret < 0) {
3667 DRM_ERROR("Failed to disable the SAGV (%d)\n", ret);
3668 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003669 }
3670
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003671 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003672 return 0;
3673}
3674
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003675bool intel_can_enable_sagv(struct drm_atomic_state *state)
Lyude656d1b82016-08-17 15:55:54 -04003676{
3677 struct drm_device *dev = state->dev;
3678 struct drm_i915_private *dev_priv = to_i915(dev);
3679 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003680 struct intel_crtc *crtc;
3681 struct intel_plane *plane;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003682 struct intel_crtc_state *cstate;
Lyude656d1b82016-08-17 15:55:54 -04003683 enum pipe pipe;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003684 int level, latency;
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003685 int sagv_block_time_us = IS_GEN9(dev_priv) ? 30 : 20;
Lyude656d1b82016-08-17 15:55:54 -04003686
Paulo Zanoni56feca92016-09-22 18:00:28 -03003687 if (!intel_has_sagv(dev_priv))
3688 return false;
3689
Lyude656d1b82016-08-17 15:55:54 -04003690 /*
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003691 * SKL+ workaround: bspec recommends we disable the SAGV when we have
Lyude656d1b82016-08-17 15:55:54 -04003692 * more then one pipe enabled
3693 *
3694 * If there are no active CRTCs, no additional checks need be performed
3695 */
3696 if (hweight32(intel_state->active_crtcs) == 0)
3697 return true;
3698 else if (hweight32(intel_state->active_crtcs) > 1)
3699 return false;
3700
3701 /* Since we're now guaranteed to only have one active CRTC... */
3702 pipe = ffs(intel_state->active_crtcs) - 1;
Ville Syrjälä98187832016-10-31 22:37:10 +02003703 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003704 cstate = to_intel_crtc_state(crtc->base.state);
Lyude656d1b82016-08-17 15:55:54 -04003705
Paulo Zanonic89cadd2016-10-10 17:30:59 -03003706 if (crtc->base.state->adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003707 return false;
3708
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003709 for_each_intel_plane_on_crtc(dev, crtc, plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003710 struct skl_plane_wm *wm =
3711 &cstate->wm.skl.optimal.planes[plane->id];
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003712
Lyude656d1b82016-08-17 15:55:54 -04003713 /* Skip this plane if it's not enabled */
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003714 if (!wm->wm[0].plane_en)
Lyude656d1b82016-08-17 15:55:54 -04003715 continue;
3716
3717 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003718 for (level = ilk_wm_max_level(dev_priv);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003719 !wm->wm[level].plane_en; --level)
Lyude656d1b82016-08-17 15:55:54 -04003720 { }
3721
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003722 latency = dev_priv->wm.skl_latency[level];
3723
3724 if (skl_needs_memory_bw_wa(intel_state) &&
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003725 plane->base.state->fb->modifier ==
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003726 I915_FORMAT_MOD_X_TILED)
3727 latency += 15;
3728
Lyude656d1b82016-08-17 15:55:54 -04003729 /*
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003730 * If any of the planes on this pipe don't enable wm levels that
3731 * incur memory latencies higher than sagv_block_time_us we
3732 * can't enable the SAGV.
Lyude656d1b82016-08-17 15:55:54 -04003733 */
Paulo Zanonifdd11c22017-08-09 13:52:45 -07003734 if (latency < sagv_block_time_us)
Lyude656d1b82016-08-17 15:55:54 -04003735 return false;
3736 }
3737
3738 return true;
3739}
3740
Damien Lespiaub9cec072014-11-04 17:06:43 +00003741static void
3742skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
Matt Roper024c9042015-09-24 15:53:11 -07003743 const struct intel_crtc_state *cstate,
Matt Roperc107acf2016-05-12 07:06:01 -07003744 struct skl_ddb_entry *alloc, /* out */
3745 int *num_active /* out */)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003746{
Matt Roperc107acf2016-05-12 07:06:01 -07003747 struct drm_atomic_state *state = cstate->base.state;
3748 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
3749 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper024c9042015-09-24 15:53:11 -07003750 struct drm_crtc *for_crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003751 unsigned int pipe_size, ddb_size;
3752 int nth_active_pipe;
Matt Roperc107acf2016-05-12 07:06:01 -07003753
Matt Ropera6d3460e2016-05-12 07:06:04 -07003754 if (WARN_ON(!state) || !cstate->base.active) {
Damien Lespiaub9cec072014-11-04 17:06:43 +00003755 alloc->start = 0;
3756 alloc->end = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003757 *num_active = hweight32(dev_priv->active_crtcs);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003758 return;
3759 }
3760
Matt Ropera6d3460e2016-05-12 07:06:04 -07003761 if (intel_state->active_pipe_changes)
3762 *num_active = hweight32(intel_state->active_crtcs);
3763 else
3764 *num_active = hweight32(dev_priv->active_crtcs);
3765
Deepak M6f3fff62016-09-15 15:01:10 +05303766 ddb_size = INTEL_INFO(dev_priv)->ddb_size;
3767 WARN_ON(ddb_size == 0);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003768
3769 ddb_size -= 4; /* 4 blocks for bypass path allocation */
3770
Matt Roperc107acf2016-05-12 07:06:01 -07003771 /*
Matt Ropera6d3460e2016-05-12 07:06:04 -07003772 * If the state doesn't change the active CRTC's, then there's
3773 * no need to recalculate; the existing pipe allocation limits
3774 * should remain unchanged. Note that we're safe from racing
3775 * commits since any racing commit that changes the active CRTC
3776 * list would need to grab _all_ crtc locks, including the one
3777 * we currently hold.
Matt Roperc107acf2016-05-12 07:06:01 -07003778 */
Matt Ropera6d3460e2016-05-12 07:06:04 -07003779 if (!intel_state->active_pipe_changes) {
Maarten Lankhorst512b5522016-11-08 13:55:34 +01003780 /*
3781 * alloc may be cleared by clear_intel_crtc_state,
3782 * copy from old state to be sure
3783 */
3784 *alloc = to_intel_crtc_state(for_crtc->state)->wm.skl.ddb;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003785 return;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003786 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07003787
3788 nth_active_pipe = hweight32(intel_state->active_crtcs &
3789 (drm_crtc_mask(for_crtc) - 1));
3790 pipe_size = ddb_size / hweight32(intel_state->active_crtcs);
3791 alloc->start = nth_active_pipe * ddb_size / *num_active;
3792 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003793}
3794
Matt Roperc107acf2016-05-12 07:06:01 -07003795static unsigned int skl_cursor_allocation(int num_active)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003796{
Matt Roperc107acf2016-05-12 07:06:01 -07003797 if (num_active == 1)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003798 return 32;
3799
3800 return 8;
3801}
3802
Damien Lespiaua269c582014-11-04 17:06:49 +00003803static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
3804{
3805 entry->start = reg & 0x3ff;
3806 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00003807 if (entry->end)
3808 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00003809}
3810
Damien Lespiau08db6652014-11-04 17:06:52 +00003811void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
3812 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00003813{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003814 struct intel_crtc *crtc;
Damien Lespiaua269c582014-11-04 17:06:49 +00003815
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003816 memset(ddb, 0, sizeof(*ddb));
3817
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003818 for_each_intel_crtc(&dev_priv->drm, crtc) {
Imre Deak4d800032016-02-17 16:31:29 +02003819 enum intel_display_power_domain power_domain;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003820 enum plane_id plane_id;
3821 enum pipe pipe = crtc->pipe;
Imre Deak4d800032016-02-17 16:31:29 +02003822
3823 power_domain = POWER_DOMAIN_PIPE(pipe);
3824 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003825 continue;
3826
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003827 for_each_plane_id_on_crtc(crtc, plane_id) {
3828 u32 val;
Damien Lespiaua269c582014-11-04 17:06:49 +00003829
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003830 if (plane_id != PLANE_CURSOR)
3831 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
3832 else
3833 val = I915_READ(CUR_BUF_CFG(pipe));
3834
3835 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane_id], val);
3836 }
Imre Deak4d800032016-02-17 16:31:29 +02003837
3838 intel_display_power_put(dev_priv, power_domain);
Damien Lespiaua269c582014-11-04 17:06:49 +00003839 }
3840}
3841
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003842/*
3843 * Determines the downscale amount of a plane for the purposes of watermark calculations.
3844 * The bspec defines downscale amount as:
3845 *
3846 * """
3847 * Horizontal down scale amount = maximum[1, Horizontal source size /
3848 * Horizontal destination size]
3849 * Vertical down scale amount = maximum[1, Vertical source size /
3850 * Vertical destination size]
3851 * Total down scale amount = Horizontal down scale amount *
3852 * Vertical down scale amount
3853 * """
3854 *
3855 * Return value is provided in 16.16 fixed point form to retain fractional part.
3856 * Caller should take care of dividing & rounding off the value.
3857 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303858static uint_fixed_16_16_t
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003859skl_plane_downscale_amount(const struct intel_crtc_state *cstate,
3860 const struct intel_plane_state *pstate)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003861{
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003862 struct intel_plane *plane = to_intel_plane(pstate->base.plane);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003863 uint32_t src_w, src_h, dst_w, dst_h;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303864 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
3865 uint_fixed_16_16_t downscale_h, downscale_w;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003866
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003867 if (WARN_ON(!intel_wm_plane_visible(cstate, pstate)))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303868 return u32_to_fixed16(0);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003869
3870 /* n.b., src is 16.16 fixed point, dst is whole integer */
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003871 if (plane->id == PLANE_CURSOR) {
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03003872 /*
3873 * Cursors only support 0/180 degree rotation,
3874 * hence no need to account for rotation here.
3875 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303876 src_w = pstate->base.src_w >> 16;
3877 src_h = pstate->base.src_h >> 16;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003878 dst_w = pstate->base.crtc_w;
3879 dst_h = pstate->base.crtc_h;
3880 } else {
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03003881 /*
3882 * Src coordinates are already rotated by 270 degrees for
3883 * the 90/270 degree plane rotation cases (to match the
3884 * GTT mapping), hence no need to account for rotation here.
3885 */
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303886 src_w = drm_rect_width(&pstate->base.src) >> 16;
3887 src_h = drm_rect_height(&pstate->base.src) >> 16;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003888 dst_w = drm_rect_width(&pstate->base.dst);
3889 dst_h = drm_rect_height(&pstate->base.dst);
3890 }
3891
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303892 fp_w_ratio = div_fixed16(src_w, dst_w);
3893 fp_h_ratio = div_fixed16(src_h, dst_h);
3894 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
3895 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003896
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303897 return mul_fixed16(downscale_w, downscale_h);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003898}
3899
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303900static uint_fixed_16_16_t
3901skl_pipe_downscale_amount(const struct intel_crtc_state *crtc_state)
3902{
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303903 uint_fixed_16_16_t pipe_downscale = u32_to_fixed16(1);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303904
3905 if (!crtc_state->base.enable)
3906 return pipe_downscale;
3907
3908 if (crtc_state->pch_pfit.enabled) {
3909 uint32_t src_w, src_h, dst_w, dst_h;
3910 uint32_t pfit_size = crtc_state->pch_pfit.size;
3911 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
3912 uint_fixed_16_16_t downscale_h, downscale_w;
3913
3914 src_w = crtc_state->pipe_src_w;
3915 src_h = crtc_state->pipe_src_h;
3916 dst_w = pfit_size >> 16;
3917 dst_h = pfit_size & 0xffff;
3918
3919 if (!dst_w || !dst_h)
3920 return pipe_downscale;
3921
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303922 fp_w_ratio = div_fixed16(src_w, dst_w);
3923 fp_h_ratio = div_fixed16(src_h, dst_h);
3924 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
3925 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303926
3927 pipe_downscale = mul_fixed16(downscale_w, downscale_h);
3928 }
3929
3930 return pipe_downscale;
3931}
3932
3933int skl_check_pipe_max_pixel_rate(struct intel_crtc *intel_crtc,
3934 struct intel_crtc_state *cstate)
3935{
3936 struct drm_crtc_state *crtc_state = &cstate->base;
3937 struct drm_atomic_state *state = crtc_state->state;
3938 struct drm_plane *plane;
3939 const struct drm_plane_state *pstate;
3940 struct intel_plane_state *intel_pstate;
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02003941 int crtc_clock, dotclk;
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303942 uint32_t pipe_max_pixel_rate;
3943 uint_fixed_16_16_t pipe_downscale;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303944 uint_fixed_16_16_t max_downscale = u32_to_fixed16(1);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303945
3946 if (!cstate->base.enable)
3947 return 0;
3948
3949 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, crtc_state) {
3950 uint_fixed_16_16_t plane_downscale;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303951 uint_fixed_16_16_t fp_9_div_8 = div_fixed16(9, 8);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303952 int bpp;
3953
3954 if (!intel_wm_plane_visible(cstate,
3955 to_intel_plane_state(pstate)))
3956 continue;
3957
3958 if (WARN_ON(!pstate->fb))
3959 return -EINVAL;
3960
3961 intel_pstate = to_intel_plane_state(pstate);
3962 plane_downscale = skl_plane_downscale_amount(cstate,
3963 intel_pstate);
3964 bpp = pstate->fb->format->cpp[0] * 8;
3965 if (bpp == 64)
3966 plane_downscale = mul_fixed16(plane_downscale,
3967 fp_9_div_8);
3968
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05303969 max_downscale = max_fixed16(plane_downscale, max_downscale);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303970 }
3971 pipe_downscale = skl_pipe_downscale_amount(cstate);
3972
3973 pipe_downscale = mul_fixed16(pipe_downscale, max_downscale);
3974
3975 crtc_clock = crtc_state->adjusted_mode.crtc_clock;
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02003976 dotclk = to_intel_atomic_state(state)->cdclk.logical.cdclk;
3977
3978 if (IS_GEMINILAKE(to_i915(intel_crtc->base.dev)))
3979 dotclk *= 2;
3980
3981 pipe_max_pixel_rate = div_round_up_u32_fixed16(dotclk, pipe_downscale);
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303982
3983 if (pipe_max_pixel_rate < crtc_clock) {
Maarten Lankhorst789f35d2017-06-01 12:34:13 +02003984 DRM_DEBUG_KMS("Max supported pixel clock with scaling exceeded\n");
Mahesh Kumar73b0ca82017-05-26 20:45:46 +05303985 return -EINVAL;
3986 }
3987
3988 return 0;
3989}
3990
Damien Lespiaub9cec072014-11-04 17:06:43 +00003991static unsigned int
Matt Roper024c9042015-09-24 15:53:11 -07003992skl_plane_relative_data_rate(const struct intel_crtc_state *cstate,
3993 const struct drm_plane_state *pstate,
3994 int y)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003995{
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003996 struct intel_plane *plane = to_intel_plane(pstate->plane);
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003997 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
Kumar, Mahesh7084b502017-05-17 17:28:23 +05303998 uint32_t data_rate;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003999 uint32_t width = 0, height = 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02004000 struct drm_framebuffer *fb;
4001 u32 format;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304002 uint_fixed_16_16_t down_scale_amount;
Matt Ropera1de91e2016-05-12 07:05:57 -07004003
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004004 if (!intel_pstate->base.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07004005 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02004006
4007 fb = pstate->fb;
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004008 format = fb->format->format;
Ville Syrjälä83054942016-11-18 21:53:00 +02004009
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004010 if (plane->id == PLANE_CURSOR)
Matt Ropera1de91e2016-05-12 07:05:57 -07004011 return 0;
4012 if (y && format != DRM_FORMAT_NV12)
4013 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004014
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004015 /*
4016 * Src coordinates are already rotated by 270 degrees for
4017 * the 90/270 degree plane rotation cases (to match the
4018 * GTT mapping), hence no need to account for rotation here.
4019 */
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004020 width = drm_rect_width(&intel_pstate->base.src) >> 16;
4021 height = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07004022
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004023 /* for planar format */
Matt Ropera1de91e2016-05-12 07:05:57 -07004024 if (format == DRM_FORMAT_NV12) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004025 if (y) /* y-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004026 data_rate = width * height *
Ville Syrjälä353c8592016-12-14 23:30:57 +02004027 fb->format->cpp[0];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004028 else /* uv-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004029 data_rate = (width / 2) * (height / 2) *
Ville Syrjälä353c8592016-12-14 23:30:57 +02004030 fb->format->cpp[1];
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004031 } else {
4032 /* for packed formats */
Ville Syrjälä353c8592016-12-14 23:30:57 +02004033 data_rate = width * height * fb->format->cpp[0];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004034 }
4035
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004036 down_scale_amount = skl_plane_downscale_amount(cstate, intel_pstate);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07004037
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304038 return mul_round_up_u32_fixed16(data_rate, down_scale_amount);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004039}
4040
4041/*
4042 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
4043 * a 8192x4096@32bpp framebuffer:
4044 * 3 * 4096 * 8192 * 4 < 2^32
4045 */
4046static unsigned int
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004047skl_get_total_relative_data_rate(struct intel_crtc_state *intel_cstate,
4048 unsigned *plane_data_rate,
4049 unsigned *plane_y_data_rate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00004050{
Matt Roper9c74d822016-05-12 07:05:58 -07004051 struct drm_crtc_state *cstate = &intel_cstate->base;
4052 struct drm_atomic_state *state = cstate->state;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02004053 struct drm_plane *plane;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02004054 const struct drm_plane_state *pstate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004055 unsigned int total_data_rate = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07004056
4057 if (WARN_ON(!state))
4058 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004059
Matt Ropera1de91e2016-05-12 07:05:57 -07004060 /* Calculate and cache data rate for each plane */
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02004061 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, cstate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004062 enum plane_id plane_id = to_intel_plane(plane)->id;
4063 unsigned int rate;
Matt Roper024c9042015-09-24 15:53:11 -07004064
Matt Ropera6d3460e2016-05-12 07:06:04 -07004065 /* packed/uv */
4066 rate = skl_plane_relative_data_rate(intel_cstate,
4067 pstate, 0);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004068 plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004069
4070 total_data_rate += rate;
Matt Roper9c74d822016-05-12 07:05:58 -07004071
Matt Ropera6d3460e2016-05-12 07:06:04 -07004072 /* y-plane */
4073 rate = skl_plane_relative_data_rate(intel_cstate,
4074 pstate, 1);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004075 plane_y_data_rate[plane_id] = rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07004076
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004077 total_data_rate += rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004078 }
4079
4080 return total_data_rate;
4081}
4082
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004083static uint16_t
4084skl_ddb_min_alloc(const struct drm_plane_state *pstate,
4085 const int y)
4086{
4087 struct drm_framebuffer *fb = pstate->fb;
4088 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
4089 uint32_t src_w, src_h;
4090 uint32_t min_scanlines = 8;
4091 uint8_t plane_bpp;
4092
4093 if (WARN_ON(!fb))
4094 return 0;
4095
4096 /* For packed formats, no y-plane, return 0 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004097 if (y && fb->format->format != DRM_FORMAT_NV12)
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004098 return 0;
4099
4100 /* For Non Y-tile return 8-blocks */
Ville Syrjäläbae781b2016-11-16 13:33:16 +02004101 if (fb->modifier != I915_FORMAT_MOD_Y_TILED &&
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07004102 fb->modifier != I915_FORMAT_MOD_Yf_TILED &&
4103 fb->modifier != I915_FORMAT_MOD_Y_TILED_CCS &&
4104 fb->modifier != I915_FORMAT_MOD_Yf_TILED_CCS)
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004105 return 8;
4106
Ville Syrjäläfce5adf2017-03-31 21:00:55 +03004107 /*
4108 * Src coordinates are already rotated by 270 degrees for
4109 * the 90/270 degree plane rotation cases (to match the
4110 * GTT mapping), hence no need to account for rotation here.
4111 */
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004112 src_w = drm_rect_width(&intel_pstate->base.src) >> 16;
4113 src_h = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004114
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004115 /* Halve UV plane width and height for NV12 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004116 if (fb->format->format == DRM_FORMAT_NV12 && !y) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004117 src_w /= 2;
4118 src_h /= 2;
4119 }
4120
Ville Syrjälä438b74a2016-12-14 23:32:55 +02004121 if (fb->format->format == DRM_FORMAT_NV12 && !y)
Ville Syrjälä353c8592016-12-14 23:30:57 +02004122 plane_bpp = fb->format->cpp[1];
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004123 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02004124 plane_bpp = fb->format->cpp[0];
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004125
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03004126 if (drm_rotation_90_or_270(pstate->rotation)) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07004127 switch (plane_bpp) {
4128 case 1:
4129 min_scanlines = 32;
4130 break;
4131 case 2:
4132 min_scanlines = 16;
4133 break;
4134 case 4:
4135 min_scanlines = 8;
4136 break;
4137 case 8:
4138 min_scanlines = 4;
4139 break;
4140 default:
4141 WARN(1, "Unsupported pixel depth %u for rotation",
4142 plane_bpp);
4143 min_scanlines = 32;
4144 }
4145 }
4146
4147 return DIV_ROUND_UP((4 * src_w * plane_bpp), 512) * min_scanlines/4 + 3;
4148}
4149
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004150static void
4151skl_ddb_calc_min(const struct intel_crtc_state *cstate, int num_active,
4152 uint16_t *minimum, uint16_t *y_minimum)
4153{
4154 const struct drm_plane_state *pstate;
4155 struct drm_plane *plane;
4156
4157 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, &cstate->base) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004158 enum plane_id plane_id = to_intel_plane(plane)->id;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004159
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004160 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004161 continue;
4162
4163 if (!pstate->visible)
4164 continue;
4165
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004166 minimum[plane_id] = skl_ddb_min_alloc(pstate, 0);
4167 y_minimum[plane_id] = skl_ddb_min_alloc(pstate, 1);
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004168 }
4169
4170 minimum[PLANE_CURSOR] = skl_cursor_allocation(num_active);
4171}
4172
Matt Roperc107acf2016-05-12 07:06:01 -07004173static int
Matt Roper024c9042015-09-24 15:53:11 -07004174skl_allocate_pipe_ddb(struct intel_crtc_state *cstate,
Damien Lespiaub9cec072014-11-04 17:06:43 +00004175 struct skl_ddb_allocation *ddb /* out */)
4176{
Matt Roperc107acf2016-05-12 07:06:01 -07004177 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07004178 struct drm_crtc *crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004179 struct drm_device *dev = crtc->dev;
4180 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4181 enum pipe pipe = intel_crtc->pipe;
Lyudece0ba282016-09-15 10:46:35 -04004182 struct skl_ddb_entry *alloc = &cstate->wm.skl.ddb;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004183 uint16_t alloc_size, start;
Maarten Lankhorstfefdd812016-10-26 15:41:33 +02004184 uint16_t minimum[I915_MAX_PLANES] = {};
4185 uint16_t y_minimum[I915_MAX_PLANES] = {};
Damien Lespiaub9cec072014-11-04 17:06:43 +00004186 unsigned int total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004187 enum plane_id plane_id;
Matt Roperc107acf2016-05-12 07:06:01 -07004188 int num_active;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004189 unsigned plane_data_rate[I915_MAX_PLANES] = {};
4190 unsigned plane_y_data_rate[I915_MAX_PLANES] = {};
Kumar, Mahesh5ba6faa2017-05-17 17:28:26 +05304191 uint16_t total_min_blocks = 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004192
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004193 /* Clear the partitioning for disabled planes. */
4194 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
4195 memset(ddb->y_plane[pipe], 0, sizeof(ddb->y_plane[pipe]));
4196
Matt Ropera6d3460e2016-05-12 07:06:04 -07004197 if (WARN_ON(!state))
4198 return 0;
4199
Matt Roperc107acf2016-05-12 07:06:01 -07004200 if (!cstate->base.active) {
Lyudece0ba282016-09-15 10:46:35 -04004201 alloc->start = alloc->end = 0;
Matt Roperc107acf2016-05-12 07:06:01 -07004202 return 0;
4203 }
4204
Matt Ropera6d3460e2016-05-12 07:06:04 -07004205 skl_ddb_get_pipe_allocation_limits(dev, cstate, alloc, &num_active);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00004206 alloc_size = skl_ddb_entry_size(alloc);
Kumar, Mahesh336031e2017-05-17 17:28:25 +05304207 if (alloc_size == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004208 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004209
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004210 skl_ddb_calc_min(cstate, num_active, minimum, y_minimum);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004211
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004212 /*
4213 * 1. Allocate the mininum required blocks for each active plane
4214 * and allocate the cursor, it doesn't require extra allocation
4215 * proportional to the data rate.
4216 */
Damien Lespiaub9cec072014-11-04 17:06:43 +00004217
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004218 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Kumar, Mahesh5ba6faa2017-05-17 17:28:26 +05304219 total_min_blocks += minimum[plane_id];
4220 total_min_blocks += y_minimum[plane_id];
Damien Lespiau80958152015-02-09 13:35:10 +00004221 }
4222
Kumar, Mahesh5ba6faa2017-05-17 17:28:26 +05304223 if (total_min_blocks > alloc_size) {
4224 DRM_DEBUG_KMS("Requested display configuration exceeds system DDB limitations");
4225 DRM_DEBUG_KMS("minimum required %d/%d\n", total_min_blocks,
4226 alloc_size);
4227 return -EINVAL;
4228 }
4229
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004230 alloc_size -= total_min_blocks;
4231 ddb->plane[pipe][PLANE_CURSOR].start = alloc->end - minimum[PLANE_CURSOR];
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004232 ddb->plane[pipe][PLANE_CURSOR].end = alloc->end;
4233
Damien Lespiaub9cec072014-11-04 17:06:43 +00004234 /*
Damien Lespiau80958152015-02-09 13:35:10 +00004235 * 2. Distribute the remaining space in proportion to the amount of
4236 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00004237 *
4238 * FIXME: we may not allocate every single block here.
4239 */
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02004240 total_data_rate = skl_get_total_relative_data_rate(cstate,
4241 plane_data_rate,
4242 plane_y_data_rate);
Matt Ropera1de91e2016-05-12 07:05:57 -07004243 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07004244 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004245
Damien Lespiau34bb56a2014-11-04 17:07:01 +00004246 start = alloc->start;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004247 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004248 unsigned int data_rate, y_data_rate;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004249 uint16_t plane_blocks, y_plane_blocks = 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004250
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004251 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02004252 continue;
4253
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004254 data_rate = plane_data_rate[plane_id];
Damien Lespiaub9cec072014-11-04 17:06:43 +00004255
4256 /*
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004257 * allocation for (packed formats) or (uv-plane part of planar format):
Damien Lespiaub9cec072014-11-04 17:06:43 +00004258 * promote the expression to 64 bits to avoid overflowing, the
4259 * result is < available as data_rate / total_data_rate < 1
4260 */
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004261 plane_blocks = minimum[plane_id];
4262 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
4263 total_data_rate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00004264
Matt Roperc107acf2016-05-12 07:06:01 -07004265 /* Leave disabled planes at (0,0) */
4266 if (data_rate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004267 ddb->plane[pipe][plane_id].start = start;
4268 ddb->plane[pipe][plane_id].end = start + plane_blocks;
Matt Roperc107acf2016-05-12 07:06:01 -07004269 }
Damien Lespiaub9cec072014-11-04 17:06:43 +00004270
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004271 start += plane_blocks;
4272
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004273 /*
4274 * allocation for y_plane part of planar format:
4275 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004276 y_data_rate = plane_y_data_rate[plane_id];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07004277
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004278 y_plane_blocks = y_minimum[plane_id];
4279 y_plane_blocks += div_u64((uint64_t)alloc_size * y_data_rate,
4280 total_data_rate);
4281
Matt Roperc107acf2016-05-12 07:06:01 -07004282 if (y_data_rate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004283 ddb->y_plane[pipe][plane_id].start = start;
4284 ddb->y_plane[pipe][plane_id].end = start + y_plane_blocks;
Matt Roperc107acf2016-05-12 07:06:01 -07004285 }
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004286
4287 start += y_plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004288 }
4289
Matt Roperc107acf2016-05-12 07:06:01 -07004290 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00004291}
4292
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004293/*
4294 * The max latency should be 257 (max the punit can code is 255 and we add 2us
Ville Syrjäläac484962016-01-20 21:05:26 +02004295 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004296 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
4297 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
4298*/
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07004299static uint_fixed_16_16_t
4300skl_wm_method1(const struct drm_i915_private *dev_priv, uint32_t pixel_rate,
4301 uint8_t cpp, uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004302{
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304303 uint32_t wm_intermediate_val;
4304 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004305
4306 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304307 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004308
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304309 wm_intermediate_val = latency * pixel_rate * cpp;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304310 ret = div_fixed16(wm_intermediate_val, 1000 * 512);
Paulo Zanoni6c64dd32017-08-11 16:38:25 -07004311
4312 if (INTEL_GEN(dev_priv) >= 10)
4313 ret = add_fixed16_u32(ret, 1);
4314
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004315 return ret;
4316}
4317
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304318static uint_fixed_16_16_t skl_wm_method2(uint32_t pixel_rate,
4319 uint32_t pipe_htotal,
4320 uint32_t latency,
4321 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004322{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004323 uint32_t wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304324 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004325
4326 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304327 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004328
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004329 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304330 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
4331 pipe_htotal * 1000);
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304332 ret = mul_u32_fixed16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004333 return ret;
4334}
4335
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304336static uint_fixed_16_16_t
4337intel_get_linetime_us(struct intel_crtc_state *cstate)
4338{
4339 uint32_t pixel_rate;
4340 uint32_t crtc_htotal;
4341 uint_fixed_16_16_t linetime_us;
4342
4343 if (!cstate->base.active)
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304344 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304345
4346 pixel_rate = cstate->pixel_rate;
4347
4348 if (WARN_ON(pixel_rate == 0))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304349 return u32_to_fixed16(0);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304350
4351 crtc_htotal = cstate->base.adjusted_mode.crtc_htotal;
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304352 linetime_us = div_fixed16(crtc_htotal * 1000, pixel_rate);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304353
4354 return linetime_us;
4355}
4356
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304357static uint32_t
4358skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *cstate,
4359 const struct intel_plane_state *pstate)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004360{
4361 uint64_t adjusted_pixel_rate;
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304362 uint_fixed_16_16_t downscale_amount;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004363
4364 /* Shouldn't reach here on disabled planes... */
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004365 if (WARN_ON(!intel_wm_plane_visible(cstate, pstate)))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004366 return 0;
4367
4368 /*
4369 * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
4370 * with additional adjustments for plane-specific scaling.
4371 */
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02004372 adjusted_pixel_rate = cstate->pixel_rate;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004373 downscale_amount = skl_plane_downscale_amount(cstate, pstate);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004374
Kumar, Mahesh7084b502017-05-17 17:28:23 +05304375 return mul_round_up_u32_fixed16(adjusted_pixel_rate,
4376 downscale_amount);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07004377}
4378
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304379static int
4380skl_compute_plane_wm_params(const struct drm_i915_private *dev_priv,
4381 struct intel_crtc_state *cstate,
4382 const struct intel_plane_state *intel_pstate,
4383 struct skl_wm_params *wp)
4384{
4385 struct intel_plane *plane = to_intel_plane(intel_pstate->base.plane);
4386 const struct drm_plane_state *pstate = &intel_pstate->base;
4387 const struct drm_framebuffer *fb = pstate->fb;
4388 uint32_t interm_pbpl;
4389 struct intel_atomic_state *state =
4390 to_intel_atomic_state(cstate->base.state);
4391 bool apply_memory_bw_wa = skl_needs_memory_bw_wa(state);
4392
4393 if (!intel_wm_plane_visible(cstate, intel_pstate))
4394 return 0;
4395
4396 wp->y_tiled = fb->modifier == I915_FORMAT_MOD_Y_TILED ||
4397 fb->modifier == I915_FORMAT_MOD_Yf_TILED ||
4398 fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
4399 fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
4400 wp->x_tiled = fb->modifier == I915_FORMAT_MOD_X_TILED;
4401 wp->rc_surface = fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
4402 fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
4403
4404 if (plane->id == PLANE_CURSOR) {
4405 wp->width = intel_pstate->base.crtc_w;
4406 } else {
4407 /*
4408 * Src coordinates are already rotated by 270 degrees for
4409 * the 90/270 degree plane rotation cases (to match the
4410 * GTT mapping), hence no need to account for rotation here.
4411 */
4412 wp->width = drm_rect_width(&intel_pstate->base.src) >> 16;
4413 }
4414
4415 wp->cpp = (fb->format->format == DRM_FORMAT_NV12) ? fb->format->cpp[1] :
4416 fb->format->cpp[0];
4417 wp->plane_pixel_rate = skl_adjusted_plane_pixel_rate(cstate,
4418 intel_pstate);
4419
4420 if (drm_rotation_90_or_270(pstate->rotation)) {
4421
4422 switch (wp->cpp) {
4423 case 1:
4424 wp->y_min_scanlines = 16;
4425 break;
4426 case 2:
4427 wp->y_min_scanlines = 8;
4428 break;
4429 case 4:
4430 wp->y_min_scanlines = 4;
4431 break;
4432 default:
4433 MISSING_CASE(wp->cpp);
4434 return -EINVAL;
4435 }
4436 } else {
4437 wp->y_min_scanlines = 4;
4438 }
4439
4440 if (apply_memory_bw_wa)
4441 wp->y_min_scanlines *= 2;
4442
4443 wp->plane_bytes_per_line = wp->width * wp->cpp;
4444 if (wp->y_tiled) {
4445 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line *
4446 wp->y_min_scanlines, 512);
4447
4448 if (INTEL_GEN(dev_priv) >= 10)
4449 interm_pbpl++;
4450
4451 wp->plane_blocks_per_line = div_fixed16(interm_pbpl,
4452 wp->y_min_scanlines);
4453 } else if (wp->x_tiled && IS_GEN9(dev_priv)) {
4454 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line, 512);
4455 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
4456 } else {
4457 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line, 512) + 1;
4458 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
4459 }
4460
4461 wp->y_tile_minimum = mul_u32_fixed16(wp->y_min_scanlines,
4462 wp->plane_blocks_per_line);
4463 wp->linetime_us = fixed16_to_u32_round_up(
4464 intel_get_linetime_us(cstate));
4465
4466 return 0;
4467}
4468
Matt Roper55994c22016-05-12 07:06:08 -07004469static int skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
4470 struct intel_crtc_state *cstate,
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304471 const struct intel_plane_state *intel_pstate,
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004472 uint16_t ddb_allocation,
Matt Roper55994c22016-05-12 07:06:08 -07004473 int level,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304474 const struct skl_wm_params *wp,
Matt Roper55994c22016-05-12 07:06:08 -07004475 uint16_t *out_blocks, /* out */
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004476 uint8_t *out_lines, /* out */
4477 bool *enabled /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004478{
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304479 const struct drm_plane_state *pstate = &intel_pstate->base;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004480 uint32_t latency = dev_priv->wm.skl_latency[level];
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304481 uint_fixed_16_16_t method1, method2;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05304482 uint_fixed_16_16_t selected_result;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004483 uint32_t res_blocks, res_lines;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03004484 struct intel_atomic_state *state =
4485 to_intel_atomic_state(cstate->base.state);
4486 bool apply_memory_bw_wa = skl_needs_memory_bw_wa(state);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004487
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02004488 if (latency == 0 ||
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004489 !intel_wm_plane_visible(cstate, intel_pstate)) {
4490 *enabled = false;
Matt Roper55994c22016-05-12 07:06:08 -07004491 return 0;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004492 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004493
Rodrigo Vivi82525c12017-06-08 08:50:00 -07004494 /* Display WA #1141: kbl,cfl */
4495 if ((IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv)) &&
4496 dev_priv->ipc_enabled)
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05304497 latency += 4;
4498
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304499 if (apply_memory_bw_wa && wp->x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03004500 latency += 15;
4501
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304502 method1 = skl_wm_method1(dev_priv, wp->plane_pixel_rate,
4503 wp->cpp, latency);
4504 method2 = skl_wm_method2(wp->plane_pixel_rate,
Matt Roper024c9042015-09-24 15:53:11 -07004505 cstate->base.adjusted_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03004506 latency,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304507 wp->plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004508
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304509 if (wp->y_tiled) {
4510 selected_result = max_fixed16(method2, wp->y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004511 } else {
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304512 if ((wp->cpp * cstate->base.adjusted_mode.crtc_htotal /
4513 512 < 1) && (wp->plane_bytes_per_line / 512 < 1))
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03004514 selected_result = method2;
Maarten Lankhorst54d20ed2017-07-17 14:02:30 +02004515 else if (ddb_allocation >=
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304516 fixed16_to_u32_round_up(wp->plane_blocks_per_line))
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304517 selected_result = min_fixed16(method1, method2);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304518 else if (latency >= wp->linetime_us)
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304519 selected_result = min_fixed16(method1, method2);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004520 else
4521 selected_result = method1;
4522 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004523
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304524 res_blocks = fixed16_to_u32_round_up(selected_result) + 1;
Kumar, Maheshd273ecc2017-05-17 17:28:22 +05304525 res_lines = div_round_up_fixed16(selected_result,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304526 wp->plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00004527
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07004528 /* Display WA #1125: skl,bxt,kbl,glk */
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304529 if (level == 0 && wp->rc_surface)
4530 res_blocks += fixed16_to_u32_round_up(wp->y_tile_minimum);
Ville Syrjälä2e2adb02017-08-01 09:58:13 -07004531
4532 /* Display WA #1126: skl,bxt,kbl,glk */
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004533 if (level >= 1 && level <= 7) {
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304534 if (wp->y_tiled) {
4535 res_blocks += fixed16_to_u32_round_up(
4536 wp->y_tile_minimum);
4537 res_lines += wp->y_min_scanlines;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004538 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004539 res_blocks++;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03004540 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00004541 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00004542
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004543 if (res_blocks >= ddb_allocation || res_lines > 31) {
4544 *enabled = false;
Matt Roper6b6bada2016-05-12 07:06:10 -07004545
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004546 /*
4547 * If there are no valid level 0 watermarks, then we can't
4548 * support this display configuration.
4549 */
4550 if (level) {
4551 return 0;
4552 } else {
4553 struct drm_plane *plane = pstate->plane;
4554
4555 DRM_DEBUG_KMS("Requested display configuration exceeds system watermark limitations\n");
4556 DRM_DEBUG_KMS("[PLANE:%d:%s] blocks required = %u/%u, lines required = %u/31\n",
4557 plane->base.id, plane->name,
4558 res_blocks, ddb_allocation, res_lines);
4559 return -EINVAL;
4560 }
Matt Roper55994c22016-05-12 07:06:08 -07004561 }
Damien Lespiaue6d66172014-11-04 17:06:55 +00004562
4563 *out_blocks = res_blocks;
4564 *out_lines = res_lines;
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004565 *enabled = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004566
Matt Roper55994c22016-05-12 07:06:08 -07004567 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004568}
4569
Matt Roperf4a96752016-05-12 07:06:06 -07004570static int
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304571skl_compute_wm_levels(const struct drm_i915_private *dev_priv,
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004572 struct skl_ddb_allocation *ddb,
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304573 struct intel_crtc_state *cstate,
4574 const struct intel_plane_state *intel_pstate,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304575 const struct skl_wm_params *wm_params,
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304576 struct skl_plane_wm *wm)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004577{
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004578 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
4579 struct drm_plane *plane = intel_pstate->base.plane;
4580 struct intel_plane *intel_plane = to_intel_plane(plane);
4581 uint16_t ddb_blocks;
4582 enum pipe pipe = intel_crtc->pipe;
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304583 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Roper55994c22016-05-12 07:06:08 -07004584 int ret;
Lyudea62163e2016-10-04 14:28:20 -04004585
Kumar, Mahesh7b751192017-05-17 17:28:24 +05304586 if (WARN_ON(!intel_pstate->base.fb))
4587 return -EINVAL;
Matt Roper024c9042015-09-24 15:53:11 -07004588
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004589 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][intel_plane->id]);
4590
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304591 for (level = 0; level <= max_level; level++) {
4592 struct skl_wm_level *result = &wm->wm[level];
4593
4594 ret = skl_compute_plane_wm(dev_priv,
4595 cstate,
4596 intel_pstate,
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004597 ddb_blocks,
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304598 level,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304599 wm_params,
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304600 &result->plane_res_b,
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004601 &result->plane_res_l,
4602 &result->plane_en);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304603 if (ret)
4604 return ret;
4605 }
Matt Roperf4a96752016-05-12 07:06:06 -07004606
4607 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004608}
4609
Damien Lespiau407b50f2014-11-04 17:06:57 +00004610static uint32_t
Matt Roper024c9042015-09-24 15:53:11 -07004611skl_compute_linetime_wm(struct intel_crtc_state *cstate)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004612{
Mahesh Kumara3a89862016-12-01 21:19:34 +05304613 struct drm_atomic_state *state = cstate->base.state;
4614 struct drm_i915_private *dev_priv = to_i915(state->dev);
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304615 uint_fixed_16_16_t linetime_us;
Mahesh Kumara3a89862016-12-01 21:19:34 +05304616 uint32_t linetime_wm;
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03004617
Kumar, Maheshd555cb52017-05-17 17:28:29 +05304618 linetime_us = intel_get_linetime_us(cstate);
4619
4620 if (is_fixed16_zero(linetime_us))
Damien Lespiau407b50f2014-11-04 17:06:57 +00004621 return 0;
4622
Kumar, Mahesheac2cb82017-07-05 20:01:46 +05304623 linetime_wm = fixed16_to_u32_round_up(mul_u32_fixed16(8, linetime_us));
Mahesh Kumara3a89862016-12-01 21:19:34 +05304624
4625 /* Display WA #1135: bxt. */
4626 if (IS_BROXTON(dev_priv) && dev_priv->ipc_enabled)
4627 linetime_wm = DIV_ROUND_UP(linetime_wm, 2);
4628
4629 return linetime_wm;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004630}
4631
Matt Roper024c9042015-09-24 15:53:11 -07004632static void skl_compute_transition_wm(struct intel_crtc_state *cstate,
Damien Lespiau9414f562014-11-04 17:06:58 +00004633 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004634{
Matt Roper024c9042015-09-24 15:53:11 -07004635 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004636 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00004637
4638 /* Until we know more, just disable transition WMs */
Lyudea62163e2016-10-04 14:28:20 -04004639 trans_wm->plane_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004640}
4641
Matt Roper55994c22016-05-12 07:06:08 -07004642static int skl_build_pipe_wm(struct intel_crtc_state *cstate,
4643 struct skl_ddb_allocation *ddb,
4644 struct skl_pipe_wm *pipe_wm)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004645{
Matt Roper024c9042015-09-24 15:53:11 -07004646 struct drm_device *dev = cstate->base.crtc->dev;
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304647 struct drm_crtc_state *crtc_state = &cstate->base;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004648 const struct drm_i915_private *dev_priv = to_i915(dev);
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304649 struct drm_plane *plane;
4650 const struct drm_plane_state *pstate;
Lyudea62163e2016-10-04 14:28:20 -04004651 struct skl_plane_wm *wm;
Matt Roper55994c22016-05-12 07:06:08 -07004652 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004653
Lyudea62163e2016-10-04 14:28:20 -04004654 /*
4655 * We'll only calculate watermarks for planes that are actually
4656 * enabled, so make sure all other planes are set as disabled.
4657 */
4658 memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
4659
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304660 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, crtc_state) {
4661 const struct intel_plane_state *intel_pstate =
4662 to_intel_plane_state(pstate);
4663 enum plane_id plane_id = to_intel_plane(plane)->id;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304664 struct skl_wm_params wm_params;
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304665
4666 wm = &pipe_wm->planes[plane_id];
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304667 memset(&wm_params, 0, sizeof(struct skl_wm_params));
4668
4669 ret = skl_compute_plane_wm_params(dev_priv, cstate,
4670 intel_pstate, &wm_params);
4671 if (ret)
4672 return ret;
Lyudea62163e2016-10-04 14:28:20 -04004673
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004674 ret = skl_compute_wm_levels(dev_priv, ddb, cstate,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304675 intel_pstate, &wm_params, wm);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304676 if (ret)
4677 return ret;
Lyudea62163e2016-10-04 14:28:20 -04004678 skl_compute_transition_wm(cstate, &wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004679 }
Matt Roper024c9042015-09-24 15:53:11 -07004680 pipe_wm->linetime = skl_compute_linetime_wm(cstate);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004681
Matt Roper55994c22016-05-12 07:06:08 -07004682 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004683}
4684
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004685static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
4686 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00004687 const struct skl_ddb_entry *entry)
4688{
4689 if (entry->end)
4690 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
4691 else
4692 I915_WRITE(reg, 0);
4693}
4694
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004695static void skl_write_wm_level(struct drm_i915_private *dev_priv,
4696 i915_reg_t reg,
4697 const struct skl_wm_level *level)
4698{
4699 uint32_t val = 0;
4700
4701 if (level->plane_en) {
4702 val |= PLANE_WM_EN;
4703 val |= level->plane_res_b;
4704 val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
4705 }
4706
4707 I915_WRITE(reg, val);
4708}
4709
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004710static void skl_write_plane_wm(struct intel_crtc *intel_crtc,
4711 const struct skl_plane_wm *wm,
4712 const struct skl_ddb_allocation *ddb,
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004713 enum plane_id plane_id)
Lyude62e0fb82016-08-22 12:50:08 -04004714{
4715 struct drm_crtc *crtc = &intel_crtc->base;
4716 struct drm_device *dev = crtc->dev;
4717 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004718 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004719 enum pipe pipe = intel_crtc->pipe;
4720
4721 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004722 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004723 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004724 }
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004725 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004726 &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004727
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004728 skl_ddb_entry_write(dev_priv, PLANE_BUF_CFG(pipe, plane_id),
4729 &ddb->plane[pipe][plane_id]);
4730 skl_ddb_entry_write(dev_priv, PLANE_NV12_BUF_CFG(pipe, plane_id),
4731 &ddb->y_plane[pipe][plane_id]);
Lyude62e0fb82016-08-22 12:50:08 -04004732}
4733
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004734static void skl_write_cursor_wm(struct intel_crtc *intel_crtc,
4735 const struct skl_plane_wm *wm,
4736 const struct skl_ddb_allocation *ddb)
Lyude62e0fb82016-08-22 12:50:08 -04004737{
4738 struct drm_crtc *crtc = &intel_crtc->base;
4739 struct drm_device *dev = crtc->dev;
4740 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004741 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004742 enum pipe pipe = intel_crtc->pipe;
4743
4744 for (level = 0; level <= max_level; level++) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004745 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
4746 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004747 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004748 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004749
4750 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004751 &ddb->plane[pipe][PLANE_CURSOR]);
Lyude62e0fb82016-08-22 12:50:08 -04004752}
4753
cpaul@redhat.com45ece232016-10-14 17:31:56 -04004754bool skl_wm_level_equals(const struct skl_wm_level *l1,
4755 const struct skl_wm_level *l2)
4756{
4757 if (l1->plane_en != l2->plane_en)
4758 return false;
4759
4760 /* If both planes aren't enabled, the rest shouldn't matter */
4761 if (!l1->plane_en)
4762 return true;
4763
4764 return (l1->plane_res_l == l2->plane_res_l &&
4765 l1->plane_res_b == l2->plane_res_b);
4766}
4767
Lyude27082492016-08-24 07:48:10 +02004768static inline bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
4769 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004770{
Lyude27082492016-08-24 07:48:10 +02004771 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004772}
4773
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01004774bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry **entries,
4775 const struct skl_ddb_entry *ddb,
4776 int ignore)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004777{
Lyudece0ba282016-09-15 10:46:35 -04004778 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004779
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01004780 for (i = 0; i < I915_MAX_PIPES; i++)
4781 if (i != ignore && entries[i] &&
4782 skl_ddb_entries_overlap(ddb, entries[i]))
Lyude27082492016-08-24 07:48:10 +02004783 return true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004784
Lyude27082492016-08-24 07:48:10 +02004785 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004786}
4787
Matt Roper55994c22016-05-12 07:06:08 -07004788static int skl_update_pipe_wm(struct drm_crtc_state *cstate,
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004789 const struct skl_pipe_wm *old_pipe_wm,
Matt Roper55994c22016-05-12 07:06:08 -07004790 struct skl_pipe_wm *pipe_wm, /* out */
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004791 struct skl_ddb_allocation *ddb, /* out */
Matt Roper55994c22016-05-12 07:06:08 -07004792 bool *changed /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004793{
Matt Roperf4a96752016-05-12 07:06:06 -07004794 struct intel_crtc_state *intel_cstate = to_intel_crtc_state(cstate);
Matt Roper55994c22016-05-12 07:06:08 -07004795 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004796
Matt Roper55994c22016-05-12 07:06:08 -07004797 ret = skl_build_pipe_wm(intel_cstate, ddb, pipe_wm);
4798 if (ret)
4799 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004800
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004801 if (!memcmp(old_pipe_wm, pipe_wm, sizeof(*pipe_wm)))
Matt Roper55994c22016-05-12 07:06:08 -07004802 *changed = false;
4803 else
4804 *changed = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004805
Matt Roper55994c22016-05-12 07:06:08 -07004806 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004807}
4808
Matt Roper9b613022016-06-27 16:42:44 -07004809static uint32_t
4810pipes_modified(struct drm_atomic_state *state)
4811{
4812 struct drm_crtc *crtc;
4813 struct drm_crtc_state *cstate;
4814 uint32_t i, ret = 0;
4815
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004816 for_each_new_crtc_in_state(state, crtc, cstate, i)
Matt Roper9b613022016-06-27 16:42:44 -07004817 ret |= drm_crtc_mask(crtc);
4818
4819 return ret;
4820}
4821
Jani Nikulabb7791b2016-10-04 12:29:17 +03004822static int
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004823skl_ddb_add_affected_planes(struct intel_crtc_state *cstate)
4824{
4825 struct drm_atomic_state *state = cstate->base.state;
4826 struct drm_device *dev = state->dev;
4827 struct drm_crtc *crtc = cstate->base.crtc;
4828 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4829 struct drm_i915_private *dev_priv = to_i915(dev);
4830 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4831 struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
4832 struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
4833 struct drm_plane_state *plane_state;
4834 struct drm_plane *plane;
4835 enum pipe pipe = intel_crtc->pipe;
4836
4837 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
4838
4839 drm_for_each_plane_mask(plane, dev, cstate->base.plane_mask) {
4840 enum plane_id plane_id = to_intel_plane(plane)->id;
4841
4842 if (skl_ddb_entry_equal(&cur_ddb->plane[pipe][plane_id],
4843 &new_ddb->plane[pipe][plane_id]) &&
4844 skl_ddb_entry_equal(&cur_ddb->y_plane[pipe][plane_id],
4845 &new_ddb->y_plane[pipe][plane_id]))
4846 continue;
4847
4848 plane_state = drm_atomic_get_plane_state(state, plane);
4849 if (IS_ERR(plane_state))
4850 return PTR_ERR(plane_state);
4851 }
4852
4853 return 0;
4854}
4855
4856static int
4857skl_compute_ddb(struct drm_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07004858{
4859 struct drm_device *dev = state->dev;
4860 struct drm_i915_private *dev_priv = to_i915(dev);
4861 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4862 struct intel_crtc *intel_crtc;
Matt Roper734fa012016-05-12 15:11:40 -07004863 struct skl_ddb_allocation *ddb = &intel_state->wm_results.ddb;
Matt Roper9b613022016-06-27 16:42:44 -07004864 uint32_t realloc_pipes = pipes_modified(state);
Matt Roper98d39492016-05-12 07:06:03 -07004865 int ret;
4866
4867 /*
4868 * If this is our first atomic update following hardware readout,
4869 * we can't trust the DDB that the BIOS programmed for us. Let's
4870 * pretend that all pipes switched active status so that we'll
4871 * ensure a full DDB recompute.
4872 */
Matt Roper1b54a882016-06-17 13:42:18 -07004873 if (dev_priv->wm.distrust_bios_wm) {
4874 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
4875 state->acquire_ctx);
4876 if (ret)
4877 return ret;
4878
Matt Roper98d39492016-05-12 07:06:03 -07004879 intel_state->active_pipe_changes = ~0;
4880
Matt Roper1b54a882016-06-17 13:42:18 -07004881 /*
4882 * We usually only initialize intel_state->active_crtcs if we
4883 * we're doing a modeset; make sure this field is always
4884 * initialized during the sanitization process that happens
4885 * on the first commit too.
4886 */
4887 if (!intel_state->modeset)
4888 intel_state->active_crtcs = dev_priv->active_crtcs;
4889 }
4890
Matt Roper98d39492016-05-12 07:06:03 -07004891 /*
4892 * If the modeset changes which CRTC's are active, we need to
4893 * recompute the DDB allocation for *all* active pipes, even
4894 * those that weren't otherwise being modified in any way by this
4895 * atomic commit. Due to the shrinking of the per-pipe allocations
4896 * when new active CRTC's are added, it's possible for a pipe that
4897 * we were already using and aren't changing at all here to suddenly
4898 * become invalid if its DDB needs exceeds its new allocation.
4899 *
4900 * Note that if we wind up doing a full DDB recompute, we can't let
4901 * any other display updates race with this transaction, so we need
4902 * to grab the lock on *all* CRTC's.
4903 */
Matt Roper734fa012016-05-12 15:11:40 -07004904 if (intel_state->active_pipe_changes) {
Matt Roper98d39492016-05-12 07:06:03 -07004905 realloc_pipes = ~0;
Matt Roper734fa012016-05-12 15:11:40 -07004906 intel_state->wm_results.dirty_pipes = ~0;
4907 }
Matt Roper98d39492016-05-12 07:06:03 -07004908
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004909 /*
4910 * We're not recomputing for the pipes not included in the commit, so
4911 * make sure we start with the current state.
4912 */
4913 memcpy(ddb, &dev_priv->wm.skl_hw.ddb, sizeof(*ddb));
4914
Matt Roper98d39492016-05-12 07:06:03 -07004915 for_each_intel_crtc_mask(dev, intel_crtc, realloc_pipes) {
4916 struct intel_crtc_state *cstate;
4917
4918 cstate = intel_atomic_get_crtc_state(state, intel_crtc);
4919 if (IS_ERR(cstate))
4920 return PTR_ERR(cstate);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004921
4922 ret = skl_allocate_pipe_ddb(cstate, ddb);
4923 if (ret)
4924 return ret;
4925
4926 ret = skl_ddb_add_affected_planes(cstate);
4927 if (ret)
4928 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07004929 }
4930
4931 return 0;
4932}
4933
Matt Roper2722efb2016-08-17 15:55:55 -04004934static void
4935skl_copy_wm_for_pipe(struct skl_wm_values *dst,
4936 struct skl_wm_values *src,
4937 enum pipe pipe)
4938{
Matt Roper2722efb2016-08-17 15:55:55 -04004939 memcpy(dst->ddb.y_plane[pipe], src->ddb.y_plane[pipe],
4940 sizeof(dst->ddb.y_plane[pipe]));
4941 memcpy(dst->ddb.plane[pipe], src->ddb.plane[pipe],
4942 sizeof(dst->ddb.plane[pipe]));
4943}
4944
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004945static void
4946skl_print_wm_changes(const struct drm_atomic_state *state)
4947{
4948 const struct drm_device *dev = state->dev;
4949 const struct drm_i915_private *dev_priv = to_i915(dev);
4950 const struct intel_atomic_state *intel_state =
4951 to_intel_atomic_state(state);
4952 const struct drm_crtc *crtc;
4953 const struct drm_crtc_state *cstate;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004954 const struct intel_plane *intel_plane;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004955 const struct skl_ddb_allocation *old_ddb = &dev_priv->wm.skl_hw.ddb;
4956 const struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
Maarten Lankhorst75704982016-11-01 12:04:10 +01004957 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004958
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004959 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst75704982016-11-01 12:04:10 +01004960 const struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4961 enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004962
Maarten Lankhorst75704982016-11-01 12:04:10 +01004963 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004964 enum plane_id plane_id = intel_plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004965 const struct skl_ddb_entry *old, *new;
4966
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004967 old = &old_ddb->plane[pipe][plane_id];
4968 new = &new_ddb->plane[pipe][plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004969
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004970 if (skl_ddb_entry_equal(old, new))
4971 continue;
4972
Maarten Lankhorst75704982016-11-01 12:04:10 +01004973 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] ddb (%d - %d) -> (%d - %d)\n",
4974 intel_plane->base.base.id,
4975 intel_plane->base.name,
4976 old->start, old->end,
4977 new->start, new->end);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004978 }
4979 }
4980}
4981
Matt Roper98d39492016-05-12 07:06:03 -07004982static int
4983skl_compute_wm(struct drm_atomic_state *state)
4984{
4985 struct drm_crtc *crtc;
4986 struct drm_crtc_state *cstate;
Matt Roper734fa012016-05-12 15:11:40 -07004987 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4988 struct skl_wm_values *results = &intel_state->wm_results;
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02004989 struct drm_device *dev = state->dev;
Matt Roper734fa012016-05-12 15:11:40 -07004990 struct skl_pipe_wm *pipe_wm;
Matt Roper98d39492016-05-12 07:06:03 -07004991 bool changed = false;
Matt Roper734fa012016-05-12 15:11:40 -07004992 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07004993
4994 /*
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02004995 * When we distrust bios wm we always need to recompute to set the
4996 * expected DDB allocations for each CRTC.
4997 */
4998 if (to_i915(dev)->wm.distrust_bios_wm)
4999 changed = true;
5000
5001 /*
Matt Roper98d39492016-05-12 07:06:03 -07005002 * If this transaction isn't actually touching any CRTC's, don't
5003 * bother with watermark calculation. Note that if we pass this
5004 * test, we're guaranteed to hold at least one CRTC state mutex,
5005 * which means we can safely use values like dev_priv->active_crtcs
5006 * since any racing commits that want to update them would need to
5007 * hold _all_ CRTC state mutexes.
5008 */
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01005009 for_each_new_crtc_in_state(state, crtc, cstate, i)
Matt Roper98d39492016-05-12 07:06:03 -07005010 changed = true;
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02005011
Matt Roper98d39492016-05-12 07:06:03 -07005012 if (!changed)
5013 return 0;
5014
Matt Roper734fa012016-05-12 15:11:40 -07005015 /* Clear all dirty flags */
5016 results->dirty_pipes = 0;
5017
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005018 ret = skl_compute_ddb(state);
Matt Roper98d39492016-05-12 07:06:03 -07005019 if (ret)
5020 return ret;
5021
Matt Roper734fa012016-05-12 15:11:40 -07005022 /*
5023 * Calculate WM's for all pipes that are part of this transaction.
5024 * Note that the DDB allocation above may have added more CRTC's that
5025 * weren't otherwise being modified (and set bits in dirty_pipes) if
5026 * pipe allocations had to change.
5027 *
5028 * FIXME: Now that we're doing this in the atomic check phase, we
5029 * should allow skl_update_pipe_wm() to return failure in cases where
5030 * no suitable watermark values can be found.
5031 */
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01005032 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Matt Roper734fa012016-05-12 15:11:40 -07005033 struct intel_crtc_state *intel_cstate =
5034 to_intel_crtc_state(cstate);
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02005035 const struct skl_pipe_wm *old_pipe_wm =
5036 &to_intel_crtc_state(crtc->state)->wm.skl.optimal;
Matt Roper734fa012016-05-12 15:11:40 -07005037
5038 pipe_wm = &intel_cstate->wm.skl.optimal;
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02005039 ret = skl_update_pipe_wm(cstate, old_pipe_wm, pipe_wm,
5040 &results->ddb, &changed);
Matt Roper734fa012016-05-12 15:11:40 -07005041 if (ret)
5042 return ret;
5043
5044 if (changed)
5045 results->dirty_pipes |= drm_crtc_mask(crtc);
5046
5047 if ((results->dirty_pipes & drm_crtc_mask(crtc)) == 0)
5048 /* This pipe's WM's did not change */
5049 continue;
5050
5051 intel_cstate->update_wm_pre = true;
Matt Roper734fa012016-05-12 15:11:40 -07005052 }
5053
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005054 skl_print_wm_changes(state);
5055
Matt Roper98d39492016-05-12 07:06:03 -07005056 return 0;
5057}
5058
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005059static void skl_atomic_update_crtc_wm(struct intel_atomic_state *state,
5060 struct intel_crtc_state *cstate)
5061{
5062 struct intel_crtc *crtc = to_intel_crtc(cstate->base.crtc);
5063 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5064 struct skl_pipe_wm *pipe_wm = &cstate->wm.skl.optimal;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005065 const struct skl_ddb_allocation *ddb = &state->wm_results.ddb;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005066 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005067 enum plane_id plane_id;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005068
5069 if (!(state->wm_results.dirty_pipes & drm_crtc_mask(&crtc->base)))
5070 return;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005071
5072 I915_WRITE(PIPE_WM_LINETIME(pipe), pipe_wm->linetime);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005073
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005074 for_each_plane_id_on_crtc(crtc, plane_id) {
5075 if (plane_id != PLANE_CURSOR)
5076 skl_write_plane_wm(crtc, &pipe_wm->planes[plane_id],
5077 ddb, plane_id);
5078 else
5079 skl_write_cursor_wm(crtc, &pipe_wm->planes[plane_id],
5080 ddb);
5081 }
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005082}
5083
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005084static void skl_initial_wm(struct intel_atomic_state *state,
5085 struct intel_crtc_state *cstate)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005086{
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005087 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02005088 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005089 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005090 struct skl_wm_values *results = &state->wm_results;
Matt Roper2722efb2016-08-17 15:55:55 -04005091 struct skl_wm_values *hw_vals = &dev_priv->wm.skl_hw;
Lyude27082492016-08-24 07:48:10 +02005092 enum pipe pipe = intel_crtc->pipe;
Bob Paauweadda50b2015-07-21 10:42:53 -07005093
Ville Syrjälä432081b2016-10-31 22:37:03 +02005094 if ((results->dirty_pipes & drm_crtc_mask(&intel_crtc->base)) == 0)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005095 return;
5096
Matt Roper734fa012016-05-12 15:11:40 -07005097 mutex_lock(&dev_priv->wm.wm_mutex);
5098
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005099 if (cstate->base.active_changed)
5100 skl_atomic_update_crtc_wm(state, cstate);
Lyude27082492016-08-24 07:48:10 +02005101
5102 skl_copy_wm_for_pipe(hw_vals, results, pipe);
Matt Roper734fa012016-05-12 15:11:40 -07005103
5104 mutex_unlock(&dev_priv->wm.wm_mutex);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005105}
5106
Ville Syrjäläd8905652016-01-14 14:53:35 +02005107static void ilk_compute_wm_config(struct drm_device *dev,
5108 struct intel_wm_config *config)
5109{
5110 struct intel_crtc *crtc;
5111
5112 /* Compute the currently _active_ config */
5113 for_each_intel_crtc(dev, crtc) {
5114 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
5115
5116 if (!wm->pipe_enabled)
5117 continue;
5118
5119 config->sprites_enabled |= wm->sprites_enabled;
5120 config->sprites_scaled |= wm->sprites_scaled;
5121 config->num_pipes_active++;
5122 }
5123}
5124
Matt Ropered4a6a72016-02-23 17:20:13 -08005125static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03005126{
Chris Wilson91c8a322016-07-05 10:40:23 +01005127 struct drm_device *dev = &dev_priv->drm;
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005128 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02005129 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02005130 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02005131 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005132 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07005133
Ville Syrjäläd8905652016-01-14 14:53:35 +02005134 ilk_compute_wm_config(dev, &config);
5135
5136 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
5137 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03005138
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005139 /* 5/6 split only in single pipe config on IVB+ */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00005140 if (INTEL_GEN(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02005141 config.num_pipes_active == 1 && config.sprites_enabled) {
5142 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
5143 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005144
Imre Deak820c1982013-12-17 14:46:36 +02005145 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03005146 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005147 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005148 }
5149
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005150 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005151 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005152
Imre Deak820c1982013-12-17 14:46:36 +02005153 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03005154
Imre Deak820c1982013-12-17 14:46:36 +02005155 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03005156}
5157
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005158static void ilk_initial_watermarks(struct intel_atomic_state *state,
5159 struct intel_crtc_state *cstate)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005160{
Matt Ropered4a6a72016-02-23 17:20:13 -08005161 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
5162 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005163
Matt Ropered4a6a72016-02-23 17:20:13 -08005164 mutex_lock(&dev_priv->wm.wm_mutex);
Matt Ropere8f1f022016-05-12 07:05:55 -07005165 intel_crtc->wm.active.ilk = cstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08005166 ilk_program_watermarks(dev_priv);
5167 mutex_unlock(&dev_priv->wm.wm_mutex);
5168}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005169
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005170static void ilk_optimize_watermarks(struct intel_atomic_state *state,
5171 struct intel_crtc_state *cstate)
Matt Ropered4a6a72016-02-23 17:20:13 -08005172{
5173 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
5174 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
5175
5176 mutex_lock(&dev_priv->wm.wm_mutex);
5177 if (cstate->wm.need_postvbl_update) {
Matt Ropere8f1f022016-05-12 07:05:55 -07005178 intel_crtc->wm.active.ilk = cstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08005179 ilk_program_watermarks(dev_priv);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005180 }
Matt Ropered4a6a72016-02-23 17:20:13 -08005181 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005182}
5183
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005184static inline void skl_wm_level_from_reg_val(uint32_t val,
5185 struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00005186{
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005187 level->plane_en = val & PLANE_WM_EN;
5188 level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
5189 level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
5190 PLANE_WM_LINES_MASK;
Pradeep Bhat30789992014-11-04 17:06:45 +00005191}
5192
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005193void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc,
5194 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00005195{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005196 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Pradeep Bhat30789992014-11-04 17:06:45 +00005197 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Pradeep Bhat30789992014-11-04 17:06:45 +00005198 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005199 int level, max_level;
5200 enum plane_id plane_id;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005201 uint32_t val;
Pradeep Bhat30789992014-11-04 17:06:45 +00005202
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005203 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00005204
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005205 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
5206 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00005207
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005208 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005209 if (plane_id != PLANE_CURSOR)
5210 val = I915_READ(PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005211 else
5212 val = I915_READ(CUR_WM(pipe, level));
5213
5214 skl_wm_level_from_reg_val(val, &wm->wm[level]);
5215 }
5216
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005217 if (plane_id != PLANE_CURSOR)
5218 val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005219 else
5220 val = I915_READ(CUR_WM_TRANS(pipe));
5221
5222 skl_wm_level_from_reg_val(val, &wm->trans_wm);
5223 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005224
Matt Roper3ef00282015-03-09 10:19:24 -07005225 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00005226 return;
5227
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005228 out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00005229}
5230
5231void skl_wm_get_hw_state(struct drm_device *dev)
5232{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005233 struct drm_i915_private *dev_priv = to_i915(dev);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005234 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
Damien Lespiaua269c582014-11-04 17:06:49 +00005235 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00005236 struct drm_crtc *crtc;
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005237 struct intel_crtc *intel_crtc;
5238 struct intel_crtc_state *cstate;
Pradeep Bhat30789992014-11-04 17:06:45 +00005239
Damien Lespiaua269c582014-11-04 17:06:49 +00005240 skl_ddb_get_hw_state(dev_priv, ddb);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005241 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5242 intel_crtc = to_intel_crtc(crtc);
5243 cstate = to_intel_crtc_state(crtc->state);
5244
5245 skl_pipe_wm_get_hw_state(crtc, &cstate->wm.skl.optimal);
5246
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02005247 if (intel_crtc->active)
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005248 hw->dirty_pipes |= drm_crtc_mask(crtc);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005249 }
Matt Ropera1de91e2016-05-12 07:05:57 -07005250
Matt Roper279e99d2016-05-12 07:06:02 -07005251 if (dev_priv->active_crtcs) {
5252 /* Fully recompute DDB on first atomic commit */
5253 dev_priv->wm.distrust_bios_wm = true;
5254 } else {
5255 /* Easy/common case; just sanitize DDB now if everything off */
5256 memset(ddb, 0, sizeof(*ddb));
5257 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005258}
5259
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005260static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
5261{
5262 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005263 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02005264 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005265 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper4e0963c2015-09-24 15:53:15 -07005266 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Matt Ropere8f1f022016-05-12 07:05:55 -07005267 struct intel_pipe_wm *active = &cstate->wm.ilk.optimal;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005268 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005269 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005270 [PIPE_A] = WM0_PIPEA_ILK,
5271 [PIPE_B] = WM0_PIPEB_ILK,
5272 [PIPE_C] = WM0_PIPEC_IVB,
5273 };
5274
5275 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005276 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02005277 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005278
Ville Syrjälä15606532016-05-13 17:55:17 +03005279 memset(active, 0, sizeof(*active));
5280
Matt Roper3ef00282015-03-09 10:19:24 -07005281 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02005282
5283 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005284 u32 tmp = hw->wm_pipe[pipe];
5285
5286 /*
5287 * For active pipes LP0 watermark is marked as
5288 * enabled, and LP1+ watermaks as disabled since
5289 * we can't really reverse compute them in case
5290 * multiple pipes are active.
5291 */
5292 active->wm[0].enable = true;
5293 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
5294 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
5295 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
5296 active->linetime = hw->wm_linetime[pipe];
5297 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005298 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005299
5300 /*
5301 * For inactive pipes, all watermark levels
5302 * should be marked as enabled but zeroed,
5303 * which is what we'd compute them to.
5304 */
5305 for (level = 0; level <= max_level; level++)
5306 active->wm[level].enable = true;
5307 }
Matt Roper4e0963c2015-09-24 15:53:15 -07005308
5309 intel_crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005310}
5311
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005312#define _FW_WM(value, plane) \
5313 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
5314#define _FW_WM_VLV(value, plane) \
5315 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
5316
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005317static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
5318 struct g4x_wm_values *wm)
5319{
5320 uint32_t tmp;
5321
5322 tmp = I915_READ(DSPFW1);
5323 wm->sr.plane = _FW_WM(tmp, SR);
5324 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5325 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
5326 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
5327
5328 tmp = I915_READ(DSPFW2);
5329 wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
5330 wm->sr.fbc = _FW_WM(tmp, FBC_SR);
5331 wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
5332 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
5333 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5334 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
5335
5336 tmp = I915_READ(DSPFW3);
5337 wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
5338 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5339 wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
5340 wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
5341}
5342
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005343static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
5344 struct vlv_wm_values *wm)
5345{
5346 enum pipe pipe;
5347 uint32_t tmp;
5348
5349 for_each_pipe(dev_priv, pipe) {
5350 tmp = I915_READ(VLV_DDL(pipe));
5351
Ville Syrjälä1b313892016-11-28 19:37:08 +02005352 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005353 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005354 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005355 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005356 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005357 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005358 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005359 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
5360 }
5361
5362 tmp = I915_READ(DSPFW1);
5363 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005364 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5365 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
5366 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005367
5368 tmp = I915_READ(DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005369 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
5370 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5371 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005372
5373 tmp = I915_READ(DSPFW3);
5374 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5375
5376 if (IS_CHERRYVIEW(dev_priv)) {
5377 tmp = I915_READ(DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005378 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5379 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005380
5381 tmp = I915_READ(DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005382 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
5383 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005384
5385 tmp = I915_READ(DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005386 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
5387 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005388
5389 tmp = I915_READ(DSPHOWM);
5390 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005391 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
5392 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
5393 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
5394 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5395 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5396 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5397 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5398 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5399 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005400 } else {
5401 tmp = I915_READ(DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005402 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5403 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005404
5405 tmp = I915_READ(DSPHOWM);
5406 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005407 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5408 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5409 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5410 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5411 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5412 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005413 }
5414}
5415
5416#undef _FW_WM
5417#undef _FW_WM_VLV
5418
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005419void g4x_wm_get_hw_state(struct drm_device *dev)
5420{
5421 struct drm_i915_private *dev_priv = to_i915(dev);
5422 struct g4x_wm_values *wm = &dev_priv->wm.g4x;
5423 struct intel_crtc *crtc;
5424
5425 g4x_read_wm_values(dev_priv, wm);
5426
5427 wm->cxsr = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
5428
5429 for_each_intel_crtc(dev, crtc) {
5430 struct intel_crtc_state *crtc_state =
5431 to_intel_crtc_state(crtc->base.state);
5432 struct g4x_wm_state *active = &crtc->wm.active.g4x;
5433 struct g4x_pipe_wm *raw;
5434 enum pipe pipe = crtc->pipe;
5435 enum plane_id plane_id;
5436 int level, max_level;
5437
5438 active->cxsr = wm->cxsr;
5439 active->hpll_en = wm->hpll_en;
5440 active->fbc_en = wm->fbc_en;
5441
5442 active->sr = wm->sr;
5443 active->hpll = wm->hpll;
5444
5445 for_each_plane_id_on_crtc(crtc, plane_id) {
5446 active->wm.plane[plane_id] =
5447 wm->pipe[pipe].plane[plane_id];
5448 }
5449
5450 if (wm->cxsr && wm->hpll_en)
5451 max_level = G4X_WM_LEVEL_HPLL;
5452 else if (wm->cxsr)
5453 max_level = G4X_WM_LEVEL_SR;
5454 else
5455 max_level = G4X_WM_LEVEL_NORMAL;
5456
5457 level = G4X_WM_LEVEL_NORMAL;
5458 raw = &crtc_state->wm.g4x.raw[level];
5459 for_each_plane_id_on_crtc(crtc, plane_id)
5460 raw->plane[plane_id] = active->wm.plane[plane_id];
5461
5462 if (++level > max_level)
5463 goto out;
5464
5465 raw = &crtc_state->wm.g4x.raw[level];
5466 raw->plane[PLANE_PRIMARY] = active->sr.plane;
5467 raw->plane[PLANE_CURSOR] = active->sr.cursor;
5468 raw->plane[PLANE_SPRITE0] = 0;
5469 raw->fbc = active->sr.fbc;
5470
5471 if (++level > max_level)
5472 goto out;
5473
5474 raw = &crtc_state->wm.g4x.raw[level];
5475 raw->plane[PLANE_PRIMARY] = active->hpll.plane;
5476 raw->plane[PLANE_CURSOR] = active->hpll.cursor;
5477 raw->plane[PLANE_SPRITE0] = 0;
5478 raw->fbc = active->hpll.fbc;
5479
5480 out:
5481 for_each_plane_id_on_crtc(crtc, plane_id)
5482 g4x_raw_plane_wm_set(crtc_state, level,
5483 plane_id, USHRT_MAX);
5484 g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
5485
5486 crtc_state->wm.g4x.optimal = *active;
5487 crtc_state->wm.g4x.intermediate = *active;
5488
5489 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
5490 pipe_name(pipe),
5491 wm->pipe[pipe].plane[PLANE_PRIMARY],
5492 wm->pipe[pipe].plane[PLANE_CURSOR],
5493 wm->pipe[pipe].plane[PLANE_SPRITE0]);
5494 }
5495
5496 DRM_DEBUG_KMS("Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
5497 wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
5498 DRM_DEBUG_KMS("Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
5499 wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
5500 DRM_DEBUG_KMS("Initial SR=%s HPLL=%s FBC=%s\n",
5501 yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
5502}
5503
5504void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
5505{
5506 struct intel_plane *plane;
5507 struct intel_crtc *crtc;
5508
5509 mutex_lock(&dev_priv->wm.wm_mutex);
5510
5511 for_each_intel_plane(&dev_priv->drm, plane) {
5512 struct intel_crtc *crtc =
5513 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
5514 struct intel_crtc_state *crtc_state =
5515 to_intel_crtc_state(crtc->base.state);
5516 struct intel_plane_state *plane_state =
5517 to_intel_plane_state(plane->base.state);
5518 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
5519 enum plane_id plane_id = plane->id;
5520 int level;
5521
5522 if (plane_state->base.visible)
5523 continue;
5524
5525 for (level = 0; level < 3; level++) {
5526 struct g4x_pipe_wm *raw =
5527 &crtc_state->wm.g4x.raw[level];
5528
5529 raw->plane[plane_id] = 0;
5530 wm_state->wm.plane[plane_id] = 0;
5531 }
5532
5533 if (plane_id == PLANE_PRIMARY) {
5534 for (level = 0; level < 3; level++) {
5535 struct g4x_pipe_wm *raw =
5536 &crtc_state->wm.g4x.raw[level];
5537 raw->fbc = 0;
5538 }
5539
5540 wm_state->sr.fbc = 0;
5541 wm_state->hpll.fbc = 0;
5542 wm_state->fbc_en = false;
5543 }
5544 }
5545
5546 for_each_intel_crtc(&dev_priv->drm, crtc) {
5547 struct intel_crtc_state *crtc_state =
5548 to_intel_crtc_state(crtc->base.state);
5549
5550 crtc_state->wm.g4x.intermediate =
5551 crtc_state->wm.g4x.optimal;
5552 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
5553 }
5554
5555 g4x_program_watermarks(dev_priv);
5556
5557 mutex_unlock(&dev_priv->wm.wm_mutex);
5558}
5559
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005560void vlv_wm_get_hw_state(struct drm_device *dev)
5561{
5562 struct drm_i915_private *dev_priv = to_i915(dev);
5563 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02005564 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005565 u32 val;
5566
5567 vlv_read_wm_values(dev_priv, wm);
5568
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005569 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
5570 wm->level = VLV_WM_LEVEL_PM2;
5571
5572 if (IS_CHERRYVIEW(dev_priv)) {
5573 mutex_lock(&dev_priv->rps.hw_lock);
5574
5575 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5576 if (val & DSP_MAXFIFO_PM5_ENABLE)
5577 wm->level = VLV_WM_LEVEL_PM5;
5578
Ville Syrjälä58590c12015-09-08 21:05:12 +03005579 /*
5580 * If DDR DVFS is disabled in the BIOS, Punit
5581 * will never ack the request. So if that happens
5582 * assume we don't have to enable/disable DDR DVFS
5583 * dynamically. To test that just set the REQ_ACK
5584 * bit to poke the Punit, but don't change the
5585 * HIGH/LOW bits so that we don't actually change
5586 * the current state.
5587 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005588 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03005589 val |= FORCE_DDR_FREQ_REQ_ACK;
5590 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
5591
5592 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
5593 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
5594 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
5595 "assuming DDR DVFS is disabled\n");
5596 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
5597 } else {
5598 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
5599 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
5600 wm->level = VLV_WM_LEVEL_DDR_DVFS;
5601 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005602
5603 mutex_unlock(&dev_priv->rps.hw_lock);
5604 }
5605
Ville Syrjäläff32c542017-03-02 19:14:57 +02005606 for_each_intel_crtc(dev, crtc) {
5607 struct intel_crtc_state *crtc_state =
5608 to_intel_crtc_state(crtc->base.state);
5609 struct vlv_wm_state *active = &crtc->wm.active.vlv;
5610 const struct vlv_fifo_state *fifo_state =
5611 &crtc_state->wm.vlv.fifo_state;
5612 enum pipe pipe = crtc->pipe;
5613 enum plane_id plane_id;
5614 int level;
5615
5616 vlv_get_fifo_size(crtc_state);
5617
5618 active->num_levels = wm->level + 1;
5619 active->cxsr = wm->cxsr;
5620
Ville Syrjäläff32c542017-03-02 19:14:57 +02005621 for (level = 0; level < active->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03005622 struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02005623 &crtc_state->wm.vlv.raw[level];
5624
5625 active->sr[level].plane = wm->sr.plane;
5626 active->sr[level].cursor = wm->sr.cursor;
5627
5628 for_each_plane_id_on_crtc(crtc, plane_id) {
5629 active->wm[level].plane[plane_id] =
5630 wm->pipe[pipe].plane[plane_id];
5631
5632 raw->plane[plane_id] =
5633 vlv_invert_wm_value(active->wm[level].plane[plane_id],
5634 fifo_state->plane[plane_id]);
5635 }
5636 }
5637
5638 for_each_plane_id_on_crtc(crtc, plane_id)
5639 vlv_raw_plane_wm_set(crtc_state, level,
5640 plane_id, USHRT_MAX);
5641 vlv_invalidate_wms(crtc, active, level);
5642
5643 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02005644 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02005645
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005646 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 +02005647 pipe_name(pipe),
5648 wm->pipe[pipe].plane[PLANE_PRIMARY],
5649 wm->pipe[pipe].plane[PLANE_CURSOR],
5650 wm->pipe[pipe].plane[PLANE_SPRITE0],
5651 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02005652 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005653
5654 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
5655 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
5656}
5657
Ville Syrjälä602ae832017-03-02 19:15:02 +02005658void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
5659{
5660 struct intel_plane *plane;
5661 struct intel_crtc *crtc;
5662
5663 mutex_lock(&dev_priv->wm.wm_mutex);
5664
5665 for_each_intel_plane(&dev_priv->drm, plane) {
5666 struct intel_crtc *crtc =
5667 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
5668 struct intel_crtc_state *crtc_state =
5669 to_intel_crtc_state(crtc->base.state);
5670 struct intel_plane_state *plane_state =
5671 to_intel_plane_state(plane->base.state);
5672 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
5673 const struct vlv_fifo_state *fifo_state =
5674 &crtc_state->wm.vlv.fifo_state;
5675 enum plane_id plane_id = plane->id;
5676 int level;
5677
5678 if (plane_state->base.visible)
5679 continue;
5680
5681 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03005682 struct g4x_pipe_wm *raw =
Ville Syrjälä602ae832017-03-02 19:15:02 +02005683 &crtc_state->wm.vlv.raw[level];
5684
5685 raw->plane[plane_id] = 0;
5686
5687 wm_state->wm[level].plane[plane_id] =
5688 vlv_invert_wm_value(raw->plane[plane_id],
5689 fifo_state->plane[plane_id]);
5690 }
5691 }
5692
5693 for_each_intel_crtc(&dev_priv->drm, crtc) {
5694 struct intel_crtc_state *crtc_state =
5695 to_intel_crtc_state(crtc->base.state);
5696
5697 crtc_state->wm.vlv.intermediate =
5698 crtc_state->wm.vlv.optimal;
5699 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
5700 }
5701
5702 vlv_program_watermarks(dev_priv);
5703
5704 mutex_unlock(&dev_priv->wm.wm_mutex);
5705}
5706
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005707void ilk_wm_get_hw_state(struct drm_device *dev)
5708{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005709 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02005710 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005711 struct drm_crtc *crtc;
5712
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01005713 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005714 ilk_pipe_wm_get_hw_state(crtc);
5715
5716 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
5717 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
5718 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
5719
5720 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00005721 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjäläcfa76982014-03-07 18:32:08 +02005722 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
5723 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
5724 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005725
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005726 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02005727 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
5728 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01005729 else if (IS_IVYBRIDGE(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02005730 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
5731 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005732
5733 hw->enable_fbc_wm =
5734 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
5735}
5736
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005737/**
5738 * intel_update_watermarks - update FIFO watermark values based on current modes
5739 *
5740 * Calculate watermark values for the various WM regs based on current mode
5741 * and plane configuration.
5742 *
5743 * There are several cases to deal with here:
5744 * - normal (i.e. non-self-refresh)
5745 * - self-refresh (SR) mode
5746 * - lines are large relative to FIFO size (buffer can hold up to 2)
5747 * - lines are small relative to FIFO size (buffer can hold more than 2
5748 * lines), so need to account for TLB latency
5749 *
5750 * The normal calculation is:
5751 * watermark = dotclock * bytes per pixel * latency
5752 * where latency is platform & configuration dependent (we assume pessimal
5753 * values here).
5754 *
5755 * The SR calculation is:
5756 * watermark = (trunc(latency/line time)+1) * surface width *
5757 * bytes per pixel
5758 * where
5759 * line time = htotal / dotclock
5760 * surface width = hdisplay for normal plane and 64 for cursor
5761 * and latency is assumed to be high, as above.
5762 *
5763 * The final value programmed to the register should always be rounded up,
5764 * and include an extra 2 entries to account for clock crossings.
5765 *
5766 * We don't use the sprite, so we can ignore that. And on Crestline we have
5767 * to set the non-SR watermarks to 8.
5768 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02005769void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005770{
Ville Syrjälä432081b2016-10-31 22:37:03 +02005771 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005772
5773 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005774 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005775}
5776
Jani Nikulae2828912016-01-18 09:19:47 +02005777/*
Daniel Vetter92703882012-08-09 16:46:01 +02005778 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02005779 */
5780DEFINE_SPINLOCK(mchdev_lock);
5781
5782/* Global for IPS driver to get at the current i915 device. Protected by
5783 * mchdev_lock. */
5784static struct drm_i915_private *i915_mch_dev;
5785
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005786bool ironlake_set_drps(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005787{
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005788 u16 rgvswctl;
5789
Chris Wilson67520412017-03-02 13:28:01 +00005790 lockdep_assert_held(&mchdev_lock);
Daniel Vetter92703882012-08-09 16:46:01 +02005791
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005792 rgvswctl = I915_READ16(MEMSWCTL);
5793 if (rgvswctl & MEMCTL_CMD_STS) {
5794 DRM_DEBUG("gpu busy, RCS change rejected\n");
5795 return false; /* still busy with another command */
5796 }
5797
5798 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
5799 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
5800 I915_WRITE16(MEMSWCTL, rgvswctl);
5801 POSTING_READ16(MEMSWCTL);
5802
5803 rgvswctl |= MEMCTL_CMD_STS;
5804 I915_WRITE16(MEMSWCTL, rgvswctl);
5805
5806 return true;
5807}
5808
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005809static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005810{
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00005811 u32 rgvmodectl;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005812 u8 fmax, fmin, fstart, vstart;
5813
Daniel Vetter92703882012-08-09 16:46:01 +02005814 spin_lock_irq(&mchdev_lock);
5815
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00005816 rgvmodectl = I915_READ(MEMMODECTL);
5817
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005818 /* Enable temp reporting */
5819 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
5820 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
5821
5822 /* 100ms RC evaluation intervals */
5823 I915_WRITE(RCUPEI, 100000);
5824 I915_WRITE(RCDNEI, 100000);
5825
5826 /* Set max/min thresholds to 90ms and 80ms respectively */
5827 I915_WRITE(RCBMAXAVG, 90000);
5828 I915_WRITE(RCBMINAVG, 80000);
5829
5830 I915_WRITE(MEMIHYST, 1);
5831
5832 /* Set up min, max, and cur for interrupt handling */
5833 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
5834 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
5835 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
5836 MEMMODE_FSTART_SHIFT;
5837
Ville Syrjälä616847e2015-09-18 20:03:19 +03005838 vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005839 PXVFREQ_PX_SHIFT;
5840
Daniel Vetter20e4d402012-08-08 23:35:39 +02005841 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
5842 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005843
Daniel Vetter20e4d402012-08-08 23:35:39 +02005844 dev_priv->ips.max_delay = fstart;
5845 dev_priv->ips.min_delay = fmin;
5846 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005847
5848 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
5849 fmax, fmin, fstart);
5850
5851 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
5852
5853 /*
5854 * Interrupts will be enabled in ironlake_irq_postinstall
5855 */
5856
5857 I915_WRITE(VIDSTART, vstart);
5858 POSTING_READ(VIDSTART);
5859
5860 rgvmodectl |= MEMMODE_SWMODE_EN;
5861 I915_WRITE(MEMMODECTL, rgvmodectl);
5862
Daniel Vetter92703882012-08-09 16:46:01 +02005863 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005864 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005865 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005866
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005867 ironlake_set_drps(dev_priv, fstart);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005868
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03005869 dev_priv->ips.last_count1 = I915_READ(DMIEC) +
5870 I915_READ(DDREC) + I915_READ(CSIEC);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005871 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03005872 dev_priv->ips.last_count2 = I915_READ(GFXEC);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005873 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02005874
5875 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005876}
5877
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005878static void ironlake_disable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005879{
Daniel Vetter92703882012-08-09 16:46:01 +02005880 u16 rgvswctl;
5881
5882 spin_lock_irq(&mchdev_lock);
5883
5884 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005885
5886 /* Ack interrupts, disable EFC interrupt */
5887 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
5888 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
5889 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
5890 I915_WRITE(DEIIR, DE_PCU_EVENT);
5891 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
5892
5893 /* Go back to the starting frequency */
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005894 ironlake_set_drps(dev_priv, dev_priv->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005895 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005896 rgvswctl |= MEMCTL_CMD_STS;
5897 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005898 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005899
Daniel Vetter92703882012-08-09 16:46:01 +02005900 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005901}
5902
Daniel Vetteracbe9472012-07-26 11:50:05 +02005903/* There's a funny hw issue where the hw returns all 0 when reading from
5904 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
5905 * ourselves, instead of doing a rmw cycle (which might result in us clearing
5906 * all limits and the gpu stuck at whatever frequency it is at atm).
5907 */
Akash Goel74ef1172015-03-06 11:07:19 +05305908static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005909{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005910 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005911
Daniel Vetter20b46e52012-07-26 11:16:14 +02005912 /* Only set the down limit when we've reached the lowest level to avoid
5913 * getting more interrupts, otherwise leave this clear. This prevents a
5914 * race in the hw when coming out of rc6: There's a tiny window where
5915 * the hw runs at the minimal clock before selecting the desired
5916 * frequency, if the down threshold expires in that window we will not
5917 * receive a down interrupt. */
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07005918 if (INTEL_GEN(dev_priv) >= 9) {
Akash Goel74ef1172015-03-06 11:07:19 +05305919 limits = (dev_priv->rps.max_freq_softlimit) << 23;
5920 if (val <= dev_priv->rps.min_freq_softlimit)
5921 limits |= (dev_priv->rps.min_freq_softlimit) << 14;
5922 } else {
5923 limits = dev_priv->rps.max_freq_softlimit << 24;
5924 if (val <= dev_priv->rps.min_freq_softlimit)
5925 limits |= dev_priv->rps.min_freq_softlimit << 16;
5926 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02005927
5928 return limits;
5929}
5930
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005931static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
5932{
5933 int new_power;
Akash Goel8a586432015-03-06 11:07:18 +05305934 u32 threshold_up = 0, threshold_down = 0; /* in % */
5935 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005936
5937 new_power = dev_priv->rps.power;
5938 switch (dev_priv->rps.power) {
5939 case LOW_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01005940 if (val > dev_priv->rps.efficient_freq + 1 &&
5941 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005942 new_power = BETWEEN;
5943 break;
5944
5945 case BETWEEN:
Chris Wilsona72b5622016-07-02 15:35:59 +01005946 if (val <= dev_priv->rps.efficient_freq &&
5947 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005948 new_power = LOW_POWER;
Chris Wilsona72b5622016-07-02 15:35:59 +01005949 else if (val >= dev_priv->rps.rp0_freq &&
5950 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005951 new_power = HIGH_POWER;
5952 break;
5953
5954 case HIGH_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01005955 if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 &&
5956 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005957 new_power = BETWEEN;
5958 break;
5959 }
5960 /* Max/min bins are special */
Chris Wilsonaed242f2015-03-18 09:48:21 +00005961 if (val <= dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005962 new_power = LOW_POWER;
Chris Wilsonaed242f2015-03-18 09:48:21 +00005963 if (val >= dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005964 new_power = HIGH_POWER;
5965 if (new_power == dev_priv->rps.power)
5966 return;
5967
5968 /* Note the units here are not exactly 1us, but 1280ns. */
5969 switch (new_power) {
5970 case LOW_POWER:
5971 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05305972 ei_up = 16000;
5973 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005974
5975 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305976 ei_down = 32000;
5977 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005978 break;
5979
5980 case BETWEEN:
5981 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05305982 ei_up = 13000;
5983 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005984
5985 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305986 ei_down = 32000;
5987 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005988 break;
5989
5990 case HIGH_POWER:
5991 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05305992 ei_up = 10000;
5993 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005994
5995 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305996 ei_down = 32000;
5997 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005998 break;
5999 }
6000
Mika Kuoppala6067a272017-02-15 15:52:59 +02006001 /* When byt can survive without system hang with dynamic
6002 * sw freq adjustments, this restriction can be lifted.
6003 */
6004 if (IS_VALLEYVIEW(dev_priv))
6005 goto skip_hw_write;
6006
Akash Goel8a586432015-03-06 11:07:18 +05306007 I915_WRITE(GEN6_RP_UP_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01006008 GT_INTERVAL_FROM_US(dev_priv, ei_up));
Akash Goel8a586432015-03-06 11:07:18 +05306009 I915_WRITE(GEN6_RP_UP_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01006010 GT_INTERVAL_FROM_US(dev_priv,
6011 ei_up * threshold_up / 100));
Akash Goel8a586432015-03-06 11:07:18 +05306012
6013 I915_WRITE(GEN6_RP_DOWN_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01006014 GT_INTERVAL_FROM_US(dev_priv, ei_down));
Akash Goel8a586432015-03-06 11:07:18 +05306015 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01006016 GT_INTERVAL_FROM_US(dev_priv,
6017 ei_down * threshold_down / 100));
Akash Goel8a586432015-03-06 11:07:18 +05306018
Chris Wilsona72b5622016-07-02 15:35:59 +01006019 I915_WRITE(GEN6_RP_CONTROL,
6020 GEN6_RP_MEDIA_TURBO |
6021 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6022 GEN6_RP_MEDIA_IS_GFX |
6023 GEN6_RP_ENABLE |
6024 GEN6_RP_UP_BUSY_AVG |
6025 GEN6_RP_DOWN_IDLE_AVG);
Akash Goel8a586432015-03-06 11:07:18 +05306026
Mika Kuoppala6067a272017-02-15 15:52:59 +02006027skip_hw_write:
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006028 dev_priv->rps.power = new_power;
Chris Wilson8fb55192015-04-07 16:20:28 +01006029 dev_priv->rps.up_threshold = threshold_up;
6030 dev_priv->rps.down_threshold = threshold_down;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006031 dev_priv->rps.last_adj = 0;
6032}
6033
Chris Wilson2876ce72014-03-28 08:03:34 +00006034static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
6035{
6036 u32 mask = 0;
6037
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006038 /* We use UP_EI_EXPIRED interupts for both up/down in manual mode */
Chris Wilson2876ce72014-03-28 08:03:34 +00006039 if (val > dev_priv->rps.min_freq_softlimit)
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006040 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
Chris Wilson2876ce72014-03-28 08:03:34 +00006041 if (val < dev_priv->rps.max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00006042 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00006043
Chris Wilson7b3c29f2014-07-10 20:31:19 +01006044 mask &= dev_priv->pm_rps_events;
6045
Imre Deak59d02a12014-12-19 19:33:26 +02006046 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00006047}
6048
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006049/* gen6_set_rps is called to update the frequency request, but should also be
6050 * called when the range (min_delay and max_delay) is modified so that we can
6051 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006052static int gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02006053{
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006054 /* min/max delay may still have been modified so be sure to
6055 * write the limits value.
6056 */
6057 if (val != dev_priv->rps.cur_freq) {
6058 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006059
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006060 if (INTEL_GEN(dev_priv) >= 9)
Akash Goel57041952015-03-06 11:07:17 +05306061 I915_WRITE(GEN6_RPNSWREQ,
6062 GEN9_FREQUENCY(val));
Chris Wilsondc979972016-05-10 14:10:04 +01006063 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006064 I915_WRITE(GEN6_RPNSWREQ,
6065 HSW_FREQUENCY(val));
6066 else
6067 I915_WRITE(GEN6_RPNSWREQ,
6068 GEN6_FREQUENCY(val) |
6069 GEN6_OFFSET(0) |
6070 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006071 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006072
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006073 /* Make sure we continue to get interrupts
6074 * until we hit the minimum or maximum frequencies.
6075 */
Akash Goel74ef1172015-03-06 11:07:19 +05306076 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00006077 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006078
Ben Widawskyb39fb292014-03-19 18:31:11 -07006079 dev_priv->rps.cur_freq = val;
Mika Kuoppala0f945922015-11-17 18:14:26 +02006080 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006081
6082 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006083}
6084
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006085static int valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006086{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006087 int err;
6088
Chris Wilsondc979972016-05-10 14:10:04 +01006089 if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006090 "Odd GPU freq value\n"))
6091 val &= ~1;
6092
Deepak Scd25dd52015-07-10 18:31:40 +05306093 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
6094
Chris Wilson8fb55192015-04-07 16:20:28 +01006095 if (val != dev_priv->rps.cur_freq) {
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006096 err = vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
6097 if (err)
6098 return err;
6099
Chris Wilsondb4c5e02017-02-10 15:03:46 +00006100 gen6_set_rps_thresholds(dev_priv, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01006101 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006102
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006103 dev_priv->rps.cur_freq = val;
6104 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006105
6106 return 0;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006107}
6108
Deepak Sa7f6e232015-05-09 18:04:44 +05306109/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05306110 *
6111 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05306112 * 1. Forcewake Media well.
6113 * 2. Request idle freq.
6114 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05306115*/
6116static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
6117{
Chris Wilsonaed242f2015-03-18 09:48:21 +00006118 u32 val = dev_priv->rps.idle_freq;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006119 int err;
Deepak S5549d252014-06-28 11:26:11 +05306120
Chris Wilsonaed242f2015-03-18 09:48:21 +00006121 if (dev_priv->rps.cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05306122 return;
6123
Chris Wilsonc9efef72017-01-02 15:28:45 +00006124 /* The punit delays the write of the frequency and voltage until it
6125 * determines the GPU is awake. During normal usage we don't want to
6126 * waste power changing the frequency if the GPU is sleeping (rc6).
6127 * However, the GPU and driver is now idle and we do not want to delay
6128 * switching to minimum voltage (reducing power whilst idle) as we do
6129 * not expect to be woken in the near future and so must flush the
6130 * change by waking the device.
6131 *
6132 * We choose to take the media powerwell (either would do to trick the
6133 * punit into committing the voltage change) as that takes a lot less
6134 * power than the render powerwell.
6135 */
Deepak Sa7f6e232015-05-09 18:04:44 +05306136 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006137 err = valleyview_set_rps(dev_priv, val);
Deepak Sa7f6e232015-05-09 18:04:44 +05306138 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006139
6140 if (err)
6141 DRM_ERROR("Failed to set RPS for idle\n");
Deepak S76c3552f2014-01-30 23:08:16 +05306142}
6143
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006144void gen6_rps_busy(struct drm_i915_private *dev_priv)
6145{
6146 mutex_lock(&dev_priv->rps.hw_lock);
6147 if (dev_priv->rps.enabled) {
Chris Wilsonbd648182017-02-10 15:03:48 +00006148 u8 freq;
6149
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006150 if (dev_priv->pm_rps_events & GEN6_PM_RP_UP_EI_EXPIRED)
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006151 gen6_rps_reset_ei(dev_priv);
6152 I915_WRITE(GEN6_PMINTRMSK,
6153 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Michał Winiarski2b83c4c2016-06-20 11:58:27 +02006154
Chris Wilsonc33d2472016-07-04 08:08:36 +01006155 gen6_enable_rps_interrupts(dev_priv);
6156
Chris Wilsonbd648182017-02-10 15:03:48 +00006157 /* Use the user's desired frequency as a guide, but for better
6158 * performance, jump directly to RPe as our starting frequency.
6159 */
6160 freq = max(dev_priv->rps.cur_freq,
6161 dev_priv->rps.efficient_freq);
6162
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006163 if (intel_set_rps(dev_priv,
Chris Wilsonbd648182017-02-10 15:03:48 +00006164 clamp(freq,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006165 dev_priv->rps.min_freq_softlimit,
6166 dev_priv->rps.max_freq_softlimit)))
6167 DRM_DEBUG_DRIVER("Failed to set idle frequency\n");
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006168 }
6169 mutex_unlock(&dev_priv->rps.hw_lock);
6170}
6171
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006172void gen6_rps_idle(struct drm_i915_private *dev_priv)
6173{
Chris Wilsonc33d2472016-07-04 08:08:36 +01006174 /* Flush our bottom-half so that it does not race with us
6175 * setting the idle frequency and so that it is bounded by
6176 * our rpm wakeref. And then disable the interrupts to stop any
6177 * futher RPS reclocking whilst we are asleep.
6178 */
6179 gen6_disable_rps_interrupts(dev_priv);
6180
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006181 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01006182 if (dev_priv->rps.enabled) {
Chris Wilsondc979972016-05-10 14:10:04 +01006183 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Deepak S76c3552f2014-01-30 23:08:16 +05306184 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02006185 else
Chris Wilsondc979972016-05-10 14:10:04 +01006186 gen6_set_rps(dev_priv, dev_priv->rps.idle_freq);
Chris Wilsonc0951f02013-10-10 21:58:50 +01006187 dev_priv->rps.last_adj = 0;
Ville Syrjälä12c100b2016-05-23 17:42:48 +03006188 I915_WRITE(GEN6_PMINTRMSK,
6189 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Chris Wilsonc0951f02013-10-10 21:58:50 +01006190 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01006191 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006192}
6193
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006194void gen6_rps_boost(struct drm_i915_gem_request *rq,
6195 struct intel_rps_client *rps)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006196{
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006197 struct drm_i915_private *i915 = rq->i915;
Chris Wilson74d290f2017-08-17 13:37:06 +01006198 unsigned long flags;
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006199 bool boost;
6200
Chris Wilson8d3afd72015-05-21 21:01:47 +01006201 /* This is intentionally racy! We peek at the state here, then
6202 * validate inside the RPS worker.
6203 */
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006204 if (!i915->rps.enabled)
Chris Wilson8d3afd72015-05-21 21:01:47 +01006205 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006206
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006207 boost = false;
Chris Wilson74d290f2017-08-17 13:37:06 +01006208 spin_lock_irqsave(&rq->lock, flags);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006209 if (!rq->waitboost && !i915_gem_request_completed(rq)) {
6210 atomic_inc(&i915->rps.num_waiters);
6211 rq->waitboost = true;
6212 boost = true;
Chris Wilsonc0951f02013-10-10 21:58:50 +01006213 }
Chris Wilson74d290f2017-08-17 13:37:06 +01006214 spin_unlock_irqrestore(&rq->lock, flags);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006215 if (!boost)
6216 return;
6217
6218 if (READ_ONCE(i915->rps.cur_freq) < i915->rps.boost_freq)
6219 schedule_work(&i915->rps.work);
6220
6221 atomic_inc(rps ? &rps->boosts : &i915->rps.boosts);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006222}
6223
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006224int intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006225{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006226 int err;
6227
Chris Wilsoncfd1c482017-02-20 09:47:07 +00006228 lockdep_assert_held(&dev_priv->rps.hw_lock);
6229 GEM_BUG_ON(val > dev_priv->rps.max_freq);
6230 GEM_BUG_ON(val < dev_priv->rps.min_freq);
6231
Chris Wilson76e4e4b2017-02-20 09:47:08 +00006232 if (!dev_priv->rps.enabled) {
6233 dev_priv->rps.cur_freq = val;
6234 return 0;
6235 }
6236
Chris Wilsondc979972016-05-10 14:10:04 +01006237 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006238 err = valleyview_set_rps(dev_priv, val);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006239 else
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006240 err = gen6_set_rps(dev_priv, val);
6241
6242 return err;
Jesse Barnes0a073b82013-04-17 15:54:58 -07006243}
6244
Chris Wilsondc979972016-05-10 14:10:04 +01006245static void gen9_disable_rc6(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00006246{
Zhe Wang20e49362014-11-04 17:07:05 +00006247 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00006248 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00006249}
6250
Chris Wilsondc979972016-05-10 14:10:04 +01006251static void gen9_disable_rps(struct drm_i915_private *dev_priv)
Akash Goel2030d682016-04-23 00:05:45 +05306252{
Akash Goel2030d682016-04-23 00:05:45 +05306253 I915_WRITE(GEN6_RP_CONTROL, 0);
6254}
6255
Chris Wilsondc979972016-05-10 14:10:04 +01006256static void gen6_disable_rps(struct drm_i915_private *dev_priv)
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006257{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006258 I915_WRITE(GEN6_RC_CONTROL, 0);
6259 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Akash Goel2030d682016-04-23 00:05:45 +05306260 I915_WRITE(GEN6_RP_CONTROL, 0);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006261}
6262
Chris Wilsondc979972016-05-10 14:10:04 +01006263static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306264{
Deepak S38807742014-05-23 21:00:15 +05306265 I915_WRITE(GEN6_RC_CONTROL, 0);
6266}
6267
Chris Wilsondc979972016-05-10 14:10:04 +01006268static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006269{
Deepak S98a2e5f2014-08-18 10:35:27 -07006270 /* we're doing forcewake before Disabling RC6,
6271 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02006272 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07006273
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006274 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006275
Mika Kuoppala59bad942015-01-16 11:34:40 +02006276 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006277}
6278
Chris Wilsondc979972016-05-10 14:10:04 +01006279static void intel_print_rc6_info(struct drm_i915_private *dev_priv, u32 mode)
Ben Widawskydc39fff2013-10-18 12:32:07 -07006280{
Chris Wilsondc979972016-05-10 14:10:04 +01006281 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Imre Deak91ca6892014-04-14 20:24:25 +03006282 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
6283 mode = GEN6_RC_CTL_RC6_ENABLE;
6284 else
6285 mode = 0;
6286 }
Chris Wilsondc979972016-05-10 14:10:04 +01006287 if (HAS_RC6p(dev_priv))
Imre Deakb99d49c2016-06-29 19:13:54 +03006288 DRM_DEBUG_DRIVER("Enabling RC6 states: "
6289 "RC6 %s RC6p %s RC6pp %s\n",
6290 onoff(mode & GEN6_RC_CTL_RC6_ENABLE),
6291 onoff(mode & GEN6_RC_CTL_RC6p_ENABLE),
6292 onoff(mode & GEN6_RC_CTL_RC6pp_ENABLE));
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07006293
6294 else
Imre Deakb99d49c2016-06-29 19:13:54 +03006295 DRM_DEBUG_DRIVER("Enabling RC6 states: RC6 %s\n",
6296 onoff(mode & GEN6_RC_CTL_RC6_ENABLE));
Ben Widawskydc39fff2013-10-18 12:32:07 -07006297}
6298
Chris Wilsondc979972016-05-10 14:10:04 +01006299static bool bxt_check_bios_rc6_setup(struct drm_i915_private *dev_priv)
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306300{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006301 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306302 bool enable_rc6 = true;
6303 unsigned long rc6_ctx_base;
Imre Deakfc619842016-06-29 19:13:55 +03006304 u32 rc_ctl;
6305 int rc_sw_target;
6306
6307 rc_ctl = I915_READ(GEN6_RC_CONTROL);
6308 rc_sw_target = (I915_READ(GEN6_RC_STATE) & RC_SW_TARGET_STATE_MASK) >>
6309 RC_SW_TARGET_STATE_SHIFT;
6310 DRM_DEBUG_DRIVER("BIOS enabled RC states: "
6311 "HW_CTRL %s HW_RC6 %s SW_TARGET_STATE %x\n",
6312 onoff(rc_ctl & GEN6_RC_CTL_HW_ENABLE),
6313 onoff(rc_ctl & GEN6_RC_CTL_RC6_ENABLE),
6314 rc_sw_target);
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306315
6316 if (!(I915_READ(RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006317 DRM_DEBUG_DRIVER("RC6 Base location not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306318 enable_rc6 = false;
6319 }
6320
6321 /*
6322 * The exact context size is not known for BXT, so assume a page size
6323 * for this check.
6324 */
6325 rc6_ctx_base = I915_READ(RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006326 if (!((rc6_ctx_base >= ggtt->stolen_reserved_base) &&
6327 (rc6_ctx_base + PAGE_SIZE <= ggtt->stolen_reserved_base +
6328 ggtt->stolen_reserved_size))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006329 DRM_DEBUG_DRIVER("RC6 Base address not as expected.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306330 enable_rc6 = false;
6331 }
6332
6333 if (!(((I915_READ(PWRCTX_MAXCNT_RCSUNIT) & IDLE_TIME_MASK) > 1) &&
6334 ((I915_READ(PWRCTX_MAXCNT_VCSUNIT0) & IDLE_TIME_MASK) > 1) &&
6335 ((I915_READ(PWRCTX_MAXCNT_BCSUNIT) & IDLE_TIME_MASK) > 1) &&
6336 ((I915_READ(PWRCTX_MAXCNT_VECSUNIT) & IDLE_TIME_MASK) > 1))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006337 DRM_DEBUG_DRIVER("Engine Idle wait time not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306338 enable_rc6 = false;
6339 }
6340
Imre Deakfc619842016-06-29 19:13:55 +03006341 if (!I915_READ(GEN8_PUSHBUS_CONTROL) ||
6342 !I915_READ(GEN8_PUSHBUS_ENABLE) ||
6343 !I915_READ(GEN8_PUSHBUS_SHIFT)) {
6344 DRM_DEBUG_DRIVER("Pushbus not setup properly.\n");
6345 enable_rc6 = false;
6346 }
6347
6348 if (!I915_READ(GEN6_GFXPAUSE)) {
6349 DRM_DEBUG_DRIVER("GFX pause not setup properly.\n");
6350 enable_rc6 = false;
6351 }
6352
6353 if (!I915_READ(GEN8_MISC_CTRL0)) {
6354 DRM_DEBUG_DRIVER("GPM control not setup properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306355 enable_rc6 = false;
6356 }
6357
6358 return enable_rc6;
6359}
6360
Chris Wilsondc979972016-05-10 14:10:04 +01006361int sanitize_rc6_option(struct drm_i915_private *dev_priv, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006362{
Daniel Vettere7d66d82015-06-15 23:23:54 +02006363 /* No RC6 before Ironlake and code is gone for ilk. */
Chris Wilsondc979972016-05-10 14:10:04 +01006364 if (INTEL_INFO(dev_priv)->gen < 6)
Imre Deake6069ca2014-04-18 16:01:02 +03006365 return 0;
6366
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306367 if (!enable_rc6)
6368 return 0;
6369
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02006370 if (IS_GEN9_LP(dev_priv) && !bxt_check_bios_rc6_setup(dev_priv)) {
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306371 DRM_INFO("RC6 disabled by BIOS\n");
6372 return 0;
6373 }
6374
Daniel Vetter456470e2012-08-08 23:35:40 +02006375 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03006376 if (enable_rc6 >= 0) {
6377 int mask;
6378
Chris Wilsondc979972016-05-10 14:10:04 +01006379 if (HAS_RC6p(dev_priv))
Imre Deake6069ca2014-04-18 16:01:02 +03006380 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
6381 INTEL_RC6pp_ENABLE;
6382 else
6383 mask = INTEL_RC6_ENABLE;
6384
6385 if ((enable_rc6 & mask) != enable_rc6)
Imre Deakb99d49c2016-06-29 19:13:54 +03006386 DRM_DEBUG_DRIVER("Adjusting RC6 mask to %d "
6387 "(requested %d, valid %d)\n",
6388 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03006389
6390 return enable_rc6 & mask;
6391 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006392
Chris Wilsondc979972016-05-10 14:10:04 +01006393 if (IS_IVYBRIDGE(dev_priv))
Ben Widawskycca84a12014-01-28 20:25:38 -08006394 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08006395
6396 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006397}
6398
Chris Wilsondc979972016-05-10 14:10:04 +01006399static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
Imre Deake6069ca2014-04-18 16:01:02 +03006400{
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006401 /* All of these values are in units of 50MHz */
Chris Wilson773ea9a2016-07-13 09:10:33 +01006402
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006403 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02006404 if (IS_GEN9_LP(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006405 u32 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07006406 dev_priv->rps.rp0_freq = (rp_state_cap >> 16) & 0xff;
6407 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
6408 dev_priv->rps.min_freq = (rp_state_cap >> 0) & 0xff;
6409 } else {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006410 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07006411 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
6412 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
6413 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
6414 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006415 /* hw_max = RP0 until we check for overclocking */
Chris Wilson773ea9a2016-07-13 09:10:33 +01006416 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006417
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006418 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01006419 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006420 IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006421 u32 ddcc_status = 0;
6422
6423 if (sandybridge_pcode_read(dev_priv,
6424 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
6425 &ddcc_status) == 0)
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006426 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08006427 clamp_t(u8,
6428 ((ddcc_status >> 8) & 0xff),
6429 dev_priv->rps.min_freq,
6430 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006431 }
6432
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006433 if (IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Akash Goelc5e06882015-06-29 14:50:19 +05306434 /* Store the frequency values in 16.66 MHZ units, which is
Chris Wilson773ea9a2016-07-13 09:10:33 +01006435 * the natural hardware unit for SKL
6436 */
Akash Goelc5e06882015-06-29 14:50:19 +05306437 dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
6438 dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
6439 dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
6440 dev_priv->rps.max_freq *= GEN9_FREQ_SCALER;
6441 dev_priv->rps.efficient_freq *= GEN9_FREQ_SCALER;
6442 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006443}
6444
Chris Wilson3a45b052016-07-13 09:10:32 +01006445static void reset_rps(struct drm_i915_private *dev_priv,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006446 int (*set)(struct drm_i915_private *, u8))
Chris Wilson3a45b052016-07-13 09:10:32 +01006447{
6448 u8 freq = dev_priv->rps.cur_freq;
6449
6450 /* force a reset */
6451 dev_priv->rps.power = -1;
6452 dev_priv->rps.cur_freq = -1;
6453
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006454 if (set(dev_priv, freq))
6455 DRM_ERROR("Failed to reset RPS to initial values\n");
Chris Wilson3a45b052016-07-13 09:10:32 +01006456}
6457
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006458/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Chris Wilsondc979972016-05-10 14:10:04 +01006459static void gen9_enable_rps(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00006460{
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006461 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
6462
Akash Goel0beb0592015-03-06 11:07:20 +05306463 /* Program defaults and thresholds for RPS*/
6464 I915_WRITE(GEN6_RC_VIDEO_FREQ,
6465 GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006466
Akash Goel0beb0592015-03-06 11:07:20 +05306467 /* 1 second timeout*/
6468 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
6469 GT_INTERVAL_FROM_US(dev_priv, 1000000));
6470
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006471 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006472
Akash Goel0beb0592015-03-06 11:07:20 +05306473 /* Leaning on the below call to gen6_set_rps to program/setup the
6474 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
6475 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
Chris Wilson3a45b052016-07-13 09:10:32 +01006476 reset_rps(dev_priv, gen6_set_rps);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006477
6478 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
6479}
6480
Chris Wilsondc979972016-05-10 14:10:04 +01006481static void gen9_enable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006482{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006483 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306484 enum intel_engine_id id;
Zhe Wang20e49362014-11-04 17:07:05 +00006485 uint32_t rc6_mask = 0;
Zhe Wang20e49362014-11-04 17:07:05 +00006486
6487 /* 1a: Software RC state - RC0 */
6488 I915_WRITE(GEN6_RC_STATE, 0);
6489
6490 /* 1b: Get forcewake during program sequence. Although the driver
6491 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006492 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00006493
6494 /* 2a: Disable RC states. */
6495 I915_WRITE(GEN6_RC_CONTROL, 0);
6496
6497 /* 2b: Program RC6 thresholds.*/
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05306498
6499 /* WaRsDoubleRc6WrlWithCoarsePowerGating: Doubling WRL only when CPG is enabled */
Chris Wilsondc979972016-05-10 14:10:04 +01006500 if (IS_SKYLAKE(dev_priv))
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05306501 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
6502 else
6503 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
Zhe Wang20e49362014-11-04 17:07:05 +00006504 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6505 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05306506 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006507 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05306508
Dave Gordon1a3d1892016-05-13 15:36:30 +01006509 if (HAS_GUC(dev_priv))
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05306510 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
6511
Zhe Wang20e49362014-11-04 17:07:05 +00006512 I915_WRITE(GEN6_RC_SLEEP, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00006513
Zhe Wang38c23522015-01-20 12:23:04 +00006514 /* 2c: Program Coarse Power Gating Policies. */
6515 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
6516 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
6517
Zhe Wang20e49362014-11-04 17:07:05 +00006518 /* 3a: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006519 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Zhe Wang20e49362014-11-04 17:07:05 +00006520 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Jani Nikula87ad3212016-01-14 12:53:34 +02006521 DRM_INFO("RC6 %s\n", onoff(rc6_mask & GEN6_RC_CTL_RC6_ENABLE));
Chris Wilson1c044f92017-01-25 17:26:01 +00006522 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
6523 I915_WRITE(GEN6_RC_CONTROL,
6524 GEN6_RC_CTL_HW_ENABLE | GEN6_RC_CTL_EI_MODE(1) | rc6_mask);
Zhe Wang20e49362014-11-04 17:07:05 +00006525
Sagar Kamblecb07bae2015-04-12 11:28:14 +05306526 /*
6527 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05306528 * WaRsDisableCoarsePowerGating:skl,bxt - Render/Media PG need to be disabled with RC6.
Sagar Kamblecb07bae2015-04-12 11:28:14 +05306529 */
Chris Wilsondc979972016-05-10 14:10:04 +01006530 if (NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05306531 I915_WRITE(GEN9_PG_ENABLE, 0);
6532 else
6533 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
6534 (GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE) : 0);
Zhe Wang38c23522015-01-20 12:23:04 +00006535
Mika Kuoppala59bad942015-01-16 11:34:40 +02006536 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00006537}
6538
Chris Wilsondc979972016-05-10 14:10:04 +01006539static void gen8_enable_rps(struct drm_i915_private *dev_priv)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006540{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006541 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306542 enum intel_engine_id id;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006543 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006544
6545 /* 1a: Software RC state - RC0 */
6546 I915_WRITE(GEN6_RC_STATE, 0);
6547
6548 /* 1c & 1d: Get forcewake during program sequence. Although the driver
6549 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006550 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006551
6552 /* 2a: Disable RC states. */
6553 I915_WRITE(GEN6_RC_CONTROL, 0);
6554
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006555 /* 2b: Program RC6 thresholds.*/
6556 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
6557 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6558 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05306559 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006560 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006561 I915_WRITE(GEN6_RC_SLEEP, 0);
Chris Wilsondc979972016-05-10 14:10:04 +01006562 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07006563 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
6564 else
6565 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006566
6567 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006568 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006569 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Chris Wilsondc979972016-05-10 14:10:04 +01006570 intel_print_rc6_info(dev_priv, rc6_mask);
6571 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07006572 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
6573 GEN7_RC_CTL_TO_MODE |
6574 rc6_mask);
6575 else
6576 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
6577 GEN6_RC_CTL_EI_MODE(1) |
6578 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006579
6580 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07006581 I915_WRITE(GEN6_RPNSWREQ,
6582 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
6583 I915_WRITE(GEN6_RC_VIDEO_FREQ,
6584 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02006585 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
6586 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006587
Daniel Vetter7526ed72014-09-29 15:07:19 +02006588 /* Docs recommend 900MHz, and 300 MHz respectively */
6589 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
6590 dev_priv->rps.max_freq_softlimit << 24 |
6591 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006592
Daniel Vetter7526ed72014-09-29 15:07:19 +02006593 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
6594 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
6595 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
6596 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006597
Daniel Vetter7526ed72014-09-29 15:07:19 +02006598 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006599
6600 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02006601 I915_WRITE(GEN6_RP_CONTROL,
6602 GEN6_RP_MEDIA_TURBO |
6603 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6604 GEN6_RP_MEDIA_IS_GFX |
6605 GEN6_RP_ENABLE |
6606 GEN6_RP_UP_BUSY_AVG |
6607 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006608
Daniel Vetter7526ed72014-09-29 15:07:19 +02006609 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006610
Chris Wilson3a45b052016-07-13 09:10:32 +01006611 reset_rps(dev_priv, gen6_set_rps);
Daniel Vetter7526ed72014-09-29 15:07:19 +02006612
Mika Kuoppala59bad942015-01-16 11:34:40 +02006613 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006614}
6615
Chris Wilsondc979972016-05-10 14:10:04 +01006616static void gen6_enable_rps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006617{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006618 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306619 enum intel_engine_id id;
Chris Wilson99ac9612016-07-13 09:10:34 +01006620 u32 rc6vids, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006621 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006622 int rc6_mode;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00006623 int ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006624
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006625 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02006626
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006627 /* Here begins a magic sequence of register writes to enable
6628 * auto-downclocking.
6629 *
6630 * Perhaps there might be some value in exposing these to
6631 * userspace...
6632 */
6633 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006634
6635 /* Clear the DBG now so we don't confuse earlier errors */
Ville Syrjälä297b32e2016-04-13 21:09:30 +03006636 gtfifodbg = I915_READ(GTFIFODBG);
6637 if (gtfifodbg) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006638 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
6639 I915_WRITE(GTFIFODBG, gtfifodbg);
6640 }
6641
Mika Kuoppala59bad942015-01-16 11:34:40 +02006642 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006643
6644 /* disable the counters and set deterministic thresholds */
6645 I915_WRITE(GEN6_RC_CONTROL, 0);
6646
6647 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
6648 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
6649 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
6650 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
6651 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
6652
Akash Goel3b3f1652016-10-13 22:44:48 +05306653 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006654 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006655
6656 I915_WRITE(GEN6_RC_SLEEP, 0);
6657 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Chris Wilsondc979972016-05-10 14:10:04 +01006658 if (IS_IVYBRIDGE(dev_priv))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07006659 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
6660 else
6661 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08006662 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006663 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
6664
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006665 /* Check if we are enabling RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006666 rc6_mode = intel_enable_rc6();
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006667 if (rc6_mode & INTEL_RC6_ENABLE)
6668 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
6669
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006670 /* We don't use those on Haswell */
Chris Wilsondc979972016-05-10 14:10:04 +01006671 if (!IS_HASWELL(dev_priv)) {
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006672 if (rc6_mode & INTEL_RC6p_ENABLE)
6673 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006674
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006675 if (rc6_mode & INTEL_RC6pp_ENABLE)
6676 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
6677 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006678
Chris Wilsondc979972016-05-10 14:10:04 +01006679 intel_print_rc6_info(dev_priv, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006680
6681 I915_WRITE(GEN6_RC_CONTROL,
6682 rc6_mask |
6683 GEN6_RC_CTL_EI_MODE(1) |
6684 GEN6_RC_CTL_HW_ENABLE);
6685
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006686 /* Power down if completely idle for over 50ms */
6687 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006688 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006689
Chris Wilson3a45b052016-07-13 09:10:32 +01006690 reset_rps(dev_priv, gen6_set_rps);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006691
Ben Widawsky31643d52012-09-26 10:34:01 -07006692 rc6vids = 0;
6693 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
Chris Wilsondc979972016-05-10 14:10:04 +01006694 if (IS_GEN6(dev_priv) && ret) {
Ben Widawsky31643d52012-09-26 10:34:01 -07006695 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
Chris Wilsondc979972016-05-10 14:10:04 +01006696 } else if (IS_GEN6(dev_priv) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
Ben Widawsky31643d52012-09-26 10:34:01 -07006697 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
6698 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
6699 rc6vids &= 0xffff00;
6700 rc6vids |= GEN6_ENCODE_RC6_VID(450);
6701 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
6702 if (ret)
6703 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
6704 }
6705
Mika Kuoppala59bad942015-01-16 11:34:40 +02006706 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006707}
6708
Chris Wilsonfb7404e2016-07-13 09:10:38 +01006709static void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006710{
6711 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01006712 unsigned int gpu_freq;
6713 unsigned int max_ia_freq, min_ring_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05306714 unsigned int max_gpu_freq, min_gpu_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006715 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03006716 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006717
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006718 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02006719
Ben Widawskyeda79642013-10-07 17:15:48 -03006720 policy = cpufreq_cpu_get(0);
6721 if (policy) {
6722 max_ia_freq = policy->cpuinfo.max_freq;
6723 cpufreq_cpu_put(policy);
6724 } else {
6725 /*
6726 * Default to measured freq if none found, PCU will ensure we
6727 * don't go over
6728 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006729 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03006730 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006731
6732 /* Convert from kHz to MHz */
6733 max_ia_freq /= 1000;
6734
Ben Widawsky153b4b952013-10-22 22:05:09 -07006735 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07006736 /* convert DDR frequency from units of 266.6MHz to bandwidth */
6737 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01006738
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006739 if (IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05306740 /* Convert GT frequency to 50 HZ units */
6741 min_gpu_freq = dev_priv->rps.min_freq / GEN9_FREQ_SCALER;
6742 max_gpu_freq = dev_priv->rps.max_freq / GEN9_FREQ_SCALER;
6743 } else {
6744 min_gpu_freq = dev_priv->rps.min_freq;
6745 max_gpu_freq = dev_priv->rps.max_freq;
6746 }
6747
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006748 /*
6749 * For each potential GPU frequency, load a ring frequency we'd like
6750 * to use for memory access. We do this by specifying the IA frequency
6751 * the PCU should use as a reference to determine the ring frequency.
6752 */
Akash Goel4c8c7742015-06-29 14:50:20 +05306753 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
6754 int diff = max_gpu_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01006755 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006756
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006757 if (IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05306758 /*
6759 * ring_freq = 2 * GT. ring_freq is in 100MHz units
6760 * No floor required for ring frequency on SKL.
6761 */
6762 ring_freq = gpu_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01006763 } else if (INTEL_INFO(dev_priv)->gen >= 8) {
Ben Widawsky46c764d2013-11-02 21:07:49 -07006764 /* max(2 * GT, DDR). NB: GT is 50MHz units */
6765 ring_freq = max(min_ring_freq, gpu_freq);
Chris Wilsondc979972016-05-10 14:10:04 +01006766 } else if (IS_HASWELL(dev_priv)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07006767 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01006768 ring_freq = max(min_ring_freq, ring_freq);
6769 /* leave ia_freq as the default, chosen by cpufreq */
6770 } else {
6771 /* On older processors, there is no separate ring
6772 * clock domain, so in order to boost the bandwidth
6773 * of the ring, we need to upclock the CPU (ia_freq).
6774 *
6775 * For GPU frequencies less than 750MHz,
6776 * just use the lowest ring freq.
6777 */
6778 if (gpu_freq < min_freq)
6779 ia_freq = 800;
6780 else
6781 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
6782 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
6783 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006784
Ben Widawsky42c05262012-09-26 10:34:00 -07006785 sandybridge_pcode_write(dev_priv,
6786 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01006787 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
6788 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
6789 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006790 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006791}
6792
Ville Syrjälä03af2042014-06-28 02:03:53 +03006793static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05306794{
6795 u32 val, rp0;
6796
Jani Nikula5b5929c2015-10-07 11:17:46 +03006797 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05306798
Imre Deak43b67992016-08-31 19:13:02 +03006799 switch (INTEL_INFO(dev_priv)->sseu.eu_total) {
Jani Nikula5b5929c2015-10-07 11:17:46 +03006800 case 8:
6801 /* (2 * 4) config */
6802 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
6803 break;
6804 case 12:
6805 /* (2 * 6) config */
6806 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
6807 break;
6808 case 16:
6809 /* (2 * 8) config */
6810 default:
6811 /* Setting (2 * 8) Min RP0 for any other combination */
6812 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
6813 break;
Deepak S095acd52015-01-17 11:05:59 +05306814 }
Jani Nikula5b5929c2015-10-07 11:17:46 +03006815
6816 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
6817
Deepak S2b6b3a02014-05-27 15:59:30 +05306818 return rp0;
6819}
6820
6821static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
6822{
6823 u32 val, rpe;
6824
6825 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
6826 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
6827
6828 return rpe;
6829}
6830
Deepak S7707df42014-07-12 18:46:14 +05306831static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
6832{
6833 u32 val, rp1;
6834
Jani Nikula5b5929c2015-10-07 11:17:46 +03006835 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
6836 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
6837
Deepak S7707df42014-07-12 18:46:14 +05306838 return rp1;
6839}
6840
Deepak S96676fe2016-08-12 18:46:41 +05306841static u32 cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
6842{
6843 u32 val, rpn;
6844
6845 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
6846 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
6847 FB_GFX_FREQ_FUSE_MASK);
6848
6849 return rpn;
6850}
6851
Deepak Sf8f2b002014-07-10 13:16:21 +05306852static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
6853{
6854 u32 val, rp1;
6855
6856 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
6857
6858 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
6859
6860 return rp1;
6861}
6862
Ville Syrjälä03af2042014-06-28 02:03:53 +03006863static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006864{
6865 u32 val, rp0;
6866
Jani Nikula64936252013-05-22 15:36:20 +03006867 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006868
6869 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
6870 /* Clamp to max */
6871 rp0 = min_t(u32, rp0, 0xea);
6872
6873 return rp0;
6874}
6875
6876static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
6877{
6878 u32 val, rpe;
6879
Jani Nikula64936252013-05-22 15:36:20 +03006880 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006881 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03006882 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006883 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
6884
6885 return rpe;
6886}
6887
Ville Syrjälä03af2042014-06-28 02:03:53 +03006888static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006889{
Imre Deak36146032014-12-04 18:39:35 +02006890 u32 val;
6891
6892 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
6893 /*
6894 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
6895 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
6896 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
6897 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
6898 * to make sure it matches what Punit accepts.
6899 */
6900 return max_t(u32, val, 0xc0);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006901}
6902
Imre Deakae484342014-03-31 15:10:44 +03006903/* Check that the pctx buffer wasn't move under us. */
6904static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
6905{
6906 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
6907
6908 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
6909 dev_priv->vlv_pctx->stolen->start);
6910}
6911
Deepak S38807742014-05-23 21:00:15 +05306912
6913/* Check that the pcbr address is not empty. */
6914static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
6915{
6916 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
6917
6918 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
6919}
6920
Chris Wilsondc979972016-05-10 14:10:04 +01006921static void cherryview_setup_pctx(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306922{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02006923 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006924 unsigned long pctx_paddr, paddr;
Deepak S38807742014-05-23 21:00:15 +05306925 u32 pcbr;
6926 int pctx_size = 32*1024;
6927
Deepak S38807742014-05-23 21:00:15 +05306928 pcbr = I915_READ(VLV_PCBR);
6929 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006930 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05306931 paddr = (dev_priv->mm.stolen_base +
Joonas Lahtinen62106b42016-03-18 10:42:57 +02006932 (ggtt->stolen_size - pctx_size));
Deepak S38807742014-05-23 21:00:15 +05306933
6934 pctx_paddr = (paddr & (~4095));
6935 I915_WRITE(VLV_PCBR, pctx_paddr);
6936 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006937
6938 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05306939}
6940
Chris Wilsondc979972016-05-10 14:10:04 +01006941static void valleyview_setup_pctx(struct drm_i915_private *dev_priv)
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006942{
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006943 struct drm_i915_gem_object *pctx;
6944 unsigned long pctx_paddr;
6945 u32 pcbr;
6946 int pctx_size = 24*1024;
6947
6948 pcbr = I915_READ(VLV_PCBR);
6949 if (pcbr) {
6950 /* BIOS set it up already, grab the pre-alloc'd space */
6951 int pcbr_offset;
6952
6953 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00006954 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006955 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02006956 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006957 pctx_size);
6958 goto out;
6959 }
6960
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006961 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
6962
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006963 /*
6964 * From the Gunit register HAS:
6965 * The Gfx driver is expected to program this register and ensure
6966 * proper allocation within Gfx stolen memory. For example, this
6967 * register should be programmed such than the PCBR range does not
6968 * overlap with other ranges, such as the frame buffer, protected
6969 * memory, or any other relevant ranges.
6970 */
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00006971 pctx = i915_gem_object_create_stolen(dev_priv, pctx_size);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006972 if (!pctx) {
6973 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
Tvrtko Ursulinee504892016-02-11 10:27:30 +00006974 goto out;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006975 }
6976
6977 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
6978 I915_WRITE(VLV_PCBR, pctx_paddr);
6979
6980out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006981 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006982 dev_priv->vlv_pctx = pctx;
6983}
6984
Chris Wilsondc979972016-05-10 14:10:04 +01006985static void valleyview_cleanup_pctx(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006986{
Imre Deakae484342014-03-31 15:10:44 +03006987 if (WARN_ON(!dev_priv->vlv_pctx))
6988 return;
6989
Chris Wilsonf0cd5182016-10-28 13:58:43 +01006990 i915_gem_object_put(dev_priv->vlv_pctx);
Imre Deakae484342014-03-31 15:10:44 +03006991 dev_priv->vlv_pctx = NULL;
6992}
6993
Ville Syrjäläc30fec62016-03-04 21:43:02 +02006994static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
6995{
6996 dev_priv->rps.gpll_ref_freq =
6997 vlv_get_cck_clock(dev_priv, "GPLL ref",
6998 CCK_GPLL_CLOCK_CONTROL,
6999 dev_priv->czclk_freq);
7000
7001 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
7002 dev_priv->rps.gpll_ref_freq);
7003}
7004
Chris Wilsondc979972016-05-10 14:10:04 +01007005static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03007006{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007007 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03007008
Chris Wilsondc979972016-05-10 14:10:04 +01007009 valleyview_setup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007010
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007011 vlv_init_gpll_ref_freq(dev_priv);
7012
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007013 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
7014 switch ((val >> 6) & 3) {
7015 case 0:
7016 case 1:
7017 dev_priv->mem_freq = 800;
7018 break;
7019 case 2:
7020 dev_priv->mem_freq = 1066;
7021 break;
7022 case 3:
7023 dev_priv->mem_freq = 1333;
7024 break;
7025 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02007026 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007027
Imre Deak4e805192014-04-14 20:24:41 +03007028 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
7029 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
7030 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007031 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03007032 dev_priv->rps.max_freq);
7033
7034 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
7035 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007036 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03007037 dev_priv->rps.efficient_freq);
7038
Deepak Sf8f2b002014-07-10 13:16:21 +05307039 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
7040 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007041 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05307042 dev_priv->rps.rp1_freq);
7043
Imre Deak4e805192014-04-14 20:24:41 +03007044 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
7045 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007046 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03007047 dev_priv->rps.min_freq);
Imre Deak4e805192014-04-14 20:24:41 +03007048}
7049
Chris Wilsondc979972016-05-10 14:10:04 +01007050static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05307051{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007052 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05307053
Chris Wilsondc979972016-05-10 14:10:04 +01007054 cherryview_setup_pctx(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307055
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007056 vlv_init_gpll_ref_freq(dev_priv);
7057
Ville Syrjäläa5805162015-05-26 20:42:30 +03007058 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02007059 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007060 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02007061
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007062 switch ((val >> 2) & 0x7) {
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007063 case 3:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007064 dev_priv->mem_freq = 2000;
7065 break;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03007066 default:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007067 dev_priv->mem_freq = 1600;
7068 break;
7069 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02007070 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007071
Deepak S2b6b3a02014-05-27 15:59:30 +05307072 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
7073 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
7074 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007075 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05307076 dev_priv->rps.max_freq);
7077
7078 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
7079 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007080 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05307081 dev_priv->rps.efficient_freq);
7082
Deepak S7707df42014-07-12 18:46:14 +05307083 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
7084 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007085 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05307086 dev_priv->rps.rp1_freq);
7087
Deepak S96676fe2016-08-12 18:46:41 +05307088 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307089 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007090 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05307091 dev_priv->rps.min_freq);
7092
Ville Syrjälä1c147622014-08-18 14:42:43 +03007093 WARN_ONCE((dev_priv->rps.max_freq |
7094 dev_priv->rps.efficient_freq |
7095 dev_priv->rps.rp1_freq |
7096 dev_priv->rps.min_freq) & 1,
7097 "Odd GPU freq values\n");
Deepak S38807742014-05-23 21:00:15 +05307098}
7099
Chris Wilsondc979972016-05-10 14:10:04 +01007100static void valleyview_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03007101{
Chris Wilsondc979972016-05-10 14:10:04 +01007102 valleyview_cleanup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007103}
7104
Chris Wilsondc979972016-05-10 14:10:04 +01007105static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05307106{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007107 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05307108 enum intel_engine_id id;
Deepak S2b6b3a02014-05-27 15:59:30 +05307109 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05307110
7111 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
7112
Ville Syrjälä297b32e2016-04-13 21:09:30 +03007113 gtfifodbg = I915_READ(GTFIFODBG) & ~(GT_FIFO_SBDEDICATE_FREE_ENTRY_CHV |
7114 GT_FIFO_FREE_ENTRIES_CHV);
Deepak S38807742014-05-23 21:00:15 +05307115 if (gtfifodbg) {
7116 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
7117 gtfifodbg);
7118 I915_WRITE(GTFIFODBG, gtfifodbg);
7119 }
7120
7121 cherryview_check_pctx(dev_priv);
7122
7123 /* 1a & 1b: Get forcewake during program sequence. Although the driver
7124 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02007125 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05307126
Ville Syrjälä160614a2015-01-19 13:50:47 +02007127 /* Disable RC states. */
7128 I915_WRITE(GEN6_RC_CONTROL, 0);
7129
Deepak S38807742014-05-23 21:00:15 +05307130 /* 2a: Program RC6 thresholds.*/
7131 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
7132 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
7133 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
7134
Akash Goel3b3f1652016-10-13 22:44:48 +05307135 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007136 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Deepak S38807742014-05-23 21:00:15 +05307137 I915_WRITE(GEN6_RC_SLEEP, 0);
7138
Deepak Sf4f71c72015-03-28 15:23:35 +05307139 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
7140 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
Deepak S38807742014-05-23 21:00:15 +05307141
7142 /* allows RC6 residency counter to work */
7143 I915_WRITE(VLV_COUNTER_CONTROL,
7144 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
7145 VLV_MEDIA_RC6_COUNT_EN |
7146 VLV_RENDER_RC6_COUNT_EN));
7147
7148 /* For now we assume BIOS is allocating and populating the PCBR */
7149 pcbr = I915_READ(VLV_PCBR);
7150
Deepak S38807742014-05-23 21:00:15 +05307151 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01007152 if ((intel_enable_rc6() & INTEL_RC6_ENABLE) &&
7153 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02007154 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05307155
7156 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
7157
Deepak S2b6b3a02014-05-27 15:59:30 +05307158 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02007159 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05307160 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
7161 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
7162 I915_WRITE(GEN6_RP_UP_EI, 66000);
7163 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
7164
7165 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7166
7167 /* 5: Enable RPS */
7168 I915_WRITE(GEN6_RP_CONTROL,
7169 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02007170 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05307171 GEN6_RP_ENABLE |
7172 GEN6_RP_UP_BUSY_AVG |
7173 GEN6_RP_DOWN_IDLE_AVG);
7174
Deepak S3ef62342015-04-29 08:36:24 +05307175 /* Setting Fixed Bias */
7176 val = VLV_OVERRIDE_EN |
7177 VLV_SOC_TDP_EN |
7178 CHV_BIAS_CPU_50_SOC_50;
7179 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
7180
Deepak S2b6b3a02014-05-27 15:59:30 +05307181 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
7182
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02007183 /* RPS code assumes GPLL is used */
7184 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
7185
Jani Nikula742f4912015-09-03 11:16:09 +03007186 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05307187 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7188
Chris Wilson3a45b052016-07-13 09:10:32 +01007189 reset_rps(dev_priv, valleyview_set_rps);
Deepak S2b6b3a02014-05-27 15:59:30 +05307190
Mika Kuoppala59bad942015-01-16 11:34:40 +02007191 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05307192}
7193
Chris Wilsondc979972016-05-10 14:10:04 +01007194static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07007195{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007196 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05307197 enum intel_engine_id id;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07007198 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07007199
7200 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
7201
Imre Deakae484342014-03-31 15:10:44 +03007202 valleyview_check_pctx(dev_priv);
7203
Ville Syrjälä297b32e2016-04-13 21:09:30 +03007204 gtfifodbg = I915_READ(GTFIFODBG);
7205 if (gtfifodbg) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07007206 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
7207 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007208 I915_WRITE(GTFIFODBG, gtfifodbg);
7209 }
7210
Deepak Sc8d9a592013-11-23 14:55:42 +05307211 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02007212 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007213
Ville Syrjälä160614a2015-01-19 13:50:47 +02007214 /* Disable RC states. */
7215 I915_WRITE(GEN6_RC_CONTROL, 0);
7216
Ville Syrjäläcad725f2015-01-19 13:50:48 +02007217 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007218 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
7219 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
7220 I915_WRITE(GEN6_RP_UP_EI, 66000);
7221 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
7222
7223 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7224
7225 I915_WRITE(GEN6_RP_CONTROL,
7226 GEN6_RP_MEDIA_TURBO |
7227 GEN6_RP_MEDIA_HW_NORMAL_MODE |
7228 GEN6_RP_MEDIA_IS_GFX |
7229 GEN6_RP_ENABLE |
7230 GEN6_RP_UP_BUSY_AVG |
7231 GEN6_RP_DOWN_IDLE_CONT);
7232
7233 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
7234 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
7235 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
7236
Akash Goel3b3f1652016-10-13 22:44:48 +05307237 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007238 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007239
Jesse Barnes2f0aa3042013-11-15 09:32:11 -08007240 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007241
7242 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07007243 I915_WRITE(VLV_COUNTER_CONTROL,
Mika Kuoppala6b7f6aa2017-03-15 18:12:59 +02007244 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
7245 VLV_MEDIA_RC0_COUNT_EN |
Deepak S31685c22014-07-03 17:33:01 -04007246 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07007247 VLV_MEDIA_RC6_COUNT_EN |
7248 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04007249
Chris Wilsondc979972016-05-10 14:10:04 +01007250 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08007251 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07007252
Chris Wilsondc979972016-05-10 14:10:04 +01007253 intel_print_rc6_info(dev_priv, rc6_mode);
Ben Widawskydc39fff2013-10-18 12:32:07 -07007254
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07007255 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007256
Deepak S3ef62342015-04-29 08:36:24 +05307257 /* Setting Fixed Bias */
7258 val = VLV_OVERRIDE_EN |
7259 VLV_SOC_TDP_EN |
7260 VLV_BIAS_CPU_125_SOC_875;
7261 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
7262
Jani Nikula64936252013-05-22 15:36:20 +03007263 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007264
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02007265 /* RPS code assumes GPLL is used */
7266 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
7267
Jani Nikula742f4912015-09-03 11:16:09 +03007268 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07007269 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7270
Chris Wilson3a45b052016-07-13 09:10:32 +01007271 reset_rps(dev_priv, valleyview_set_rps);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007272
Mika Kuoppala59bad942015-01-16 11:34:40 +02007273 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007274}
7275
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007276static unsigned long intel_pxfreq(u32 vidfreq)
7277{
7278 unsigned long freq;
7279 int div = (vidfreq & 0x3f0000) >> 16;
7280 int post = (vidfreq & 0x3000) >> 12;
7281 int pre = (vidfreq & 0x7);
7282
7283 if (!pre)
7284 return 0;
7285
7286 freq = ((div * 133333) / ((1<<post) * pre));
7287
7288 return freq;
7289}
7290
Daniel Vettereb48eb02012-04-26 23:28:12 +02007291static const struct cparams {
7292 u16 i;
7293 u16 t;
7294 u16 m;
7295 u16 c;
7296} cparams[] = {
7297 { 1, 1333, 301, 28664 },
7298 { 1, 1066, 294, 24460 },
7299 { 1, 800, 294, 25192 },
7300 { 0, 1333, 276, 27605 },
7301 { 0, 1066, 276, 27605 },
7302 { 0, 800, 231, 23784 },
7303};
7304
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007305static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007306{
7307 u64 total_count, diff, ret;
7308 u32 count1, count2, count3, m = 0, c = 0;
7309 unsigned long now = jiffies_to_msecs(jiffies), diff1;
7310 int i;
7311
Chris Wilson67520412017-03-02 13:28:01 +00007312 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007313
Daniel Vetter20e4d402012-08-08 23:35:39 +02007314 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007315
7316 /* Prevent division-by-zero if we are asking too fast.
7317 * Also, we don't get interesting results if we are polling
7318 * faster than once in 10ms, so just return the saved value
7319 * in such cases.
7320 */
7321 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02007322 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007323
7324 count1 = I915_READ(DMIEC);
7325 count2 = I915_READ(DDREC);
7326 count3 = I915_READ(CSIEC);
7327
7328 total_count = count1 + count2 + count3;
7329
7330 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02007331 if (total_count < dev_priv->ips.last_count1) {
7332 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007333 diff += total_count;
7334 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007335 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007336 }
7337
7338 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007339 if (cparams[i].i == dev_priv->ips.c_m &&
7340 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02007341 m = cparams[i].m;
7342 c = cparams[i].c;
7343 break;
7344 }
7345 }
7346
7347 diff = div_u64(diff, diff1);
7348 ret = ((m * diff) + c);
7349 ret = div_u64(ret, 10);
7350
Daniel Vetter20e4d402012-08-08 23:35:39 +02007351 dev_priv->ips.last_count1 = total_count;
7352 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007353
Daniel Vetter20e4d402012-08-08 23:35:39 +02007354 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007355
7356 return ret;
7357}
7358
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007359unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
7360{
7361 unsigned long val;
7362
Chris Wilsondc979972016-05-10 14:10:04 +01007363 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007364 return 0;
7365
7366 spin_lock_irq(&mchdev_lock);
7367
7368 val = __i915_chipset_val(dev_priv);
7369
7370 spin_unlock_irq(&mchdev_lock);
7371
7372 return val;
7373}
7374
Daniel Vettereb48eb02012-04-26 23:28:12 +02007375unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
7376{
7377 unsigned long m, x, b;
7378 u32 tsfs;
7379
7380 tsfs = I915_READ(TSFS);
7381
7382 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
7383 x = I915_READ8(TR1);
7384
7385 b = tsfs & TSFS_INTR_MASK;
7386
7387 return ((m * x) / 127) - b;
7388}
7389
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007390static int _pxvid_to_vd(u8 pxvid)
7391{
7392 if (pxvid == 0)
7393 return 0;
7394
7395 if (pxvid >= 8 && pxvid < 31)
7396 pxvid = 31;
7397
7398 return (pxvid + 2) * 125;
7399}
7400
7401static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007402{
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007403 const int vd = _pxvid_to_vd(pxvid);
7404 const int vm = vd - 1125;
7405
Chris Wilsondc979972016-05-10 14:10:04 +01007406 if (INTEL_INFO(dev_priv)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007407 return vm > 0 ? vm : 0;
7408
7409 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007410}
7411
Daniel Vetter02d71952012-08-09 16:44:54 +02007412static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007413{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00007414 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007415 u32 count;
7416
Chris Wilson67520412017-03-02 13:28:01 +00007417 lockdep_assert_held(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007418
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00007419 now = ktime_get_raw_ns();
7420 diffms = now - dev_priv->ips.last_time2;
7421 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007422
7423 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02007424 if (!diffms)
7425 return;
7426
7427 count = I915_READ(GFXEC);
7428
Daniel Vetter20e4d402012-08-08 23:35:39 +02007429 if (count < dev_priv->ips.last_count2) {
7430 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007431 diff += count;
7432 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007433 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007434 }
7435
Daniel Vetter20e4d402012-08-08 23:35:39 +02007436 dev_priv->ips.last_count2 = count;
7437 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007438
7439 /* More magic constants... */
7440 diff = diff * 1181;
7441 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02007442 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007443}
7444
Daniel Vetter02d71952012-08-09 16:44:54 +02007445void i915_update_gfx_val(struct drm_i915_private *dev_priv)
7446{
Chris Wilsondc979972016-05-10 14:10:04 +01007447 if (INTEL_INFO(dev_priv)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02007448 return;
7449
Daniel Vetter92703882012-08-09 16:46:01 +02007450 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007451
7452 __i915_update_gfx_val(dev_priv);
7453
Daniel Vetter92703882012-08-09 16:46:01 +02007454 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007455}
7456
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007457static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007458{
7459 unsigned long t, corr, state1, corr2, state2;
7460 u32 pxvid, ext_v;
7461
Chris Wilson67520412017-03-02 13:28:01 +00007462 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007463
Ville Syrjälä616847e2015-09-18 20:03:19 +03007464 pxvid = I915_READ(PXVFREQ(dev_priv->rps.cur_freq));
Daniel Vettereb48eb02012-04-26 23:28:12 +02007465 pxvid = (pxvid >> 24) & 0x7f;
7466 ext_v = pvid_to_extvid(dev_priv, pxvid);
7467
7468 state1 = ext_v;
7469
7470 t = i915_mch_val(dev_priv);
7471
7472 /* Revel in the empirically derived constants */
7473
7474 /* Correction factor in 1/100000 units */
7475 if (t > 80)
7476 corr = ((t * 2349) + 135940);
7477 else if (t >= 50)
7478 corr = ((t * 964) + 29317);
7479 else /* < 50 */
7480 corr = ((t * 301) + 1004);
7481
7482 corr = corr * ((150142 * state1) / 10000 - 78642);
7483 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02007484 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007485
7486 state2 = (corr2 * state1) / 10000;
7487 state2 /= 100; /* convert to mW */
7488
Daniel Vetter02d71952012-08-09 16:44:54 +02007489 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007490
Daniel Vetter20e4d402012-08-08 23:35:39 +02007491 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007492}
7493
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007494unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
7495{
7496 unsigned long val;
7497
Chris Wilsondc979972016-05-10 14:10:04 +01007498 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007499 return 0;
7500
7501 spin_lock_irq(&mchdev_lock);
7502
7503 val = __i915_gfx_val(dev_priv);
7504
7505 spin_unlock_irq(&mchdev_lock);
7506
7507 return val;
7508}
7509
Daniel Vettereb48eb02012-04-26 23:28:12 +02007510/**
7511 * i915_read_mch_val - return value for IPS use
7512 *
7513 * Calculate and return a value for the IPS driver to use when deciding whether
7514 * we have thermal and power headroom to increase CPU or GPU power budget.
7515 */
7516unsigned long i915_read_mch_val(void)
7517{
7518 struct drm_i915_private *dev_priv;
7519 unsigned long chipset_val, graphics_val, ret = 0;
7520
Daniel Vetter92703882012-08-09 16:46:01 +02007521 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007522 if (!i915_mch_dev)
7523 goto out_unlock;
7524 dev_priv = i915_mch_dev;
7525
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007526 chipset_val = __i915_chipset_val(dev_priv);
7527 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007528
7529 ret = chipset_val + graphics_val;
7530
7531out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007532 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007533
7534 return ret;
7535}
7536EXPORT_SYMBOL_GPL(i915_read_mch_val);
7537
7538/**
7539 * i915_gpu_raise - raise GPU frequency limit
7540 *
7541 * Raise the limit; IPS indicates we have thermal headroom.
7542 */
7543bool i915_gpu_raise(void)
7544{
7545 struct drm_i915_private *dev_priv;
7546 bool ret = true;
7547
Daniel Vetter92703882012-08-09 16:46:01 +02007548 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007549 if (!i915_mch_dev) {
7550 ret = false;
7551 goto out_unlock;
7552 }
7553 dev_priv = i915_mch_dev;
7554
Daniel Vetter20e4d402012-08-08 23:35:39 +02007555 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
7556 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007557
7558out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007559 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007560
7561 return ret;
7562}
7563EXPORT_SYMBOL_GPL(i915_gpu_raise);
7564
7565/**
7566 * i915_gpu_lower - lower GPU frequency limit
7567 *
7568 * IPS indicates we're close to a thermal limit, so throttle back the GPU
7569 * frequency maximum.
7570 */
7571bool i915_gpu_lower(void)
7572{
7573 struct drm_i915_private *dev_priv;
7574 bool ret = true;
7575
Daniel Vetter92703882012-08-09 16:46:01 +02007576 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007577 if (!i915_mch_dev) {
7578 ret = false;
7579 goto out_unlock;
7580 }
7581 dev_priv = i915_mch_dev;
7582
Daniel Vetter20e4d402012-08-08 23:35:39 +02007583 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
7584 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007585
7586out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007587 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007588
7589 return ret;
7590}
7591EXPORT_SYMBOL_GPL(i915_gpu_lower);
7592
7593/**
7594 * i915_gpu_busy - indicate GPU business to IPS
7595 *
7596 * Tell the IPS driver whether or not the GPU is busy.
7597 */
7598bool i915_gpu_busy(void)
7599{
Daniel Vettereb48eb02012-04-26 23:28:12 +02007600 bool ret = false;
7601
Daniel Vetter92703882012-08-09 16:46:01 +02007602 spin_lock_irq(&mchdev_lock);
Chris Wilsondcff85c2016-08-05 10:14:11 +01007603 if (i915_mch_dev)
7604 ret = i915_mch_dev->gt.awake;
Daniel Vetter92703882012-08-09 16:46:01 +02007605 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007606
7607 return ret;
7608}
7609EXPORT_SYMBOL_GPL(i915_gpu_busy);
7610
7611/**
7612 * i915_gpu_turbo_disable - disable graphics turbo
7613 *
7614 * Disable graphics turbo by resetting the max frequency and setting the
7615 * current frequency to the default.
7616 */
7617bool i915_gpu_turbo_disable(void)
7618{
7619 struct drm_i915_private *dev_priv;
7620 bool ret = true;
7621
Daniel Vetter92703882012-08-09 16:46:01 +02007622 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007623 if (!i915_mch_dev) {
7624 ret = false;
7625 goto out_unlock;
7626 }
7627 dev_priv = i915_mch_dev;
7628
Daniel Vetter20e4d402012-08-08 23:35:39 +02007629 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007630
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01007631 if (!ironlake_set_drps(dev_priv, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02007632 ret = false;
7633
7634out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007635 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007636
7637 return ret;
7638}
7639EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
7640
7641/**
7642 * Tells the intel_ips driver that the i915 driver is now loaded, if
7643 * IPS got loaded first.
7644 *
7645 * This awkward dance is so that neither module has to depend on the
7646 * other in order for IPS to do the appropriate communication of
7647 * GPU turbo limits to i915.
7648 */
7649static void
7650ips_ping_for_i915_load(void)
7651{
7652 void (*link)(void);
7653
7654 link = symbol_get(ips_link_to_i915_driver);
7655 if (link) {
7656 link();
7657 symbol_put(ips_link_to_i915_driver);
7658 }
7659}
7660
7661void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
7662{
Daniel Vetter02d71952012-08-09 16:44:54 +02007663 /* We only register the i915 ips part with intel-ips once everything is
7664 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02007665 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007666 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02007667 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007668
7669 ips_ping_for_i915_load();
7670}
7671
7672void intel_gpu_ips_teardown(void)
7673{
Daniel Vetter92703882012-08-09 16:46:01 +02007674 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007675 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02007676 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007677}
Deepak S76c3552f2014-01-30 23:08:16 +05307678
Chris Wilsondc979972016-05-10 14:10:04 +01007679static void intel_init_emon(struct drm_i915_private *dev_priv)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007680{
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007681 u32 lcfuse;
7682 u8 pxw[16];
7683 int i;
7684
7685 /* Disable to program */
7686 I915_WRITE(ECR, 0);
7687 POSTING_READ(ECR);
7688
7689 /* Program energy weights for various events */
7690 I915_WRITE(SDEW, 0x15040d00);
7691 I915_WRITE(CSIEW0, 0x007f0000);
7692 I915_WRITE(CSIEW1, 0x1e220004);
7693 I915_WRITE(CSIEW2, 0x04000004);
7694
7695 for (i = 0; i < 5; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007696 I915_WRITE(PEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007697 for (i = 0; i < 3; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007698 I915_WRITE(DEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007699
7700 /* Program P-state weights to account for frequency power adjustment */
7701 for (i = 0; i < 16; i++) {
Ville Syrjälä616847e2015-09-18 20:03:19 +03007702 u32 pxvidfreq = I915_READ(PXVFREQ(i));
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007703 unsigned long freq = intel_pxfreq(pxvidfreq);
7704 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
7705 PXVFREQ_PX_SHIFT;
7706 unsigned long val;
7707
7708 val = vid * vid;
7709 val *= (freq / 1000);
7710 val *= 255;
7711 val /= (127*127*900);
7712 if (val > 0xff)
7713 DRM_ERROR("bad pxval: %ld\n", val);
7714 pxw[i] = val;
7715 }
7716 /* Render standby states get 0 weight */
7717 pxw[14] = 0;
7718 pxw[15] = 0;
7719
7720 for (i = 0; i < 4; i++) {
7721 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
7722 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
Ville Syrjälä616847e2015-09-18 20:03:19 +03007723 I915_WRITE(PXW(i), val);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007724 }
7725
7726 /* Adjust magic regs to magic values (more experimental results) */
7727 I915_WRITE(OGW0, 0);
7728 I915_WRITE(OGW1, 0);
7729 I915_WRITE(EG0, 0x00007f00);
7730 I915_WRITE(EG1, 0x0000000e);
7731 I915_WRITE(EG2, 0x000e0000);
7732 I915_WRITE(EG3, 0x68000300);
7733 I915_WRITE(EG4, 0x42000000);
7734 I915_WRITE(EG5, 0x00140031);
7735 I915_WRITE(EG6, 0);
7736 I915_WRITE(EG7, 0);
7737
7738 for (i = 0; i < 8; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007739 I915_WRITE(PXWL(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007740
7741 /* Enable PMON + select events */
7742 I915_WRITE(ECR, 0x80000019);
7743
7744 lcfuse = I915_READ(LCFUSE02);
7745
Daniel Vetter20e4d402012-08-08 23:35:39 +02007746 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007747}
7748
Chris Wilsondc979972016-05-10 14:10:04 +01007749void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007750{
Imre Deakb268c692015-12-15 20:10:31 +02007751 /*
7752 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
7753 * requirement.
7754 */
7755 if (!i915.enable_rc6) {
7756 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
7757 intel_runtime_pm_get(dev_priv);
7758 }
Imre Deake6069ca2014-04-18 16:01:02 +03007759
Chris Wilsonb5163db2016-08-10 13:58:24 +01007760 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson773ea9a2016-07-13 09:10:33 +01007761 mutex_lock(&dev_priv->rps.hw_lock);
7762
7763 /* Initialize RPS limits (for userspace) */
Chris Wilsondc979972016-05-10 14:10:04 +01007764 if (IS_CHERRYVIEW(dev_priv))
7765 cherryview_init_gt_powersave(dev_priv);
7766 else if (IS_VALLEYVIEW(dev_priv))
7767 valleyview_init_gt_powersave(dev_priv);
Chris Wilson2a13ae72016-08-02 11:15:27 +01007768 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson773ea9a2016-07-13 09:10:33 +01007769 gen6_init_rps_frequencies(dev_priv);
7770
7771 /* Derive initial user preferences/limits from the hardware limits */
7772 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
7773 dev_priv->rps.cur_freq = dev_priv->rps.idle_freq;
7774
7775 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
7776 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
7777
7778 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
7779 dev_priv->rps.min_freq_softlimit =
7780 max_t(int,
7781 dev_priv->rps.efficient_freq,
7782 intel_freq_opcode(dev_priv, 450));
7783
Chris Wilson99ac9612016-07-13 09:10:34 +01007784 /* After setting max-softlimit, find the overclock max freq */
7785 if (IS_GEN6(dev_priv) ||
7786 IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
7787 u32 params = 0;
7788
7789 sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &params);
7790 if (params & BIT(31)) { /* OC supported */
7791 DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
7792 (dev_priv->rps.max_freq & 0xff) * 50,
7793 (params & 0xff) * 50);
7794 dev_priv->rps.max_freq = params & 0xff;
7795 }
7796 }
7797
Chris Wilson29ecd78d2016-07-13 09:10:35 +01007798 /* Finally allow us to boost to max by default */
7799 dev_priv->rps.boost_freq = dev_priv->rps.max_freq;
7800
Chris Wilson773ea9a2016-07-13 09:10:33 +01007801 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb5163db2016-08-10 13:58:24 +01007802 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson54b4f682016-07-21 21:16:19 +01007803
7804 intel_autoenable_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03007805}
7806
Chris Wilsondc979972016-05-10 14:10:04 +01007807void intel_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007808{
Ville Syrjälä8dac1e12016-08-02 14:07:33 +03007809 if (IS_VALLEYVIEW(dev_priv))
Chris Wilsondc979972016-05-10 14:10:04 +01007810 valleyview_cleanup_gt_powersave(dev_priv);
Imre Deakb268c692015-12-15 20:10:31 +02007811
7812 if (!i915.enable_rc6)
7813 intel_runtime_pm_put(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03007814}
7815
Chris Wilson54b4f682016-07-21 21:16:19 +01007816/**
7817 * intel_suspend_gt_powersave - suspend PM work and helper threads
7818 * @dev_priv: i915 device
7819 *
7820 * We don't want to disable RC6 or other features here, we just want
7821 * to make sure any work we've queued has finished and won't bother
7822 * us while we're suspended.
7823 */
7824void intel_suspend_gt_powersave(struct drm_i915_private *dev_priv)
7825{
7826 if (INTEL_GEN(dev_priv) < 6)
7827 return;
7828
7829 if (cancel_delayed_work_sync(&dev_priv->rps.autoenable_work))
7830 intel_runtime_pm_put(dev_priv);
7831
7832 /* gen6_rps_idle() will be called later to disable interrupts */
7833}
7834
Chris Wilsonb7137e02016-07-13 09:10:37 +01007835void intel_sanitize_gt_powersave(struct drm_i915_private *dev_priv)
7836{
7837 dev_priv->rps.enabled = true; /* force disabling */
7838 intel_disable_gt_powersave(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007839
7840 gen6_reset_rps_interrupts(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07007841}
7842
Chris Wilsondc979972016-05-10 14:10:04 +01007843void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
Daniel Vetter8090c6b2012-06-24 16:42:32 +02007844{
Chris Wilsonb7137e02016-07-13 09:10:37 +01007845 if (!READ_ONCE(dev_priv->rps.enabled))
7846 return;
Jesse Barnes1a01ab32012-11-02 11:14:00 -07007847
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007848 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007849
Chris Wilsonb7137e02016-07-13 09:10:37 +01007850 if (INTEL_GEN(dev_priv) >= 9) {
7851 gen9_disable_rc6(dev_priv);
7852 gen9_disable_rps(dev_priv);
7853 } else if (IS_CHERRYVIEW(dev_priv)) {
7854 cherryview_disable_rps(dev_priv);
7855 } else if (IS_VALLEYVIEW(dev_priv)) {
7856 valleyview_disable_rps(dev_priv);
7857 } else if (INTEL_GEN(dev_priv) >= 6) {
7858 gen6_disable_rps(dev_priv);
7859 } else if (IS_IRONLAKE_M(dev_priv)) {
7860 ironlake_disable_drps(dev_priv);
7861 }
7862
7863 dev_priv->rps.enabled = false;
7864 mutex_unlock(&dev_priv->rps.hw_lock);
7865}
7866
7867void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
7868{
Chris Wilson54b4f682016-07-21 21:16:19 +01007869 /* We shouldn't be disabling as we submit, so this should be less
7870 * racy than it appears!
7871 */
Chris Wilsonb7137e02016-07-13 09:10:37 +01007872 if (READ_ONCE(dev_priv->rps.enabled))
7873 return;
7874
7875 /* Powersaving is controlled by the host when inside a VM */
7876 if (intel_vgpu_active(dev_priv))
7877 return;
7878
7879 mutex_lock(&dev_priv->rps.hw_lock);
Imre Deak3cc134e2014-11-19 15:30:03 +02007880
Chris Wilsondc979972016-05-10 14:10:04 +01007881 if (IS_CHERRYVIEW(dev_priv)) {
7882 cherryview_enable_rps(dev_priv);
7883 } else if (IS_VALLEYVIEW(dev_priv)) {
7884 valleyview_enable_rps(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007885 } else if (INTEL_GEN(dev_priv) >= 9) {
Chris Wilsondc979972016-05-10 14:10:04 +01007886 gen9_enable_rc6(dev_priv);
7887 gen9_enable_rps(dev_priv);
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07007888 if (IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv))
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007889 gen6_update_ring_freq(dev_priv);
Chris Wilsondc979972016-05-10 14:10:04 +01007890 } else if (IS_BROADWELL(dev_priv)) {
7891 gen8_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007892 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007893 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilsondc979972016-05-10 14:10:04 +01007894 gen6_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007895 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007896 } else if (IS_IRONLAKE_M(dev_priv)) {
7897 ironlake_enable_drps(dev_priv);
7898 intel_init_emon(dev_priv);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007899 }
Chris Wilsonaed242f2015-03-18 09:48:21 +00007900
7901 WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
7902 WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
7903
7904 WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
7905 WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
7906
Chris Wilson54b4f682016-07-21 21:16:19 +01007907 dev_priv->rps.enabled = true;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007908 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007909}
Imre Deakc6df39b2014-04-14 20:24:29 +03007910
Chris Wilson54b4f682016-07-21 21:16:19 +01007911static void __intel_autoenable_gt_powersave(struct work_struct *work)
7912{
7913 struct drm_i915_private *dev_priv =
7914 container_of(work, typeof(*dev_priv), rps.autoenable_work.work);
7915 struct intel_engine_cs *rcs;
7916 struct drm_i915_gem_request *req;
7917
7918 if (READ_ONCE(dev_priv->rps.enabled))
7919 goto out;
7920
Akash Goel3b3f1652016-10-13 22:44:48 +05307921 rcs = dev_priv->engine[RCS];
Chris Wilsone8a9c582016-12-18 15:37:20 +00007922 if (rcs->last_retired_context)
Chris Wilson54b4f682016-07-21 21:16:19 +01007923 goto out;
7924
7925 if (!rcs->init_context)
7926 goto out;
7927
7928 mutex_lock(&dev_priv->drm.struct_mutex);
7929
7930 req = i915_gem_request_alloc(rcs, dev_priv->kernel_context);
7931 if (IS_ERR(req))
7932 goto unlock;
7933
7934 if (!i915.enable_execlists && i915_switch_context(req) == 0)
7935 rcs->init_context(req);
7936
7937 /* Mark the device busy, calling intel_enable_gt_powersave() */
Chris Wilsone642c852017-03-17 11:47:09 +00007938 i915_add_request(req);
Chris Wilson54b4f682016-07-21 21:16:19 +01007939
7940unlock:
7941 mutex_unlock(&dev_priv->drm.struct_mutex);
7942out:
7943 intel_runtime_pm_put(dev_priv);
7944}
7945
7946void intel_autoenable_gt_powersave(struct drm_i915_private *dev_priv)
7947{
7948 if (READ_ONCE(dev_priv->rps.enabled))
7949 return;
7950
7951 if (IS_IRONLAKE_M(dev_priv)) {
7952 ironlake_enable_drps(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007953 intel_init_emon(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007954 } else if (INTEL_INFO(dev_priv)->gen >= 6) {
7955 /*
7956 * PCU communication is slow and this doesn't need to be
7957 * done at any specific time, so do this out of our fast path
7958 * to make resume and init faster.
7959 *
7960 * We depend on the HW RC6 power context save/restore
7961 * mechanism when entering D3 through runtime PM suspend. So
7962 * disable RPM until RPS/RC6 is properly setup. We can only
7963 * get here via the driver load/system resume/runtime resume
7964 * paths, so the _noresume version is enough (and in case of
7965 * runtime resume it's necessary).
7966 */
7967 if (queue_delayed_work(dev_priv->wq,
7968 &dev_priv->rps.autoenable_work,
7969 round_jiffies_up_relative(HZ)))
7970 intel_runtime_pm_get_noresume(dev_priv);
7971 }
7972}
7973
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007974static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007975{
Daniel Vetter3107bd42012-10-31 22:52:31 +01007976 /*
7977 * On Ibex Peak and Cougar Point, we need to disable clock
7978 * gating for the panel power sequencer or it will fail to
7979 * start up when no ports are active.
7980 */
7981 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
7982}
7983
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007984static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007985{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007986 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007987
Damien Lespiau055e3932014-08-18 13:49:10 +01007988 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007989 I915_WRITE(DSPCNTR(pipe),
7990 I915_READ(DSPCNTR(pipe)) |
7991 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007992
7993 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
7994 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007995 }
7996}
7997
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007998static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä017636c2013-12-05 15:51:37 +02007999{
Ville Syrjälä017636c2013-12-05 15:51:37 +02008000 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
8001 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
8002 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
8003
8004 /*
8005 * Don't touch WM1S_LP_EN here.
8006 * Doing so could cause underruns.
8007 */
8008}
8009
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008010static void ilk_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008011{
Damien Lespiau231e54f2012-10-19 17:55:41 +01008012 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008013
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01008014 /*
8015 * Required for FBC
8016 * WaFbcDisableDpfcClockGating:ilk
8017 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008018 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
8019 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
8020 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008021
8022 I915_WRITE(PCH_3DCGDIS0,
8023 MARIUNIT_CLOCK_GATE_DISABLE |
8024 SVSMUNIT_CLOCK_GATE_DISABLE);
8025 I915_WRITE(PCH_3DCGDIS1,
8026 VFMUNIT_CLOCK_GATE_DISABLE);
8027
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008028 /*
8029 * According to the spec the following bits should be set in
8030 * order to enable memory self-refresh
8031 * The bit 22/21 of 0x42004
8032 * The bit 5 of 0x42020
8033 * The bit 15 of 0x45000
8034 */
8035 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8036 (I915_READ(ILK_DISPLAY_CHICKEN2) |
8037 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008038 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008039 I915_WRITE(DISP_ARB_CTL,
8040 (I915_READ(DISP_ARB_CTL) |
8041 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02008042
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008043 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008044
8045 /*
8046 * Based on the document from hardware guys the following bits
8047 * should be set unconditionally in order to enable FBC.
8048 * The bit 22 of 0x42000
8049 * The bit 22 of 0x42004
8050 * The bit 7,8,9 of 0x42020.
8051 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008052 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01008053 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008054 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8055 I915_READ(ILK_DISPLAY_CHICKEN1) |
8056 ILK_FBCQ_DIS);
8057 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8058 I915_READ(ILK_DISPLAY_CHICKEN2) |
8059 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008060 }
8061
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008062 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
8063
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008064 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8065 I915_READ(ILK_DISPLAY_CHICKEN2) |
8066 ILK_ELPIN_409_SELECT);
8067 I915_WRITE(_3D_CHICKEN2,
8068 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
8069 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02008070
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008071 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02008072 I915_WRITE(CACHE_MODE_0,
8073 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01008074
Akash Goel4e046322014-04-04 17:14:38 +05308075 /* WaDisable_RenderCache_OperationalFlush:ilk */
8076 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8077
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008078 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03008079
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008080 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008081}
8082
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008083static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01008084{
Daniel Vetter3107bd42012-10-31 22:52:31 +01008085 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008086 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01008087
8088 /*
8089 * On Ibex Peak and Cougar Point, we need to disable clock
8090 * gating for the panel power sequencer or it will fail to
8091 * start up when no ports are active.
8092 */
Jesse Barnescd664072013-10-02 10:34:19 -07008093 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
8094 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
8095 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008096 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
8097 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01008098 /* The below fixes the weird display corruption, a few pixels shifted
8099 * downward, on (only) LVDS of some HP laptops with IVY.
8100 */
Damien Lespiau055e3932014-08-18 13:49:10 +01008101 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03008102 val = I915_READ(TRANS_CHICKEN2(pipe));
8103 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
8104 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008105 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008106 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03008107 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
8108 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
8109 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008110 I915_WRITE(TRANS_CHICKEN2(pipe), val);
8111 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01008112 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01008113 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01008114 I915_WRITE(TRANS_CHICKEN1(pipe),
8115 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
8116 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008117}
8118
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008119static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008120{
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008121 uint32_t tmp;
8122
8123 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02008124 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
8125 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
8126 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008127}
8128
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008129static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008130{
Damien Lespiau231e54f2012-10-19 17:55:41 +01008131 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008132
Damien Lespiau231e54f2012-10-19 17:55:41 +01008133 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008134
8135 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8136 I915_READ(ILK_DISPLAY_CHICKEN2) |
8137 ILK_ELPIN_409_SELECT);
8138
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008139 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01008140 I915_WRITE(_3D_CHICKEN,
8141 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
8142
Akash Goel4e046322014-04-04 17:14:38 +05308143 /* WaDisable_RenderCache_OperationalFlush:snb */
8144 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8145
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008146 /*
8147 * BSpec recoomends 8x4 when MSAA is used,
8148 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008149 *
8150 * Note that PS/WM thread counts depend on the WIZ hashing
8151 * disable bit, which we don't touch here, but it's good
8152 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008153 */
8154 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008155 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008156
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008157 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008158
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008159 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02008160 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008161
8162 I915_WRITE(GEN6_UCGCTL1,
8163 I915_READ(GEN6_UCGCTL1) |
8164 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
8165 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
8166
8167 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
8168 * gating disable must be set. Failure to set it results in
8169 * flickering pixels due to Z write ordering failures after
8170 * some amount of runtime in the Mesa "fire" demo, and Unigine
8171 * Sanctuary and Tropics, and apparently anything else with
8172 * alpha test or pixel discard.
8173 *
8174 * According to the spec, bit 11 (RCCUNIT) must also be set,
8175 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008176 *
Ville Syrjäläef593182014-01-22 21:32:47 +02008177 * WaDisableRCCUnitClockGating:snb
8178 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008179 */
8180 I915_WRITE(GEN6_UCGCTL2,
8181 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
8182 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
8183
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02008184 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02008185 I915_WRITE(_3D_CHICKEN3,
8186 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008187
8188 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02008189 * Bspec says:
8190 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
8191 * 3DSTATE_SF number of SF output attributes is more than 16."
8192 */
8193 I915_WRITE(_3D_CHICKEN3,
8194 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
8195
8196 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008197 * According to the spec the following bits should be
8198 * set in order to enable memory self-refresh and fbc:
8199 * The bit21 and bit22 of 0x42000
8200 * The bit21 and bit22 of 0x42004
8201 * The bit5 and bit7 of 0x42020
8202 * The bit14 of 0x70180
8203 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01008204 *
8205 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008206 */
8207 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8208 I915_READ(ILK_DISPLAY_CHICKEN1) |
8209 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
8210 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8211 I915_READ(ILK_DISPLAY_CHICKEN2) |
8212 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01008213 I915_WRITE(ILK_DSPCLK_GATE_D,
8214 I915_READ(ILK_DSPCLK_GATE_D) |
8215 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
8216 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008217
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008218 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07008219
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008220 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008221
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008222 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008223}
8224
8225static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
8226{
8227 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
8228
Ville Syrjälä3aad9052014-01-22 21:32:59 +02008229 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02008230 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02008231 *
8232 * This actually overrides the dispatch
8233 * mode for all thread types.
8234 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008235 reg &= ~GEN7_FF_SCHED_MASK;
8236 reg |= GEN7_FF_TS_SCHED_HW;
8237 reg |= GEN7_FF_VS_SCHED_HW;
8238 reg |= GEN7_FF_DS_SCHED_HW;
8239
8240 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
8241}
8242
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008243static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008244{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008245 /*
8246 * TODO: this bit should only be enabled when really needed, then
8247 * disabled when not needed anymore in order to save power.
8248 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008249 if (HAS_PCH_LPT_LP(dev_priv))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008250 I915_WRITE(SOUTH_DSPCLK_GATE_D,
8251 I915_READ(SOUTH_DSPCLK_GATE_D) |
8252 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03008253
8254 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03008255 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
8256 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03008257 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008258}
8259
Ville Syrjälä712bf362016-10-31 22:37:23 +02008260static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03008261{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008262 if (HAS_PCH_LPT_LP(dev_priv)) {
Imre Deak7d708ee2013-04-17 14:04:50 +03008263 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
8264
8265 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8266 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8267 }
8268}
8269
Imre Deak450174f2016-05-03 15:54:21 +03008270static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
8271 int general_prio_credits,
8272 int high_prio_credits)
8273{
8274 u32 misccpctl;
8275
8276 /* WaTempDisableDOPClkGating:bdw */
8277 misccpctl = I915_READ(GEN7_MISCCPCTL);
8278 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
8279
8280 I915_WRITE(GEN8_L3SQCREG1,
8281 L3_GENERAL_PRIO_CREDITS(general_prio_credits) |
8282 L3_HIGH_PRIO_CREDITS(high_prio_credits));
8283
8284 /*
8285 * Wait at least 100 clocks before re-enabling clock gating.
8286 * See the definition of L3SQCREG1 in BSpec.
8287 */
8288 POSTING_READ(GEN8_L3SQCREG1);
8289 udelay(1);
8290 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
8291}
8292
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008293static void cnp_init_clock_gating(struct drm_i915_private *dev_priv)
8294{
8295 if (!HAS_PCH_CNP(dev_priv))
8296 return;
8297
8298 /* Wa #1181 */
8299 I915_WRITE(SOUTH_DSPCLK_GATE_D, CNP_PWM_CGE_GATING_DISABLE);
8300}
8301
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008302static void cnl_init_clock_gating(struct drm_i915_private *dev_priv)
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008303{
Rodrigo Vivi8f067832017-09-05 12:30:13 -07008304 u32 val;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008305 cnp_init_clock_gating(dev_priv);
8306
Rodrigo Vivi1a25db62017-08-15 16:16:51 -07008307 /* This is not an Wa. Enable for better image quality */
8308 I915_WRITE(_3D_CHICKEN3,
8309 _MASKED_BIT_ENABLE(_3D_CHICKEN3_AA_LINE_QUALITY_FIX_ENABLE));
8310
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008311 /* WaEnableChickenDCPR:cnl */
8312 I915_WRITE(GEN8_CHICKEN_DCPR_1,
8313 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
8314
8315 /* WaFbcWakeMemOn:cnl */
8316 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
8317 DISP_FBC_MEMORY_WAKE);
8318
8319 /* WaSarbUnitClockGatingDisable:cnl (pre-prod) */
8320 if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_B0))
8321 I915_WRITE(SLICE_UNIT_LEVEL_CLKGATE,
8322 I915_READ(SLICE_UNIT_LEVEL_CLKGATE) |
8323 SARBUNIT_CLKGATE_DIS);
Rodrigo Vivi8f067832017-09-05 12:30:13 -07008324
8325 /* Display WA #1133: WaFbcSkipSegments:cnl */
8326 val = I915_READ(ILK_DPFC_CHICKEN);
8327 val &= ~GLK_SKIP_SEG_COUNT_MASK;
8328 val |= GLK_SKIP_SEG_EN | GLK_SKIP_SEG_COUNT(1);
8329 I915_WRITE(ILK_DPFC_CHICKEN, val);
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008330}
8331
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008332static void cfl_init_clock_gating(struct drm_i915_private *dev_priv)
8333{
8334 cnp_init_clock_gating(dev_priv);
8335 gen9_init_clock_gating(dev_priv);
8336
8337 /* WaFbcNukeOnHostModify:cfl */
8338 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8339 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
8340}
8341
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008342static void kbl_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008343{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008344 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008345
8346 /* WaDisableSDEUnitClockGating:kbl */
8347 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
8348 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8349 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03008350
8351 /* WaDisableGamClockGating:kbl */
8352 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
8353 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
8354 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008355
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008356 /* WaFbcNukeOnHostModify:kbl */
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008357 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8358 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008359}
8360
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008361static void skl_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008362{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008363 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03008364
8365 /* WAC6entrylatency:skl */
8366 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
8367 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008368
8369 /* WaFbcNukeOnHostModify:skl */
8370 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8371 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008372}
8373
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008374static void bdw_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008375{
Damien Lespiau07d27e22014-03-03 17:31:46 +00008376 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008377
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008378 ilk_init_lp_watermarks(dev_priv);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07008379
Ben Widawskyab57fff2013-12-12 15:28:04 -08008380 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07008381 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008382
Ben Widawskyab57fff2013-12-12 15:28:04 -08008383 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008384 I915_WRITE(CHICKEN_PAR1_1,
8385 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
8386
Ben Widawskyab57fff2013-12-12 15:28:04 -08008387 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01008388 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00008389 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02008390 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02008391 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008392 }
Ben Widawsky63801f22013-12-12 17:26:03 -08008393
Ben Widawskyab57fff2013-12-12 15:28:04 -08008394 /* WaVSRefCountFullforceMissDisable:bdw */
8395 /* WaDSRefCountFullforceMissDisable:bdw */
8396 I915_WRITE(GEN7_FF_THREAD_MODE,
8397 I915_READ(GEN7_FF_THREAD_MODE) &
8398 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02008399
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02008400 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
8401 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02008402
8403 /* WaDisableSDEUnitClockGating:bdw */
8404 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8405 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00008406
Imre Deak450174f2016-05-03 15:54:21 +03008407 /* WaProgramL3SqcReg1Default:bdw */
8408 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03008409
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008410 /*
8411 * WaGttCachingOffByDefault:bdw
8412 * GTT cache may not work with big pages, so if those
8413 * are ever enabled GTT cache may need to be disabled.
8414 */
8415 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
8416
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03008417 /* WaKVMNotificationOnConfigChange:bdw */
8418 I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
8419 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
8420
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008421 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00008422
8423 /* WaDisableDopClockGating:bdw
8424 *
8425 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
8426 * clock gating.
8427 */
8428 I915_WRITE(GEN6_UCGCTL1,
8429 I915_READ(GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008430}
8431
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008432static void hsw_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008433{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008434 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008435
Francisco Jerezf3fc4882013-10-02 15:53:16 -07008436 /* L3 caching of data atomics doesn't work -- disable it. */
8437 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
8438 I915_WRITE(HSW_ROW_CHICKEN3,
8439 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
8440
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008441 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008442 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8443 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8444 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8445
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02008446 /* WaVSRefCountFullforceMissDisable:hsw */
8447 I915_WRITE(GEN7_FF_THREAD_MODE,
8448 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008449
Akash Goel4e046322014-04-04 17:14:38 +05308450 /* WaDisable_RenderCache_OperationalFlush:hsw */
8451 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8452
Chia-I Wufe27c602014-01-28 13:29:33 +08008453 /* enable HiZ Raw Stall Optimization */
8454 I915_WRITE(CACHE_MODE_0_GEN7,
8455 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
8456
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008457 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008458 I915_WRITE(CACHE_MODE_1,
8459 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03008460
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008461 /*
8462 * BSpec recommends 8x4 when MSAA is used,
8463 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008464 *
8465 * Note that PS/WM thread counts depend on the WIZ hashing
8466 * disable bit, which we don't touch here, but it's good
8467 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008468 */
8469 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008470 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008471
Kenneth Graunke94411592014-12-31 16:23:00 -08008472 /* WaSampleCChickenBitEnable:hsw */
8473 I915_WRITE(HALF_SLICE_CHICKEN3,
8474 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
8475
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008476 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07008477 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
8478
Paulo Zanoni90a88642013-05-03 17:23:45 -03008479 /* WaRsPkgCStateDisplayPMReq:hsw */
8480 I915_WRITE(CHICKEN_PAR1_1,
8481 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03008482
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008483 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008484}
8485
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008486static void ivb_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008487{
Ben Widawsky20848222012-05-04 18:58:59 -07008488 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008489
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008490 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008491
Damien Lespiau231e54f2012-10-19 17:55:41 +01008492 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008493
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008494 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05008495 I915_WRITE(_3D_CHICKEN3,
8496 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
8497
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008498 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008499 I915_WRITE(IVB_CHICKEN3,
8500 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8501 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8502
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008503 /* WaDisablePSDDualDispatchEnable:ivb */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008504 if (IS_IVB_GT1(dev_priv))
Jesse Barnes12f33822012-10-25 12:15:45 -07008505 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
8506 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07008507
Akash Goel4e046322014-04-04 17:14:38 +05308508 /* WaDisable_RenderCache_OperationalFlush:ivb */
8509 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8510
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008511 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008512 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
8513 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
8514
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008515 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008516 I915_WRITE(GEN7_L3CNTLREG1,
8517 GEN7_WA_FOR_GEN7_L3_CONTROL);
8518 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07008519 GEN7_WA_L3_CHICKEN_MODE);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008520 if (IS_IVB_GT1(dev_priv))
Jesse Barnes8ab43972012-10-25 12:15:42 -07008521 I915_WRITE(GEN7_ROW_CHICKEN2,
8522 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02008523 else {
8524 /* must write both registers */
8525 I915_WRITE(GEN7_ROW_CHICKEN2,
8526 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07008527 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
8528 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02008529 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008530
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008531 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05008532 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
8533 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
8534
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02008535 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07008536 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008537 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008538 */
8539 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02008540 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07008541
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008542 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008543 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8544 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8545 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8546
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008547 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008548
8549 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02008550
Chris Wilson22721342014-03-04 09:41:43 +00008551 if (0) { /* causes HiZ corruption on ivb:gt1 */
8552 /* enable HiZ Raw Stall Optimization */
8553 I915_WRITE(CACHE_MODE_0_GEN7,
8554 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
8555 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08008556
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008557 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02008558 I915_WRITE(CACHE_MODE_1,
8559 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07008560
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008561 /*
8562 * BSpec recommends 8x4 when MSAA is used,
8563 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008564 *
8565 * Note that PS/WM thread counts depend on the WIZ hashing
8566 * disable bit, which we don't touch here, but it's good
8567 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008568 */
8569 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008570 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008571
Ben Widawsky20848222012-05-04 18:58:59 -07008572 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
8573 snpcr &= ~GEN6_MBC_SNPCR_MASK;
8574 snpcr |= GEN6_MBC_SNPCR_MED;
8575 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008576
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008577 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008578 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008579
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008580 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008581}
8582
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008583static void vlv_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008584{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008585 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05008586 I915_WRITE(_3D_CHICKEN3,
8587 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
8588
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008589 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008590 I915_WRITE(IVB_CHICKEN3,
8591 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8592 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8593
Ville Syrjäläfad7d362014-01-22 21:32:39 +02008594 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008595 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07008596 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08008597 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
8598 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07008599
Akash Goel4e046322014-04-04 17:14:38 +05308600 /* WaDisable_RenderCache_OperationalFlush:vlv */
8601 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8602
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008603 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05008604 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
8605 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
8606
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008607 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07008608 I915_WRITE(GEN7_ROW_CHICKEN2,
8609 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
8610
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008611 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008612 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8613 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8614 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8615
Ville Syrjälä46680e02014-01-22 21:33:01 +02008616 gen7_setup_fixed_func_scheduler(dev_priv);
8617
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02008618 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07008619 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008620 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008621 */
8622 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02008623 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07008624
Akash Goelc98f5062014-03-24 23:00:07 +05308625 /* WaDisableL3Bank2xClockGate:vlv
8626 * Disabling L3 clock gating- MMIO 940c[25] = 1
8627 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
8628 I915_WRITE(GEN7_UCGCTL4,
8629 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07008630
Ville Syrjäläafd58e72014-01-22 21:33:03 +02008631 /*
8632 * BSpec says this must be set, even though
8633 * WaDisable4x2SubspanOptimization isn't listed for VLV.
8634 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02008635 I915_WRITE(CACHE_MODE_1,
8636 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07008637
8638 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02008639 * BSpec recommends 8x4 when MSAA is used,
8640 * however in practice 16x4 seems fastest.
8641 *
8642 * Note that PS/WM thread counts depend on the WIZ hashing
8643 * disable bit, which we don't touch here, but it's good
8644 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
8645 */
8646 I915_WRITE(GEN7_GT_MODE,
8647 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
8648
8649 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02008650 * WaIncreaseL3CreditsForVLVB0:vlv
8651 * This is the hardware default actually.
8652 */
8653 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
8654
8655 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008656 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07008657 * Disable clock gating on th GCFG unit to prevent a delay
8658 * in the reporting of vblank events.
8659 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02008660 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008661}
8662
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008663static void chv_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03008664{
Ville Syrjälä232ce332014-04-09 13:28:35 +03008665 /* WaVSRefCountFullforceMissDisable:chv */
8666 /* WaDSRefCountFullforceMissDisable:chv */
8667 I915_WRITE(GEN7_FF_THREAD_MODE,
8668 I915_READ(GEN7_FF_THREAD_MODE) &
8669 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03008670
8671 /* WaDisableSemaphoreAndSyncFlipWait:chv */
8672 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
8673 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03008674
8675 /* WaDisableCSUnitClockGating:chv */
8676 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
8677 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03008678
8679 /* WaDisableSDEUnitClockGating:chv */
8680 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8681 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008682
8683 /*
Imre Deak450174f2016-05-03 15:54:21 +03008684 * WaProgramL3SqcReg1Default:chv
8685 * See gfxspecs/Related Documents/Performance Guide/
8686 * LSQC Setting Recommendations.
8687 */
8688 gen8_set_l3sqc_credits(dev_priv, 38, 2);
8689
8690 /*
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008691 * GTT cache may not work with big pages, so if those
8692 * are ever enabled GTT cache may need to be disabled.
8693 */
8694 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03008695}
8696
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008697static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008698{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008699 uint32_t dspclk_gate;
8700
8701 I915_WRITE(RENCLK_GATE_D1, 0);
8702 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
8703 GS_UNIT_CLOCK_GATE_DISABLE |
8704 CL_UNIT_CLOCK_GATE_DISABLE);
8705 I915_WRITE(RAMCLK_GATE_D, 0);
8706 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
8707 OVRUNIT_CLOCK_GATE_DISABLE |
8708 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008709 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008710 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
8711 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02008712
8713 /* WaDisableRenderCachePipelinedFlush */
8714 I915_WRITE(CACHE_MODE_0,
8715 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03008716
Akash Goel4e046322014-04-04 17:14:38 +05308717 /* WaDisable_RenderCache_OperationalFlush:g4x */
8718 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8719
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008720 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008721}
8722
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008723static void i965gm_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008724{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008725 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
8726 I915_WRITE(RENCLK_GATE_D2, 0);
8727 I915_WRITE(DSPCLK_GATE_D, 0);
8728 I915_WRITE(RAMCLK_GATE_D, 0);
8729 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03008730 I915_WRITE(MI_ARB_STATE,
8731 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05308732
8733 /* WaDisable_RenderCache_OperationalFlush:gen4 */
8734 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008735}
8736
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008737static void i965g_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008738{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008739 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
8740 I965_RCC_CLOCK_GATE_DISABLE |
8741 I965_RCPB_CLOCK_GATE_DISABLE |
8742 I965_ISC_CLOCK_GATE_DISABLE |
8743 I965_FBC_CLOCK_GATE_DISABLE);
8744 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03008745 I915_WRITE(MI_ARB_STATE,
8746 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05308747
8748 /* WaDisable_RenderCache_OperationalFlush:gen4 */
8749 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008750}
8751
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008752static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008753{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008754 u32 dstate = I915_READ(D_STATE);
8755
8756 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
8757 DSTATE_DOT_CLOCK_GATING;
8758 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01008759
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008760 if (IS_PINEVIEW(dev_priv))
Chris Wilson13a86b82012-04-24 14:51:43 +01008761 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02008762
8763 /* IIR "flip pending" means done if this bit is set */
8764 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02008765
8766 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02008767 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02008768
8769 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
8770 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03008771
8772 I915_WRITE(MI_ARB_STATE,
8773 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008774}
8775
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008776static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008777{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008778 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02008779
8780 /* interrupts should cause a wake up from C3 */
8781 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
8782 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03008783
8784 I915_WRITE(MEM_MODE,
8785 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008786}
8787
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008788static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008789{
Ville Syrjälä10383922014-08-15 01:21:54 +03008790 I915_WRITE(MEM_MODE,
8791 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
8792 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008793}
8794
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008795void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008796{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008797 dev_priv->display.init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008798}
8799
Ville Syrjälä712bf362016-10-31 22:37:23 +02008800void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03008801{
Ville Syrjälä712bf362016-10-31 22:37:23 +02008802 if (HAS_PCH_LPT(dev_priv))
8803 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03008804}
8805
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008806static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02008807{
8808 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
8809}
8810
8811/**
8812 * intel_init_clock_gating_hooks - setup the clock gating hooks
8813 * @dev_priv: device private
8814 *
8815 * Setup the hooks that configure which clocks of a given platform can be
8816 * gated and also apply various GT and display specific workarounds for these
8817 * platforms. Note that some GT specific workarounds are applied separately
8818 * when GPU contexts or batchbuffers start their execution.
8819 */
8820void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
8821{
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008822 if (IS_CANNONLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008823 dev_priv->display.init_clock_gating = cnl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008824 else if (IS_COFFEELAKE(dev_priv))
8825 dev_priv->display.init_clock_gating = cfl_init_clock_gating;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008826 else if (IS_SKYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008827 dev_priv->display.init_clock_gating = skl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008828 else if (IS_KABYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008829 dev_priv->display.init_clock_gating = kbl_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02008830 else if (IS_BROXTON(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02008831 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02008832 else if (IS_GEMINILAKE(dev_priv))
8833 dev_priv->display.init_clock_gating = glk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008834 else if (IS_BROADWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008835 dev_priv->display.init_clock_gating = bdw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008836 else if (IS_CHERRYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008837 dev_priv->display.init_clock_gating = chv_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008838 else if (IS_HASWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008839 dev_priv->display.init_clock_gating = hsw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008840 else if (IS_IVYBRIDGE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008841 dev_priv->display.init_clock_gating = ivb_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008842 else if (IS_VALLEYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008843 dev_priv->display.init_clock_gating = vlv_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008844 else if (IS_GEN6(dev_priv))
8845 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
8846 else if (IS_GEN5(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008847 dev_priv->display.init_clock_gating = ilk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008848 else if (IS_G4X(dev_priv))
8849 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02008850 else if (IS_I965GM(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008851 dev_priv->display.init_clock_gating = i965gm_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02008852 else if (IS_I965G(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008853 dev_priv->display.init_clock_gating = i965g_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008854 else if (IS_GEN3(dev_priv))
8855 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
8856 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
8857 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
8858 else if (IS_GEN2(dev_priv))
8859 dev_priv->display.init_clock_gating = i830_init_clock_gating;
8860 else {
8861 MISSING_CASE(INTEL_DEVID(dev_priv));
8862 dev_priv->display.init_clock_gating = nop_init_clock_gating;
8863 }
8864}
8865
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008866/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008867void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008868{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02008869 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008870
Daniel Vetterc921aba2012-04-26 23:28:17 +02008871 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008872 if (IS_PINEVIEW(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02008873 i915_pineview_get_mem_freq(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008874 else if (IS_GEN5(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02008875 i915_ironlake_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02008876
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008877 /* For FIFO watermark updates */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008878 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008879 skl_setup_wm_latency(dev_priv);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01008880 dev_priv->display.initial_watermarks = skl_initial_wm;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01008881 dev_priv->display.atomic_update_watermarks = skl_atomic_update_crtc_wm;
Matt Roper98d39492016-05-12 07:06:03 -07008882 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008883 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008884 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03008885
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008886 if ((IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008887 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008888 (!IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008889 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07008890 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08008891 dev_priv->display.compute_intermediate_wm =
8892 ilk_compute_intermediate_wm;
8893 dev_priv->display.initial_watermarks =
8894 ilk_initial_watermarks;
8895 dev_priv->display.optimize_watermarks =
8896 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02008897 } else {
8898 DRM_DEBUG_KMS("Failed to read display plane latency. "
8899 "Disable CxSR\n");
8900 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02008901 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008902 vlv_setup_wm_latency(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02008903 dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
Ville Syrjälä4841da52017-03-02 19:14:59 +02008904 dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02008905 dev_priv->display.initial_watermarks = vlv_initial_watermarks;
Ville Syrjälä4841da52017-03-02 19:14:59 +02008906 dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
Ville Syrjäläff32c542017-03-02 19:14:57 +02008907 dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03008908 } else if (IS_G4X(dev_priv)) {
8909 g4x_setup_wm_latency(dev_priv);
8910 dev_priv->display.compute_pipe_wm = g4x_compute_pipe_wm;
8911 dev_priv->display.compute_intermediate_wm = g4x_compute_intermediate_wm;
8912 dev_priv->display.initial_watermarks = g4x_initial_watermarks;
8913 dev_priv->display.optimize_watermarks = g4x_optimize_watermarks;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008914 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008915 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008916 dev_priv->is_ddr3,
8917 dev_priv->fsb_freq,
8918 dev_priv->mem_freq)) {
8919 DRM_INFO("failed to find known CxSR latency "
8920 "(found ddr%s fsb freq %d, mem freq %d), "
8921 "disabling CxSR\n",
8922 (dev_priv->is_ddr3 == 1) ? "3" : "2",
8923 dev_priv->fsb_freq, dev_priv->mem_freq);
8924 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03008925 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008926 dev_priv->display.update_wm = NULL;
8927 } else
8928 dev_priv->display.update_wm = pineview_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008929 } else if (IS_GEN4(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008930 dev_priv->display.update_wm = i965_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008931 } else if (IS_GEN3(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008932 dev_priv->display.update_wm = i9xx_update_wm;
8933 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008934 } else if (IS_GEN2(dev_priv)) {
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008935 if (INTEL_INFO(dev_priv)->num_pipes == 1) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008936 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008937 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008938 } else {
8939 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008940 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008941 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008942 } else {
8943 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008944 }
8945}
8946
Lyude87660502016-08-17 15:55:53 -04008947static inline int gen6_check_mailbox_status(struct drm_i915_private *dev_priv)
8948{
8949 uint32_t flags =
8950 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
8951
8952 switch (flags) {
8953 case GEN6_PCODE_SUCCESS:
8954 return 0;
8955 case GEN6_PCODE_UNIMPLEMENTED_CMD:
Chris Wilson5a9cfff2017-07-28 09:50:22 +01008956 return -ENODEV;
Lyude87660502016-08-17 15:55:53 -04008957 case GEN6_PCODE_ILLEGAL_CMD:
8958 return -ENXIO;
8959 case GEN6_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Chris Wilson7850d1c2016-08-26 11:59:26 +01008960 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Lyude87660502016-08-17 15:55:53 -04008961 return -EOVERFLOW;
8962 case GEN6_PCODE_TIMEOUT:
8963 return -ETIMEDOUT;
8964 default:
Michal Wajdeczkof0d66152017-03-28 08:45:12 +00008965 MISSING_CASE(flags);
Lyude87660502016-08-17 15:55:53 -04008966 return 0;
8967 }
8968}
8969
8970static inline int gen7_check_mailbox_status(struct drm_i915_private *dev_priv)
8971{
8972 uint32_t flags =
8973 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
8974
8975 switch (flags) {
8976 case GEN6_PCODE_SUCCESS:
8977 return 0;
8978 case GEN6_PCODE_ILLEGAL_CMD:
8979 return -ENXIO;
8980 case GEN7_PCODE_TIMEOUT:
8981 return -ETIMEDOUT;
8982 case GEN7_PCODE_ILLEGAL_DATA:
8983 return -EINVAL;
8984 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
8985 return -EOVERFLOW;
8986 default:
8987 MISSING_CASE(flags);
8988 return 0;
8989 }
8990}
8991
Tom O'Rourke151a49d2014-11-13 18:50:10 -08008992int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07008993{
Lyude87660502016-08-17 15:55:53 -04008994 int status;
8995
Jesse Barnes4fc688c2012-11-02 11:14:01 -07008996 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07008997
Chris Wilson3f5582d2016-06-30 15:32:45 +01008998 /* GEN6_PCODE_* are outside of the forcewake domain, we can
8999 * use te fw I915_READ variants to reduce the amount of work
9000 * required when reading/writing.
9001 */
9002
9003 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009004 DRM_DEBUG_DRIVER("warning: pcode (read from mbox %x) mailbox access failed for %ps\n",
9005 mbox, __builtin_return_address(0));
Ben Widawsky42c05262012-09-26 10:34:00 -07009006 return -EAGAIN;
9007 }
9008
Chris Wilson3f5582d2016-06-30 15:32:45 +01009009 I915_WRITE_FW(GEN6_PCODE_DATA, *val);
9010 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
9011 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07009012
Chris Wilsone09a3032017-04-11 11:13:39 +01009013 if (__intel_wait_for_register_fw(dev_priv,
9014 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
9015 500, 0, NULL)) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009016 DRM_ERROR("timeout waiting for pcode read (from mbox %x) to finish for %ps\n",
9017 mbox, __builtin_return_address(0));
Ben Widawsky42c05262012-09-26 10:34:00 -07009018 return -ETIMEDOUT;
9019 }
9020
Chris Wilson3f5582d2016-06-30 15:32:45 +01009021 *val = I915_READ_FW(GEN6_PCODE_DATA);
9022 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07009023
Lyude87660502016-08-17 15:55:53 -04009024 if (INTEL_GEN(dev_priv) > 6)
9025 status = gen7_check_mailbox_status(dev_priv);
9026 else
9027 status = gen6_check_mailbox_status(dev_priv);
9028
9029 if (status) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009030 DRM_DEBUG_DRIVER("warning: pcode (read from mbox %x) mailbox access failed for %ps: %d\n",
9031 mbox, __builtin_return_address(0), status);
Lyude87660502016-08-17 15:55:53 -04009032 return status;
9033 }
9034
Ben Widawsky42c05262012-09-26 10:34:00 -07009035 return 0;
9036}
9037
Chris Wilson3f5582d2016-06-30 15:32:45 +01009038int sandybridge_pcode_write(struct drm_i915_private *dev_priv,
Lyude87660502016-08-17 15:55:53 -04009039 u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07009040{
Lyude87660502016-08-17 15:55:53 -04009041 int status;
9042
Jesse Barnes4fc688c2012-11-02 11:14:01 -07009043 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07009044
Chris Wilson3f5582d2016-06-30 15:32:45 +01009045 /* GEN6_PCODE_* are outside of the forcewake domain, we can
9046 * use te fw I915_READ variants to reduce the amount of work
9047 * required when reading/writing.
9048 */
9049
9050 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009051 DRM_DEBUG_DRIVER("warning: pcode (write of 0x%08x to mbox %x) mailbox access failed for %ps\n",
9052 val, mbox, __builtin_return_address(0));
Ben Widawsky42c05262012-09-26 10:34:00 -07009053 return -EAGAIN;
9054 }
9055
Chris Wilson3f5582d2016-06-30 15:32:45 +01009056 I915_WRITE_FW(GEN6_PCODE_DATA, val);
Imre Deak8bf41b72016-11-28 17:29:27 +02009057 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
Chris Wilson3f5582d2016-06-30 15:32:45 +01009058 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07009059
Chris Wilsone09a3032017-04-11 11:13:39 +01009060 if (__intel_wait_for_register_fw(dev_priv,
9061 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
9062 500, 0, NULL)) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009063 DRM_ERROR("timeout waiting for pcode write of 0x%08x to mbox %x to finish for %ps\n",
9064 val, mbox, __builtin_return_address(0));
Ben Widawsky42c05262012-09-26 10:34:00 -07009065 return -ETIMEDOUT;
9066 }
9067
Chris Wilson3f5582d2016-06-30 15:32:45 +01009068 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07009069
Lyude87660502016-08-17 15:55:53 -04009070 if (INTEL_GEN(dev_priv) > 6)
9071 status = gen7_check_mailbox_status(dev_priv);
9072 else
9073 status = gen6_check_mailbox_status(dev_priv);
9074
9075 if (status) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009076 DRM_DEBUG_DRIVER("warning: pcode (write of 0x%08x to mbox %x) mailbox access failed for %ps: %d\n",
9077 val, mbox, __builtin_return_address(0), status);
Lyude87660502016-08-17 15:55:53 -04009078 return status;
9079 }
9080
Ben Widawsky42c05262012-09-26 10:34:00 -07009081 return 0;
9082}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07009083
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009084static bool skl_pcode_try_request(struct drm_i915_private *dev_priv, u32 mbox,
9085 u32 request, u32 reply_mask, u32 reply,
9086 u32 *status)
9087{
9088 u32 val = request;
9089
9090 *status = sandybridge_pcode_read(dev_priv, mbox, &val);
9091
9092 return *status || ((val & reply_mask) == reply);
9093}
9094
9095/**
9096 * skl_pcode_request - send PCODE request until acknowledgment
9097 * @dev_priv: device private
9098 * @mbox: PCODE mailbox ID the request is targeted for
9099 * @request: request ID
9100 * @reply_mask: mask used to check for request acknowledgment
9101 * @reply: value used to check for request acknowledgment
9102 * @timeout_base_ms: timeout for polling with preemption enabled
9103 *
9104 * Keep resending the @request to @mbox until PCODE acknowledges it, PCODE
Imre Deak01299362017-02-24 16:32:10 +02009105 * reports an error or an overall timeout of @timeout_base_ms+50 ms expires.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009106 * The request is acknowledged once the PCODE reply dword equals @reply after
9107 * applying @reply_mask. Polling is first attempted with preemption enabled
Imre Deak01299362017-02-24 16:32:10 +02009108 * for @timeout_base_ms and if this times out for another 50 ms with
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009109 * preemption disabled.
9110 *
9111 * Returns 0 on success, %-ETIMEDOUT in case of a timeout, <0 in case of some
9112 * other error as reported by PCODE.
9113 */
9114int skl_pcode_request(struct drm_i915_private *dev_priv, u32 mbox, u32 request,
9115 u32 reply_mask, u32 reply, int timeout_base_ms)
9116{
9117 u32 status;
9118 int ret;
9119
9120 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
9121
9122#define COND skl_pcode_try_request(dev_priv, mbox, request, reply_mask, reply, \
9123 &status)
9124
9125 /*
9126 * Prime the PCODE by doing a request first. Normally it guarantees
9127 * that a subsequent request, at most @timeout_base_ms later, succeeds.
9128 * _wait_for() doesn't guarantee when its passed condition is evaluated
9129 * first, so send the first request explicitly.
9130 */
9131 if (COND) {
9132 ret = 0;
9133 goto out;
9134 }
9135 ret = _wait_for(COND, timeout_base_ms * 1000, 10);
9136 if (!ret)
9137 goto out;
9138
9139 /*
9140 * The above can time out if the number of requests was low (2 in the
9141 * worst case) _and_ PCODE was busy for some reason even after a
9142 * (queued) request and @timeout_base_ms delay. As a workaround retry
9143 * the poll with preemption disabled to maximize the number of
Imre Deak01299362017-02-24 16:32:10 +02009144 * requests. Increase the timeout from @timeout_base_ms to 50ms to
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009145 * account for interrupts that could reduce the number of these
Imre Deak01299362017-02-24 16:32:10 +02009146 * requests, and for any quirks of the PCODE firmware that delays
9147 * the request completion.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009148 */
9149 DRM_DEBUG_KMS("PCODE timeout, retrying with preemption disabled\n");
9150 WARN_ON_ONCE(timeout_base_ms > 3);
9151 preempt_disable();
Imre Deak01299362017-02-24 16:32:10 +02009152 ret = wait_for_atomic(COND, 50);
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009153 preempt_enable();
9154
9155out:
9156 return ret ? ret : status;
9157#undef COND
9158}
9159
Ville Syrjälädd06f882014-11-10 22:55:12 +02009160static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
9161{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009162 /*
9163 * N = val - 0xb7
9164 * Slow = Fast = GPLL ref * N
9165 */
9166 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * (val - 0xb7), 1000);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009167}
9168
Fengguang Wub55dd642014-07-12 11:21:39 +02009169static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009170{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009171 return DIV_ROUND_CLOSEST(1000 * val, dev_priv->rps.gpll_ref_freq) + 0xb7;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009172}
9173
Fengguang Wub55dd642014-07-12 11:21:39 +02009174static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05309175{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009176 /*
9177 * N = val / 2
9178 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
9179 */
9180 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * val, 2 * 2 * 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05309181}
9182
Fengguang Wub55dd642014-07-12 11:21:39 +02009183static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05309184{
Ville Syrjälä1c147622014-08-18 14:42:43 +03009185 /* CHV needs even values */
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009186 return DIV_ROUND_CLOSEST(2 * 1000 * val, dev_priv->rps.gpll_ref_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05309187}
9188
Ville Syrjälä616bc822015-01-23 21:04:25 +02009189int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
9190{
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07009191 if (INTEL_GEN(dev_priv) >= 9)
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009192 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
9193 GEN9_FREQ_SCALER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009194 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009195 return chv_gpu_freq(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009196 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009197 return byt_gpu_freq(dev_priv, val);
9198 else
9199 return val * GT_FREQUENCY_MULTIPLIER;
9200}
9201
Ville Syrjälä616bc822015-01-23 21:04:25 +02009202int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
9203{
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07009204 if (INTEL_GEN(dev_priv) >= 9)
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009205 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
9206 GT_FREQUENCY_MULTIPLIER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009207 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009208 return chv_freq_opcode(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009209 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009210 return byt_freq_opcode(dev_priv, val);
9211 else
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009212 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
Deepak S22b1b2f2014-07-12 14:54:33 +05309213}
9214
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00009215void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01009216{
Daniel Vetterf742a552013-12-06 10:17:53 +01009217 mutex_init(&dev_priv->rps.hw_lock);
9218
Chris Wilson54b4f682016-07-21 21:16:19 +01009219 INIT_DELAYED_WORK(&dev_priv->rps.autoenable_work,
9220 __intel_autoenable_gt_powersave);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01009221 atomic_set(&dev_priv->rps.num_waiters, 0);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03009222
Paulo Zanoni33688d92014-03-07 20:08:19 -03009223 dev_priv->pm.suspended = false;
Imre Deak1f814da2015-12-16 02:52:19 +02009224 atomic_set(&dev_priv->pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01009225}
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009226
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009227static u64 vlv_residency_raw(struct drm_i915_private *dev_priv,
9228 const i915_reg_t reg)
9229{
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009230 u32 lower, upper, tmp;
Chris Wilson71cc2b12017-03-24 16:54:18 +00009231 int loop = 2;
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009232
9233 /* The register accessed do not need forcewake. We borrow
9234 * uncore lock to prevent concurrent access to range reg.
9235 */
9236 spin_lock_irq(&dev_priv->uncore.lock);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009237
9238 /* vlv and chv residency counters are 40 bits in width.
9239 * With a control bit, we can choose between upper or lower
9240 * 32bit window into this counter.
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009241 *
9242 * Although we always use the counter in high-range mode elsewhere,
9243 * userspace may attempt to read the value before rc6 is initialised,
9244 * before we have set the default VLV_COUNTER_CONTROL value. So always
9245 * set the high bit to be safe.
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009246 */
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009247 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9248 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009249 upper = I915_READ_FW(reg);
9250 do {
9251 tmp = upper;
9252
9253 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9254 _MASKED_BIT_DISABLE(VLV_COUNT_RANGE_HIGH));
9255 lower = I915_READ_FW(reg);
9256
9257 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9258 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
9259 upper = I915_READ_FW(reg);
Chris Wilson71cc2b12017-03-24 16:54:18 +00009260 } while (upper != tmp && --loop);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009261
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009262 /* Everywhere else we always use VLV_COUNTER_CONTROL with the
9263 * VLV_COUNT_RANGE_HIGH bit set - so it is safe to leave it set
9264 * now.
9265 */
9266
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009267 spin_unlock_irq(&dev_priv->uncore.lock);
9268
9269 return lower | (u64)upper << 8;
9270}
9271
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009272u64 intel_rc6_residency_us(struct drm_i915_private *dev_priv,
9273 const i915_reg_t reg)
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009274{
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009275 u64 time_hw, units, div;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009276
9277 if (!intel_enable_rc6())
9278 return 0;
9279
9280 intel_runtime_pm_get(dev_priv);
9281
9282 /* On VLV and CHV, residency time is in CZ units rather than 1.28us */
9283 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009284 units = 1000;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009285 div = dev_priv->czclk_freq;
9286
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009287 time_hw = vlv_residency_raw(dev_priv, reg);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009288 } else if (IS_GEN9_LP(dev_priv)) {
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009289 units = 1000;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009290 div = 1200; /* 833.33ns */
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009291
9292 time_hw = I915_READ(reg);
9293 } else {
9294 units = 128000; /* 1.28us */
9295 div = 100000;
9296
9297 time_hw = I915_READ(reg);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009298 }
9299
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009300 intel_runtime_pm_put(dev_priv);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009301 return DIV_ROUND_UP_ULL(time_hw * units, div);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009302}