blob: 3168a557777394168aea1acc10ff3b99bad36771 [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,
Kumar, Maheshca476672017-08-17 19:15:24 +05304633 struct skl_wm_params *wp,
4634 struct skl_wm_level *wm_l0,
4635 uint16_t ddb_allocation,
Damien Lespiau9414f562014-11-04 17:06:58 +00004636 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00004637{
Kumar, Maheshca476672017-08-17 19:15:24 +05304638 struct drm_device *dev = cstate->base.crtc->dev;
4639 const struct drm_i915_private *dev_priv = to_i915(dev);
4640 uint16_t trans_min, trans_y_tile_min;
4641 const uint16_t trans_amount = 10; /* This is configurable amount */
4642 uint16_t trans_offset_b, res_blocks;
Damien Lespiau9414f562014-11-04 17:06:58 +00004643
Kumar, Maheshca476672017-08-17 19:15:24 +05304644 if (!cstate->base.active)
4645 goto exit;
4646
4647 /* Transition WM are not recommended by HW team for GEN9 */
4648 if (INTEL_GEN(dev_priv) <= 9)
4649 goto exit;
4650
4651 /* Transition WM don't make any sense if ipc is disabled */
4652 if (!dev_priv->ipc_enabled)
4653 goto exit;
4654
4655 if (INTEL_GEN(dev_priv) >= 10)
4656 trans_min = 4;
4657
4658 trans_offset_b = trans_min + trans_amount;
4659
4660 if (wp->y_tiled) {
4661 trans_y_tile_min = (uint16_t) mul_round_up_u32_fixed16(2,
4662 wp->y_tile_minimum);
4663 res_blocks = max(wm_l0->plane_res_b, trans_y_tile_min) +
4664 trans_offset_b;
4665 } else {
4666 res_blocks = wm_l0->plane_res_b + trans_offset_b;
4667
4668 /* WA BUG:1938466 add one block for non y-tile planes */
4669 if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_A0))
4670 res_blocks += 1;
4671
4672 }
4673
4674 res_blocks += 1;
4675
4676 if (res_blocks < ddb_allocation) {
4677 trans_wm->plane_res_b = res_blocks;
4678 trans_wm->plane_en = true;
4679 return;
4680 }
4681
4682exit:
Lyudea62163e2016-10-04 14:28:20 -04004683 trans_wm->plane_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00004684}
4685
Matt Roper55994c22016-05-12 07:06:08 -07004686static int skl_build_pipe_wm(struct intel_crtc_state *cstate,
4687 struct skl_ddb_allocation *ddb,
4688 struct skl_pipe_wm *pipe_wm)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004689{
Matt Roper024c9042015-09-24 15:53:11 -07004690 struct drm_device *dev = cstate->base.crtc->dev;
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304691 struct drm_crtc_state *crtc_state = &cstate->base;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004692 const struct drm_i915_private *dev_priv = to_i915(dev);
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304693 struct drm_plane *plane;
4694 const struct drm_plane_state *pstate;
Lyudea62163e2016-10-04 14:28:20 -04004695 struct skl_plane_wm *wm;
Matt Roper55994c22016-05-12 07:06:08 -07004696 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004697
Lyudea62163e2016-10-04 14:28:20 -04004698 /*
4699 * We'll only calculate watermarks for planes that are actually
4700 * enabled, so make sure all other planes are set as disabled.
4701 */
4702 memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
4703
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304704 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, crtc_state) {
4705 const struct intel_plane_state *intel_pstate =
4706 to_intel_plane_state(pstate);
4707 enum plane_id plane_id = to_intel_plane(plane)->id;
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304708 struct skl_wm_params wm_params;
Kumar, Maheshca476672017-08-17 19:15:24 +05304709 enum pipe pipe = to_intel_crtc(cstate->base.crtc)->pipe;
4710 uint16_t ddb_blocks;
Kumar, Mahesheb2fdcd2017-05-17 17:28:27 +05304711
4712 wm = &pipe_wm->planes[plane_id];
Kumar, Maheshca476672017-08-17 19:15:24 +05304713 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][plane_id]);
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304714 memset(&wm_params, 0, sizeof(struct skl_wm_params));
4715
4716 ret = skl_compute_plane_wm_params(dev_priv, cstate,
4717 intel_pstate, &wm_params);
4718 if (ret)
4719 return ret;
Lyudea62163e2016-10-04 14:28:20 -04004720
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004721 ret = skl_compute_wm_levels(dev_priv, ddb, cstate,
Kumar, Mahesh7e452fd2017-08-17 19:15:23 +05304722 intel_pstate, &wm_params, wm);
Kumar, Maheshd2f5e362017-05-17 17:28:28 +05304723 if (ret)
4724 return ret;
Kumar, Maheshca476672017-08-17 19:15:24 +05304725 skl_compute_transition_wm(cstate, &wm_params, &wm->wm[0],
4726 ddb_blocks, &wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004727 }
Matt Roper024c9042015-09-24 15:53:11 -07004728 pipe_wm->linetime = skl_compute_linetime_wm(cstate);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004729
Matt Roper55994c22016-05-12 07:06:08 -07004730 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004731}
4732
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004733static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
4734 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00004735 const struct skl_ddb_entry *entry)
4736{
4737 if (entry->end)
4738 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
4739 else
4740 I915_WRITE(reg, 0);
4741}
4742
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004743static void skl_write_wm_level(struct drm_i915_private *dev_priv,
4744 i915_reg_t reg,
4745 const struct skl_wm_level *level)
4746{
4747 uint32_t val = 0;
4748
4749 if (level->plane_en) {
4750 val |= PLANE_WM_EN;
4751 val |= level->plane_res_b;
4752 val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
4753 }
4754
4755 I915_WRITE(reg, val);
4756}
4757
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004758static void skl_write_plane_wm(struct intel_crtc *intel_crtc,
4759 const struct skl_plane_wm *wm,
4760 const struct skl_ddb_allocation *ddb,
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004761 enum plane_id plane_id)
Lyude62e0fb82016-08-22 12:50:08 -04004762{
4763 struct drm_crtc *crtc = &intel_crtc->base;
4764 struct drm_device *dev = crtc->dev;
4765 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004766 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004767 enum pipe pipe = intel_crtc->pipe;
4768
4769 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004770 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004771 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004772 }
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004773 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004774 &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004775
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004776 skl_ddb_entry_write(dev_priv, PLANE_BUF_CFG(pipe, plane_id),
4777 &ddb->plane[pipe][plane_id]);
4778 skl_ddb_entry_write(dev_priv, PLANE_NV12_BUF_CFG(pipe, plane_id),
4779 &ddb->y_plane[pipe][plane_id]);
Lyude62e0fb82016-08-22 12:50:08 -04004780}
4781
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004782static void skl_write_cursor_wm(struct intel_crtc *intel_crtc,
4783 const struct skl_plane_wm *wm,
4784 const struct skl_ddb_allocation *ddb)
Lyude62e0fb82016-08-22 12:50:08 -04004785{
4786 struct drm_crtc *crtc = &intel_crtc->base;
4787 struct drm_device *dev = crtc->dev;
4788 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004789 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004790 enum pipe pipe = intel_crtc->pipe;
4791
4792 for (level = 0; level <= max_level; level++) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004793 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
4794 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004795 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004796 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004797
4798 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004799 &ddb->plane[pipe][PLANE_CURSOR]);
Lyude62e0fb82016-08-22 12:50:08 -04004800}
4801
cpaul@redhat.com45ece232016-10-14 17:31:56 -04004802bool skl_wm_level_equals(const struct skl_wm_level *l1,
4803 const struct skl_wm_level *l2)
4804{
4805 if (l1->plane_en != l2->plane_en)
4806 return false;
4807
4808 /* If both planes aren't enabled, the rest shouldn't matter */
4809 if (!l1->plane_en)
4810 return true;
4811
4812 return (l1->plane_res_l == l2->plane_res_l &&
4813 l1->plane_res_b == l2->plane_res_b);
4814}
4815
Lyude27082492016-08-24 07:48:10 +02004816static inline bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
4817 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004818{
Lyude27082492016-08-24 07:48:10 +02004819 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004820}
4821
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01004822bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry **entries,
4823 const struct skl_ddb_entry *ddb,
4824 int ignore)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004825{
Lyudece0ba282016-09-15 10:46:35 -04004826 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004827
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01004828 for (i = 0; i < I915_MAX_PIPES; i++)
4829 if (i != ignore && entries[i] &&
4830 skl_ddb_entries_overlap(ddb, entries[i]))
Lyude27082492016-08-24 07:48:10 +02004831 return true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004832
Lyude27082492016-08-24 07:48:10 +02004833 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004834}
4835
Matt Roper55994c22016-05-12 07:06:08 -07004836static int skl_update_pipe_wm(struct drm_crtc_state *cstate,
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004837 const struct skl_pipe_wm *old_pipe_wm,
Matt Roper55994c22016-05-12 07:06:08 -07004838 struct skl_pipe_wm *pipe_wm, /* out */
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004839 struct skl_ddb_allocation *ddb, /* out */
Matt Roper55994c22016-05-12 07:06:08 -07004840 bool *changed /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004841{
Matt Roperf4a96752016-05-12 07:06:06 -07004842 struct intel_crtc_state *intel_cstate = to_intel_crtc_state(cstate);
Matt Roper55994c22016-05-12 07:06:08 -07004843 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004844
Matt Roper55994c22016-05-12 07:06:08 -07004845 ret = skl_build_pipe_wm(intel_cstate, ddb, pipe_wm);
4846 if (ret)
4847 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004848
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004849 if (!memcmp(old_pipe_wm, pipe_wm, sizeof(*pipe_wm)))
Matt Roper55994c22016-05-12 07:06:08 -07004850 *changed = false;
4851 else
4852 *changed = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004853
Matt Roper55994c22016-05-12 07:06:08 -07004854 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004855}
4856
Matt Roper9b613022016-06-27 16:42:44 -07004857static uint32_t
4858pipes_modified(struct drm_atomic_state *state)
4859{
4860 struct drm_crtc *crtc;
4861 struct drm_crtc_state *cstate;
4862 uint32_t i, ret = 0;
4863
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004864 for_each_new_crtc_in_state(state, crtc, cstate, i)
Matt Roper9b613022016-06-27 16:42:44 -07004865 ret |= drm_crtc_mask(crtc);
4866
4867 return ret;
4868}
4869
Jani Nikulabb7791b2016-10-04 12:29:17 +03004870static int
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004871skl_ddb_add_affected_planes(struct intel_crtc_state *cstate)
4872{
4873 struct drm_atomic_state *state = cstate->base.state;
4874 struct drm_device *dev = state->dev;
4875 struct drm_crtc *crtc = cstate->base.crtc;
4876 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4877 struct drm_i915_private *dev_priv = to_i915(dev);
4878 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4879 struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
4880 struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
4881 struct drm_plane_state *plane_state;
4882 struct drm_plane *plane;
4883 enum pipe pipe = intel_crtc->pipe;
4884
4885 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
4886
4887 drm_for_each_plane_mask(plane, dev, cstate->base.plane_mask) {
4888 enum plane_id plane_id = to_intel_plane(plane)->id;
4889
4890 if (skl_ddb_entry_equal(&cur_ddb->plane[pipe][plane_id],
4891 &new_ddb->plane[pipe][plane_id]) &&
4892 skl_ddb_entry_equal(&cur_ddb->y_plane[pipe][plane_id],
4893 &new_ddb->y_plane[pipe][plane_id]))
4894 continue;
4895
4896 plane_state = drm_atomic_get_plane_state(state, plane);
4897 if (IS_ERR(plane_state))
4898 return PTR_ERR(plane_state);
4899 }
4900
4901 return 0;
4902}
4903
4904static int
4905skl_compute_ddb(struct drm_atomic_state *state)
Matt Roper98d39492016-05-12 07:06:03 -07004906{
4907 struct drm_device *dev = state->dev;
4908 struct drm_i915_private *dev_priv = to_i915(dev);
4909 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4910 struct intel_crtc *intel_crtc;
Matt Roper734fa012016-05-12 15:11:40 -07004911 struct skl_ddb_allocation *ddb = &intel_state->wm_results.ddb;
Matt Roper9b613022016-06-27 16:42:44 -07004912 uint32_t realloc_pipes = pipes_modified(state);
Matt Roper98d39492016-05-12 07:06:03 -07004913 int ret;
4914
4915 /*
4916 * If this is our first atomic update following hardware readout,
4917 * we can't trust the DDB that the BIOS programmed for us. Let's
4918 * pretend that all pipes switched active status so that we'll
4919 * ensure a full DDB recompute.
4920 */
Matt Roper1b54a882016-06-17 13:42:18 -07004921 if (dev_priv->wm.distrust_bios_wm) {
4922 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
4923 state->acquire_ctx);
4924 if (ret)
4925 return ret;
4926
Matt Roper98d39492016-05-12 07:06:03 -07004927 intel_state->active_pipe_changes = ~0;
4928
Matt Roper1b54a882016-06-17 13:42:18 -07004929 /*
4930 * We usually only initialize intel_state->active_crtcs if we
4931 * we're doing a modeset; make sure this field is always
4932 * initialized during the sanitization process that happens
4933 * on the first commit too.
4934 */
4935 if (!intel_state->modeset)
4936 intel_state->active_crtcs = dev_priv->active_crtcs;
4937 }
4938
Matt Roper98d39492016-05-12 07:06:03 -07004939 /*
4940 * If the modeset changes which CRTC's are active, we need to
4941 * recompute the DDB allocation for *all* active pipes, even
4942 * those that weren't otherwise being modified in any way by this
4943 * atomic commit. Due to the shrinking of the per-pipe allocations
4944 * when new active CRTC's are added, it's possible for a pipe that
4945 * we were already using and aren't changing at all here to suddenly
4946 * become invalid if its DDB needs exceeds its new allocation.
4947 *
4948 * Note that if we wind up doing a full DDB recompute, we can't let
4949 * any other display updates race with this transaction, so we need
4950 * to grab the lock on *all* CRTC's.
4951 */
Matt Roper734fa012016-05-12 15:11:40 -07004952 if (intel_state->active_pipe_changes) {
Matt Roper98d39492016-05-12 07:06:03 -07004953 realloc_pipes = ~0;
Matt Roper734fa012016-05-12 15:11:40 -07004954 intel_state->wm_results.dirty_pipes = ~0;
4955 }
Matt Roper98d39492016-05-12 07:06:03 -07004956
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004957 /*
4958 * We're not recomputing for the pipes not included in the commit, so
4959 * make sure we start with the current state.
4960 */
4961 memcpy(ddb, &dev_priv->wm.skl_hw.ddb, sizeof(*ddb));
4962
Matt Roper98d39492016-05-12 07:06:03 -07004963 for_each_intel_crtc_mask(dev, intel_crtc, realloc_pipes) {
4964 struct intel_crtc_state *cstate;
4965
4966 cstate = intel_atomic_get_crtc_state(state, intel_crtc);
4967 if (IS_ERR(cstate))
4968 return PTR_ERR(cstate);
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07004969
4970 ret = skl_allocate_pipe_ddb(cstate, ddb);
4971 if (ret)
4972 return ret;
4973
4974 ret = skl_ddb_add_affected_planes(cstate);
4975 if (ret)
4976 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07004977 }
4978
4979 return 0;
4980}
4981
Matt Roper2722efb2016-08-17 15:55:55 -04004982static void
4983skl_copy_wm_for_pipe(struct skl_wm_values *dst,
4984 struct skl_wm_values *src,
4985 enum pipe pipe)
4986{
Matt Roper2722efb2016-08-17 15:55:55 -04004987 memcpy(dst->ddb.y_plane[pipe], src->ddb.y_plane[pipe],
4988 sizeof(dst->ddb.y_plane[pipe]));
4989 memcpy(dst->ddb.plane[pipe], src->ddb.plane[pipe],
4990 sizeof(dst->ddb.plane[pipe]));
4991}
4992
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004993static void
4994skl_print_wm_changes(const struct drm_atomic_state *state)
4995{
4996 const struct drm_device *dev = state->dev;
4997 const struct drm_i915_private *dev_priv = to_i915(dev);
4998 const struct intel_atomic_state *intel_state =
4999 to_intel_atomic_state(state);
5000 const struct drm_crtc *crtc;
5001 const struct drm_crtc_state *cstate;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005002 const struct intel_plane *intel_plane;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005003 const struct skl_ddb_allocation *old_ddb = &dev_priv->wm.skl_hw.ddb;
5004 const struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
Maarten Lankhorst75704982016-11-01 12:04:10 +01005005 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005006
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01005007 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst75704982016-11-01 12:04:10 +01005008 const struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5009 enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005010
Maarten Lankhorst75704982016-11-01 12:04:10 +01005011 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005012 enum plane_id plane_id = intel_plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005013 const struct skl_ddb_entry *old, *new;
5014
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005015 old = &old_ddb->plane[pipe][plane_id];
5016 new = &new_ddb->plane[pipe][plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005017
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005018 if (skl_ddb_entry_equal(old, new))
5019 continue;
5020
Maarten Lankhorst75704982016-11-01 12:04:10 +01005021 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] ddb (%d - %d) -> (%d - %d)\n",
5022 intel_plane->base.base.id,
5023 intel_plane->base.name,
5024 old->start, old->end,
5025 new->start, new->end);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005026 }
5027 }
5028}
5029
Matt Roper98d39492016-05-12 07:06:03 -07005030static int
5031skl_compute_wm(struct drm_atomic_state *state)
5032{
5033 struct drm_crtc *crtc;
5034 struct drm_crtc_state *cstate;
Matt Roper734fa012016-05-12 15:11:40 -07005035 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
5036 struct skl_wm_values *results = &intel_state->wm_results;
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02005037 struct drm_device *dev = state->dev;
Matt Roper734fa012016-05-12 15:11:40 -07005038 struct skl_pipe_wm *pipe_wm;
Matt Roper98d39492016-05-12 07:06:03 -07005039 bool changed = false;
Matt Roper734fa012016-05-12 15:11:40 -07005040 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07005041
5042 /*
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02005043 * When we distrust bios wm we always need to recompute to set the
5044 * expected DDB allocations for each CRTC.
5045 */
5046 if (to_i915(dev)->wm.distrust_bios_wm)
5047 changed = true;
5048
5049 /*
Matt Roper98d39492016-05-12 07:06:03 -07005050 * If this transaction isn't actually touching any CRTC's, don't
5051 * bother with watermark calculation. Note that if we pass this
5052 * test, we're guaranteed to hold at least one CRTC state mutex,
5053 * which means we can safely use values like dev_priv->active_crtcs
5054 * since any racing commits that want to update them would need to
5055 * hold _all_ CRTC state mutexes.
5056 */
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01005057 for_each_new_crtc_in_state(state, crtc, cstate, i)
Matt Roper98d39492016-05-12 07:06:03 -07005058 changed = true;
Maarten Lankhorst367d73d2017-05-31 17:42:36 +02005059
Matt Roper98d39492016-05-12 07:06:03 -07005060 if (!changed)
5061 return 0;
5062
Matt Roper734fa012016-05-12 15:11:40 -07005063 /* Clear all dirty flags */
5064 results->dirty_pipes = 0;
5065
Rodrigo Vivi9a30a262017-06-13 10:52:30 -07005066 ret = skl_compute_ddb(state);
Matt Roper98d39492016-05-12 07:06:03 -07005067 if (ret)
5068 return ret;
5069
Matt Roper734fa012016-05-12 15:11:40 -07005070 /*
5071 * Calculate WM's for all pipes that are part of this transaction.
5072 * Note that the DDB allocation above may have added more CRTC's that
5073 * weren't otherwise being modified (and set bits in dirty_pipes) if
5074 * pipe allocations had to change.
5075 *
5076 * FIXME: Now that we're doing this in the atomic check phase, we
5077 * should allow skl_update_pipe_wm() to return failure in cases where
5078 * no suitable watermark values can be found.
5079 */
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01005080 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Matt Roper734fa012016-05-12 15:11:40 -07005081 struct intel_crtc_state *intel_cstate =
5082 to_intel_crtc_state(cstate);
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02005083 const struct skl_pipe_wm *old_pipe_wm =
5084 &to_intel_crtc_state(crtc->state)->wm.skl.optimal;
Matt Roper734fa012016-05-12 15:11:40 -07005085
5086 pipe_wm = &intel_cstate->wm.skl.optimal;
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02005087 ret = skl_update_pipe_wm(cstate, old_pipe_wm, pipe_wm,
5088 &results->ddb, &changed);
Matt Roper734fa012016-05-12 15:11:40 -07005089 if (ret)
5090 return ret;
5091
5092 if (changed)
5093 results->dirty_pipes |= drm_crtc_mask(crtc);
5094
5095 if ((results->dirty_pipes & drm_crtc_mask(crtc)) == 0)
5096 /* This pipe's WM's did not change */
5097 continue;
5098
5099 intel_cstate->update_wm_pre = true;
Matt Roper734fa012016-05-12 15:11:40 -07005100 }
5101
cpaul@redhat.com413fc532016-10-14 17:31:54 -04005102 skl_print_wm_changes(state);
5103
Matt Roper98d39492016-05-12 07:06:03 -07005104 return 0;
5105}
5106
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005107static void skl_atomic_update_crtc_wm(struct intel_atomic_state *state,
5108 struct intel_crtc_state *cstate)
5109{
5110 struct intel_crtc *crtc = to_intel_crtc(cstate->base.crtc);
5111 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5112 struct skl_pipe_wm *pipe_wm = &cstate->wm.skl.optimal;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005113 const struct skl_ddb_allocation *ddb = &state->wm_results.ddb;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005114 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005115 enum plane_id plane_id;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005116
5117 if (!(state->wm_results.dirty_pipes & drm_crtc_mask(&crtc->base)))
5118 return;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005119
5120 I915_WRITE(PIPE_WM_LINETIME(pipe), pipe_wm->linetime);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005121
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005122 for_each_plane_id_on_crtc(crtc, plane_id) {
5123 if (plane_id != PLANE_CURSOR)
5124 skl_write_plane_wm(crtc, &pipe_wm->planes[plane_id],
5125 ddb, plane_id);
5126 else
5127 skl_write_cursor_wm(crtc, &pipe_wm->planes[plane_id],
5128 ddb);
5129 }
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005130}
5131
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005132static void skl_initial_wm(struct intel_atomic_state *state,
5133 struct intel_crtc_state *cstate)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005134{
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005135 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02005136 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005137 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005138 struct skl_wm_values *results = &state->wm_results;
Matt Roper2722efb2016-08-17 15:55:55 -04005139 struct skl_wm_values *hw_vals = &dev_priv->wm.skl_hw;
Lyude27082492016-08-24 07:48:10 +02005140 enum pipe pipe = intel_crtc->pipe;
Bob Paauweadda50b2015-07-21 10:42:53 -07005141
Ville Syrjälä432081b2016-10-31 22:37:03 +02005142 if ((results->dirty_pipes & drm_crtc_mask(&intel_crtc->base)) == 0)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005143 return;
5144
Matt Roper734fa012016-05-12 15:11:40 -07005145 mutex_lock(&dev_priv->wm.wm_mutex);
5146
Maarten Lankhorste62929b2016-11-08 13:55:33 +01005147 if (cstate->base.active_changed)
5148 skl_atomic_update_crtc_wm(state, cstate);
Lyude27082492016-08-24 07:48:10 +02005149
5150 skl_copy_wm_for_pipe(hw_vals, results, pipe);
Matt Roper734fa012016-05-12 15:11:40 -07005151
5152 mutex_unlock(&dev_priv->wm.wm_mutex);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00005153}
5154
Ville Syrjäläd8905652016-01-14 14:53:35 +02005155static void ilk_compute_wm_config(struct drm_device *dev,
5156 struct intel_wm_config *config)
5157{
5158 struct intel_crtc *crtc;
5159
5160 /* Compute the currently _active_ config */
5161 for_each_intel_crtc(dev, crtc) {
5162 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
5163
5164 if (!wm->pipe_enabled)
5165 continue;
5166
5167 config->sprites_enabled |= wm->sprites_enabled;
5168 config->sprites_scaled |= wm->sprites_scaled;
5169 config->num_pipes_active++;
5170 }
5171}
5172
Matt Ropered4a6a72016-02-23 17:20:13 -08005173static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03005174{
Chris Wilson91c8a322016-07-05 10:40:23 +01005175 struct drm_device *dev = &dev_priv->drm;
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005176 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02005177 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02005178 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02005179 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005180 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07005181
Ville Syrjäläd8905652016-01-14 14:53:35 +02005182 ilk_compute_wm_config(dev, &config);
5183
5184 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
5185 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03005186
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005187 /* 5/6 split only in single pipe config on IVB+ */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00005188 if (INTEL_GEN(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02005189 config.num_pipes_active == 1 && config.sprites_enabled) {
5190 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
5191 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03005192
Imre Deak820c1982013-12-17 14:46:36 +02005193 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03005194 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005195 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005196 }
5197
Ville Syrjälä198a1e92013-10-09 19:17:58 +03005198 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03005199 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03005200
Imre Deak820c1982013-12-17 14:46:36 +02005201 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03005202
Imre Deak820c1982013-12-17 14:46:36 +02005203 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03005204}
5205
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005206static void ilk_initial_watermarks(struct intel_atomic_state *state,
5207 struct intel_crtc_state *cstate)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005208{
Matt Ropered4a6a72016-02-23 17:20:13 -08005209 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
5210 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005211
Matt Ropered4a6a72016-02-23 17:20:13 -08005212 mutex_lock(&dev_priv->wm.wm_mutex);
Matt Ropere8f1f022016-05-12 07:05:55 -07005213 intel_crtc->wm.active.ilk = cstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08005214 ilk_program_watermarks(dev_priv);
5215 mutex_unlock(&dev_priv->wm.wm_mutex);
5216}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005217
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01005218static void ilk_optimize_watermarks(struct intel_atomic_state *state,
5219 struct intel_crtc_state *cstate)
Matt Ropered4a6a72016-02-23 17:20:13 -08005220{
5221 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
5222 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
5223
5224 mutex_lock(&dev_priv->wm.wm_mutex);
5225 if (cstate->wm.need_postvbl_update) {
Matt Ropere8f1f022016-05-12 07:05:55 -07005226 intel_crtc->wm.active.ilk = cstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08005227 ilk_program_watermarks(dev_priv);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005228 }
Matt Ropered4a6a72016-02-23 17:20:13 -08005229 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07005230}
5231
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005232static inline void skl_wm_level_from_reg_val(uint32_t val,
5233 struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00005234{
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005235 level->plane_en = val & PLANE_WM_EN;
5236 level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
5237 level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
5238 PLANE_WM_LINES_MASK;
Pradeep Bhat30789992014-11-04 17:06:45 +00005239}
5240
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005241void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc,
5242 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00005243{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005244 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Pradeep Bhat30789992014-11-04 17:06:45 +00005245 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Pradeep Bhat30789992014-11-04 17:06:45 +00005246 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005247 int level, max_level;
5248 enum plane_id plane_id;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005249 uint32_t val;
Pradeep Bhat30789992014-11-04 17:06:45 +00005250
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005251 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00005252
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005253 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
5254 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00005255
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005256 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005257 if (plane_id != PLANE_CURSOR)
5258 val = I915_READ(PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005259 else
5260 val = I915_READ(CUR_WM(pipe, level));
5261
5262 skl_wm_level_from_reg_val(val, &wm->wm[level]);
5263 }
5264
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02005265 if (plane_id != PLANE_CURSOR)
5266 val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02005267 else
5268 val = I915_READ(CUR_WM_TRANS(pipe));
5269
5270 skl_wm_level_from_reg_val(val, &wm->trans_wm);
5271 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005272
Matt Roper3ef00282015-03-09 10:19:24 -07005273 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00005274 return;
5275
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005276 out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00005277}
5278
5279void skl_wm_get_hw_state(struct drm_device *dev)
5280{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005281 struct drm_i915_private *dev_priv = to_i915(dev);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005282 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
Damien Lespiaua269c582014-11-04 17:06:49 +00005283 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00005284 struct drm_crtc *crtc;
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005285 struct intel_crtc *intel_crtc;
5286 struct intel_crtc_state *cstate;
Pradeep Bhat30789992014-11-04 17:06:45 +00005287
Damien Lespiaua269c582014-11-04 17:06:49 +00005288 skl_ddb_get_hw_state(dev_priv, ddb);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005289 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5290 intel_crtc = to_intel_crtc(crtc);
5291 cstate = to_intel_crtc_state(crtc->state);
5292
5293 skl_pipe_wm_get_hw_state(crtc, &cstate->wm.skl.optimal);
5294
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02005295 if (intel_crtc->active)
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005296 hw->dirty_pipes |= drm_crtc_mask(crtc);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04005297 }
Matt Ropera1de91e2016-05-12 07:05:57 -07005298
Matt Roper279e99d2016-05-12 07:06:02 -07005299 if (dev_priv->active_crtcs) {
5300 /* Fully recompute DDB on first atomic commit */
5301 dev_priv->wm.distrust_bios_wm = true;
5302 } else {
5303 /* Easy/common case; just sanitize DDB now if everything off */
5304 memset(ddb, 0, sizeof(*ddb));
5305 }
Pradeep Bhat30789992014-11-04 17:06:45 +00005306}
5307
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005308static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
5309{
5310 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005311 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02005312 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005313 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper4e0963c2015-09-24 15:53:15 -07005314 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Matt Ropere8f1f022016-05-12 07:05:55 -07005315 struct intel_pipe_wm *active = &cstate->wm.ilk.optimal;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005316 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005317 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005318 [PIPE_A] = WM0_PIPEA_ILK,
5319 [PIPE_B] = WM0_PIPEB_ILK,
5320 [PIPE_C] = WM0_PIPEC_IVB,
5321 };
5322
5323 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005324 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02005325 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005326
Ville Syrjälä15606532016-05-13 17:55:17 +03005327 memset(active, 0, sizeof(*active));
5328
Matt Roper3ef00282015-03-09 10:19:24 -07005329 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02005330
5331 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005332 u32 tmp = hw->wm_pipe[pipe];
5333
5334 /*
5335 * For active pipes LP0 watermark is marked as
5336 * enabled, and LP1+ watermaks as disabled since
5337 * we can't really reverse compute them in case
5338 * multiple pipes are active.
5339 */
5340 active->wm[0].enable = true;
5341 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
5342 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
5343 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
5344 active->linetime = hw->wm_linetime[pipe];
5345 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005346 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005347
5348 /*
5349 * For inactive pipes, all watermark levels
5350 * should be marked as enabled but zeroed,
5351 * which is what we'd compute them to.
5352 */
5353 for (level = 0; level <= max_level; level++)
5354 active->wm[level].enable = true;
5355 }
Matt Roper4e0963c2015-09-24 15:53:15 -07005356
5357 intel_crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005358}
5359
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005360#define _FW_WM(value, plane) \
5361 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
5362#define _FW_WM_VLV(value, plane) \
5363 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
5364
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005365static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
5366 struct g4x_wm_values *wm)
5367{
5368 uint32_t tmp;
5369
5370 tmp = I915_READ(DSPFW1);
5371 wm->sr.plane = _FW_WM(tmp, SR);
5372 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5373 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
5374 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
5375
5376 tmp = I915_READ(DSPFW2);
5377 wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
5378 wm->sr.fbc = _FW_WM(tmp, FBC_SR);
5379 wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
5380 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
5381 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5382 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
5383
5384 tmp = I915_READ(DSPFW3);
5385 wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
5386 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5387 wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
5388 wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
5389}
5390
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005391static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
5392 struct vlv_wm_values *wm)
5393{
5394 enum pipe pipe;
5395 uint32_t tmp;
5396
5397 for_each_pipe(dev_priv, pipe) {
5398 tmp = I915_READ(VLV_DDL(pipe));
5399
Ville Syrjälä1b313892016-11-28 19:37:08 +02005400 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005401 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005402 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005403 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005404 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005405 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005406 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005407 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
5408 }
5409
5410 tmp = I915_READ(DSPFW1);
5411 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005412 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
5413 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
5414 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005415
5416 tmp = I915_READ(DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005417 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
5418 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
5419 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005420
5421 tmp = I915_READ(DSPFW3);
5422 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
5423
5424 if (IS_CHERRYVIEW(dev_priv)) {
5425 tmp = I915_READ(DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005426 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5427 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005428
5429 tmp = I915_READ(DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005430 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
5431 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005432
5433 tmp = I915_READ(DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005434 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
5435 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005436
5437 tmp = I915_READ(DSPHOWM);
5438 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005439 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
5440 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
5441 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
5442 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5443 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5444 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5445 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5446 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5447 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005448 } else {
5449 tmp = I915_READ(DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02005450 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
5451 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005452
5453 tmp = I915_READ(DSPHOWM);
5454 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02005455 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
5456 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
5457 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
5458 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
5459 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
5460 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005461 }
5462}
5463
5464#undef _FW_WM
5465#undef _FW_WM_VLV
5466
Ville Syrjälä04548cb2017-04-21 21:14:29 +03005467void g4x_wm_get_hw_state(struct drm_device *dev)
5468{
5469 struct drm_i915_private *dev_priv = to_i915(dev);
5470 struct g4x_wm_values *wm = &dev_priv->wm.g4x;
5471 struct intel_crtc *crtc;
5472
5473 g4x_read_wm_values(dev_priv, wm);
5474
5475 wm->cxsr = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
5476
5477 for_each_intel_crtc(dev, crtc) {
5478 struct intel_crtc_state *crtc_state =
5479 to_intel_crtc_state(crtc->base.state);
5480 struct g4x_wm_state *active = &crtc->wm.active.g4x;
5481 struct g4x_pipe_wm *raw;
5482 enum pipe pipe = crtc->pipe;
5483 enum plane_id plane_id;
5484 int level, max_level;
5485
5486 active->cxsr = wm->cxsr;
5487 active->hpll_en = wm->hpll_en;
5488 active->fbc_en = wm->fbc_en;
5489
5490 active->sr = wm->sr;
5491 active->hpll = wm->hpll;
5492
5493 for_each_plane_id_on_crtc(crtc, plane_id) {
5494 active->wm.plane[plane_id] =
5495 wm->pipe[pipe].plane[plane_id];
5496 }
5497
5498 if (wm->cxsr && wm->hpll_en)
5499 max_level = G4X_WM_LEVEL_HPLL;
5500 else if (wm->cxsr)
5501 max_level = G4X_WM_LEVEL_SR;
5502 else
5503 max_level = G4X_WM_LEVEL_NORMAL;
5504
5505 level = G4X_WM_LEVEL_NORMAL;
5506 raw = &crtc_state->wm.g4x.raw[level];
5507 for_each_plane_id_on_crtc(crtc, plane_id)
5508 raw->plane[plane_id] = active->wm.plane[plane_id];
5509
5510 if (++level > max_level)
5511 goto out;
5512
5513 raw = &crtc_state->wm.g4x.raw[level];
5514 raw->plane[PLANE_PRIMARY] = active->sr.plane;
5515 raw->plane[PLANE_CURSOR] = active->sr.cursor;
5516 raw->plane[PLANE_SPRITE0] = 0;
5517 raw->fbc = active->sr.fbc;
5518
5519 if (++level > max_level)
5520 goto out;
5521
5522 raw = &crtc_state->wm.g4x.raw[level];
5523 raw->plane[PLANE_PRIMARY] = active->hpll.plane;
5524 raw->plane[PLANE_CURSOR] = active->hpll.cursor;
5525 raw->plane[PLANE_SPRITE0] = 0;
5526 raw->fbc = active->hpll.fbc;
5527
5528 out:
5529 for_each_plane_id_on_crtc(crtc, plane_id)
5530 g4x_raw_plane_wm_set(crtc_state, level,
5531 plane_id, USHRT_MAX);
5532 g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
5533
5534 crtc_state->wm.g4x.optimal = *active;
5535 crtc_state->wm.g4x.intermediate = *active;
5536
5537 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
5538 pipe_name(pipe),
5539 wm->pipe[pipe].plane[PLANE_PRIMARY],
5540 wm->pipe[pipe].plane[PLANE_CURSOR],
5541 wm->pipe[pipe].plane[PLANE_SPRITE0]);
5542 }
5543
5544 DRM_DEBUG_KMS("Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
5545 wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
5546 DRM_DEBUG_KMS("Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
5547 wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
5548 DRM_DEBUG_KMS("Initial SR=%s HPLL=%s FBC=%s\n",
5549 yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
5550}
5551
5552void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
5553{
5554 struct intel_plane *plane;
5555 struct intel_crtc *crtc;
5556
5557 mutex_lock(&dev_priv->wm.wm_mutex);
5558
5559 for_each_intel_plane(&dev_priv->drm, plane) {
5560 struct intel_crtc *crtc =
5561 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
5562 struct intel_crtc_state *crtc_state =
5563 to_intel_crtc_state(crtc->base.state);
5564 struct intel_plane_state *plane_state =
5565 to_intel_plane_state(plane->base.state);
5566 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
5567 enum plane_id plane_id = plane->id;
5568 int level;
5569
5570 if (plane_state->base.visible)
5571 continue;
5572
5573 for (level = 0; level < 3; level++) {
5574 struct g4x_pipe_wm *raw =
5575 &crtc_state->wm.g4x.raw[level];
5576
5577 raw->plane[plane_id] = 0;
5578 wm_state->wm.plane[plane_id] = 0;
5579 }
5580
5581 if (plane_id == PLANE_PRIMARY) {
5582 for (level = 0; level < 3; level++) {
5583 struct g4x_pipe_wm *raw =
5584 &crtc_state->wm.g4x.raw[level];
5585 raw->fbc = 0;
5586 }
5587
5588 wm_state->sr.fbc = 0;
5589 wm_state->hpll.fbc = 0;
5590 wm_state->fbc_en = false;
5591 }
5592 }
5593
5594 for_each_intel_crtc(&dev_priv->drm, crtc) {
5595 struct intel_crtc_state *crtc_state =
5596 to_intel_crtc_state(crtc->base.state);
5597
5598 crtc_state->wm.g4x.intermediate =
5599 crtc_state->wm.g4x.optimal;
5600 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
5601 }
5602
5603 g4x_program_watermarks(dev_priv);
5604
5605 mutex_unlock(&dev_priv->wm.wm_mutex);
5606}
5607
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005608void vlv_wm_get_hw_state(struct drm_device *dev)
5609{
5610 struct drm_i915_private *dev_priv = to_i915(dev);
5611 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02005612 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005613 u32 val;
5614
5615 vlv_read_wm_values(dev_priv, wm);
5616
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005617 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
5618 wm->level = VLV_WM_LEVEL_PM2;
5619
5620 if (IS_CHERRYVIEW(dev_priv)) {
5621 mutex_lock(&dev_priv->rps.hw_lock);
5622
5623 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5624 if (val & DSP_MAXFIFO_PM5_ENABLE)
5625 wm->level = VLV_WM_LEVEL_PM5;
5626
Ville Syrjälä58590c12015-09-08 21:05:12 +03005627 /*
5628 * If DDR DVFS is disabled in the BIOS, Punit
5629 * will never ack the request. So if that happens
5630 * assume we don't have to enable/disable DDR DVFS
5631 * dynamically. To test that just set the REQ_ACK
5632 * bit to poke the Punit, but don't change the
5633 * HIGH/LOW bits so that we don't actually change
5634 * the current state.
5635 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005636 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03005637 val |= FORCE_DDR_FREQ_REQ_ACK;
5638 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
5639
5640 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
5641 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
5642 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
5643 "assuming DDR DVFS is disabled\n");
5644 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
5645 } else {
5646 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
5647 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
5648 wm->level = VLV_WM_LEVEL_DDR_DVFS;
5649 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005650
5651 mutex_unlock(&dev_priv->rps.hw_lock);
5652 }
5653
Ville Syrjäläff32c542017-03-02 19:14:57 +02005654 for_each_intel_crtc(dev, crtc) {
5655 struct intel_crtc_state *crtc_state =
5656 to_intel_crtc_state(crtc->base.state);
5657 struct vlv_wm_state *active = &crtc->wm.active.vlv;
5658 const struct vlv_fifo_state *fifo_state =
5659 &crtc_state->wm.vlv.fifo_state;
5660 enum pipe pipe = crtc->pipe;
5661 enum plane_id plane_id;
5662 int level;
5663
5664 vlv_get_fifo_size(crtc_state);
5665
5666 active->num_levels = wm->level + 1;
5667 active->cxsr = wm->cxsr;
5668
Ville Syrjäläff32c542017-03-02 19:14:57 +02005669 for (level = 0; level < active->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03005670 struct g4x_pipe_wm *raw =
Ville Syrjäläff32c542017-03-02 19:14:57 +02005671 &crtc_state->wm.vlv.raw[level];
5672
5673 active->sr[level].plane = wm->sr.plane;
5674 active->sr[level].cursor = wm->sr.cursor;
5675
5676 for_each_plane_id_on_crtc(crtc, plane_id) {
5677 active->wm[level].plane[plane_id] =
5678 wm->pipe[pipe].plane[plane_id];
5679
5680 raw->plane[plane_id] =
5681 vlv_invert_wm_value(active->wm[level].plane[plane_id],
5682 fifo_state->plane[plane_id]);
5683 }
5684 }
5685
5686 for_each_plane_id_on_crtc(crtc, plane_id)
5687 vlv_raw_plane_wm_set(crtc_state, level,
5688 plane_id, USHRT_MAX);
5689 vlv_invalidate_wms(crtc, active, level);
5690
5691 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02005692 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02005693
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005694 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 +02005695 pipe_name(pipe),
5696 wm->pipe[pipe].plane[PLANE_PRIMARY],
5697 wm->pipe[pipe].plane[PLANE_CURSOR],
5698 wm->pipe[pipe].plane[PLANE_SPRITE0],
5699 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02005700 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03005701
5702 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
5703 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
5704}
5705
Ville Syrjälä602ae832017-03-02 19:15:02 +02005706void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
5707{
5708 struct intel_plane *plane;
5709 struct intel_crtc *crtc;
5710
5711 mutex_lock(&dev_priv->wm.wm_mutex);
5712
5713 for_each_intel_plane(&dev_priv->drm, plane) {
5714 struct intel_crtc *crtc =
5715 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
5716 struct intel_crtc_state *crtc_state =
5717 to_intel_crtc_state(crtc->base.state);
5718 struct intel_plane_state *plane_state =
5719 to_intel_plane_state(plane->base.state);
5720 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
5721 const struct vlv_fifo_state *fifo_state =
5722 &crtc_state->wm.vlv.fifo_state;
5723 enum plane_id plane_id = plane->id;
5724 int level;
5725
5726 if (plane_state->base.visible)
5727 continue;
5728
5729 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä114d7dc2017-04-21 21:14:21 +03005730 struct g4x_pipe_wm *raw =
Ville Syrjälä602ae832017-03-02 19:15:02 +02005731 &crtc_state->wm.vlv.raw[level];
5732
5733 raw->plane[plane_id] = 0;
5734
5735 wm_state->wm[level].plane[plane_id] =
5736 vlv_invert_wm_value(raw->plane[plane_id],
5737 fifo_state->plane[plane_id]);
5738 }
5739 }
5740
5741 for_each_intel_crtc(&dev_priv->drm, crtc) {
5742 struct intel_crtc_state *crtc_state =
5743 to_intel_crtc_state(crtc->base.state);
5744
5745 crtc_state->wm.vlv.intermediate =
5746 crtc_state->wm.vlv.optimal;
5747 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
5748 }
5749
5750 vlv_program_watermarks(dev_priv);
5751
5752 mutex_unlock(&dev_priv->wm.wm_mutex);
5753}
5754
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005755void ilk_wm_get_hw_state(struct drm_device *dev)
5756{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005757 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02005758 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005759 struct drm_crtc *crtc;
5760
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01005761 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005762 ilk_pipe_wm_get_hw_state(crtc);
5763
5764 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
5765 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
5766 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
5767
5768 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00005769 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjäläcfa76982014-03-07 18:32:08 +02005770 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
5771 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
5772 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005773
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005774 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02005775 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
5776 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01005777 else if (IS_IVYBRIDGE(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02005778 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
5779 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03005780
5781 hw->enable_fbc_wm =
5782 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
5783}
5784
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005785/**
5786 * intel_update_watermarks - update FIFO watermark values based on current modes
5787 *
5788 * Calculate watermark values for the various WM regs based on current mode
5789 * and plane configuration.
5790 *
5791 * There are several cases to deal with here:
5792 * - normal (i.e. non-self-refresh)
5793 * - self-refresh (SR) mode
5794 * - lines are large relative to FIFO size (buffer can hold up to 2)
5795 * - lines are small relative to FIFO size (buffer can hold more than 2
5796 * lines), so need to account for TLB latency
5797 *
5798 * The normal calculation is:
5799 * watermark = dotclock * bytes per pixel * latency
5800 * where latency is platform & configuration dependent (we assume pessimal
5801 * values here).
5802 *
5803 * The SR calculation is:
5804 * watermark = (trunc(latency/line time)+1) * surface width *
5805 * bytes per pixel
5806 * where
5807 * line time = htotal / dotclock
5808 * surface width = hdisplay for normal plane and 64 for cursor
5809 * and latency is assumed to be high, as above.
5810 *
5811 * The final value programmed to the register should always be rounded up,
5812 * and include an extra 2 entries to account for clock crossings.
5813 *
5814 * We don't use the sprite, so we can ignore that. And on Crestline we have
5815 * to set the non-SR watermarks to 8.
5816 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02005817void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005818{
Ville Syrjälä432081b2016-10-31 22:37:03 +02005819 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005820
5821 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005822 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03005823}
5824
Jani Nikulae2828912016-01-18 09:19:47 +02005825/*
Daniel Vetter92703882012-08-09 16:46:01 +02005826 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02005827 */
5828DEFINE_SPINLOCK(mchdev_lock);
5829
5830/* Global for IPS driver to get at the current i915 device. Protected by
5831 * mchdev_lock. */
5832static struct drm_i915_private *i915_mch_dev;
5833
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005834bool ironlake_set_drps(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005835{
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005836 u16 rgvswctl;
5837
Chris Wilson67520412017-03-02 13:28:01 +00005838 lockdep_assert_held(&mchdev_lock);
Daniel Vetter92703882012-08-09 16:46:01 +02005839
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005840 rgvswctl = I915_READ16(MEMSWCTL);
5841 if (rgvswctl & MEMCTL_CMD_STS) {
5842 DRM_DEBUG("gpu busy, RCS change rejected\n");
5843 return false; /* still busy with another command */
5844 }
5845
5846 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
5847 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
5848 I915_WRITE16(MEMSWCTL, rgvswctl);
5849 POSTING_READ16(MEMSWCTL);
5850
5851 rgvswctl |= MEMCTL_CMD_STS;
5852 I915_WRITE16(MEMSWCTL, rgvswctl);
5853
5854 return true;
5855}
5856
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005857static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005858{
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00005859 u32 rgvmodectl;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005860 u8 fmax, fmin, fstart, vstart;
5861
Daniel Vetter92703882012-08-09 16:46:01 +02005862 spin_lock_irq(&mchdev_lock);
5863
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00005864 rgvmodectl = I915_READ(MEMMODECTL);
5865
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005866 /* Enable temp reporting */
5867 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
5868 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
5869
5870 /* 100ms RC evaluation intervals */
5871 I915_WRITE(RCUPEI, 100000);
5872 I915_WRITE(RCDNEI, 100000);
5873
5874 /* Set max/min thresholds to 90ms and 80ms respectively */
5875 I915_WRITE(RCBMAXAVG, 90000);
5876 I915_WRITE(RCBMINAVG, 80000);
5877
5878 I915_WRITE(MEMIHYST, 1);
5879
5880 /* Set up min, max, and cur for interrupt handling */
5881 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
5882 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
5883 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
5884 MEMMODE_FSTART_SHIFT;
5885
Ville Syrjälä616847e2015-09-18 20:03:19 +03005886 vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005887 PXVFREQ_PX_SHIFT;
5888
Daniel Vetter20e4d402012-08-08 23:35:39 +02005889 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
5890 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005891
Daniel Vetter20e4d402012-08-08 23:35:39 +02005892 dev_priv->ips.max_delay = fstart;
5893 dev_priv->ips.min_delay = fmin;
5894 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005895
5896 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
5897 fmax, fmin, fstart);
5898
5899 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
5900
5901 /*
5902 * Interrupts will be enabled in ironlake_irq_postinstall
5903 */
5904
5905 I915_WRITE(VIDSTART, vstart);
5906 POSTING_READ(VIDSTART);
5907
5908 rgvmodectl |= MEMMODE_SWMODE_EN;
5909 I915_WRITE(MEMMODECTL, rgvmodectl);
5910
Daniel Vetter92703882012-08-09 16:46:01 +02005911 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005912 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005913 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005914
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005915 ironlake_set_drps(dev_priv, fstart);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005916
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03005917 dev_priv->ips.last_count1 = I915_READ(DMIEC) +
5918 I915_READ(DDREC) + I915_READ(CSIEC);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005919 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03005920 dev_priv->ips.last_count2 = I915_READ(GFXEC);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005921 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02005922
5923 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005924}
5925
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005926static void ironlake_disable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005927{
Daniel Vetter92703882012-08-09 16:46:01 +02005928 u16 rgvswctl;
5929
5930 spin_lock_irq(&mchdev_lock);
5931
5932 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005933
5934 /* Ack interrupts, disable EFC interrupt */
5935 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
5936 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
5937 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
5938 I915_WRITE(DEIIR, DE_PCU_EVENT);
5939 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
5940
5941 /* Go back to the starting frequency */
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005942 ironlake_set_drps(dev_priv, dev_priv->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005943 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005944 rgvswctl |= MEMCTL_CMD_STS;
5945 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005946 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005947
Daniel Vetter92703882012-08-09 16:46:01 +02005948 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005949}
5950
Daniel Vetteracbe9472012-07-26 11:50:05 +02005951/* There's a funny hw issue where the hw returns all 0 when reading from
5952 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
5953 * ourselves, instead of doing a rmw cycle (which might result in us clearing
5954 * all limits and the gpu stuck at whatever frequency it is at atm).
5955 */
Akash Goel74ef1172015-03-06 11:07:19 +05305956static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005957{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005958 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005959
Daniel Vetter20b46e52012-07-26 11:16:14 +02005960 /* Only set the down limit when we've reached the lowest level to avoid
5961 * getting more interrupts, otherwise leave this clear. This prevents a
5962 * race in the hw when coming out of rc6: There's a tiny window where
5963 * the hw runs at the minimal clock before selecting the desired
5964 * frequency, if the down threshold expires in that window we will not
5965 * receive a down interrupt. */
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07005966 if (INTEL_GEN(dev_priv) >= 9) {
Akash Goel74ef1172015-03-06 11:07:19 +05305967 limits = (dev_priv->rps.max_freq_softlimit) << 23;
5968 if (val <= dev_priv->rps.min_freq_softlimit)
5969 limits |= (dev_priv->rps.min_freq_softlimit) << 14;
5970 } else {
5971 limits = dev_priv->rps.max_freq_softlimit << 24;
5972 if (val <= dev_priv->rps.min_freq_softlimit)
5973 limits |= dev_priv->rps.min_freq_softlimit << 16;
5974 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02005975
5976 return limits;
5977}
5978
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005979static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
5980{
5981 int new_power;
Akash Goel8a586432015-03-06 11:07:18 +05305982 u32 threshold_up = 0, threshold_down = 0; /* in % */
5983 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005984
5985 new_power = dev_priv->rps.power;
5986 switch (dev_priv->rps.power) {
5987 case LOW_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01005988 if (val > dev_priv->rps.efficient_freq + 1 &&
5989 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005990 new_power = BETWEEN;
5991 break;
5992
5993 case BETWEEN:
Chris Wilsona72b5622016-07-02 15:35:59 +01005994 if (val <= dev_priv->rps.efficient_freq &&
5995 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005996 new_power = LOW_POWER;
Chris Wilsona72b5622016-07-02 15:35:59 +01005997 else if (val >= dev_priv->rps.rp0_freq &&
5998 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005999 new_power = HIGH_POWER;
6000 break;
6001
6002 case HIGH_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01006003 if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 &&
6004 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006005 new_power = BETWEEN;
6006 break;
6007 }
6008 /* Max/min bins are special */
Chris Wilsonaed242f2015-03-18 09:48:21 +00006009 if (val <= dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006010 new_power = LOW_POWER;
Chris Wilsonaed242f2015-03-18 09:48:21 +00006011 if (val >= dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006012 new_power = HIGH_POWER;
6013 if (new_power == dev_priv->rps.power)
6014 return;
6015
6016 /* Note the units here are not exactly 1us, but 1280ns. */
6017 switch (new_power) {
6018 case LOW_POWER:
6019 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05306020 ei_up = 16000;
6021 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006022
6023 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306024 ei_down = 32000;
6025 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006026 break;
6027
6028 case BETWEEN:
6029 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05306030 ei_up = 13000;
6031 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006032
6033 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306034 ei_down = 32000;
6035 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006036 break;
6037
6038 case HIGH_POWER:
6039 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05306040 ei_up = 10000;
6041 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006042
6043 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05306044 ei_down = 32000;
6045 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006046 break;
6047 }
6048
Mika Kuoppala6067a272017-02-15 15:52:59 +02006049 /* When byt can survive without system hang with dynamic
6050 * sw freq adjustments, this restriction can be lifted.
6051 */
6052 if (IS_VALLEYVIEW(dev_priv))
6053 goto skip_hw_write;
6054
Akash Goel8a586432015-03-06 11:07:18 +05306055 I915_WRITE(GEN6_RP_UP_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01006056 GT_INTERVAL_FROM_US(dev_priv, ei_up));
Akash Goel8a586432015-03-06 11:07:18 +05306057 I915_WRITE(GEN6_RP_UP_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01006058 GT_INTERVAL_FROM_US(dev_priv,
6059 ei_up * threshold_up / 100));
Akash Goel8a586432015-03-06 11:07:18 +05306060
6061 I915_WRITE(GEN6_RP_DOWN_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01006062 GT_INTERVAL_FROM_US(dev_priv, ei_down));
Akash Goel8a586432015-03-06 11:07:18 +05306063 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01006064 GT_INTERVAL_FROM_US(dev_priv,
6065 ei_down * threshold_down / 100));
Akash Goel8a586432015-03-06 11:07:18 +05306066
Chris Wilsona72b5622016-07-02 15:35:59 +01006067 I915_WRITE(GEN6_RP_CONTROL,
6068 GEN6_RP_MEDIA_TURBO |
6069 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6070 GEN6_RP_MEDIA_IS_GFX |
6071 GEN6_RP_ENABLE |
6072 GEN6_RP_UP_BUSY_AVG |
6073 GEN6_RP_DOWN_IDLE_AVG);
Akash Goel8a586432015-03-06 11:07:18 +05306074
Mika Kuoppala6067a272017-02-15 15:52:59 +02006075skip_hw_write:
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006076 dev_priv->rps.power = new_power;
Chris Wilson8fb55192015-04-07 16:20:28 +01006077 dev_priv->rps.up_threshold = threshold_up;
6078 dev_priv->rps.down_threshold = threshold_down;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006079 dev_priv->rps.last_adj = 0;
6080}
6081
Chris Wilson2876ce72014-03-28 08:03:34 +00006082static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
6083{
6084 u32 mask = 0;
6085
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006086 /* We use UP_EI_EXPIRED interupts for both up/down in manual mode */
Chris Wilson2876ce72014-03-28 08:03:34 +00006087 if (val > dev_priv->rps.min_freq_softlimit)
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006088 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
Chris Wilson2876ce72014-03-28 08:03:34 +00006089 if (val < dev_priv->rps.max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00006090 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00006091
Chris Wilson7b3c29f2014-07-10 20:31:19 +01006092 mask &= dev_priv->pm_rps_events;
6093
Imre Deak59d02a12014-12-19 19:33:26 +02006094 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00006095}
6096
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006097/* gen6_set_rps is called to update the frequency request, but should also be
6098 * called when the range (min_delay and max_delay) is modified so that we can
6099 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006100static int gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02006101{
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006102 /* min/max delay may still have been modified so be sure to
6103 * write the limits value.
6104 */
6105 if (val != dev_priv->rps.cur_freq) {
6106 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006107
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006108 if (INTEL_GEN(dev_priv) >= 9)
Akash Goel57041952015-03-06 11:07:17 +05306109 I915_WRITE(GEN6_RPNSWREQ,
6110 GEN9_FREQUENCY(val));
Chris Wilsondc979972016-05-10 14:10:04 +01006111 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00006112 I915_WRITE(GEN6_RPNSWREQ,
6113 HSW_FREQUENCY(val));
6114 else
6115 I915_WRITE(GEN6_RPNSWREQ,
6116 GEN6_FREQUENCY(val) |
6117 GEN6_OFFSET(0) |
6118 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06006119 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006120
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006121 /* Make sure we continue to get interrupts
6122 * until we hit the minimum or maximum frequencies.
6123 */
Akash Goel74ef1172015-03-06 11:07:19 +05306124 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00006125 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01006126
Ben Widawskyb39fb292014-03-19 18:31:11 -07006127 dev_priv->rps.cur_freq = val;
Mika Kuoppala0f945922015-11-17 18:14:26 +02006128 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006129
6130 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006131}
6132
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006133static int valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006134{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006135 int err;
6136
Chris Wilsondc979972016-05-10 14:10:04 +01006137 if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006138 "Odd GPU freq value\n"))
6139 val &= ~1;
6140
Deepak Scd25dd52015-07-10 18:31:40 +05306141 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
6142
Chris Wilson8fb55192015-04-07 16:20:28 +01006143 if (val != dev_priv->rps.cur_freq) {
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006144 err = vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
6145 if (err)
6146 return err;
6147
Chris Wilsondb4c5e02017-02-10 15:03:46 +00006148 gen6_set_rps_thresholds(dev_priv, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01006149 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006150
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006151 dev_priv->rps.cur_freq = val;
6152 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006153
6154 return 0;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006155}
6156
Deepak Sa7f6e232015-05-09 18:04:44 +05306157/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05306158 *
6159 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05306160 * 1. Forcewake Media well.
6161 * 2. Request idle freq.
6162 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05306163*/
6164static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
6165{
Chris Wilsonaed242f2015-03-18 09:48:21 +00006166 u32 val = dev_priv->rps.idle_freq;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006167 int err;
Deepak S5549d252014-06-28 11:26:11 +05306168
Chris Wilsonaed242f2015-03-18 09:48:21 +00006169 if (dev_priv->rps.cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05306170 return;
6171
Chris Wilsonc9efef72017-01-02 15:28:45 +00006172 /* The punit delays the write of the frequency and voltage until it
6173 * determines the GPU is awake. During normal usage we don't want to
6174 * waste power changing the frequency if the GPU is sleeping (rc6).
6175 * However, the GPU and driver is now idle and we do not want to delay
6176 * switching to minimum voltage (reducing power whilst idle) as we do
6177 * not expect to be woken in the near future and so must flush the
6178 * change by waking the device.
6179 *
6180 * We choose to take the media powerwell (either would do to trick the
6181 * punit into committing the voltage change) as that takes a lot less
6182 * power than the render powerwell.
6183 */
Deepak Sa7f6e232015-05-09 18:04:44 +05306184 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006185 err = valleyview_set_rps(dev_priv, val);
Deepak Sa7f6e232015-05-09 18:04:44 +05306186 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006187
6188 if (err)
6189 DRM_ERROR("Failed to set RPS for idle\n");
Deepak S76c3552f2014-01-30 23:08:16 +05306190}
6191
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006192void gen6_rps_busy(struct drm_i915_private *dev_priv)
6193{
6194 mutex_lock(&dev_priv->rps.hw_lock);
6195 if (dev_priv->rps.enabled) {
Chris Wilsonbd648182017-02-10 15:03:48 +00006196 u8 freq;
6197
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00006198 if (dev_priv->pm_rps_events & GEN6_PM_RP_UP_EI_EXPIRED)
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006199 gen6_rps_reset_ei(dev_priv);
6200 I915_WRITE(GEN6_PMINTRMSK,
6201 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Michał Winiarski2b83c4c2016-06-20 11:58:27 +02006202
Chris Wilsonc33d2472016-07-04 08:08:36 +01006203 gen6_enable_rps_interrupts(dev_priv);
6204
Chris Wilsonbd648182017-02-10 15:03:48 +00006205 /* Use the user's desired frequency as a guide, but for better
6206 * performance, jump directly to RPe as our starting frequency.
6207 */
6208 freq = max(dev_priv->rps.cur_freq,
6209 dev_priv->rps.efficient_freq);
6210
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006211 if (intel_set_rps(dev_priv,
Chris Wilsonbd648182017-02-10 15:03:48 +00006212 clamp(freq,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006213 dev_priv->rps.min_freq_softlimit,
6214 dev_priv->rps.max_freq_softlimit)))
6215 DRM_DEBUG_DRIVER("Failed to set idle frequency\n");
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006216 }
6217 mutex_unlock(&dev_priv->rps.hw_lock);
6218}
6219
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006220void gen6_rps_idle(struct drm_i915_private *dev_priv)
6221{
Chris Wilsonc33d2472016-07-04 08:08:36 +01006222 /* Flush our bottom-half so that it does not race with us
6223 * setting the idle frequency and so that it is bounded by
6224 * our rpm wakeref. And then disable the interrupts to stop any
6225 * futher RPS reclocking whilst we are asleep.
6226 */
6227 gen6_disable_rps_interrupts(dev_priv);
6228
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006229 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01006230 if (dev_priv->rps.enabled) {
Chris Wilsondc979972016-05-10 14:10:04 +01006231 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Deepak S76c3552f2014-01-30 23:08:16 +05306232 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02006233 else
Chris Wilsondc979972016-05-10 14:10:04 +01006234 gen6_set_rps(dev_priv, dev_priv->rps.idle_freq);
Chris Wilsonc0951f02013-10-10 21:58:50 +01006235 dev_priv->rps.last_adj = 0;
Ville Syrjälä12c100b2016-05-23 17:42:48 +03006236 I915_WRITE(GEN6_PMINTRMSK,
6237 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Chris Wilsonc0951f02013-10-10 21:58:50 +01006238 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01006239 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006240}
6241
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006242void gen6_rps_boost(struct drm_i915_gem_request *rq,
6243 struct intel_rps_client *rps)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006244{
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006245 struct drm_i915_private *i915 = rq->i915;
Chris Wilson74d290f2017-08-17 13:37:06 +01006246 unsigned long flags;
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006247 bool boost;
6248
Chris Wilson8d3afd72015-05-21 21:01:47 +01006249 /* This is intentionally racy! We peek at the state here, then
6250 * validate inside the RPS worker.
6251 */
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006252 if (!i915->rps.enabled)
Chris Wilson8d3afd72015-05-21 21:01:47 +01006253 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00006254
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006255 boost = false;
Chris Wilson74d290f2017-08-17 13:37:06 +01006256 spin_lock_irqsave(&rq->lock, flags);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006257 if (!rq->waitboost && !i915_gem_request_completed(rq)) {
6258 atomic_inc(&i915->rps.num_waiters);
6259 rq->waitboost = true;
6260 boost = true;
Chris Wilsonc0951f02013-10-10 21:58:50 +01006261 }
Chris Wilson74d290f2017-08-17 13:37:06 +01006262 spin_unlock_irqrestore(&rq->lock, flags);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01006263 if (!boost)
6264 return;
6265
6266 if (READ_ONCE(i915->rps.cur_freq) < i915->rps.boost_freq)
6267 schedule_work(&i915->rps.work);
6268
6269 atomic_inc(rps ? &rps->boosts : &i915->rps.boosts);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01006270}
6271
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006272int intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006273{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006274 int err;
6275
Chris Wilsoncfd1c482017-02-20 09:47:07 +00006276 lockdep_assert_held(&dev_priv->rps.hw_lock);
6277 GEM_BUG_ON(val > dev_priv->rps.max_freq);
6278 GEM_BUG_ON(val < dev_priv->rps.min_freq);
6279
Chris Wilson76e4e4b2017-02-20 09:47:08 +00006280 if (!dev_priv->rps.enabled) {
6281 dev_priv->rps.cur_freq = val;
6282 return 0;
6283 }
6284
Chris Wilsondc979972016-05-10 14:10:04 +01006285 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006286 err = valleyview_set_rps(dev_priv, val);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02006287 else
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006288 err = gen6_set_rps(dev_priv, val);
6289
6290 return err;
Jesse Barnes0a073b82013-04-17 15:54:58 -07006291}
6292
Chris Wilsondc979972016-05-10 14:10:04 +01006293static void gen9_disable_rc6(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00006294{
Zhe Wang20e49362014-11-04 17:07:05 +00006295 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00006296 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00006297}
6298
Chris Wilsondc979972016-05-10 14:10:04 +01006299static void gen9_disable_rps(struct drm_i915_private *dev_priv)
Akash Goel2030d682016-04-23 00:05:45 +05306300{
Akash Goel2030d682016-04-23 00:05:45 +05306301 I915_WRITE(GEN6_RP_CONTROL, 0);
6302}
6303
Chris Wilsondc979972016-05-10 14:10:04 +01006304static void gen6_disable_rps(struct drm_i915_private *dev_priv)
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006305{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006306 I915_WRITE(GEN6_RC_CONTROL, 0);
6307 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Akash Goel2030d682016-04-23 00:05:45 +05306308 I915_WRITE(GEN6_RP_CONTROL, 0);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02006309}
6310
Chris Wilsondc979972016-05-10 14:10:04 +01006311static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306312{
Deepak S38807742014-05-23 21:00:15 +05306313 I915_WRITE(GEN6_RC_CONTROL, 0);
6314}
6315
Chris Wilsondc979972016-05-10 14:10:04 +01006316static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006317{
Deepak S98a2e5f2014-08-18 10:35:27 -07006318 /* we're doing forcewake before Disabling RC6,
6319 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02006320 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07006321
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006322 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006323
Mika Kuoppala59bad942015-01-16 11:34:40 +02006324 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006325}
6326
Chris Wilsondc979972016-05-10 14:10:04 +01006327static void intel_print_rc6_info(struct drm_i915_private *dev_priv, u32 mode)
Ben Widawskydc39fff2013-10-18 12:32:07 -07006328{
Chris Wilsondc979972016-05-10 14:10:04 +01006329 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Imre Deak91ca6892014-04-14 20:24:25 +03006330 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
6331 mode = GEN6_RC_CTL_RC6_ENABLE;
6332 else
6333 mode = 0;
6334 }
Chris Wilsondc979972016-05-10 14:10:04 +01006335 if (HAS_RC6p(dev_priv))
Imre Deakb99d49c2016-06-29 19:13:54 +03006336 DRM_DEBUG_DRIVER("Enabling RC6 states: "
6337 "RC6 %s RC6p %s RC6pp %s\n",
6338 onoff(mode & GEN6_RC_CTL_RC6_ENABLE),
6339 onoff(mode & GEN6_RC_CTL_RC6p_ENABLE),
6340 onoff(mode & GEN6_RC_CTL_RC6pp_ENABLE));
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07006341
6342 else
Imre Deakb99d49c2016-06-29 19:13:54 +03006343 DRM_DEBUG_DRIVER("Enabling RC6 states: RC6 %s\n",
6344 onoff(mode & GEN6_RC_CTL_RC6_ENABLE));
Ben Widawskydc39fff2013-10-18 12:32:07 -07006345}
6346
Chris Wilsondc979972016-05-10 14:10:04 +01006347static bool bxt_check_bios_rc6_setup(struct drm_i915_private *dev_priv)
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306348{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006349 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306350 bool enable_rc6 = true;
6351 unsigned long rc6_ctx_base;
Imre Deakfc619842016-06-29 19:13:55 +03006352 u32 rc_ctl;
6353 int rc_sw_target;
6354
6355 rc_ctl = I915_READ(GEN6_RC_CONTROL);
6356 rc_sw_target = (I915_READ(GEN6_RC_STATE) & RC_SW_TARGET_STATE_MASK) >>
6357 RC_SW_TARGET_STATE_SHIFT;
6358 DRM_DEBUG_DRIVER("BIOS enabled RC states: "
6359 "HW_CTRL %s HW_RC6 %s SW_TARGET_STATE %x\n",
6360 onoff(rc_ctl & GEN6_RC_CTL_HW_ENABLE),
6361 onoff(rc_ctl & GEN6_RC_CTL_RC6_ENABLE),
6362 rc_sw_target);
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306363
6364 if (!(I915_READ(RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006365 DRM_DEBUG_DRIVER("RC6 Base location not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306366 enable_rc6 = false;
6367 }
6368
6369 /*
6370 * The exact context size is not known for BXT, so assume a page size
6371 * for this check.
6372 */
6373 rc6_ctx_base = I915_READ(RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006374 if (!((rc6_ctx_base >= ggtt->stolen_reserved_base) &&
6375 (rc6_ctx_base + PAGE_SIZE <= ggtt->stolen_reserved_base +
6376 ggtt->stolen_reserved_size))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006377 DRM_DEBUG_DRIVER("RC6 Base address not as expected.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306378 enable_rc6 = false;
6379 }
6380
6381 if (!(((I915_READ(PWRCTX_MAXCNT_RCSUNIT) & IDLE_TIME_MASK) > 1) &&
6382 ((I915_READ(PWRCTX_MAXCNT_VCSUNIT0) & IDLE_TIME_MASK) > 1) &&
6383 ((I915_READ(PWRCTX_MAXCNT_BCSUNIT) & IDLE_TIME_MASK) > 1) &&
6384 ((I915_READ(PWRCTX_MAXCNT_VECSUNIT) & IDLE_TIME_MASK) > 1))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03006385 DRM_DEBUG_DRIVER("Engine Idle wait time not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306386 enable_rc6 = false;
6387 }
6388
Imre Deakfc619842016-06-29 19:13:55 +03006389 if (!I915_READ(GEN8_PUSHBUS_CONTROL) ||
6390 !I915_READ(GEN8_PUSHBUS_ENABLE) ||
6391 !I915_READ(GEN8_PUSHBUS_SHIFT)) {
6392 DRM_DEBUG_DRIVER("Pushbus not setup properly.\n");
6393 enable_rc6 = false;
6394 }
6395
6396 if (!I915_READ(GEN6_GFXPAUSE)) {
6397 DRM_DEBUG_DRIVER("GFX pause not setup properly.\n");
6398 enable_rc6 = false;
6399 }
6400
6401 if (!I915_READ(GEN8_MISC_CTRL0)) {
6402 DRM_DEBUG_DRIVER("GPM control not setup properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306403 enable_rc6 = false;
6404 }
6405
6406 return enable_rc6;
6407}
6408
Chris Wilsondc979972016-05-10 14:10:04 +01006409int sanitize_rc6_option(struct drm_i915_private *dev_priv, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006410{
Daniel Vettere7d66d82015-06-15 23:23:54 +02006411 /* No RC6 before Ironlake and code is gone for ilk. */
Chris Wilsondc979972016-05-10 14:10:04 +01006412 if (INTEL_INFO(dev_priv)->gen < 6)
Imre Deake6069ca2014-04-18 16:01:02 +03006413 return 0;
6414
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306415 if (!enable_rc6)
6416 return 0;
6417
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02006418 if (IS_GEN9_LP(dev_priv) && !bxt_check_bios_rc6_setup(dev_priv)) {
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05306419 DRM_INFO("RC6 disabled by BIOS\n");
6420 return 0;
6421 }
6422
Daniel Vetter456470e2012-08-08 23:35:40 +02006423 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03006424 if (enable_rc6 >= 0) {
6425 int mask;
6426
Chris Wilsondc979972016-05-10 14:10:04 +01006427 if (HAS_RC6p(dev_priv))
Imre Deake6069ca2014-04-18 16:01:02 +03006428 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
6429 INTEL_RC6pp_ENABLE;
6430 else
6431 mask = INTEL_RC6_ENABLE;
6432
6433 if ((enable_rc6 & mask) != enable_rc6)
Imre Deakb99d49c2016-06-29 19:13:54 +03006434 DRM_DEBUG_DRIVER("Adjusting RC6 mask to %d "
6435 "(requested %d, valid %d)\n",
6436 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03006437
6438 return enable_rc6 & mask;
6439 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006440
Chris Wilsondc979972016-05-10 14:10:04 +01006441 if (IS_IVYBRIDGE(dev_priv))
Ben Widawskycca84a12014-01-28 20:25:38 -08006442 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08006443
6444 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006445}
6446
Chris Wilsondc979972016-05-10 14:10:04 +01006447static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
Imre Deake6069ca2014-04-18 16:01:02 +03006448{
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006449 /* All of these values are in units of 50MHz */
Chris Wilson773ea9a2016-07-13 09:10:33 +01006450
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006451 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02006452 if (IS_GEN9_LP(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006453 u32 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07006454 dev_priv->rps.rp0_freq = (rp_state_cap >> 16) & 0xff;
6455 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
6456 dev_priv->rps.min_freq = (rp_state_cap >> 0) & 0xff;
6457 } else {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006458 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07006459 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
6460 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
6461 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
6462 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006463 /* hw_max = RP0 until we check for overclocking */
Chris Wilson773ea9a2016-07-13 09:10:33 +01006464 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006465
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006466 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01006467 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006468 IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01006469 u32 ddcc_status = 0;
6470
6471 if (sandybridge_pcode_read(dev_priv,
6472 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
6473 &ddcc_status) == 0)
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006474 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08006475 clamp_t(u8,
6476 ((ddcc_status >> 8) & 0xff),
6477 dev_priv->rps.min_freq,
6478 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006479 }
6480
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006481 if (IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Akash Goelc5e06882015-06-29 14:50:19 +05306482 /* Store the frequency values in 16.66 MHZ units, which is
Chris Wilson773ea9a2016-07-13 09:10:33 +01006483 * the natural hardware unit for SKL
6484 */
Akash Goelc5e06882015-06-29 14:50:19 +05306485 dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
6486 dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
6487 dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
6488 dev_priv->rps.max_freq *= GEN9_FREQ_SCALER;
6489 dev_priv->rps.efficient_freq *= GEN9_FREQ_SCALER;
6490 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07006491}
6492
Chris Wilson3a45b052016-07-13 09:10:32 +01006493static void reset_rps(struct drm_i915_private *dev_priv,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006494 int (*set)(struct drm_i915_private *, u8))
Chris Wilson3a45b052016-07-13 09:10:32 +01006495{
6496 u8 freq = dev_priv->rps.cur_freq;
6497
6498 /* force a reset */
6499 dev_priv->rps.power = -1;
6500 dev_priv->rps.cur_freq = -1;
6501
Chris Wilson9fcee2f2017-01-26 10:19:19 +00006502 if (set(dev_priv, freq))
6503 DRM_ERROR("Failed to reset RPS to initial values\n");
Chris Wilson3a45b052016-07-13 09:10:32 +01006504}
6505
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006506/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Chris Wilsondc979972016-05-10 14:10:04 +01006507static void gen9_enable_rps(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00006508{
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006509 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
6510
Akash Goel0beb0592015-03-06 11:07:20 +05306511 /* Program defaults and thresholds for RPS*/
6512 I915_WRITE(GEN6_RC_VIDEO_FREQ,
6513 GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006514
Akash Goel0beb0592015-03-06 11:07:20 +05306515 /* 1 second timeout*/
6516 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
6517 GT_INTERVAL_FROM_US(dev_priv, 1000000));
6518
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006519 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006520
Akash Goel0beb0592015-03-06 11:07:20 +05306521 /* Leaning on the below call to gen6_set_rps to program/setup the
6522 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
6523 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
Chris Wilson3a45b052016-07-13 09:10:32 +01006524 reset_rps(dev_priv, gen6_set_rps);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006525
6526 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
6527}
6528
Chris Wilsondc979972016-05-10 14:10:04 +01006529static void gen9_enable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006530{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006531 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306532 enum intel_engine_id id;
Zhe Wang20e49362014-11-04 17:07:05 +00006533 uint32_t rc6_mask = 0;
Zhe Wang20e49362014-11-04 17:07:05 +00006534
6535 /* 1a: Software RC state - RC0 */
6536 I915_WRITE(GEN6_RC_STATE, 0);
6537
6538 /* 1b: Get forcewake during program sequence. Although the driver
6539 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006540 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00006541
6542 /* 2a: Disable RC states. */
6543 I915_WRITE(GEN6_RC_CONTROL, 0);
6544
6545 /* 2b: Program RC6 thresholds.*/
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05306546
6547 /* WaRsDoubleRc6WrlWithCoarsePowerGating: Doubling WRL only when CPG is enabled */
Chris Wilsondc979972016-05-10 14:10:04 +01006548 if (IS_SKYLAKE(dev_priv))
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05306549 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
6550 else
6551 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
Zhe Wang20e49362014-11-04 17:07:05 +00006552 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6553 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05306554 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006555 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05306556
Dave Gordon1a3d1892016-05-13 15:36:30 +01006557 if (HAS_GUC(dev_priv))
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05306558 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
6559
Zhe Wang20e49362014-11-04 17:07:05 +00006560 I915_WRITE(GEN6_RC_SLEEP, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00006561
Zhe Wang38c23522015-01-20 12:23:04 +00006562 /* 2c: Program Coarse Power Gating Policies. */
6563 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
6564 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
6565
Zhe Wang20e49362014-11-04 17:07:05 +00006566 /* 3a: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006567 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Zhe Wang20e49362014-11-04 17:07:05 +00006568 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Jani Nikula87ad3212016-01-14 12:53:34 +02006569 DRM_INFO("RC6 %s\n", onoff(rc6_mask & GEN6_RC_CTL_RC6_ENABLE));
Chris Wilson1c044f92017-01-25 17:26:01 +00006570 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
6571 I915_WRITE(GEN6_RC_CONTROL,
6572 GEN6_RC_CTL_HW_ENABLE | GEN6_RC_CTL_EI_MODE(1) | rc6_mask);
Zhe Wang20e49362014-11-04 17:07:05 +00006573
Sagar Kamblecb07bae2015-04-12 11:28:14 +05306574 /*
6575 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05306576 * WaRsDisableCoarsePowerGating:skl,bxt - Render/Media PG need to be disabled with RC6.
Sagar Kamblecb07bae2015-04-12 11:28:14 +05306577 */
Chris Wilsondc979972016-05-10 14:10:04 +01006578 if (NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05306579 I915_WRITE(GEN9_PG_ENABLE, 0);
6580 else
6581 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
6582 (GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE) : 0);
Zhe Wang38c23522015-01-20 12:23:04 +00006583
Mika Kuoppala59bad942015-01-16 11:34:40 +02006584 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00006585}
6586
Chris Wilsondc979972016-05-10 14:10:04 +01006587static void gen8_enable_rps(struct drm_i915_private *dev_priv)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006588{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006589 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306590 enum intel_engine_id id;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08006591 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006592
6593 /* 1a: Software RC state - RC0 */
6594 I915_WRITE(GEN6_RC_STATE, 0);
6595
6596 /* 1c & 1d: Get forcewake during program sequence. Although the driver
6597 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006598 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006599
6600 /* 2a: Disable RC states. */
6601 I915_WRITE(GEN6_RC_CONTROL, 0);
6602
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006603 /* 2b: Program RC6 thresholds.*/
6604 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
6605 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6606 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05306607 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006608 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006609 I915_WRITE(GEN6_RC_SLEEP, 0);
Chris Wilsondc979972016-05-10 14:10:04 +01006610 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07006611 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
6612 else
6613 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006614
6615 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006616 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006617 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Chris Wilsondc979972016-05-10 14:10:04 +01006618 intel_print_rc6_info(dev_priv, rc6_mask);
6619 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07006620 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
6621 GEN7_RC_CTL_TO_MODE |
6622 rc6_mask);
6623 else
6624 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
6625 GEN6_RC_CTL_EI_MODE(1) |
6626 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006627
6628 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07006629 I915_WRITE(GEN6_RPNSWREQ,
6630 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
6631 I915_WRITE(GEN6_RC_VIDEO_FREQ,
6632 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02006633 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
6634 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006635
Daniel Vetter7526ed72014-09-29 15:07:19 +02006636 /* Docs recommend 900MHz, and 300 MHz respectively */
6637 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
6638 dev_priv->rps.max_freq_softlimit << 24 |
6639 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006640
Daniel Vetter7526ed72014-09-29 15:07:19 +02006641 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
6642 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
6643 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
6644 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006645
Daniel Vetter7526ed72014-09-29 15:07:19 +02006646 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006647
6648 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02006649 I915_WRITE(GEN6_RP_CONTROL,
6650 GEN6_RP_MEDIA_TURBO |
6651 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6652 GEN6_RP_MEDIA_IS_GFX |
6653 GEN6_RP_ENABLE |
6654 GEN6_RP_UP_BUSY_AVG |
6655 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006656
Daniel Vetter7526ed72014-09-29 15:07:19 +02006657 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006658
Chris Wilson3a45b052016-07-13 09:10:32 +01006659 reset_rps(dev_priv, gen6_set_rps);
Daniel Vetter7526ed72014-09-29 15:07:19 +02006660
Mika Kuoppala59bad942015-01-16 11:34:40 +02006661 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006662}
6663
Chris Wilsondc979972016-05-10 14:10:04 +01006664static void gen6_enable_rps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006665{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006666 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306667 enum intel_engine_id id;
Chris Wilson99ac9612016-07-13 09:10:34 +01006668 u32 rc6vids, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006669 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006670 int rc6_mode;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00006671 int ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006672
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006673 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02006674
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006675 /* Here begins a magic sequence of register writes to enable
6676 * auto-downclocking.
6677 *
6678 * Perhaps there might be some value in exposing these to
6679 * userspace...
6680 */
6681 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006682
6683 /* Clear the DBG now so we don't confuse earlier errors */
Ville Syrjälä297b32e2016-04-13 21:09:30 +03006684 gtfifodbg = I915_READ(GTFIFODBG);
6685 if (gtfifodbg) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006686 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
6687 I915_WRITE(GTFIFODBG, gtfifodbg);
6688 }
6689
Mika Kuoppala59bad942015-01-16 11:34:40 +02006690 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006691
6692 /* disable the counters and set deterministic thresholds */
6693 I915_WRITE(GEN6_RC_CONTROL, 0);
6694
6695 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
6696 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
6697 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
6698 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
6699 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
6700
Akash Goel3b3f1652016-10-13 22:44:48 +05306701 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006702 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006703
6704 I915_WRITE(GEN6_RC_SLEEP, 0);
6705 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Chris Wilsondc979972016-05-10 14:10:04 +01006706 if (IS_IVYBRIDGE(dev_priv))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07006707 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
6708 else
6709 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08006710 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006711 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
6712
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006713 /* Check if we are enabling RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006714 rc6_mode = intel_enable_rc6();
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006715 if (rc6_mode & INTEL_RC6_ENABLE)
6716 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
6717
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006718 /* We don't use those on Haswell */
Chris Wilsondc979972016-05-10 14:10:04 +01006719 if (!IS_HASWELL(dev_priv)) {
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006720 if (rc6_mode & INTEL_RC6p_ENABLE)
6721 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006722
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03006723 if (rc6_mode & INTEL_RC6pp_ENABLE)
6724 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
6725 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006726
Chris Wilsondc979972016-05-10 14:10:04 +01006727 intel_print_rc6_info(dev_priv, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006728
6729 I915_WRITE(GEN6_RC_CONTROL,
6730 rc6_mask |
6731 GEN6_RC_CTL_EI_MODE(1) |
6732 GEN6_RC_CTL_HW_ENABLE);
6733
Chris Wilsondd75fdc2013-09-25 17:34:57 +01006734 /* Power down if completely idle for over 50ms */
6735 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006736 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006737
Chris Wilson3a45b052016-07-13 09:10:32 +01006738 reset_rps(dev_priv, gen6_set_rps);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006739
Ben Widawsky31643d52012-09-26 10:34:01 -07006740 rc6vids = 0;
6741 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
Chris Wilsondc979972016-05-10 14:10:04 +01006742 if (IS_GEN6(dev_priv) && ret) {
Ben Widawsky31643d52012-09-26 10:34:01 -07006743 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
Chris Wilsondc979972016-05-10 14:10:04 +01006744 } else if (IS_GEN6(dev_priv) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
Ben Widawsky31643d52012-09-26 10:34:01 -07006745 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
6746 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
6747 rc6vids &= 0xffff00;
6748 rc6vids |= GEN6_ENCODE_RC6_VID(450);
6749 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
6750 if (ret)
6751 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
6752 }
6753
Mika Kuoppala59bad942015-01-16 11:34:40 +02006754 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006755}
6756
Chris Wilsonfb7404e2016-07-13 09:10:38 +01006757static void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006758{
6759 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01006760 unsigned int gpu_freq;
6761 unsigned int max_ia_freq, min_ring_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05306762 unsigned int max_gpu_freq, min_gpu_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006763 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03006764 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006765
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006766 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02006767
Ben Widawskyeda79642013-10-07 17:15:48 -03006768 policy = cpufreq_cpu_get(0);
6769 if (policy) {
6770 max_ia_freq = policy->cpuinfo.max_freq;
6771 cpufreq_cpu_put(policy);
6772 } else {
6773 /*
6774 * Default to measured freq if none found, PCU will ensure we
6775 * don't go over
6776 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006777 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03006778 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006779
6780 /* Convert from kHz to MHz */
6781 max_ia_freq /= 1000;
6782
Ben Widawsky153b4b952013-10-22 22:05:09 -07006783 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07006784 /* convert DDR frequency from units of 266.6MHz to bandwidth */
6785 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01006786
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006787 if (IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05306788 /* Convert GT frequency to 50 HZ units */
6789 min_gpu_freq = dev_priv->rps.min_freq / GEN9_FREQ_SCALER;
6790 max_gpu_freq = dev_priv->rps.max_freq / GEN9_FREQ_SCALER;
6791 } else {
6792 min_gpu_freq = dev_priv->rps.min_freq;
6793 max_gpu_freq = dev_priv->rps.max_freq;
6794 }
6795
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006796 /*
6797 * For each potential GPU frequency, load a ring frequency we'd like
6798 * to use for memory access. We do this by specifying the IA frequency
6799 * the PCU should use as a reference to determine the ring frequency.
6800 */
Akash Goel4c8c7742015-06-29 14:50:20 +05306801 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
6802 int diff = max_gpu_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01006803 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006804
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07006805 if (IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05306806 /*
6807 * ring_freq = 2 * GT. ring_freq is in 100MHz units
6808 * No floor required for ring frequency on SKL.
6809 */
6810 ring_freq = gpu_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01006811 } else if (INTEL_INFO(dev_priv)->gen >= 8) {
Ben Widawsky46c764d2013-11-02 21:07:49 -07006812 /* max(2 * GT, DDR). NB: GT is 50MHz units */
6813 ring_freq = max(min_ring_freq, gpu_freq);
Chris Wilsondc979972016-05-10 14:10:04 +01006814 } else if (IS_HASWELL(dev_priv)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07006815 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01006816 ring_freq = max(min_ring_freq, ring_freq);
6817 /* leave ia_freq as the default, chosen by cpufreq */
6818 } else {
6819 /* On older processors, there is no separate ring
6820 * clock domain, so in order to boost the bandwidth
6821 * of the ring, we need to upclock the CPU (ia_freq).
6822 *
6823 * For GPU frequencies less than 750MHz,
6824 * just use the lowest ring freq.
6825 */
6826 if (gpu_freq < min_freq)
6827 ia_freq = 800;
6828 else
6829 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
6830 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
6831 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006832
Ben Widawsky42c05262012-09-26 10:34:00 -07006833 sandybridge_pcode_write(dev_priv,
6834 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01006835 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
6836 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
6837 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006838 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03006839}
6840
Ville Syrjälä03af2042014-06-28 02:03:53 +03006841static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05306842{
6843 u32 val, rp0;
6844
Jani Nikula5b5929c2015-10-07 11:17:46 +03006845 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05306846
Imre Deak43b67992016-08-31 19:13:02 +03006847 switch (INTEL_INFO(dev_priv)->sseu.eu_total) {
Jani Nikula5b5929c2015-10-07 11:17:46 +03006848 case 8:
6849 /* (2 * 4) config */
6850 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
6851 break;
6852 case 12:
6853 /* (2 * 6) config */
6854 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
6855 break;
6856 case 16:
6857 /* (2 * 8) config */
6858 default:
6859 /* Setting (2 * 8) Min RP0 for any other combination */
6860 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
6861 break;
Deepak S095acd52015-01-17 11:05:59 +05306862 }
Jani Nikula5b5929c2015-10-07 11:17:46 +03006863
6864 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
6865
Deepak S2b6b3a02014-05-27 15:59:30 +05306866 return rp0;
6867}
6868
6869static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
6870{
6871 u32 val, rpe;
6872
6873 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
6874 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
6875
6876 return rpe;
6877}
6878
Deepak S7707df42014-07-12 18:46:14 +05306879static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
6880{
6881 u32 val, rp1;
6882
Jani Nikula5b5929c2015-10-07 11:17:46 +03006883 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
6884 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
6885
Deepak S7707df42014-07-12 18:46:14 +05306886 return rp1;
6887}
6888
Deepak S96676fe2016-08-12 18:46:41 +05306889static u32 cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
6890{
6891 u32 val, rpn;
6892
6893 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
6894 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
6895 FB_GFX_FREQ_FUSE_MASK);
6896
6897 return rpn;
6898}
6899
Deepak Sf8f2b002014-07-10 13:16:21 +05306900static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
6901{
6902 u32 val, rp1;
6903
6904 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
6905
6906 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
6907
6908 return rp1;
6909}
6910
Ville Syrjälä03af2042014-06-28 02:03:53 +03006911static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006912{
6913 u32 val, rp0;
6914
Jani Nikula64936252013-05-22 15:36:20 +03006915 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006916
6917 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
6918 /* Clamp to max */
6919 rp0 = min_t(u32, rp0, 0xea);
6920
6921 return rp0;
6922}
6923
6924static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
6925{
6926 u32 val, rpe;
6927
Jani Nikula64936252013-05-22 15:36:20 +03006928 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006929 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03006930 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006931 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
6932
6933 return rpe;
6934}
6935
Ville Syrjälä03af2042014-06-28 02:03:53 +03006936static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006937{
Imre Deak36146032014-12-04 18:39:35 +02006938 u32 val;
6939
6940 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
6941 /*
6942 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
6943 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
6944 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
6945 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
6946 * to make sure it matches what Punit accepts.
6947 */
6948 return max_t(u32, val, 0xc0);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006949}
6950
Imre Deakae484342014-03-31 15:10:44 +03006951/* Check that the pctx buffer wasn't move under us. */
6952static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
6953{
6954 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
6955
6956 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
6957 dev_priv->vlv_pctx->stolen->start);
6958}
6959
Deepak S38807742014-05-23 21:00:15 +05306960
6961/* Check that the pcbr address is not empty. */
6962static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
6963{
6964 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
6965
6966 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
6967}
6968
Chris Wilsondc979972016-05-10 14:10:04 +01006969static void cherryview_setup_pctx(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306970{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02006971 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006972 unsigned long pctx_paddr, paddr;
Deepak S38807742014-05-23 21:00:15 +05306973 u32 pcbr;
6974 int pctx_size = 32*1024;
6975
Deepak S38807742014-05-23 21:00:15 +05306976 pcbr = I915_READ(VLV_PCBR);
6977 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006978 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05306979 paddr = (dev_priv->mm.stolen_base +
Joonas Lahtinen62106b42016-03-18 10:42:57 +02006980 (ggtt->stolen_size - pctx_size));
Deepak S38807742014-05-23 21:00:15 +05306981
6982 pctx_paddr = (paddr & (~4095));
6983 I915_WRITE(VLV_PCBR, pctx_paddr);
6984 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006985
6986 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05306987}
6988
Chris Wilsondc979972016-05-10 14:10:04 +01006989static void valleyview_setup_pctx(struct drm_i915_private *dev_priv)
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006990{
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006991 struct drm_i915_gem_object *pctx;
6992 unsigned long pctx_paddr;
6993 u32 pcbr;
6994 int pctx_size = 24*1024;
6995
6996 pcbr = I915_READ(VLV_PCBR);
6997 if (pcbr) {
6998 /* BIOS set it up already, grab the pre-alloc'd space */
6999 int pcbr_offset;
7000
7001 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00007002 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007003 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02007004 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007005 pctx_size);
7006 goto out;
7007 }
7008
Ville Syrjäläce611ef2014-11-07 21:33:46 +02007009 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
7010
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007011 /*
7012 * From the Gunit register HAS:
7013 * The Gfx driver is expected to program this register and ensure
7014 * proper allocation within Gfx stolen memory. For example, this
7015 * register should be programmed such than the PCBR range does not
7016 * overlap with other ranges, such as the frame buffer, protected
7017 * memory, or any other relevant ranges.
7018 */
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00007019 pctx = i915_gem_object_create_stolen(dev_priv, pctx_size);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007020 if (!pctx) {
7021 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
Tvrtko Ursulinee504892016-02-11 10:27:30 +00007022 goto out;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007023 }
7024
7025 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
7026 I915_WRITE(VLV_PCBR, pctx_paddr);
7027
7028out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02007029 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07007030 dev_priv->vlv_pctx = pctx;
7031}
7032
Chris Wilsondc979972016-05-10 14:10:04 +01007033static void valleyview_cleanup_pctx(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007034{
Imre Deakae484342014-03-31 15:10:44 +03007035 if (WARN_ON(!dev_priv->vlv_pctx))
7036 return;
7037
Chris Wilsonf0cd5182016-10-28 13:58:43 +01007038 i915_gem_object_put(dev_priv->vlv_pctx);
Imre Deakae484342014-03-31 15:10:44 +03007039 dev_priv->vlv_pctx = NULL;
7040}
7041
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007042static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
7043{
7044 dev_priv->rps.gpll_ref_freq =
7045 vlv_get_cck_clock(dev_priv, "GPLL ref",
7046 CCK_GPLL_CLOCK_CONTROL,
7047 dev_priv->czclk_freq);
7048
7049 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
7050 dev_priv->rps.gpll_ref_freq);
7051}
7052
Chris Wilsondc979972016-05-10 14:10:04 +01007053static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03007054{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007055 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03007056
Chris Wilsondc979972016-05-10 14:10:04 +01007057 valleyview_setup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007058
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007059 vlv_init_gpll_ref_freq(dev_priv);
7060
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007061 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
7062 switch ((val >> 6) & 3) {
7063 case 0:
7064 case 1:
7065 dev_priv->mem_freq = 800;
7066 break;
7067 case 2:
7068 dev_priv->mem_freq = 1066;
7069 break;
7070 case 3:
7071 dev_priv->mem_freq = 1333;
7072 break;
7073 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02007074 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007075
Imre Deak4e805192014-04-14 20:24:41 +03007076 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
7077 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
7078 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007079 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03007080 dev_priv->rps.max_freq);
7081
7082 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
7083 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007084 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03007085 dev_priv->rps.efficient_freq);
7086
Deepak Sf8f2b002014-07-10 13:16:21 +05307087 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
7088 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007089 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05307090 dev_priv->rps.rp1_freq);
7091
Imre Deak4e805192014-04-14 20:24:41 +03007092 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
7093 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007094 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03007095 dev_priv->rps.min_freq);
Imre Deak4e805192014-04-14 20:24:41 +03007096}
7097
Chris Wilsondc979972016-05-10 14:10:04 +01007098static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05307099{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007100 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05307101
Chris Wilsondc979972016-05-10 14:10:04 +01007102 cherryview_setup_pctx(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307103
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007104 vlv_init_gpll_ref_freq(dev_priv);
7105
Ville Syrjäläa5805162015-05-26 20:42:30 +03007106 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02007107 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007108 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02007109
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007110 switch ((val >> 2) & 0x7) {
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007111 case 3:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007112 dev_priv->mem_freq = 2000;
7113 break;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03007114 default:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007115 dev_priv->mem_freq = 1600;
7116 break;
7117 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02007118 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03007119
Deepak S2b6b3a02014-05-27 15:59:30 +05307120 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
7121 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
7122 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007123 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05307124 dev_priv->rps.max_freq);
7125
7126 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
7127 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007128 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05307129 dev_priv->rps.efficient_freq);
7130
Deepak S7707df42014-07-12 18:46:14 +05307131 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
7132 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007133 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05307134 dev_priv->rps.rp1_freq);
7135
Deepak S96676fe2016-08-12 18:46:41 +05307136 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05307137 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02007138 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05307139 dev_priv->rps.min_freq);
7140
Ville Syrjälä1c147622014-08-18 14:42:43 +03007141 WARN_ONCE((dev_priv->rps.max_freq |
7142 dev_priv->rps.efficient_freq |
7143 dev_priv->rps.rp1_freq |
7144 dev_priv->rps.min_freq) & 1,
7145 "Odd GPU freq values\n");
Deepak S38807742014-05-23 21:00:15 +05307146}
7147
Chris Wilsondc979972016-05-10 14:10:04 +01007148static void valleyview_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03007149{
Chris Wilsondc979972016-05-10 14:10:04 +01007150 valleyview_cleanup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03007151}
7152
Chris Wilsondc979972016-05-10 14:10:04 +01007153static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05307154{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007155 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05307156 enum intel_engine_id id;
Deepak S2b6b3a02014-05-27 15:59:30 +05307157 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05307158
7159 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
7160
Ville Syrjälä297b32e2016-04-13 21:09:30 +03007161 gtfifodbg = I915_READ(GTFIFODBG) & ~(GT_FIFO_SBDEDICATE_FREE_ENTRY_CHV |
7162 GT_FIFO_FREE_ENTRIES_CHV);
Deepak S38807742014-05-23 21:00:15 +05307163 if (gtfifodbg) {
7164 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
7165 gtfifodbg);
7166 I915_WRITE(GTFIFODBG, gtfifodbg);
7167 }
7168
7169 cherryview_check_pctx(dev_priv);
7170
7171 /* 1a & 1b: Get forcewake during program sequence. Although the driver
7172 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02007173 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05307174
Ville Syrjälä160614a2015-01-19 13:50:47 +02007175 /* Disable RC states. */
7176 I915_WRITE(GEN6_RC_CONTROL, 0);
7177
Deepak S38807742014-05-23 21:00:15 +05307178 /* 2a: Program RC6 thresholds.*/
7179 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
7180 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
7181 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
7182
Akash Goel3b3f1652016-10-13 22:44:48 +05307183 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007184 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Deepak S38807742014-05-23 21:00:15 +05307185 I915_WRITE(GEN6_RC_SLEEP, 0);
7186
Deepak Sf4f71c72015-03-28 15:23:35 +05307187 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
7188 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
Deepak S38807742014-05-23 21:00:15 +05307189
7190 /* allows RC6 residency counter to work */
7191 I915_WRITE(VLV_COUNTER_CONTROL,
7192 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
7193 VLV_MEDIA_RC6_COUNT_EN |
7194 VLV_RENDER_RC6_COUNT_EN));
7195
7196 /* For now we assume BIOS is allocating and populating the PCBR */
7197 pcbr = I915_READ(VLV_PCBR);
7198
Deepak S38807742014-05-23 21:00:15 +05307199 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01007200 if ((intel_enable_rc6() & INTEL_RC6_ENABLE) &&
7201 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02007202 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05307203
7204 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
7205
Deepak S2b6b3a02014-05-27 15:59:30 +05307206 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02007207 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05307208 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
7209 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
7210 I915_WRITE(GEN6_RP_UP_EI, 66000);
7211 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
7212
7213 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7214
7215 /* 5: Enable RPS */
7216 I915_WRITE(GEN6_RP_CONTROL,
7217 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02007218 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05307219 GEN6_RP_ENABLE |
7220 GEN6_RP_UP_BUSY_AVG |
7221 GEN6_RP_DOWN_IDLE_AVG);
7222
Deepak S3ef62342015-04-29 08:36:24 +05307223 /* Setting Fixed Bias */
7224 val = VLV_OVERRIDE_EN |
7225 VLV_SOC_TDP_EN |
7226 CHV_BIAS_CPU_50_SOC_50;
7227 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
7228
Deepak S2b6b3a02014-05-27 15:59:30 +05307229 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
7230
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02007231 /* RPS code assumes GPLL is used */
7232 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
7233
Jani Nikula742f4912015-09-03 11:16:09 +03007234 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05307235 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7236
Chris Wilson3a45b052016-07-13 09:10:32 +01007237 reset_rps(dev_priv, valleyview_set_rps);
Deepak S2b6b3a02014-05-27 15:59:30 +05307238
Mika Kuoppala59bad942015-01-16 11:34:40 +02007239 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05307240}
7241
Chris Wilsondc979972016-05-10 14:10:04 +01007242static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07007243{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007244 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05307245 enum intel_engine_id id;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07007246 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07007247
7248 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
7249
Imre Deakae484342014-03-31 15:10:44 +03007250 valleyview_check_pctx(dev_priv);
7251
Ville Syrjälä297b32e2016-04-13 21:09:30 +03007252 gtfifodbg = I915_READ(GTFIFODBG);
7253 if (gtfifodbg) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07007254 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
7255 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007256 I915_WRITE(GTFIFODBG, gtfifodbg);
7257 }
7258
Deepak Sc8d9a592013-11-23 14:55:42 +05307259 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02007260 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007261
Ville Syrjälä160614a2015-01-19 13:50:47 +02007262 /* Disable RC states. */
7263 I915_WRITE(GEN6_RC_CONTROL, 0);
7264
Ville Syrjäläcad725f2015-01-19 13:50:48 +02007265 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007266 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
7267 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
7268 I915_WRITE(GEN6_RP_UP_EI, 66000);
7269 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
7270
7271 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
7272
7273 I915_WRITE(GEN6_RP_CONTROL,
7274 GEN6_RP_MEDIA_TURBO |
7275 GEN6_RP_MEDIA_HW_NORMAL_MODE |
7276 GEN6_RP_MEDIA_IS_GFX |
7277 GEN6_RP_ENABLE |
7278 GEN6_RP_UP_BUSY_AVG |
7279 GEN6_RP_DOWN_IDLE_CONT);
7280
7281 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
7282 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
7283 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
7284
Akash Goel3b3f1652016-10-13 22:44:48 +05307285 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00007286 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007287
Jesse Barnes2f0aa3042013-11-15 09:32:11 -08007288 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007289
7290 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07007291 I915_WRITE(VLV_COUNTER_CONTROL,
Mika Kuoppala6b7f6aa2017-03-15 18:12:59 +02007292 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
7293 VLV_MEDIA_RC0_COUNT_EN |
Deepak S31685c22014-07-03 17:33:01 -04007294 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07007295 VLV_MEDIA_RC6_COUNT_EN |
7296 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04007297
Chris Wilsondc979972016-05-10 14:10:04 +01007298 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08007299 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07007300
Chris Wilsondc979972016-05-10 14:10:04 +01007301 intel_print_rc6_info(dev_priv, rc6_mode);
Ben Widawskydc39fff2013-10-18 12:32:07 -07007302
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07007303 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007304
Deepak S3ef62342015-04-29 08:36:24 +05307305 /* Setting Fixed Bias */
7306 val = VLV_OVERRIDE_EN |
7307 VLV_SOC_TDP_EN |
7308 VLV_BIAS_CPU_125_SOC_875;
7309 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
7310
Jani Nikula64936252013-05-22 15:36:20 +03007311 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007312
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02007313 /* RPS code assumes GPLL is used */
7314 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
7315
Jani Nikula742f4912015-09-03 11:16:09 +03007316 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07007317 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
7318
Chris Wilson3a45b052016-07-13 09:10:32 +01007319 reset_rps(dev_priv, valleyview_set_rps);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007320
Mika Kuoppala59bad942015-01-16 11:34:40 +02007321 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007322}
7323
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007324static unsigned long intel_pxfreq(u32 vidfreq)
7325{
7326 unsigned long freq;
7327 int div = (vidfreq & 0x3f0000) >> 16;
7328 int post = (vidfreq & 0x3000) >> 12;
7329 int pre = (vidfreq & 0x7);
7330
7331 if (!pre)
7332 return 0;
7333
7334 freq = ((div * 133333) / ((1<<post) * pre));
7335
7336 return freq;
7337}
7338
Daniel Vettereb48eb02012-04-26 23:28:12 +02007339static const struct cparams {
7340 u16 i;
7341 u16 t;
7342 u16 m;
7343 u16 c;
7344} cparams[] = {
7345 { 1, 1333, 301, 28664 },
7346 { 1, 1066, 294, 24460 },
7347 { 1, 800, 294, 25192 },
7348 { 0, 1333, 276, 27605 },
7349 { 0, 1066, 276, 27605 },
7350 { 0, 800, 231, 23784 },
7351};
7352
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007353static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007354{
7355 u64 total_count, diff, ret;
7356 u32 count1, count2, count3, m = 0, c = 0;
7357 unsigned long now = jiffies_to_msecs(jiffies), diff1;
7358 int i;
7359
Chris Wilson67520412017-03-02 13:28:01 +00007360 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007361
Daniel Vetter20e4d402012-08-08 23:35:39 +02007362 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007363
7364 /* Prevent division-by-zero if we are asking too fast.
7365 * Also, we don't get interesting results if we are polling
7366 * faster than once in 10ms, so just return the saved value
7367 * in such cases.
7368 */
7369 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02007370 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007371
7372 count1 = I915_READ(DMIEC);
7373 count2 = I915_READ(DDREC);
7374 count3 = I915_READ(CSIEC);
7375
7376 total_count = count1 + count2 + count3;
7377
7378 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02007379 if (total_count < dev_priv->ips.last_count1) {
7380 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007381 diff += total_count;
7382 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007383 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007384 }
7385
7386 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007387 if (cparams[i].i == dev_priv->ips.c_m &&
7388 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02007389 m = cparams[i].m;
7390 c = cparams[i].c;
7391 break;
7392 }
7393 }
7394
7395 diff = div_u64(diff, diff1);
7396 ret = ((m * diff) + c);
7397 ret = div_u64(ret, 10);
7398
Daniel Vetter20e4d402012-08-08 23:35:39 +02007399 dev_priv->ips.last_count1 = total_count;
7400 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007401
Daniel Vetter20e4d402012-08-08 23:35:39 +02007402 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007403
7404 return ret;
7405}
7406
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007407unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
7408{
7409 unsigned long val;
7410
Chris Wilsondc979972016-05-10 14:10:04 +01007411 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007412 return 0;
7413
7414 spin_lock_irq(&mchdev_lock);
7415
7416 val = __i915_chipset_val(dev_priv);
7417
7418 spin_unlock_irq(&mchdev_lock);
7419
7420 return val;
7421}
7422
Daniel Vettereb48eb02012-04-26 23:28:12 +02007423unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
7424{
7425 unsigned long m, x, b;
7426 u32 tsfs;
7427
7428 tsfs = I915_READ(TSFS);
7429
7430 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
7431 x = I915_READ8(TR1);
7432
7433 b = tsfs & TSFS_INTR_MASK;
7434
7435 return ((m * x) / 127) - b;
7436}
7437
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007438static int _pxvid_to_vd(u8 pxvid)
7439{
7440 if (pxvid == 0)
7441 return 0;
7442
7443 if (pxvid >= 8 && pxvid < 31)
7444 pxvid = 31;
7445
7446 return (pxvid + 2) * 125;
7447}
7448
7449static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007450{
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007451 const int vd = _pxvid_to_vd(pxvid);
7452 const int vm = vd - 1125;
7453
Chris Wilsondc979972016-05-10 14:10:04 +01007454 if (INTEL_INFO(dev_priv)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02007455 return vm > 0 ? vm : 0;
7456
7457 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007458}
7459
Daniel Vetter02d71952012-08-09 16:44:54 +02007460static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007461{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00007462 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007463 u32 count;
7464
Chris Wilson67520412017-03-02 13:28:01 +00007465 lockdep_assert_held(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007466
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00007467 now = ktime_get_raw_ns();
7468 diffms = now - dev_priv->ips.last_time2;
7469 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007470
7471 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02007472 if (!diffms)
7473 return;
7474
7475 count = I915_READ(GFXEC);
7476
Daniel Vetter20e4d402012-08-08 23:35:39 +02007477 if (count < dev_priv->ips.last_count2) {
7478 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007479 diff += count;
7480 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02007481 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007482 }
7483
Daniel Vetter20e4d402012-08-08 23:35:39 +02007484 dev_priv->ips.last_count2 = count;
7485 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007486
7487 /* More magic constants... */
7488 diff = diff * 1181;
7489 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02007490 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007491}
7492
Daniel Vetter02d71952012-08-09 16:44:54 +02007493void i915_update_gfx_val(struct drm_i915_private *dev_priv)
7494{
Chris Wilsondc979972016-05-10 14:10:04 +01007495 if (INTEL_INFO(dev_priv)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02007496 return;
7497
Daniel Vetter92703882012-08-09 16:46:01 +02007498 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007499
7500 __i915_update_gfx_val(dev_priv);
7501
Daniel Vetter92703882012-08-09 16:46:01 +02007502 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007503}
7504
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007505static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02007506{
7507 unsigned long t, corr, state1, corr2, state2;
7508 u32 pxvid, ext_v;
7509
Chris Wilson67520412017-03-02 13:28:01 +00007510 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02007511
Ville Syrjälä616847e2015-09-18 20:03:19 +03007512 pxvid = I915_READ(PXVFREQ(dev_priv->rps.cur_freq));
Daniel Vettereb48eb02012-04-26 23:28:12 +02007513 pxvid = (pxvid >> 24) & 0x7f;
7514 ext_v = pvid_to_extvid(dev_priv, pxvid);
7515
7516 state1 = ext_v;
7517
7518 t = i915_mch_val(dev_priv);
7519
7520 /* Revel in the empirically derived constants */
7521
7522 /* Correction factor in 1/100000 units */
7523 if (t > 80)
7524 corr = ((t * 2349) + 135940);
7525 else if (t >= 50)
7526 corr = ((t * 964) + 29317);
7527 else /* < 50 */
7528 corr = ((t * 301) + 1004);
7529
7530 corr = corr * ((150142 * state1) / 10000 - 78642);
7531 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02007532 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007533
7534 state2 = (corr2 * state1) / 10000;
7535 state2 /= 100; /* convert to mW */
7536
Daniel Vetter02d71952012-08-09 16:44:54 +02007537 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007538
Daniel Vetter20e4d402012-08-08 23:35:39 +02007539 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007540}
7541
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007542unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
7543{
7544 unsigned long val;
7545
Chris Wilsondc979972016-05-10 14:10:04 +01007546 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007547 return 0;
7548
7549 spin_lock_irq(&mchdev_lock);
7550
7551 val = __i915_gfx_val(dev_priv);
7552
7553 spin_unlock_irq(&mchdev_lock);
7554
7555 return val;
7556}
7557
Daniel Vettereb48eb02012-04-26 23:28:12 +02007558/**
7559 * i915_read_mch_val - return value for IPS use
7560 *
7561 * Calculate and return a value for the IPS driver to use when deciding whether
7562 * we have thermal and power headroom to increase CPU or GPU power budget.
7563 */
7564unsigned long i915_read_mch_val(void)
7565{
7566 struct drm_i915_private *dev_priv;
7567 unsigned long chipset_val, graphics_val, ret = 0;
7568
Daniel Vetter92703882012-08-09 16:46:01 +02007569 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007570 if (!i915_mch_dev)
7571 goto out_unlock;
7572 dev_priv = i915_mch_dev;
7573
Chris Wilsonf531dcb22012-09-25 10:16:12 +01007574 chipset_val = __i915_chipset_val(dev_priv);
7575 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007576
7577 ret = chipset_val + graphics_val;
7578
7579out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007580 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007581
7582 return ret;
7583}
7584EXPORT_SYMBOL_GPL(i915_read_mch_val);
7585
7586/**
7587 * i915_gpu_raise - raise GPU frequency limit
7588 *
7589 * Raise the limit; IPS indicates we have thermal headroom.
7590 */
7591bool i915_gpu_raise(void)
7592{
7593 struct drm_i915_private *dev_priv;
7594 bool ret = true;
7595
Daniel Vetter92703882012-08-09 16:46:01 +02007596 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007597 if (!i915_mch_dev) {
7598 ret = false;
7599 goto out_unlock;
7600 }
7601 dev_priv = i915_mch_dev;
7602
Daniel Vetter20e4d402012-08-08 23:35:39 +02007603 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
7604 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007605
7606out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007607 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007608
7609 return ret;
7610}
7611EXPORT_SYMBOL_GPL(i915_gpu_raise);
7612
7613/**
7614 * i915_gpu_lower - lower GPU frequency limit
7615 *
7616 * IPS indicates we're close to a thermal limit, so throttle back the GPU
7617 * frequency maximum.
7618 */
7619bool i915_gpu_lower(void)
7620{
7621 struct drm_i915_private *dev_priv;
7622 bool ret = true;
7623
Daniel Vetter92703882012-08-09 16:46:01 +02007624 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007625 if (!i915_mch_dev) {
7626 ret = false;
7627 goto out_unlock;
7628 }
7629 dev_priv = i915_mch_dev;
7630
Daniel Vetter20e4d402012-08-08 23:35:39 +02007631 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
7632 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007633
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_lower);
7640
7641/**
7642 * i915_gpu_busy - indicate GPU business to IPS
7643 *
7644 * Tell the IPS driver whether or not the GPU is busy.
7645 */
7646bool i915_gpu_busy(void)
7647{
Daniel Vettereb48eb02012-04-26 23:28:12 +02007648 bool ret = false;
7649
Daniel Vetter92703882012-08-09 16:46:01 +02007650 spin_lock_irq(&mchdev_lock);
Chris Wilsondcff85c2016-08-05 10:14:11 +01007651 if (i915_mch_dev)
7652 ret = i915_mch_dev->gt.awake;
Daniel Vetter92703882012-08-09 16:46:01 +02007653 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007654
7655 return ret;
7656}
7657EXPORT_SYMBOL_GPL(i915_gpu_busy);
7658
7659/**
7660 * i915_gpu_turbo_disable - disable graphics turbo
7661 *
7662 * Disable graphics turbo by resetting the max frequency and setting the
7663 * current frequency to the default.
7664 */
7665bool i915_gpu_turbo_disable(void)
7666{
7667 struct drm_i915_private *dev_priv;
7668 bool ret = true;
7669
Daniel Vetter92703882012-08-09 16:46:01 +02007670 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007671 if (!i915_mch_dev) {
7672 ret = false;
7673 goto out_unlock;
7674 }
7675 dev_priv = i915_mch_dev;
7676
Daniel Vetter20e4d402012-08-08 23:35:39 +02007677 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02007678
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01007679 if (!ironlake_set_drps(dev_priv, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02007680 ret = false;
7681
7682out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02007683 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007684
7685 return ret;
7686}
7687EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
7688
7689/**
7690 * Tells the intel_ips driver that the i915 driver is now loaded, if
7691 * IPS got loaded first.
7692 *
7693 * This awkward dance is so that neither module has to depend on the
7694 * other in order for IPS to do the appropriate communication of
7695 * GPU turbo limits to i915.
7696 */
7697static void
7698ips_ping_for_i915_load(void)
7699{
7700 void (*link)(void);
7701
7702 link = symbol_get(ips_link_to_i915_driver);
7703 if (link) {
7704 link();
7705 symbol_put(ips_link_to_i915_driver);
7706 }
7707}
7708
7709void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
7710{
Daniel Vetter02d71952012-08-09 16:44:54 +02007711 /* We only register the i915 ips part with intel-ips once everything is
7712 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02007713 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007714 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02007715 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007716
7717 ips_ping_for_i915_load();
7718}
7719
7720void intel_gpu_ips_teardown(void)
7721{
Daniel Vetter92703882012-08-09 16:46:01 +02007722 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007723 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02007724 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02007725}
Deepak S76c3552f2014-01-30 23:08:16 +05307726
Chris Wilsondc979972016-05-10 14:10:04 +01007727static void intel_init_emon(struct drm_i915_private *dev_priv)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007728{
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007729 u32 lcfuse;
7730 u8 pxw[16];
7731 int i;
7732
7733 /* Disable to program */
7734 I915_WRITE(ECR, 0);
7735 POSTING_READ(ECR);
7736
7737 /* Program energy weights for various events */
7738 I915_WRITE(SDEW, 0x15040d00);
7739 I915_WRITE(CSIEW0, 0x007f0000);
7740 I915_WRITE(CSIEW1, 0x1e220004);
7741 I915_WRITE(CSIEW2, 0x04000004);
7742
7743 for (i = 0; i < 5; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007744 I915_WRITE(PEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007745 for (i = 0; i < 3; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007746 I915_WRITE(DEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007747
7748 /* Program P-state weights to account for frequency power adjustment */
7749 for (i = 0; i < 16; i++) {
Ville Syrjälä616847e2015-09-18 20:03:19 +03007750 u32 pxvidfreq = I915_READ(PXVFREQ(i));
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007751 unsigned long freq = intel_pxfreq(pxvidfreq);
7752 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
7753 PXVFREQ_PX_SHIFT;
7754 unsigned long val;
7755
7756 val = vid * vid;
7757 val *= (freq / 1000);
7758 val *= 255;
7759 val /= (127*127*900);
7760 if (val > 0xff)
7761 DRM_ERROR("bad pxval: %ld\n", val);
7762 pxw[i] = val;
7763 }
7764 /* Render standby states get 0 weight */
7765 pxw[14] = 0;
7766 pxw[15] = 0;
7767
7768 for (i = 0; i < 4; i++) {
7769 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
7770 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
Ville Syrjälä616847e2015-09-18 20:03:19 +03007771 I915_WRITE(PXW(i), val);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007772 }
7773
7774 /* Adjust magic regs to magic values (more experimental results) */
7775 I915_WRITE(OGW0, 0);
7776 I915_WRITE(OGW1, 0);
7777 I915_WRITE(EG0, 0x00007f00);
7778 I915_WRITE(EG1, 0x0000000e);
7779 I915_WRITE(EG2, 0x000e0000);
7780 I915_WRITE(EG3, 0x68000300);
7781 I915_WRITE(EG4, 0x42000000);
7782 I915_WRITE(EG5, 0x00140031);
7783 I915_WRITE(EG6, 0);
7784 I915_WRITE(EG7, 0);
7785
7786 for (i = 0; i < 8; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03007787 I915_WRITE(PXWL(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007788
7789 /* Enable PMON + select events */
7790 I915_WRITE(ECR, 0x80000019);
7791
7792 lcfuse = I915_READ(LCFUSE02);
7793
Daniel Vetter20e4d402012-08-08 23:35:39 +02007794 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03007795}
7796
Chris Wilsondc979972016-05-10 14:10:04 +01007797void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007798{
Imre Deakb268c692015-12-15 20:10:31 +02007799 /*
7800 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
7801 * requirement.
7802 */
7803 if (!i915.enable_rc6) {
7804 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
7805 intel_runtime_pm_get(dev_priv);
7806 }
Imre Deake6069ca2014-04-18 16:01:02 +03007807
Chris Wilsonb5163db2016-08-10 13:58:24 +01007808 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson773ea9a2016-07-13 09:10:33 +01007809 mutex_lock(&dev_priv->rps.hw_lock);
7810
7811 /* Initialize RPS limits (for userspace) */
Chris Wilsondc979972016-05-10 14:10:04 +01007812 if (IS_CHERRYVIEW(dev_priv))
7813 cherryview_init_gt_powersave(dev_priv);
7814 else if (IS_VALLEYVIEW(dev_priv))
7815 valleyview_init_gt_powersave(dev_priv);
Chris Wilson2a13ae72016-08-02 11:15:27 +01007816 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson773ea9a2016-07-13 09:10:33 +01007817 gen6_init_rps_frequencies(dev_priv);
7818
7819 /* Derive initial user preferences/limits from the hardware limits */
7820 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
7821 dev_priv->rps.cur_freq = dev_priv->rps.idle_freq;
7822
7823 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
7824 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
7825
7826 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
7827 dev_priv->rps.min_freq_softlimit =
7828 max_t(int,
7829 dev_priv->rps.efficient_freq,
7830 intel_freq_opcode(dev_priv, 450));
7831
Chris Wilson99ac9612016-07-13 09:10:34 +01007832 /* After setting max-softlimit, find the overclock max freq */
7833 if (IS_GEN6(dev_priv) ||
7834 IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
7835 u32 params = 0;
7836
7837 sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &params);
7838 if (params & BIT(31)) { /* OC supported */
7839 DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
7840 (dev_priv->rps.max_freq & 0xff) * 50,
7841 (params & 0xff) * 50);
7842 dev_priv->rps.max_freq = params & 0xff;
7843 }
7844 }
7845
Chris Wilson29ecd78d2016-07-13 09:10:35 +01007846 /* Finally allow us to boost to max by default */
7847 dev_priv->rps.boost_freq = dev_priv->rps.max_freq;
7848
Chris Wilson773ea9a2016-07-13 09:10:33 +01007849 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb5163db2016-08-10 13:58:24 +01007850 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson54b4f682016-07-21 21:16:19 +01007851
7852 intel_autoenable_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03007853}
7854
Chris Wilsondc979972016-05-10 14:10:04 +01007855void intel_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03007856{
Ville Syrjälä8dac1e12016-08-02 14:07:33 +03007857 if (IS_VALLEYVIEW(dev_priv))
Chris Wilsondc979972016-05-10 14:10:04 +01007858 valleyview_cleanup_gt_powersave(dev_priv);
Imre Deakb268c692015-12-15 20:10:31 +02007859
7860 if (!i915.enable_rc6)
7861 intel_runtime_pm_put(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03007862}
7863
Chris Wilson54b4f682016-07-21 21:16:19 +01007864/**
7865 * intel_suspend_gt_powersave - suspend PM work and helper threads
7866 * @dev_priv: i915 device
7867 *
7868 * We don't want to disable RC6 or other features here, we just want
7869 * to make sure any work we've queued has finished and won't bother
7870 * us while we're suspended.
7871 */
7872void intel_suspend_gt_powersave(struct drm_i915_private *dev_priv)
7873{
7874 if (INTEL_GEN(dev_priv) < 6)
7875 return;
7876
7877 if (cancel_delayed_work_sync(&dev_priv->rps.autoenable_work))
7878 intel_runtime_pm_put(dev_priv);
7879
7880 /* gen6_rps_idle() will be called later to disable interrupts */
7881}
7882
Chris Wilsonb7137e02016-07-13 09:10:37 +01007883void intel_sanitize_gt_powersave(struct drm_i915_private *dev_priv)
7884{
7885 dev_priv->rps.enabled = true; /* force disabling */
7886 intel_disable_gt_powersave(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007887
7888 gen6_reset_rps_interrupts(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07007889}
7890
Chris Wilsondc979972016-05-10 14:10:04 +01007891void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
Daniel Vetter8090c6b2012-06-24 16:42:32 +02007892{
Chris Wilsonb7137e02016-07-13 09:10:37 +01007893 if (!READ_ONCE(dev_priv->rps.enabled))
7894 return;
Jesse Barnes1a01ab32012-11-02 11:14:00 -07007895
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007896 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007897
Chris Wilsonb7137e02016-07-13 09:10:37 +01007898 if (INTEL_GEN(dev_priv) >= 9) {
7899 gen9_disable_rc6(dev_priv);
7900 gen9_disable_rps(dev_priv);
7901 } else if (IS_CHERRYVIEW(dev_priv)) {
7902 cherryview_disable_rps(dev_priv);
7903 } else if (IS_VALLEYVIEW(dev_priv)) {
7904 valleyview_disable_rps(dev_priv);
7905 } else if (INTEL_GEN(dev_priv) >= 6) {
7906 gen6_disable_rps(dev_priv);
7907 } else if (IS_IRONLAKE_M(dev_priv)) {
7908 ironlake_disable_drps(dev_priv);
7909 }
7910
7911 dev_priv->rps.enabled = false;
7912 mutex_unlock(&dev_priv->rps.hw_lock);
7913}
7914
7915void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
7916{
Chris Wilson54b4f682016-07-21 21:16:19 +01007917 /* We shouldn't be disabling as we submit, so this should be less
7918 * racy than it appears!
7919 */
Chris Wilsonb7137e02016-07-13 09:10:37 +01007920 if (READ_ONCE(dev_priv->rps.enabled))
7921 return;
7922
7923 /* Powersaving is controlled by the host when inside a VM */
7924 if (intel_vgpu_active(dev_priv))
7925 return;
7926
7927 mutex_lock(&dev_priv->rps.hw_lock);
Imre Deak3cc134e2014-11-19 15:30:03 +02007928
Chris Wilsondc979972016-05-10 14:10:04 +01007929 if (IS_CHERRYVIEW(dev_priv)) {
7930 cherryview_enable_rps(dev_priv);
7931 } else if (IS_VALLEYVIEW(dev_priv)) {
7932 valleyview_enable_rps(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007933 } else if (INTEL_GEN(dev_priv) >= 9) {
Chris Wilsondc979972016-05-10 14:10:04 +01007934 gen9_enable_rc6(dev_priv);
7935 gen9_enable_rps(dev_priv);
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07007936 if (IS_GEN9_BC(dev_priv) || IS_CANNONLAKE(dev_priv))
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007937 gen6_update_ring_freq(dev_priv);
Chris Wilsondc979972016-05-10 14:10:04 +01007938 } else if (IS_BROADWELL(dev_priv)) {
7939 gen8_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007940 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007941 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilsondc979972016-05-10 14:10:04 +01007942 gen6_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007943 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007944 } else if (IS_IRONLAKE_M(dev_priv)) {
7945 ironlake_enable_drps(dev_priv);
7946 intel_init_emon(dev_priv);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007947 }
Chris Wilsonaed242f2015-03-18 09:48:21 +00007948
7949 WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
7950 WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
7951
7952 WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
7953 WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
7954
Chris Wilson54b4f682016-07-21 21:16:19 +01007955 dev_priv->rps.enabled = true;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007956 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007957}
Imre Deakc6df39b2014-04-14 20:24:29 +03007958
Chris Wilson54b4f682016-07-21 21:16:19 +01007959static void __intel_autoenable_gt_powersave(struct work_struct *work)
7960{
7961 struct drm_i915_private *dev_priv =
7962 container_of(work, typeof(*dev_priv), rps.autoenable_work.work);
7963 struct intel_engine_cs *rcs;
7964 struct drm_i915_gem_request *req;
7965
7966 if (READ_ONCE(dev_priv->rps.enabled))
7967 goto out;
7968
Akash Goel3b3f1652016-10-13 22:44:48 +05307969 rcs = dev_priv->engine[RCS];
Chris Wilsone8a9c582016-12-18 15:37:20 +00007970 if (rcs->last_retired_context)
Chris Wilson54b4f682016-07-21 21:16:19 +01007971 goto out;
7972
7973 if (!rcs->init_context)
7974 goto out;
7975
7976 mutex_lock(&dev_priv->drm.struct_mutex);
7977
7978 req = i915_gem_request_alloc(rcs, dev_priv->kernel_context);
7979 if (IS_ERR(req))
7980 goto unlock;
7981
7982 if (!i915.enable_execlists && i915_switch_context(req) == 0)
7983 rcs->init_context(req);
7984
7985 /* Mark the device busy, calling intel_enable_gt_powersave() */
Chris Wilsone642c852017-03-17 11:47:09 +00007986 i915_add_request(req);
Chris Wilson54b4f682016-07-21 21:16:19 +01007987
7988unlock:
7989 mutex_unlock(&dev_priv->drm.struct_mutex);
7990out:
7991 intel_runtime_pm_put(dev_priv);
7992}
7993
7994void intel_autoenable_gt_powersave(struct drm_i915_private *dev_priv)
7995{
7996 if (READ_ONCE(dev_priv->rps.enabled))
7997 return;
7998
7999 if (IS_IRONLAKE_M(dev_priv)) {
8000 ironlake_enable_drps(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01008001 intel_init_emon(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01008002 } else if (INTEL_INFO(dev_priv)->gen >= 6) {
8003 /*
8004 * PCU communication is slow and this doesn't need to be
8005 * done at any specific time, so do this out of our fast path
8006 * to make resume and init faster.
8007 *
8008 * We depend on the HW RC6 power context save/restore
8009 * mechanism when entering D3 through runtime PM suspend. So
8010 * disable RPM until RPS/RC6 is properly setup. We can only
8011 * get here via the driver load/system resume/runtime resume
8012 * paths, so the _noresume version is enough (and in case of
8013 * runtime resume it's necessary).
8014 */
8015 if (queue_delayed_work(dev_priv->wq,
8016 &dev_priv->rps.autoenable_work,
8017 round_jiffies_up_relative(HZ)))
8018 intel_runtime_pm_get_noresume(dev_priv);
8019 }
8020}
8021
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008022static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01008023{
Daniel Vetter3107bd42012-10-31 22:52:31 +01008024 /*
8025 * On Ibex Peak and Cougar Point, we need to disable clock
8026 * gating for the panel power sequencer or it will fail to
8027 * start up when no ports are active.
8028 */
8029 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
8030}
8031
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008032static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008033{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03008034 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008035
Damien Lespiau055e3932014-08-18 13:49:10 +01008036 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008037 I915_WRITE(DSPCNTR(pipe),
8038 I915_READ(DSPCNTR(pipe)) |
8039 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03008040
8041 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
8042 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03008043 }
8044}
8045
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008046static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä017636c2013-12-05 15:51:37 +02008047{
Ville Syrjälä017636c2013-12-05 15:51:37 +02008048 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
8049 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
8050 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
8051
8052 /*
8053 * Don't touch WM1S_LP_EN here.
8054 * Doing so could cause underruns.
8055 */
8056}
8057
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008058static void ilk_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008059{
Damien Lespiau231e54f2012-10-19 17:55:41 +01008060 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008061
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01008062 /*
8063 * Required for FBC
8064 * WaFbcDisableDpfcClockGating:ilk
8065 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008066 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
8067 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
8068 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008069
8070 I915_WRITE(PCH_3DCGDIS0,
8071 MARIUNIT_CLOCK_GATE_DISABLE |
8072 SVSMUNIT_CLOCK_GATE_DISABLE);
8073 I915_WRITE(PCH_3DCGDIS1,
8074 VFMUNIT_CLOCK_GATE_DISABLE);
8075
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008076 /*
8077 * According to the spec the following bits should be set in
8078 * order to enable memory self-refresh
8079 * The bit 22/21 of 0x42004
8080 * The bit 5 of 0x42020
8081 * The bit 15 of 0x45000
8082 */
8083 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8084 (I915_READ(ILK_DISPLAY_CHICKEN2) |
8085 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008086 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008087 I915_WRITE(DISP_ARB_CTL,
8088 (I915_READ(DISP_ARB_CTL) |
8089 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02008090
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008091 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008092
8093 /*
8094 * Based on the document from hardware guys the following bits
8095 * should be set unconditionally in order to enable FBC.
8096 * The bit 22 of 0x42000
8097 * The bit 22 of 0x42004
8098 * The bit 7,8,9 of 0x42020.
8099 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008100 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01008101 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008102 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8103 I915_READ(ILK_DISPLAY_CHICKEN1) |
8104 ILK_FBCQ_DIS);
8105 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8106 I915_READ(ILK_DISPLAY_CHICKEN2) |
8107 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008108 }
8109
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01008110 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
8111
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008112 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8113 I915_READ(ILK_DISPLAY_CHICKEN2) |
8114 ILK_ELPIN_409_SELECT);
8115 I915_WRITE(_3D_CHICKEN2,
8116 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
8117 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02008118
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008119 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02008120 I915_WRITE(CACHE_MODE_0,
8121 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01008122
Akash Goel4e046322014-04-04 17:14:38 +05308123 /* WaDisable_RenderCache_OperationalFlush:ilk */
8124 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8125
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008126 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03008127
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008128 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008129}
8130
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008131static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01008132{
Daniel Vetter3107bd42012-10-31 22:52:31 +01008133 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008134 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01008135
8136 /*
8137 * On Ibex Peak and Cougar Point, we need to disable clock
8138 * gating for the panel power sequencer or it will fail to
8139 * start up when no ports are active.
8140 */
Jesse Barnescd664072013-10-02 10:34:19 -07008141 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
8142 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
8143 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008144 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
8145 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01008146 /* The below fixes the weird display corruption, a few pixels shifted
8147 * downward, on (only) LVDS of some HP laptops with IVY.
8148 */
Damien Lespiau055e3932014-08-18 13:49:10 +01008149 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03008150 val = I915_READ(TRANS_CHICKEN2(pipe));
8151 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
8152 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008153 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008154 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03008155 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
8156 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
8157 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03008158 I915_WRITE(TRANS_CHICKEN2(pipe), val);
8159 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01008160 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01008161 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01008162 I915_WRITE(TRANS_CHICKEN1(pipe),
8163 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
8164 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008165}
8166
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008167static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008168{
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008169 uint32_t tmp;
8170
8171 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02008172 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
8173 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
8174 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008175}
8176
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008177static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008178{
Damien Lespiau231e54f2012-10-19 17:55:41 +01008179 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008180
Damien Lespiau231e54f2012-10-19 17:55:41 +01008181 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008182
8183 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8184 I915_READ(ILK_DISPLAY_CHICKEN2) |
8185 ILK_ELPIN_409_SELECT);
8186
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008187 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01008188 I915_WRITE(_3D_CHICKEN,
8189 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
8190
Akash Goel4e046322014-04-04 17:14:38 +05308191 /* WaDisable_RenderCache_OperationalFlush:snb */
8192 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8193
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008194 /*
8195 * BSpec recoomends 8x4 when MSAA is used,
8196 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008197 *
8198 * Note that PS/WM thread counts depend on the WIZ hashing
8199 * disable bit, which we don't touch here, but it's good
8200 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008201 */
8202 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008203 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02008204
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008205 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008206
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008207 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02008208 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008209
8210 I915_WRITE(GEN6_UCGCTL1,
8211 I915_READ(GEN6_UCGCTL1) |
8212 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
8213 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
8214
8215 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
8216 * gating disable must be set. Failure to set it results in
8217 * flickering pixels due to Z write ordering failures after
8218 * some amount of runtime in the Mesa "fire" demo, and Unigine
8219 * Sanctuary and Tropics, and apparently anything else with
8220 * alpha test or pixel discard.
8221 *
8222 * According to the spec, bit 11 (RCCUNIT) must also be set,
8223 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008224 *
Ville Syrjäläef593182014-01-22 21:32:47 +02008225 * WaDisableRCCUnitClockGating:snb
8226 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008227 */
8228 I915_WRITE(GEN6_UCGCTL2,
8229 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
8230 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
8231
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02008232 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02008233 I915_WRITE(_3D_CHICKEN3,
8234 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008235
8236 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02008237 * Bspec says:
8238 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
8239 * 3DSTATE_SF number of SF output attributes is more than 16."
8240 */
8241 I915_WRITE(_3D_CHICKEN3,
8242 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
8243
8244 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008245 * According to the spec the following bits should be
8246 * set in order to enable memory self-refresh and fbc:
8247 * The bit21 and bit22 of 0x42000
8248 * The bit21 and bit22 of 0x42004
8249 * The bit5 and bit7 of 0x42020
8250 * The bit14 of 0x70180
8251 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01008252 *
8253 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008254 */
8255 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8256 I915_READ(ILK_DISPLAY_CHICKEN1) |
8257 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
8258 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8259 I915_READ(ILK_DISPLAY_CHICKEN2) |
8260 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01008261 I915_WRITE(ILK_DSPCLK_GATE_D,
8262 I915_READ(ILK_DSPCLK_GATE_D) |
8263 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
8264 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008265
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008266 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07008267
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008268 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008269
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008270 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008271}
8272
8273static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
8274{
8275 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
8276
Ville Syrjälä3aad9052014-01-22 21:32:59 +02008277 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02008278 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02008279 *
8280 * This actually overrides the dispatch
8281 * mode for all thread types.
8282 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008283 reg &= ~GEN7_FF_SCHED_MASK;
8284 reg |= GEN7_FF_TS_SCHED_HW;
8285 reg |= GEN7_FF_VS_SCHED_HW;
8286 reg |= GEN7_FF_DS_SCHED_HW;
8287
8288 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
8289}
8290
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008291static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008292{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008293 /*
8294 * TODO: this bit should only be enabled when really needed, then
8295 * disabled when not needed anymore in order to save power.
8296 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008297 if (HAS_PCH_LPT_LP(dev_priv))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008298 I915_WRITE(SOUTH_DSPCLK_GATE_D,
8299 I915_READ(SOUTH_DSPCLK_GATE_D) |
8300 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03008301
8302 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03008303 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
8304 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03008305 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02008306}
8307
Ville Syrjälä712bf362016-10-31 22:37:23 +02008308static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03008309{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01008310 if (HAS_PCH_LPT_LP(dev_priv)) {
Imre Deak7d708ee2013-04-17 14:04:50 +03008311 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
8312
8313 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8314 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8315 }
8316}
8317
Imre Deak450174f2016-05-03 15:54:21 +03008318static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
8319 int general_prio_credits,
8320 int high_prio_credits)
8321{
8322 u32 misccpctl;
8323
8324 /* WaTempDisableDOPClkGating:bdw */
8325 misccpctl = I915_READ(GEN7_MISCCPCTL);
8326 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
8327
8328 I915_WRITE(GEN8_L3SQCREG1,
8329 L3_GENERAL_PRIO_CREDITS(general_prio_credits) |
8330 L3_HIGH_PRIO_CREDITS(high_prio_credits));
8331
8332 /*
8333 * Wait at least 100 clocks before re-enabling clock gating.
8334 * See the definition of L3SQCREG1 in BSpec.
8335 */
8336 POSTING_READ(GEN8_L3SQCREG1);
8337 udelay(1);
8338 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
8339}
8340
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008341static void cnp_init_clock_gating(struct drm_i915_private *dev_priv)
8342{
8343 if (!HAS_PCH_CNP(dev_priv))
8344 return;
8345
8346 /* Wa #1181 */
8347 I915_WRITE(SOUTH_DSPCLK_GATE_D, CNP_PWM_CGE_GATING_DISABLE);
8348}
8349
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008350static void cnl_init_clock_gating(struct drm_i915_private *dev_priv)
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008351{
Rodrigo Vivi8f067832017-09-05 12:30:13 -07008352 u32 val;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008353 cnp_init_clock_gating(dev_priv);
8354
Rodrigo Vivi1a25db62017-08-15 16:16:51 -07008355 /* This is not an Wa. Enable for better image quality */
8356 I915_WRITE(_3D_CHICKEN3,
8357 _MASKED_BIT_ENABLE(_3D_CHICKEN3_AA_LINE_QUALITY_FIX_ENABLE));
8358
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008359 /* WaEnableChickenDCPR:cnl */
8360 I915_WRITE(GEN8_CHICKEN_DCPR_1,
8361 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
8362
8363 /* WaFbcWakeMemOn:cnl */
8364 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
8365 DISP_FBC_MEMORY_WAKE);
8366
8367 /* WaSarbUnitClockGatingDisable:cnl (pre-prod) */
8368 if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_B0))
8369 I915_WRITE(SLICE_UNIT_LEVEL_CLKGATE,
8370 I915_READ(SLICE_UNIT_LEVEL_CLKGATE) |
8371 SARBUNIT_CLKGATE_DIS);
Rodrigo Vivi8f067832017-09-05 12:30:13 -07008372
8373 /* Display WA #1133: WaFbcSkipSegments:cnl */
8374 val = I915_READ(ILK_DPFC_CHICKEN);
8375 val &= ~GLK_SKIP_SEG_COUNT_MASK;
8376 val |= GLK_SKIP_SEG_EN | GLK_SKIP_SEG_COUNT(1);
8377 I915_WRITE(ILK_DPFC_CHICKEN, val);
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008378}
8379
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008380static void cfl_init_clock_gating(struct drm_i915_private *dev_priv)
8381{
8382 cnp_init_clock_gating(dev_priv);
8383 gen9_init_clock_gating(dev_priv);
8384
8385 /* WaFbcNukeOnHostModify:cfl */
8386 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8387 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
8388}
8389
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008390static void kbl_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008391{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008392 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008393
8394 /* WaDisableSDEUnitClockGating:kbl */
8395 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
8396 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8397 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03008398
8399 /* WaDisableGamClockGating:kbl */
8400 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
8401 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
8402 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008403
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008404 /* WaFbcNukeOnHostModify:kbl */
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008405 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8406 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03008407}
8408
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008409static void skl_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008410{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008411 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03008412
8413 /* WAC6entrylatency:skl */
8414 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
8415 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03008416
8417 /* WaFbcNukeOnHostModify:skl */
8418 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
8419 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02008420}
8421
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008422static void bdw_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008423{
Damien Lespiau07d27e22014-03-03 17:31:46 +00008424 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008425
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008426 ilk_init_lp_watermarks(dev_priv);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07008427
Ben Widawskyab57fff2013-12-12 15:28:04 -08008428 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07008429 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008430
Ben Widawskyab57fff2013-12-12 15:28:04 -08008431 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008432 I915_WRITE(CHICKEN_PAR1_1,
8433 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
8434
Ben Widawskyab57fff2013-12-12 15:28:04 -08008435 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01008436 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00008437 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02008438 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02008439 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07008440 }
Ben Widawsky63801f22013-12-12 17:26:03 -08008441
Ben Widawskyab57fff2013-12-12 15:28:04 -08008442 /* WaVSRefCountFullforceMissDisable:bdw */
8443 /* WaDSRefCountFullforceMissDisable:bdw */
8444 I915_WRITE(GEN7_FF_THREAD_MODE,
8445 I915_READ(GEN7_FF_THREAD_MODE) &
8446 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02008447
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02008448 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
8449 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02008450
8451 /* WaDisableSDEUnitClockGating:bdw */
8452 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8453 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00008454
Imre Deak450174f2016-05-03 15:54:21 +03008455 /* WaProgramL3SqcReg1Default:bdw */
8456 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03008457
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008458 /*
8459 * WaGttCachingOffByDefault:bdw
8460 * GTT cache may not work with big pages, so if those
8461 * are ever enabled GTT cache may need to be disabled.
8462 */
8463 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
8464
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03008465 /* WaKVMNotificationOnConfigChange:bdw */
8466 I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
8467 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
8468
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008469 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00008470
8471 /* WaDisableDopClockGating:bdw
8472 *
8473 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
8474 * clock gating.
8475 */
8476 I915_WRITE(GEN6_UCGCTL1,
8477 I915_READ(GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07008478}
8479
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008480static void hsw_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008481{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008482 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008483
Francisco Jerezf3fc4882013-10-02 15:53:16 -07008484 /* L3 caching of data atomics doesn't work -- disable it. */
8485 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
8486 I915_WRITE(HSW_ROW_CHICKEN3,
8487 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
8488
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008489 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008490 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8491 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8492 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8493
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02008494 /* WaVSRefCountFullforceMissDisable:hsw */
8495 I915_WRITE(GEN7_FF_THREAD_MODE,
8496 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008497
Akash Goel4e046322014-04-04 17:14:38 +05308498 /* WaDisable_RenderCache_OperationalFlush:hsw */
8499 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8500
Chia-I Wufe27c602014-01-28 13:29:33 +08008501 /* enable HiZ Raw Stall Optimization */
8502 I915_WRITE(CACHE_MODE_0_GEN7,
8503 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
8504
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008505 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008506 I915_WRITE(CACHE_MODE_1,
8507 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03008508
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008509 /*
8510 * BSpec recommends 8x4 when MSAA is used,
8511 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008512 *
8513 * Note that PS/WM thread counts depend on the WIZ hashing
8514 * disable bit, which we don't touch here, but it's good
8515 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008516 */
8517 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008518 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02008519
Kenneth Graunke94411592014-12-31 16:23:00 -08008520 /* WaSampleCChickenBitEnable:hsw */
8521 I915_WRITE(HALF_SLICE_CHICKEN3,
8522 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
8523
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008524 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07008525 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
8526
Paulo Zanoni90a88642013-05-03 17:23:45 -03008527 /* WaRsPkgCStateDisplayPMReq:hsw */
8528 I915_WRITE(CHICKEN_PAR1_1,
8529 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03008530
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008531 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03008532}
8533
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008534static void ivb_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008535{
Ben Widawsky20848222012-05-04 18:58:59 -07008536 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008537
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008538 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008539
Damien Lespiau231e54f2012-10-19 17:55:41 +01008540 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008541
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008542 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05008543 I915_WRITE(_3D_CHICKEN3,
8544 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
8545
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008546 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008547 I915_WRITE(IVB_CHICKEN3,
8548 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8549 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8550
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008551 /* WaDisablePSDDualDispatchEnable:ivb */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008552 if (IS_IVB_GT1(dev_priv))
Jesse Barnes12f33822012-10-25 12:15:45 -07008553 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
8554 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07008555
Akash Goel4e046322014-04-04 17:14:38 +05308556 /* WaDisable_RenderCache_OperationalFlush:ivb */
8557 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8558
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008559 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008560 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
8561 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
8562
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008563 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008564 I915_WRITE(GEN7_L3CNTLREG1,
8565 GEN7_WA_FOR_GEN7_L3_CONTROL);
8566 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07008567 GEN7_WA_L3_CHICKEN_MODE);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008568 if (IS_IVB_GT1(dev_priv))
Jesse Barnes8ab43972012-10-25 12:15:42 -07008569 I915_WRITE(GEN7_ROW_CHICKEN2,
8570 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02008571 else {
8572 /* must write both registers */
8573 I915_WRITE(GEN7_ROW_CHICKEN2,
8574 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07008575 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
8576 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02008577 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008578
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008579 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05008580 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
8581 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
8582
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02008583 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07008584 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008585 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008586 */
8587 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02008588 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07008589
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008590 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008591 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8592 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8593 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8594
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008595 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008596
8597 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02008598
Chris Wilson22721342014-03-04 09:41:43 +00008599 if (0) { /* causes HiZ corruption on ivb:gt1 */
8600 /* enable HiZ Raw Stall Optimization */
8601 I915_WRITE(CACHE_MODE_0_GEN7,
8602 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
8603 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08008604
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008605 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02008606 I915_WRITE(CACHE_MODE_1,
8607 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07008608
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008609 /*
8610 * BSpec recommends 8x4 when MSAA is used,
8611 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02008612 *
8613 * Note that PS/WM thread counts depend on the WIZ hashing
8614 * disable bit, which we don't touch here, but it's good
8615 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008616 */
8617 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00008618 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02008619
Ben Widawsky20848222012-05-04 18:58:59 -07008620 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
8621 snpcr &= ~GEN6_MBC_SNPCR_MASK;
8622 snpcr |= GEN6_MBC_SNPCR_MED;
8623 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01008624
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008625 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008626 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01008627
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008628 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008629}
8630
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008631static void vlv_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008632{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008633 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05008634 I915_WRITE(_3D_CHICKEN3,
8635 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
8636
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008637 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008638 I915_WRITE(IVB_CHICKEN3,
8639 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8640 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8641
Ville Syrjäläfad7d362014-01-22 21:32:39 +02008642 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008643 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07008644 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08008645 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
8646 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07008647
Akash Goel4e046322014-04-04 17:14:38 +05308648 /* WaDisable_RenderCache_OperationalFlush:vlv */
8649 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8650
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008651 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05008652 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
8653 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
8654
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008655 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07008656 I915_WRITE(GEN7_ROW_CHICKEN2,
8657 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
8658
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008659 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008660 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
8661 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
8662 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
8663
Ville Syrjälä46680e02014-01-22 21:33:01 +02008664 gen7_setup_fixed_func_scheduler(dev_priv);
8665
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02008666 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07008667 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008668 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07008669 */
8670 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02008671 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07008672
Akash Goelc98f5062014-03-24 23:00:07 +05308673 /* WaDisableL3Bank2xClockGate:vlv
8674 * Disabling L3 clock gating- MMIO 940c[25] = 1
8675 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
8676 I915_WRITE(GEN7_UCGCTL4,
8677 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07008678
Ville Syrjäläafd58e72014-01-22 21:33:03 +02008679 /*
8680 * BSpec says this must be set, even though
8681 * WaDisable4x2SubspanOptimization isn't listed for VLV.
8682 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02008683 I915_WRITE(CACHE_MODE_1,
8684 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07008685
8686 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02008687 * BSpec recommends 8x4 when MSAA is used,
8688 * however in practice 16x4 seems fastest.
8689 *
8690 * Note that PS/WM thread counts depend on the WIZ hashing
8691 * disable bit, which we don't touch here, but it's good
8692 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
8693 */
8694 I915_WRITE(GEN7_GT_MODE,
8695 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
8696
8697 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02008698 * WaIncreaseL3CreditsForVLVB0:vlv
8699 * This is the hardware default actually.
8700 */
8701 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
8702
8703 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01008704 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07008705 * Disable clock gating on th GCFG unit to prevent a delay
8706 * in the reporting of vblank events.
8707 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02008708 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008709}
8710
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008711static void chv_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03008712{
Ville Syrjälä232ce332014-04-09 13:28:35 +03008713 /* WaVSRefCountFullforceMissDisable:chv */
8714 /* WaDSRefCountFullforceMissDisable:chv */
8715 I915_WRITE(GEN7_FF_THREAD_MODE,
8716 I915_READ(GEN7_FF_THREAD_MODE) &
8717 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03008718
8719 /* WaDisableSemaphoreAndSyncFlipWait:chv */
8720 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
8721 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03008722
8723 /* WaDisableCSUnitClockGating:chv */
8724 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
8725 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03008726
8727 /* WaDisableSDEUnitClockGating:chv */
8728 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
8729 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008730
8731 /*
Imre Deak450174f2016-05-03 15:54:21 +03008732 * WaProgramL3SqcReg1Default:chv
8733 * See gfxspecs/Related Documents/Performance Guide/
8734 * LSQC Setting Recommendations.
8735 */
8736 gen8_set_l3sqc_credits(dev_priv, 38, 2);
8737
8738 /*
Ville Syrjälä6d50b062015-05-19 20:32:57 +03008739 * GTT cache may not work with big pages, so if those
8740 * are ever enabled GTT cache may need to be disabled.
8741 */
8742 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03008743}
8744
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008745static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008746{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008747 uint32_t dspclk_gate;
8748
8749 I915_WRITE(RENCLK_GATE_D1, 0);
8750 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
8751 GS_UNIT_CLOCK_GATE_DISABLE |
8752 CL_UNIT_CLOCK_GATE_DISABLE);
8753 I915_WRITE(RAMCLK_GATE_D, 0);
8754 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
8755 OVRUNIT_CLOCK_GATE_DISABLE |
8756 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008757 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008758 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
8759 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02008760
8761 /* WaDisableRenderCachePipelinedFlush */
8762 I915_WRITE(CACHE_MODE_0,
8763 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03008764
Akash Goel4e046322014-04-04 17:14:38 +05308765 /* WaDisable_RenderCache_OperationalFlush:g4x */
8766 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
8767
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008768 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008769}
8770
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008771static void i965gm_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008772{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008773 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
8774 I915_WRITE(RENCLK_GATE_D2, 0);
8775 I915_WRITE(DSPCLK_GATE_D, 0);
8776 I915_WRITE(RAMCLK_GATE_D, 0);
8777 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03008778 I915_WRITE(MI_ARB_STATE,
8779 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05308780
8781 /* WaDisable_RenderCache_OperationalFlush:gen4 */
8782 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008783}
8784
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008785static void i965g_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008786{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008787 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
8788 I965_RCC_CLOCK_GATE_DISABLE |
8789 I965_RCPB_CLOCK_GATE_DISABLE |
8790 I965_ISC_CLOCK_GATE_DISABLE |
8791 I965_FBC_CLOCK_GATE_DISABLE);
8792 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03008793 I915_WRITE(MI_ARB_STATE,
8794 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05308795
8796 /* WaDisable_RenderCache_OperationalFlush:gen4 */
8797 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008798}
8799
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008800static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008801{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008802 u32 dstate = I915_READ(D_STATE);
8803
8804 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
8805 DSTATE_DOT_CLOCK_GATING;
8806 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01008807
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008808 if (IS_PINEVIEW(dev_priv))
Chris Wilson13a86b82012-04-24 14:51:43 +01008809 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02008810
8811 /* IIR "flip pending" means done if this bit is set */
8812 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02008813
8814 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02008815 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02008816
8817 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
8818 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03008819
8820 I915_WRITE(MI_ARB_STATE,
8821 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008822}
8823
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008824static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008825{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008826 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02008827
8828 /* interrupts should cause a wake up from C3 */
8829 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
8830 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03008831
8832 I915_WRITE(MEM_MODE,
8833 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008834}
8835
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008836static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008837{
Ville Syrjälä10383922014-08-15 01:21:54 +03008838 I915_WRITE(MEM_MODE,
8839 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
8840 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008841}
8842
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008843void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008844{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008845 dev_priv->display.init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03008846}
8847
Ville Syrjälä712bf362016-10-31 22:37:23 +02008848void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03008849{
Ville Syrjälä712bf362016-10-31 22:37:23 +02008850 if (HAS_PCH_LPT(dev_priv))
8851 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03008852}
8853
Ville Syrjälä46f16e62016-10-31 22:37:22 +02008854static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02008855{
8856 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
8857}
8858
8859/**
8860 * intel_init_clock_gating_hooks - setup the clock gating hooks
8861 * @dev_priv: device private
8862 *
8863 * Setup the hooks that configure which clocks of a given platform can be
8864 * gated and also apply various GT and display specific workarounds for these
8865 * platforms. Note that some GT specific workarounds are applied separately
8866 * when GPU contexts or batchbuffers start their execution.
8867 */
8868void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
8869{
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008870 if (IS_CANNONLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008871 dev_priv->display.init_clock_gating = cnl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008872 else if (IS_COFFEELAKE(dev_priv))
8873 dev_priv->display.init_clock_gating = cfl_init_clock_gating;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07008874 else if (IS_SKYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008875 dev_priv->display.init_clock_gating = skl_init_clock_gating;
Rodrigo Vivi0a46ddd2017-08-30 21:52:23 -07008876 else if (IS_KABYLAKE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008877 dev_priv->display.init_clock_gating = kbl_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02008878 else if (IS_BROXTON(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02008879 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02008880 else if (IS_GEMINILAKE(dev_priv))
8881 dev_priv->display.init_clock_gating = glk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008882 else if (IS_BROADWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008883 dev_priv->display.init_clock_gating = bdw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008884 else if (IS_CHERRYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008885 dev_priv->display.init_clock_gating = chv_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008886 else if (IS_HASWELL(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008887 dev_priv->display.init_clock_gating = hsw_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008888 else if (IS_IVYBRIDGE(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008889 dev_priv->display.init_clock_gating = ivb_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008890 else if (IS_VALLEYVIEW(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008891 dev_priv->display.init_clock_gating = vlv_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008892 else if (IS_GEN6(dev_priv))
8893 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
8894 else if (IS_GEN5(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008895 dev_priv->display.init_clock_gating = ilk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008896 else if (IS_G4X(dev_priv))
8897 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02008898 else if (IS_I965GM(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008899 dev_priv->display.init_clock_gating = i965gm_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02008900 else if (IS_I965G(dev_priv))
Rodrigo Vivi91200c02017-08-28 22:20:26 -07008901 dev_priv->display.init_clock_gating = i965g_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02008902 else if (IS_GEN3(dev_priv))
8903 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
8904 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
8905 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
8906 else if (IS_GEN2(dev_priv))
8907 dev_priv->display.init_clock_gating = i830_init_clock_gating;
8908 else {
8909 MISSING_CASE(INTEL_DEVID(dev_priv));
8910 dev_priv->display.init_clock_gating = nop_init_clock_gating;
8911 }
8912}
8913
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008914/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008915void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008916{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02008917 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008918
Daniel Vetterc921aba2012-04-26 23:28:17 +02008919 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008920 if (IS_PINEVIEW(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02008921 i915_pineview_get_mem_freq(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008922 else if (IS_GEN5(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02008923 i915_ironlake_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02008924
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008925 /* For FIFO watermark updates */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008926 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008927 skl_setup_wm_latency(dev_priv);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01008928 dev_priv->display.initial_watermarks = skl_initial_wm;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01008929 dev_priv->display.atomic_update_watermarks = skl_atomic_update_crtc_wm;
Matt Roper98d39492016-05-12 07:06:03 -07008930 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008931 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008932 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03008933
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008934 if ((IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008935 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008936 (!IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02008937 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07008938 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08008939 dev_priv->display.compute_intermediate_wm =
8940 ilk_compute_intermediate_wm;
8941 dev_priv->display.initial_watermarks =
8942 ilk_initial_watermarks;
8943 dev_priv->display.optimize_watermarks =
8944 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02008945 } else {
8946 DRM_DEBUG_KMS("Failed to read display plane latency. "
8947 "Disable CxSR\n");
8948 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02008949 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008950 vlv_setup_wm_latency(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02008951 dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
Ville Syrjälä4841da52017-03-02 19:14:59 +02008952 dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02008953 dev_priv->display.initial_watermarks = vlv_initial_watermarks;
Ville Syrjälä4841da52017-03-02 19:14:59 +02008954 dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
Ville Syrjäläff32c542017-03-02 19:14:57 +02008955 dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
Ville Syrjälä04548cb2017-04-21 21:14:29 +03008956 } else if (IS_G4X(dev_priv)) {
8957 g4x_setup_wm_latency(dev_priv);
8958 dev_priv->display.compute_pipe_wm = g4x_compute_pipe_wm;
8959 dev_priv->display.compute_intermediate_wm = g4x_compute_intermediate_wm;
8960 dev_priv->display.initial_watermarks = g4x_initial_watermarks;
8961 dev_priv->display.optimize_watermarks = g4x_optimize_watermarks;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008962 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008963 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008964 dev_priv->is_ddr3,
8965 dev_priv->fsb_freq,
8966 dev_priv->mem_freq)) {
8967 DRM_INFO("failed to find known CxSR latency "
8968 "(found ddr%s fsb freq %d, mem freq %d), "
8969 "disabling CxSR\n",
8970 (dev_priv->is_ddr3 == 1) ? "3" : "2",
8971 dev_priv->fsb_freq, dev_priv->mem_freq);
8972 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03008973 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008974 dev_priv->display.update_wm = NULL;
8975 } else
8976 dev_priv->display.update_wm = pineview_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008977 } else if (IS_GEN4(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008978 dev_priv->display.update_wm = i965_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008979 } else if (IS_GEN3(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008980 dev_priv->display.update_wm = i9xx_update_wm;
8981 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008982 } else if (IS_GEN2(dev_priv)) {
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008983 if (INTEL_INFO(dev_priv)->num_pipes == 1) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008984 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008985 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008986 } else {
8987 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008988 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008989 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008990 } else {
8991 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008992 }
8993}
8994
Lyude87660502016-08-17 15:55:53 -04008995static inline int gen6_check_mailbox_status(struct drm_i915_private *dev_priv)
8996{
8997 uint32_t flags =
8998 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
8999
9000 switch (flags) {
9001 case GEN6_PCODE_SUCCESS:
9002 return 0;
9003 case GEN6_PCODE_UNIMPLEMENTED_CMD:
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009004 return -ENODEV;
Lyude87660502016-08-17 15:55:53 -04009005 case GEN6_PCODE_ILLEGAL_CMD:
9006 return -ENXIO;
9007 case GEN6_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Chris Wilson7850d1c2016-08-26 11:59:26 +01009008 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Lyude87660502016-08-17 15:55:53 -04009009 return -EOVERFLOW;
9010 case GEN6_PCODE_TIMEOUT:
9011 return -ETIMEDOUT;
9012 default:
Michal Wajdeczkof0d66152017-03-28 08:45:12 +00009013 MISSING_CASE(flags);
Lyude87660502016-08-17 15:55:53 -04009014 return 0;
9015 }
9016}
9017
9018static inline int gen7_check_mailbox_status(struct drm_i915_private *dev_priv)
9019{
9020 uint32_t flags =
9021 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
9022
9023 switch (flags) {
9024 case GEN6_PCODE_SUCCESS:
9025 return 0;
9026 case GEN6_PCODE_ILLEGAL_CMD:
9027 return -ENXIO;
9028 case GEN7_PCODE_TIMEOUT:
9029 return -ETIMEDOUT;
9030 case GEN7_PCODE_ILLEGAL_DATA:
9031 return -EINVAL;
9032 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
9033 return -EOVERFLOW;
9034 default:
9035 MISSING_CASE(flags);
9036 return 0;
9037 }
9038}
9039
Tom O'Rourke151a49d2014-11-13 18:50:10 -08009040int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07009041{
Lyude87660502016-08-17 15:55:53 -04009042 int status;
9043
Jesse Barnes4fc688c2012-11-02 11:14:01 -07009044 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07009045
Chris Wilson3f5582d2016-06-30 15:32:45 +01009046 /* GEN6_PCODE_* are outside of the forcewake domain, we can
9047 * use te fw I915_READ variants to reduce the amount of work
9048 * required when reading/writing.
9049 */
9050
9051 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009052 DRM_DEBUG_DRIVER("warning: pcode (read from mbox %x) mailbox access failed for %ps\n",
9053 mbox, __builtin_return_address(0));
Ben Widawsky42c05262012-09-26 10:34:00 -07009054 return -EAGAIN;
9055 }
9056
Chris Wilson3f5582d2016-06-30 15:32:45 +01009057 I915_WRITE_FW(GEN6_PCODE_DATA, *val);
9058 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
9059 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07009060
Chris Wilsone09a3032017-04-11 11:13:39 +01009061 if (__intel_wait_for_register_fw(dev_priv,
9062 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
9063 500, 0, NULL)) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009064 DRM_ERROR("timeout waiting for pcode read (from mbox %x) to finish for %ps\n",
9065 mbox, __builtin_return_address(0));
Ben Widawsky42c05262012-09-26 10:34:00 -07009066 return -ETIMEDOUT;
9067 }
9068
Chris Wilson3f5582d2016-06-30 15:32:45 +01009069 *val = I915_READ_FW(GEN6_PCODE_DATA);
9070 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07009071
Lyude87660502016-08-17 15:55:53 -04009072 if (INTEL_GEN(dev_priv) > 6)
9073 status = gen7_check_mailbox_status(dev_priv);
9074 else
9075 status = gen6_check_mailbox_status(dev_priv);
9076
9077 if (status) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009078 DRM_DEBUG_DRIVER("warning: pcode (read from mbox %x) mailbox access failed for %ps: %d\n",
9079 mbox, __builtin_return_address(0), status);
Lyude87660502016-08-17 15:55:53 -04009080 return status;
9081 }
9082
Ben Widawsky42c05262012-09-26 10:34:00 -07009083 return 0;
9084}
9085
Chris Wilson3f5582d2016-06-30 15:32:45 +01009086int sandybridge_pcode_write(struct drm_i915_private *dev_priv,
Lyude87660502016-08-17 15:55:53 -04009087 u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07009088{
Lyude87660502016-08-17 15:55:53 -04009089 int status;
9090
Jesse Barnes4fc688c2012-11-02 11:14:01 -07009091 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07009092
Chris Wilson3f5582d2016-06-30 15:32:45 +01009093 /* GEN6_PCODE_* are outside of the forcewake domain, we can
9094 * use te fw I915_READ variants to reduce the amount of work
9095 * required when reading/writing.
9096 */
9097
9098 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009099 DRM_DEBUG_DRIVER("warning: pcode (write of 0x%08x to mbox %x) mailbox access failed for %ps\n",
9100 val, mbox, __builtin_return_address(0));
Ben Widawsky42c05262012-09-26 10:34:00 -07009101 return -EAGAIN;
9102 }
9103
Chris Wilson3f5582d2016-06-30 15:32:45 +01009104 I915_WRITE_FW(GEN6_PCODE_DATA, val);
Imre Deak8bf41b72016-11-28 17:29:27 +02009105 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
Chris Wilson3f5582d2016-06-30 15:32:45 +01009106 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07009107
Chris Wilsone09a3032017-04-11 11:13:39 +01009108 if (__intel_wait_for_register_fw(dev_priv,
9109 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
9110 500, 0, NULL)) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009111 DRM_ERROR("timeout waiting for pcode write of 0x%08x to mbox %x to finish for %ps\n",
9112 val, mbox, __builtin_return_address(0));
Ben Widawsky42c05262012-09-26 10:34:00 -07009113 return -ETIMEDOUT;
9114 }
9115
Chris Wilson3f5582d2016-06-30 15:32:45 +01009116 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07009117
Lyude87660502016-08-17 15:55:53 -04009118 if (INTEL_GEN(dev_priv) > 6)
9119 status = gen7_check_mailbox_status(dev_priv);
9120 else
9121 status = gen6_check_mailbox_status(dev_priv);
9122
9123 if (status) {
Chris Wilson5a9cfff2017-07-28 09:50:22 +01009124 DRM_DEBUG_DRIVER("warning: pcode (write of 0x%08x to mbox %x) mailbox access failed for %ps: %d\n",
9125 val, mbox, __builtin_return_address(0), status);
Lyude87660502016-08-17 15:55:53 -04009126 return status;
9127 }
9128
Ben Widawsky42c05262012-09-26 10:34:00 -07009129 return 0;
9130}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07009131
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009132static bool skl_pcode_try_request(struct drm_i915_private *dev_priv, u32 mbox,
9133 u32 request, u32 reply_mask, u32 reply,
9134 u32 *status)
9135{
9136 u32 val = request;
9137
9138 *status = sandybridge_pcode_read(dev_priv, mbox, &val);
9139
9140 return *status || ((val & reply_mask) == reply);
9141}
9142
9143/**
9144 * skl_pcode_request - send PCODE request until acknowledgment
9145 * @dev_priv: device private
9146 * @mbox: PCODE mailbox ID the request is targeted for
9147 * @request: request ID
9148 * @reply_mask: mask used to check for request acknowledgment
9149 * @reply: value used to check for request acknowledgment
9150 * @timeout_base_ms: timeout for polling with preemption enabled
9151 *
9152 * Keep resending the @request to @mbox until PCODE acknowledges it, PCODE
Imre Deak01299362017-02-24 16:32:10 +02009153 * reports an error or an overall timeout of @timeout_base_ms+50 ms expires.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009154 * The request is acknowledged once the PCODE reply dword equals @reply after
9155 * applying @reply_mask. Polling is first attempted with preemption enabled
Imre Deak01299362017-02-24 16:32:10 +02009156 * for @timeout_base_ms and if this times out for another 50 ms with
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009157 * preemption disabled.
9158 *
9159 * Returns 0 on success, %-ETIMEDOUT in case of a timeout, <0 in case of some
9160 * other error as reported by PCODE.
9161 */
9162int skl_pcode_request(struct drm_i915_private *dev_priv, u32 mbox, u32 request,
9163 u32 reply_mask, u32 reply, int timeout_base_ms)
9164{
9165 u32 status;
9166 int ret;
9167
9168 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
9169
9170#define COND skl_pcode_try_request(dev_priv, mbox, request, reply_mask, reply, \
9171 &status)
9172
9173 /*
9174 * Prime the PCODE by doing a request first. Normally it guarantees
9175 * that a subsequent request, at most @timeout_base_ms later, succeeds.
9176 * _wait_for() doesn't guarantee when its passed condition is evaluated
9177 * first, so send the first request explicitly.
9178 */
9179 if (COND) {
9180 ret = 0;
9181 goto out;
9182 }
9183 ret = _wait_for(COND, timeout_base_ms * 1000, 10);
9184 if (!ret)
9185 goto out;
9186
9187 /*
9188 * The above can time out if the number of requests was low (2 in the
9189 * worst case) _and_ PCODE was busy for some reason even after a
9190 * (queued) request and @timeout_base_ms delay. As a workaround retry
9191 * the poll with preemption disabled to maximize the number of
Imre Deak01299362017-02-24 16:32:10 +02009192 * requests. Increase the timeout from @timeout_base_ms to 50ms to
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009193 * account for interrupts that could reduce the number of these
Imre Deak01299362017-02-24 16:32:10 +02009194 * requests, and for any quirks of the PCODE firmware that delays
9195 * the request completion.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009196 */
9197 DRM_DEBUG_KMS("PCODE timeout, retrying with preemption disabled\n");
9198 WARN_ON_ONCE(timeout_base_ms > 3);
9199 preempt_disable();
Imre Deak01299362017-02-24 16:32:10 +02009200 ret = wait_for_atomic(COND, 50);
Imre Deaka0b8a1f2016-12-05 18:27:37 +02009201 preempt_enable();
9202
9203out:
9204 return ret ? ret : status;
9205#undef COND
9206}
9207
Ville Syrjälädd06f882014-11-10 22:55:12 +02009208static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
9209{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009210 /*
9211 * N = val - 0xb7
9212 * Slow = Fast = GPLL ref * N
9213 */
9214 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * (val - 0xb7), 1000);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009215}
9216
Fengguang Wub55dd642014-07-12 11:21:39 +02009217static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009218{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009219 return DIV_ROUND_CLOSEST(1000 * val, dev_priv->rps.gpll_ref_freq) + 0xb7;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07009220}
9221
Fengguang Wub55dd642014-07-12 11:21:39 +02009222static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05309223{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009224 /*
9225 * N = val / 2
9226 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
9227 */
9228 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * val, 2 * 2 * 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05309229}
9230
Fengguang Wub55dd642014-07-12 11:21:39 +02009231static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05309232{
Ville Syrjälä1c147622014-08-18 14:42:43 +03009233 /* CHV needs even values */
Ville Syrjäläc30fec62016-03-04 21:43:02 +02009234 return DIV_ROUND_CLOSEST(2 * 1000 * val, dev_priv->rps.gpll_ref_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05309235}
9236
Ville Syrjälä616bc822015-01-23 21:04:25 +02009237int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
9238{
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07009239 if (INTEL_GEN(dev_priv) >= 9)
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009240 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
9241 GEN9_FREQ_SCALER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009242 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009243 return chv_gpu_freq(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009244 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009245 return byt_gpu_freq(dev_priv, val);
9246 else
9247 return val * GT_FREQUENCY_MULTIPLIER;
9248}
9249
Ville Syrjälä616bc822015-01-23 21:04:25 +02009250int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
9251{
Rodrigo Vivi35ceabf2017-07-06 13:41:13 -07009252 if (INTEL_GEN(dev_priv) >= 9)
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009253 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
9254 GT_FREQUENCY_MULTIPLIER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009255 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009256 return chv_freq_opcode(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009257 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02009258 return byt_freq_opcode(dev_priv, val);
9259 else
Mika Kuoppala500a3d22015-11-13 19:29:41 +02009260 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
Deepak S22b1b2f2014-07-12 14:54:33 +05309261}
9262
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00009263void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01009264{
Daniel Vetterf742a552013-12-06 10:17:53 +01009265 mutex_init(&dev_priv->rps.hw_lock);
9266
Chris Wilson54b4f682016-07-21 21:16:19 +01009267 INIT_DELAYED_WORK(&dev_priv->rps.autoenable_work,
9268 __intel_autoenable_gt_powersave);
Chris Wilson7b92c1b2017-06-28 13:35:48 +01009269 atomic_set(&dev_priv->rps.num_waiters, 0);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03009270
Paulo Zanoni33688d92014-03-07 20:08:19 -03009271 dev_priv->pm.suspended = false;
Imre Deak1f814da2015-12-16 02:52:19 +02009272 atomic_set(&dev_priv->pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01009273}
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009274
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009275static u64 vlv_residency_raw(struct drm_i915_private *dev_priv,
9276 const i915_reg_t reg)
9277{
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009278 u32 lower, upper, tmp;
Chris Wilson71cc2b12017-03-24 16:54:18 +00009279 int loop = 2;
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009280
9281 /* The register accessed do not need forcewake. We borrow
9282 * uncore lock to prevent concurrent access to range reg.
9283 */
9284 spin_lock_irq(&dev_priv->uncore.lock);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009285
9286 /* vlv and chv residency counters are 40 bits in width.
9287 * With a control bit, we can choose between upper or lower
9288 * 32bit window into this counter.
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009289 *
9290 * Although we always use the counter in high-range mode elsewhere,
9291 * userspace may attempt to read the value before rc6 is initialised,
9292 * before we have set the default VLV_COUNTER_CONTROL value. So always
9293 * set the high bit to be safe.
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009294 */
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009295 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9296 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009297 upper = I915_READ_FW(reg);
9298 do {
9299 tmp = upper;
9300
9301 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9302 _MASKED_BIT_DISABLE(VLV_COUNT_RANGE_HIGH));
9303 lower = I915_READ_FW(reg);
9304
9305 I915_WRITE_FW(VLV_COUNTER_CONTROL,
9306 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
9307 upper = I915_READ_FW(reg);
Chris Wilson71cc2b12017-03-24 16:54:18 +00009308 } while (upper != tmp && --loop);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009309
Chris Wilsonfacbeca2017-03-17 12:59:18 +00009310 /* Everywhere else we always use VLV_COUNTER_CONTROL with the
9311 * VLV_COUNT_RANGE_HIGH bit set - so it is safe to leave it set
9312 * now.
9313 */
9314
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009315 spin_unlock_irq(&dev_priv->uncore.lock);
9316
9317 return lower | (u64)upper << 8;
9318}
9319
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009320u64 intel_rc6_residency_us(struct drm_i915_private *dev_priv,
9321 const i915_reg_t reg)
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009322{
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009323 u64 time_hw, units, div;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009324
9325 if (!intel_enable_rc6())
9326 return 0;
9327
9328 intel_runtime_pm_get(dev_priv);
9329
9330 /* On VLV and CHV, residency time is in CZ units rather than 1.28us */
9331 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009332 units = 1000;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009333 div = dev_priv->czclk_freq;
9334
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009335 time_hw = vlv_residency_raw(dev_priv, reg);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009336 } else if (IS_GEN9_LP(dev_priv)) {
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02009337 units = 1000;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009338 div = 1200; /* 833.33ns */
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009339
9340 time_hw = I915_READ(reg);
9341 } else {
9342 units = 128000; /* 1.28us */
9343 div = 100000;
9344
9345 time_hw = I915_READ(reg);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009346 }
9347
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009348 intel_runtime_pm_put(dev_priv);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02009349 return DIV_ROUND_UP_ULL(time_hw * units, div);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02009350}